CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStageMap.java

281 lines
9.6 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.client.CompositeSnapshot;
2021-05-08 03:09:00 +02:00
import it.cavallium.dbengine.database.Delta;
import it.cavallium.dbengine.database.LLUtils;
2022-05-20 23:59:56 +02:00
import it.cavallium.dbengine.database.SubStageEntry;
2021-05-02 19:18:15 +02:00
import it.cavallium.dbengine.database.UpdateMode;
2021-05-08 03:09:00 +02:00
import it.cavallium.dbengine.database.UpdateReturnMode;
2021-11-08 10:49:59 +01:00
import it.cavallium.dbengine.database.serialization.KVSerializationFunction;
2021-08-22 21:23:22 +02:00
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializationFunction;
2021-12-18 18:16:56 +01:00
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectSortedMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectSortedMaps;
import java.util.Map;
import java.util.Map.Entry;
2021-05-02 19:18:15 +02:00
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
2021-05-02 19:18:15 +02:00
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
@SuppressWarnings("unused")
2021-12-18 18:16:56 +01:00
public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends
DatabaseStageEntry<Object2ObjectSortedMap<T, U>> {
Mono<US> at(@Nullable CompositeSnapshot snapshot, T key);
2022-01-22 23:21:40 +01:00
default Mono<Boolean> containsKey(@Nullable CompositeSnapshot snapshot, T key) {
2022-05-21 15:28:52 +02:00
return Mono.usingWhen(this.at(snapshot, key),
stage -> stage.isEmpty(snapshot).map(empty -> !empty),
2022-05-21 22:41:48 +02:00
LLUtils::finalizeResource
2022-05-21 15:28:52 +02:00
);
2022-01-22 23:21:40 +01:00
}
2022-05-21 23:49:06 +02:00
default Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T key) {
2022-05-21 15:28:52 +02:00
return Mono.usingWhen(this.at(snapshot, key),
2022-05-21 23:49:06 +02:00
stage -> stage.get(snapshot),
2022-05-21 22:41:48 +02:00
LLUtils::finalizeResource
2022-05-21 15:28:52 +02:00
);
2021-03-18 16:19:41 +01:00
}
default Mono<U> getValueOrDefault(@Nullable CompositeSnapshot snapshot, T key, Mono<U> defaultValue) {
return getValue(snapshot, key).switchIfEmpty(defaultValue).single();
}
default Mono<Void> putValue(T key, U value) {
2022-05-21 22:41:48 +02:00
return Mono.usingWhen(at(null, key).single(), stage -> stage.set(value), LLUtils::finalizeResource);
}
2021-05-02 19:18:15 +02:00
Mono<UpdateMode> getUpdateMode();
default Mono<U> updateValue(T key,
UpdateReturnMode updateReturnMode,
2021-08-22 21:23:22 +02:00
SerializationFunction<@Nullable U, @Nullable U> updater) {
2022-05-21 15:28:52 +02:00
return Mono.usingWhen(at(null, key).single(),
stage -> stage.update(updater, updateReturnMode),
2022-05-21 22:41:48 +02:00
LLUtils::finalizeResource
2022-05-21 15:28:52 +02:00
);
2021-03-18 16:19:41 +01:00
}
2021-11-08 10:49:59 +01:00
default Flux<Boolean> updateMulti(Flux<T> keys, KVSerializationFunction<T, @Nullable U, @Nullable U> updater) {
return keys.flatMapSequential(key -> this.updateValue(key, prevValue -> updater.apply(key, prevValue)));
2021-07-17 11:52:08 +02:00
}
2021-08-22 21:23:22 +02:00
default Mono<Boolean> updateValue(T key, SerializationFunction<@Nullable U, @Nullable U> updater) {
2022-03-02 18:33:58 +01:00
return updateValueAndGetDelta(key, updater).map(LLUtils::isDeltaChanged).single();
2021-05-08 03:09:00 +02:00
}
default Mono<Delta<U>> updateValueAndGetDelta(T key,
2021-08-22 21:23:22 +02:00
SerializationFunction<@Nullable U, @Nullable U> updater) {
2022-01-26 19:03:51 +01:00
var stageMono = this.at(null, key).single();
return stageMono.flatMap(stage -> stage
2022-03-02 18:33:58 +01:00
.updateAndGetDelta(updater)
2022-01-26 19:03:51 +01:00
.doFinally(s -> stage.close()));
2021-05-08 03:09:00 +02:00
}
default Mono<U> putValueAndGetPrevious(T key, U value) {
2022-05-21 15:28:52 +02:00
return Mono.usingWhen(at(null, key).single(),
stage -> stage.setAndGetPrevious(value),
2022-05-21 22:41:48 +02:00
LLUtils::finalizeResource
2022-05-21 15:28:52 +02:00
);
}
/**
* @return true if the key was associated with any value, false if the key didn't exist.
*/
2021-05-02 19:18:15 +02:00
default Mono<Boolean> putValueAndGetChanged(T key, U value) {
2022-05-21 15:28:52 +02:00
return Mono
2022-05-21 22:41:48 +02:00
.usingWhen(at(null, key).single(), stage -> stage.setAndGetChanged(value), LLUtils::finalizeResource)
2022-05-21 15:28:52 +02:00
.single();
}
default Mono<Void> remove(T key) {
return removeAndGetStatus(key).then();
}
default Mono<U> removeAndGetPrevious(T key) {
2022-05-21 22:41:48 +02:00
return Mono.usingWhen(at(null, key), DatabaseStage::clearAndGetPrevious, LLUtils::finalizeResource);
}
default Mono<Boolean> removeAndGetStatus(T key) {
return removeAndGetPrevious(key).map(o -> true).defaultIfEmpty(false);
}
2021-07-18 19:37:24 +02:00
/**
* GetMulti must return the elements in sequence!
*/
2022-05-21 23:49:06 +02:00
default Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys) {
2022-05-21 15:28:52 +02:00
return keys.flatMapSequential(key -> this
2022-05-21 23:49:06 +02:00
.getValue(snapshot, key)
2021-11-08 10:49:59 +01:00
.map(Optional::of)
2022-05-21 23:49:06 +02:00
.defaultIfEmpty(Optional.empty())
);
}
default Mono<Void> putMulti(Flux<Entry<T, U>> entries) {
return entries.flatMap(entry -> this.putValue(entry.getKey(), entry.getValue())).then();
}
2022-05-20 23:59:56 +02:00
Flux<SubStageEntry<T, US>> getAllStages(@Nullable CompositeSnapshot snapshot, boolean smallRange);
2022-03-24 23:56:23 +01:00
default Flux<Entry<T, U>> getAllValues(@Nullable CompositeSnapshot snapshot, boolean smallRange) {
return this
2022-03-24 23:56:23 +01:00
.getAllStages(snapshot, smallRange)
2021-09-23 20:57:28 +02:00
.flatMapSequential(stage -> stage
2021-03-14 03:13:19 +01:00
.getValue()
2022-05-21 23:49:06 +02:00
.get(snapshot)
2021-09-23 20:57:28 +02:00
.map(value -> Map.entry(stage.getKey(), value))
.doFinally(s -> stage.getValue().close())
2021-03-14 03:13:19 +01:00
);
}
default Mono<Void> setAllValues(Flux<Entry<T, U>> entries) {
return setAllValuesAndGetPrevious(entries).then();
}
2021-01-31 15:47:48 +01:00
Flux<Entry<T, U>> setAllValuesAndGetPrevious(Flux<Entry<T, U>> entries);
default Mono<Void> clear() {
return setAllValues(Flux.empty());
}
2022-03-24 23:56:23 +01:00
default Mono<Void> replaceAllValues(boolean canKeysChange,
Function<Entry<T, U>, Mono<Entry<T, U>>> entriesReplacer,
boolean smallRange) {
2021-03-14 13:08:03 +01:00
if (canKeysChange) {
2022-03-24 23:56:23 +01:00
return this.setAllValues(this.getAllValues(null, smallRange).flatMap(entriesReplacer)).then();
2021-03-14 13:08:03 +01:00
} else {
return this
2022-03-24 23:56:23 +01:00
.getAllValues(null, smallRange)
2021-03-14 13:08:03 +01:00
.flatMap(entriesReplacer)
.flatMap(replacedEntry -> this
.at(null, replacedEntry.getKey())
2021-09-23 20:57:28 +02:00
.flatMap(stage -> stage
.set(replacedEntry.getValue())
.doFinally(s -> stage.close())
)
)
2021-03-14 13:08:03 +01:00
.then();
}
}
default Mono<Void> replaceAll(Function<Entry<T, US>, Mono<Void>> entriesReplacer) {
return this
2022-03-24 23:56:23 +01:00
.getAllStages(null, false)
2021-09-23 20:57:28 +02:00
.flatMap(stage -> entriesReplacer.apply(stage)
.doFinally(s -> stage.getValue().close())
2021-05-02 19:18:15 +02:00
)
.then();
}
@Override
2021-12-18 18:16:56 +01:00
default Mono<Object2ObjectSortedMap<T, U>> setAndGetPrevious(Object2ObjectSortedMap<T, U> value) {
return this
2021-12-18 18:16:56 +01:00
.setAllValuesAndGetPrevious(Flux.fromIterable(value.entrySet()))
.collectMap(Entry::getKey, Entry::getValue, Object2ObjectLinkedOpenHashMap::new)
.map(map -> (Object2ObjectSortedMap<T, U>) map)
2021-09-23 20:57:28 +02:00
.filter(map -> !map.isEmpty());
}
2021-05-02 19:18:15 +02:00
@Override
2021-12-18 18:16:56 +01:00
default Mono<Boolean> setAndGetChanged(Object2ObjectSortedMap<T, U> value) {
2021-05-02 19:18:15 +02:00
return this
.setAndGetPrevious(value)
2021-09-23 20:57:28 +02:00
.map(oldValue -> !Objects.equals(oldValue, value.isEmpty() ? null : value))
2021-05-02 19:18:15 +02:00
.switchIfEmpty(Mono.fromSupplier(() -> !value.isEmpty()));
}
2021-02-06 19:21:31 +01:00
@Override
2022-03-02 18:33:58 +01:00
default Mono<Delta<Object2ObjectSortedMap<T, U>>> updateAndGetDelta(SerializationFunction<@Nullable Object2ObjectSortedMap<T, U>, @Nullable Object2ObjectSortedMap<T, U>> updater) {
2021-02-06 19:21:31 +01:00
return this
2021-05-02 19:18:15 +02:00
.getUpdateMode()
2021-02-06 19:21:31 +01:00
.single()
2021-05-02 19:18:15 +02:00
.flatMap(updateMode -> {
if (updateMode == UpdateMode.ALLOW_UNSAFE) {
return this
2022-03-24 23:56:23 +01:00
.getAllValues(null, true)
2021-12-18 18:16:56 +01:00
.collectMap(Entry::getKey, Entry::getValue, Object2ObjectLinkedOpenHashMap::new)
.map(map -> (Object2ObjectSortedMap<T, U>) map)
2021-05-02 19:18:15 +02:00
.single()
2021-12-18 18:16:56 +01:00
.<Tuple2<Optional<Object2ObjectSortedMap<T, U>>, Optional<Object2ObjectSortedMap<T, U>>>>handle((v, sink) -> {
2021-05-02 19:18:15 +02:00
if (v.isEmpty()) {
v = null;
}
2021-08-22 21:23:22 +02:00
try {
var result = updater.apply(v);
if (result != null && result.isEmpty()) {
result = null;
}
sink.next(Tuples.of(Optional.ofNullable(v), Optional.ofNullable(result)));
} catch (SerializationException ex) {
sink.error(ex);
2021-05-02 19:18:15 +02:00
}
})
.flatMap(result -> Mono
2021-05-08 03:09:00 +02:00
.justOrEmpty(result.getT2())
2021-12-18 18:16:56 +01:00
.flatMap(values -> this.setAllValues(Flux.fromIterable(values.entrySet())))
2021-05-21 00:19:40 +02:00
.thenReturn(new Delta<>(result.getT1().orElse(null), result.getT2().orElse(null)))
2021-05-02 19:18:15 +02:00
);
} else if (updateMode == UpdateMode.ALLOW) {
return Mono.fromCallable(() -> {
throw new UnsupportedOperationException("Maps can't be updated atomically");
});
} else if (updateMode == UpdateMode.DISALLOW) {
return Mono.fromCallable(() -> {
throw new UnsupportedOperationException("Map can't be updated because updates are disabled");
});
} else {
2021-05-02 19:18:15 +02:00
return Mono.fromCallable(() -> {
throw new UnsupportedOperationException("Unknown update mode: " + updateMode);
});
}
2021-05-02 19:18:15 +02:00
});
2021-02-06 19:21:31 +01:00
}
@Override
2021-12-18 18:16:56 +01:00
default Mono<Object2ObjectSortedMap<T, U>> clearAndGetPrevious() {
return this.setAndGetPrevious(Object2ObjectSortedMaps.emptyMap());
}
@Override
2022-05-21 23:49:06 +02:00
default Mono<Object2ObjectSortedMap<T, U>> get(@Nullable CompositeSnapshot snapshot) {
2021-09-23 20:57:28 +02:00
return this
2022-03-24 23:56:23 +01:00
.getAllValues(snapshot, true)
2021-12-18 18:16:56 +01:00
.collectMap(Entry::getKey, Entry::getValue, Object2ObjectLinkedOpenHashMap::new)
.map(map -> (Object2ObjectSortedMap<T, U>) map)
2021-09-23 11:30:44 +02:00
.filter(map -> !map.isEmpty());
}
@Override
2021-02-24 16:43:07 +01:00
default Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
2021-09-23 20:57:28 +02:00
return this
2022-03-24 23:56:23 +01:00
.getAllStages(snapshot, false)
2021-09-23 20:57:28 +02:00
.doOnNext(stage -> stage.getValue().close())
2021-05-02 19:18:15 +02:00
.count();
}
/**
* Value getter doesn't lock data. Please make sure to lock before getting data.
*/
2021-03-04 22:01:50 +01:00
default ValueGetterBlocking<T, U> getDbValueGetter(@Nullable CompositeSnapshot snapshot) {
return k -> getValue(snapshot, k).block();
}
2021-03-04 22:01:50 +01:00
default ValueGetter<T, U> getAsyncDbValueGetter(@Nullable CompositeSnapshot snapshot) {
return k -> getValue(snapshot, k);
}
2021-07-17 11:52:08 +02:00
default ValueTransformer<T, U> getAsyncDbValueTransformer(@Nullable CompositeSnapshot snapshot) {
2021-11-08 10:49:59 +01:00
return keys -> {
var sharedKeys = keys.publish().refCount(2);
2021-11-08 18:52:52 +01:00
var values = DatabaseStageMap.this.getMulti(snapshot, sharedKeys);
2021-11-08 10:49:59 +01:00
return Flux.zip(sharedKeys, values, Map::entry);
2021-07-17 11:52:08 +02:00
};
}
}