2021-01-30 20:16:14 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Buffer;
|
2021-01-30 20:16:14 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
2021-05-08 03:09:00 +02:00
|
|
|
import it.cavallium.dbengine.database.Delta;
|
2021-07-17 11:52:08 +02:00
|
|
|
import it.cavallium.dbengine.database.ExtraKeyOperationResult;
|
|
|
|
import it.cavallium.dbengine.database.KeyOperationResult;
|
2021-05-08 03:09:00 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
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-08-22 21:23:22 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.BiSerializationFunction;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationException;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationFunction;
|
2021-01-30 20:16:14 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
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;
|
2021-07-18 19:37:24 +02:00
|
|
|
import java.util.Queue;
|
2021-07-17 11:52:08 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2021-07-18 19:37:24 +02:00
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2021-07-17 11:52:08 +02:00
|
|
|
import java.util.function.BiFunction;
|
2021-01-30 20:16:14 +01:00
|
|
|
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;
|
2021-05-08 03:09:00 +02:00
|
|
|
import reactor.util.function.Tuple3;
|
2021-05-02 19:18:15 +02:00
|
|
|
import reactor.util.function.Tuples;
|
2021-01-30 20:16:14 +01:00
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
2021-01-31 00:36:21 +01:00
|
|
|
public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends DatabaseStageEntry<Map<T, U>> {
|
2021-01-30 20:16:14 +01:00
|
|
|
|
|
|
|
Mono<US> at(@Nullable CompositeSnapshot snapshot, T key);
|
|
|
|
|
2021-03-18 16:19:41 +01:00
|
|
|
default Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T key, boolean existsAlmostCertainly) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
this.at(snapshot, key),
|
|
|
|
stage -> stage.get(snapshot, existsAlmostCertainly),
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
);
|
2021-03-18 16:19:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
default Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T key) {
|
2021-03-18 16:19:41 +01:00
|
|
|
return getValue(snapshot, key, false);
|
2021-01-30 20:16:14 +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) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
at(null, key).single(),
|
|
|
|
stage -> stage.set(value),
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-02 19:18:15 +02:00
|
|
|
Mono<UpdateMode> getUpdateMode();
|
|
|
|
|
2021-08-22 18:20:05 +02:00
|
|
|
default Mono<U> updateValue(T key,
|
|
|
|
UpdateReturnMode updateReturnMode,
|
|
|
|
boolean existsAlmostCertainly,
|
2021-08-22 21:23:22 +02:00
|
|
|
SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
this.at(null, key).single(),
|
|
|
|
stage -> stage.update(updater, updateReturnMode, existsAlmostCertainly),
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
);
|
2021-03-18 16:19:41 +01:00
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default <X> Flux<ExtraKeyOperationResult<T, X>> updateMulti(Flux<Tuple2<T, X>> entries,
|
|
|
|
BiSerializationFunction<@Nullable U, X, @Nullable U> updater) {
|
2021-07-17 11:52:08 +02:00
|
|
|
return entries
|
|
|
|
.flatMapSequential(entry -> this
|
|
|
|
.updateValue(entry.getT1(), prevValue -> updater.apply(prevValue, entry.getT2()))
|
|
|
|
.map(changed -> new ExtraKeyOperationResult<>(entry.getT1(), entry.getT2(), changed))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<U> updateValue(T key, UpdateReturnMode updateReturnMode, SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return updateValue(key, updateReturnMode, false, updater);
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<Boolean> updateValue(T key, SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return updateValueAndGetDelta(key, false, updater).map(LLUtils::isDeltaChanged).single();
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<Boolean> updateValue(T key, boolean existsAlmostCertainly, SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return updateValueAndGetDelta(key, existsAlmostCertainly, updater).map(LLUtils::isDeltaChanged).single();
|
|
|
|
}
|
|
|
|
|
2021-08-22 18:20:05 +02:00
|
|
|
default Mono<Delta<U>> updateValueAndGetDelta(T key,
|
|
|
|
boolean existsAlmostCertainly,
|
2021-08-22 21:23:22 +02:00
|
|
|
SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
this.at(null, key).single(),
|
|
|
|
stage -> stage.updateAndGetDelta(updater, existsAlmostCertainly),
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<Delta<U>> updateValueAndGetDelta(T key, SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return updateValueAndGetDelta(key, false, updater);
|
2021-02-06 19:21:31 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
default Mono<U> putValueAndGetPrevious(T key, U value) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
at(null, key).single(),
|
|
|
|
stage -> stage.setAndGetPrevious(value),
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +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) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
at(null, key).single(),
|
|
|
|
stage -> stage.setAndGetChanged(value),
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
).single();
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default Mono<Void> remove(T key) {
|
|
|
|
return removeAndGetStatus(key).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
default Mono<U> removeAndGetPrevious(T key) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return Mono.usingWhen(
|
|
|
|
at(null, key),
|
|
|
|
DatabaseStage::clearAndGetPrevious,
|
|
|
|
stage -> Mono.fromRunnable(stage::release)
|
|
|
|
);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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!
|
|
|
|
*/
|
2021-07-23 15:20:33 +02:00
|
|
|
default Flux<Entry<T, Optional<U>>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys, boolean existsAlmostCertainly) {
|
2021-07-18 19:37:24 +02:00
|
|
|
return keys
|
|
|
|
.flatMapSequential(key -> this
|
|
|
|
.getValue(snapshot, key, existsAlmostCertainly)
|
2021-07-23 15:20:33 +02:00
|
|
|
.map(value -> Map.entry(key, Optional.of(value)))
|
|
|
|
.switchIfEmpty(Mono.fromSupplier(() -> Map.entry(key, Optional.empty())))
|
2021-09-08 00:22:39 +02:00
|
|
|
)
|
|
|
|
.doOnDiscard(Entry.class, unknownEntry -> {
|
|
|
|
if (unknownEntry.getValue() instanceof Optional optionalBuffer
|
|
|
|
&& optionalBuffer.isPresent()
|
|
|
|
&& optionalBuffer.get() instanceof Buffer buffer) {
|
|
|
|
buffer.close();
|
|
|
|
}
|
|
|
|
});
|
2021-03-18 16:19:41 +01:00
|
|
|
}
|
|
|
|
|
2021-07-18 19:37:24 +02:00
|
|
|
/**
|
|
|
|
* GetMulti must return the elements in sequence!
|
|
|
|
*/
|
2021-07-23 15:20:33 +02:00
|
|
|
default Flux<Entry<T, Optional<U>>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys) {
|
2021-03-18 16:19:41 +01:00
|
|
|
return getMulti(snapshot, keys, false);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default Mono<Void> putMulti(Flux<Entry<T, U>> entries) {
|
|
|
|
return entries.flatMap(entry -> this.putValue(entry.getKey(), entry.getValue())).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
Flux<Entry<T, US>> getAllStages(@Nullable CompositeSnapshot snapshot);
|
|
|
|
|
|
|
|
default Flux<Entry<T, U>> getAllValues(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return this
|
2021-03-11 15:47:19 +01:00
|
|
|
.getAllStages(snapshot)
|
2021-03-14 03:13:19 +01:00
|
|
|
.flatMapSequential(entry -> entry
|
|
|
|
.getValue()
|
2021-03-18 16:19:41 +01:00
|
|
|
.get(snapshot, true)
|
2021-03-14 03:13:19 +01:00
|
|
|
.map(value -> Map.entry(entry.getKey(), value))
|
2021-05-12 21:41:47 +02:00
|
|
|
.doAfterTerminate(() -> entry.getValue().release())
|
2021-03-14 03:13:19 +01:00
|
|
|
);
|
2021-01-30 20:16:14 +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);
|
2021-01-30 20:16:14 +01:00
|
|
|
|
|
|
|
default Mono<Void> clear() {
|
|
|
|
return setAllValues(Flux.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
default Mono<Void> replaceAllValues(boolean canKeysChange, Function<Entry<T, U>, Mono<Entry<T, U>>> entriesReplacer) {
|
2021-03-14 13:08:03 +01:00
|
|
|
if (canKeysChange) {
|
|
|
|
return this.setAllValues(this.getAllValues(null).flatMap(entriesReplacer)).then();
|
|
|
|
} else {
|
|
|
|
return this
|
|
|
|
.getAllValues(null)
|
|
|
|
.flatMap(entriesReplacer)
|
|
|
|
.flatMap(replacedEntry -> this
|
|
|
|
.at(null, replacedEntry.getKey())
|
2021-05-12 21:41:47 +02:00
|
|
|
.flatMap(v -> v.set(replacedEntry.getValue()).doAfterTerminate(v::release)))
|
2021-03-14 13:08:03 +01:00
|
|
|
.then();
|
|
|
|
}
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default Mono<Void> replaceAll(Function<Entry<T, US>, Mono<Void>> entriesReplacer) {
|
|
|
|
return this
|
|
|
|
.getAllStages(null)
|
2021-05-02 19:18:15 +02:00
|
|
|
.flatMap(stage -> Mono
|
|
|
|
.defer(() -> entriesReplacer.apply(stage))
|
2021-05-12 21:41:47 +02:00
|
|
|
.doAfterTerminate(() -> stage.getValue().release())
|
2021-05-02 19:18:15 +02:00
|
|
|
)
|
2021-01-30 20:16:14 +01:00
|
|
|
.then();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
default Mono<Map<T, U>> setAndGetPrevious(Map<T, U> value) {
|
|
|
|
return this
|
2021-06-30 10:58:49 +02:00
|
|
|
.setAllValuesAndGetPrevious(Flux.fromIterable(Map.copyOf(value).entrySet()))
|
2021-01-30 20:16:14 +01:00
|
|
|
.collectMap(Entry::getKey, Entry::getValue, HashMap::new);
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:18:15 +02:00
|
|
|
@Override
|
|
|
|
default Mono<Boolean> setAndGetChanged(Map<T, U> value) {
|
|
|
|
return this
|
|
|
|
.setAndGetPrevious(value)
|
|
|
|
.map(oldValue -> !Objects.equals(oldValue, value))
|
|
|
|
.switchIfEmpty(Mono.fromSupplier(() -> !value.isEmpty()));
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:21:31 +01:00
|
|
|
@Override
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<Delta<Map<T, U>>> updateAndGetDelta(SerializationFunction<@Nullable Map<T, U>, @Nullable Map<T, U>> updater,
|
2021-05-08 03:09:00 +02:00
|
|
|
boolean existsAlmostCertainly) {
|
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
|
|
|
|
.getAllValues(null)
|
|
|
|
.collectMap(Entry::getKey, Entry::getValue, HashMap::new)
|
|
|
|
.single()
|
2021-05-08 03:09:00 +02:00
|
|
|
.<Tuple2<Optional<Map<T, U>>, Optional<Map<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-06-30 10:58:49 +02:00
|
|
|
.flatMap(values -> this.setAllValues(Flux.fromIterable(Map.copyOf(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");
|
|
|
|
});
|
2021-04-30 19:15:04 +02:00
|
|
|
} else {
|
2021-05-02 19:18:15 +02:00
|
|
|
return Mono.fromCallable(() -> {
|
|
|
|
throw new UnsupportedOperationException("Unknown update mode: " + updateMode);
|
|
|
|
});
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-05-02 19:18:15 +02:00
|
|
|
});
|
2021-02-06 19:21:31 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
@Override
|
|
|
|
default Mono<Map<T, U>> clearAndGetPrevious() {
|
|
|
|
return this.setAndGetPrevious(Map.of());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-18 16:19:41 +01:00
|
|
|
default Mono<Map<T, U>> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
|
2021-01-30 20:16:14 +01:00
|
|
|
return getAllValues(snapshot)
|
2021-09-23 11:30:44 +02:00
|
|
|
.collectMap(Entry::getKey, Entry::getValue, HashMap::new)
|
|
|
|
.filter(map -> !map.isEmpty());
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-24 16:43:07 +01:00
|
|
|
default Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
2021-05-02 19:18:15 +02:00
|
|
|
return getAllStages(snapshot)
|
|
|
|
.flatMap(stage -> Mono
|
|
|
|
.fromRunnable(() -> stage.getValue().release())
|
|
|
|
.thenReturn(true)
|
|
|
|
)
|
|
|
|
.count();
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 22:01:50 +01:00
|
|
|
default ValueGetter<T, U> getAsyncDbValueGetter(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return k -> getValue(snapshot, k);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
2021-07-17 11:52:08 +02:00
|
|
|
|
|
|
|
default ValueTransformer<T, U> getAsyncDbValueTransformer(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return new ValueTransformer<>() {
|
|
|
|
@Override
|
2021-07-23 15:20:33 +02:00
|
|
|
public <X> Flux<Tuple3<X, T, Optional<U>>> transform(Flux<Tuple2<X, T>> keys) {
|
2021-07-17 11:52:08 +02:00
|
|
|
return Flux.defer(() -> {
|
2021-07-18 19:37:24 +02:00
|
|
|
ConcurrentLinkedQueue<X> extraValues = new ConcurrentLinkedQueue<>();
|
|
|
|
return getMulti(snapshot, keys.map(key -> {
|
|
|
|
extraValues.add(key.getT1());
|
|
|
|
return key.getT2();
|
|
|
|
})).map(result -> {
|
|
|
|
var extraValue = extraValues.remove();
|
|
|
|
return Tuples.of(extraValue, result.getKey(), result.getValue());
|
|
|
|
});
|
2021-07-17 11:52:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|