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-11-08 12:06:32 +01:00
|
|
|
import io.net5.buffer.api.Resource;
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Send;
|
|
|
|
import io.net5.buffer.api.internal.ResourceSupport;
|
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-01-30 20:16:14 +01:00
|
|
|
import it.cavallium.dbengine.database.LLDictionary;
|
2021-01-31 21:23:43 +01:00
|
|
|
import it.cavallium.dbengine.database.LLDictionaryResultType;
|
2021-08-28 22:42:51 +02:00
|
|
|
import it.cavallium.dbengine.database.LLEntry;
|
2021-01-31 21:23:43 +01: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-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-02-02 19:40:37 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
2021-06-06 02:23:51 +02:00
|
|
|
import java.util.Collections;
|
2021-01-31 21:23:43 +01:00
|
|
|
import java.util.HashMap;
|
2021-09-08 00:22:39 +02:00
|
|
|
import java.util.List;
|
2021-01-30 20:16:14 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2021-05-02 19:18:15 +02:00
|
|
|
import java.util.Objects;
|
2021-07-23 15:20:33 +02:00
|
|
|
import java.util.Optional;
|
2021-09-23 02:15:58 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2021-01-30 20:16:14 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
2021-08-22 21:23:22 +02:00
|
|
|
import reactor.core.publisher.SynchronousSink;
|
2021-07-17 11:52:08 +02:00
|
|
|
import reactor.util.function.Tuple2;
|
|
|
|
import reactor.util.function.Tuples;
|
2021-01-30 20:16:14 +01:00
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
/**
|
|
|
|
* Optimized implementation of "DatabaseMapDictionary with SubStageGetterSingle"
|
|
|
|
*/
|
|
|
|
public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U, DatabaseStageEntry<U>> {
|
2021-01-30 20:16:14 +01:00
|
|
|
|
2021-09-02 17:15:40 +02:00
|
|
|
private final Serializer<U> valueSerializer;
|
2021-01-30 20:16:14 +01:00
|
|
|
|
2021-02-01 11:00:27 +01:00
|
|
|
protected DatabaseMapDictionary(LLDictionary dictionary,
|
2021-09-23 02:15:58 +02:00
|
|
|
@NotNull Send<Buffer> prefixKey,
|
2021-09-02 17:15:40 +02:00
|
|
|
SerializerFixedBinaryLength<T> keySuffixSerializer,
|
2021-09-23 20:57:28 +02:00
|
|
|
Serializer<U> valueSerializer,
|
2021-10-01 19:17:33 +02:00
|
|
|
Runnable onClose) {
|
2021-04-30 19:15:04 +02:00
|
|
|
// Do not retain or release or use the prefixKey here
|
2021-10-01 19:17:33 +02:00
|
|
|
super(dictionary, prefixKey, keySuffixSerializer, new SubStageGetterSingle<>(valueSerializer), 0, onClose);
|
2021-01-31 21:23:43 +01:00
|
|
|
this.valueSerializer = valueSerializer;
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
public static <T, U> DatabaseMapDictionary<T, U> simple(LLDictionary dictionary,
|
2021-09-02 17:15:40 +02:00
|
|
|
SerializerFixedBinaryLength<T> keySerializer,
|
2021-09-23 20:57:28 +02:00
|
|
|
Serializer<U> valueSerializer,
|
2021-10-01 19:17:33 +02:00
|
|
|
Runnable onClose) {
|
2021-09-23 20:57:28 +02:00
|
|
|
return new DatabaseMapDictionary<>(dictionary, LLUtils.empty(dictionary.getAllocator()), keySerializer,
|
2021-10-01 19:17:33 +02:00
|
|
|
valueSerializer, onClose);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
public static <T, U> DatabaseMapDictionary<T, U> tail(LLDictionary dictionary,
|
2021-08-29 23:18:03 +02:00
|
|
|
Send<Buffer> prefixKey,
|
2021-09-02 17:15:40 +02:00
|
|
|
SerializerFixedBinaryLength<T> keySuffixSerializer,
|
2021-09-23 20:57:28 +02:00
|
|
|
Serializer<U> valueSerializer,
|
2021-10-01 19:17:33 +02:00
|
|
|
Runnable onClose) {
|
|
|
|
return new DatabaseMapDictionary<>(dictionary, prefixKey, keySuffixSerializer, valueSerializer, onClose);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 00:22:05 +02:00
|
|
|
private void deserializeValue(Send<Buffer> valueToReceive, SynchronousSink<U> sink) {
|
|
|
|
try (var value = valueToReceive.receive()) {
|
|
|
|
sink.next(valueSerializer.deserialize(value));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Send<Buffer> serializeValue(U value) throws SerializationException {
|
|
|
|
var valSizeHint = valueSerializer.getSerializedSizeHint();
|
|
|
|
if (valSizeHint == -1) valSizeHint = 128;
|
|
|
|
try (var valBuf = dictionary.getAllocator().allocate(valSizeHint)) {
|
|
|
|
valueSerializer.serialize(value, valBuf);
|
|
|
|
return valBuf.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Send<Buffer> serializeKeySuffixToKey(T keySuffix) throws SerializationException {
|
|
|
|
try (var keyBuf = keyPrefix.copy()) {
|
|
|
|
assert keyBuf.readableBytes() == keyPrefixLength;
|
|
|
|
keyBuf.ensureWritable(keySuffixLength + keyExtLength);
|
|
|
|
serializeSuffix(keySuffix, keyBuf);
|
|
|
|
assert keyBuf.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength;
|
|
|
|
return keyBuf.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 02:15:58 +02:00
|
|
|
private Send<Buffer> toKey(Send<Buffer> suffixKeyToSend) {
|
|
|
|
try (var suffixKey = suffixKeyToSend.receive()) {
|
2021-10-19 00:22:05 +02:00
|
|
|
assert suffixKeyLengthConsistency(suffixKey.readableBytes());
|
2021-09-23 02:15:58 +02:00
|
|
|
if (keyPrefix.readableBytes() > 0) {
|
|
|
|
try (var result = LLUtils.compositeBuffer(dictionary.getAllocator(),
|
|
|
|
LLUtils.copy(dictionary.getAllocator(), keyPrefix),
|
|
|
|
suffixKey.send()
|
|
|
|
)) {
|
|
|
|
assert result.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength;
|
|
|
|
return result.send();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert suffixKey.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength;
|
|
|
|
return suffixKey.send();
|
|
|
|
}
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
2021-03-18 16:19:41 +01:00
|
|
|
public Mono<Map<T, U>> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return dictionary
|
|
|
|
.getRange(resolveSnapshot(snapshot), rangeMono, existsAlmostCertainly)
|
2021-08-31 09:14:46 +02:00
|
|
|
.<Entry<T, U>>handle((entrySend, sink) -> {
|
2021-10-19 00:22:05 +02:00
|
|
|
Entry<T, U> deserializedEntry;
|
|
|
|
try {
|
|
|
|
try (var entry = entrySend.receive()) {
|
|
|
|
T key;
|
|
|
|
try (var serializedKey = entry.getKey().receive()) {
|
|
|
|
splitPrefix(serializedKey).close();
|
|
|
|
suffixKeyLengthConsistency(serializedKey.readableBytes());
|
|
|
|
key = deserializeSuffix(serializedKey);
|
|
|
|
}
|
|
|
|
U value;
|
|
|
|
try (var valueBuf = entry.getValue().receive()) {
|
|
|
|
value = valueSerializer.deserialize(valueBuf);
|
|
|
|
}
|
|
|
|
deserializedEntry = Map.entry(key, value);
|
2021-09-23 02:15:58 +02:00
|
|
|
}
|
2021-10-19 00:22:05 +02:00
|
|
|
sink.next(deserializedEntry);
|
|
|
|
} catch (Throwable ex) {
|
2021-08-22 21:23:22 +02:00
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collectMap(Entry::getKey, Entry::getValue, HashMap::new)
|
2021-08-22 18:20:05 +02:00
|
|
|
.filter(map -> !map.isEmpty());
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Map<T, U>> setAndGetPrevious(Map<T, U> value) {
|
2021-08-22 21:23:22 +02:00
|
|
|
return this
|
|
|
|
.get(null, false)
|
|
|
|
.concatWith(dictionary.setRange(rangeMono, Flux
|
2021-08-22 19:54:23 +02:00
|
|
|
.fromIterable(Collections.unmodifiableMap(value).entrySet())
|
2021-10-19 00:22:05 +02:00
|
|
|
.handle(this::serializeEntrySink)
|
2021-08-22 21:23:22 +02:00
|
|
|
).then(Mono.empty()))
|
|
|
|
.singleOrEmpty()
|
|
|
|
.transform(LLUtils::handleDiscard);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Map<T, U>> clearAndGetPrevious() {
|
2021-05-02 19:18:15 +02:00
|
|
|
return this
|
|
|
|
.setAndGetPrevious(Map.of());
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
2021-02-24 16:43:07 +01:00
|
|
|
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return dictionary.sizeRange(resolveSnapshot(snapshot), rangeMono, fast);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 03:13:19 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return dictionary.isRangeEmpty(resolveSnapshot(snapshot), rangeMono);
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<DatabaseStageEntry<U>> at(@Nullable CompositeSnapshot snapshot, T keySuffix) {
|
2021-09-02 17:15:40 +02:00
|
|
|
return Mono.fromCallable(() ->
|
2021-10-19 00:22:05 +02:00
|
|
|
new DatabaseSingle<>(dictionary, serializeKeySuffixToKey(keySuffix), valueSerializer, null));
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
2021-03-18 16:19:41 +01:00
|
|
|
public Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T keySuffix, boolean existsAlmostCertainly) {
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
2021-09-22 18:33:28 +02:00
|
|
|
.get(resolveSnapshot(snapshot),
|
2021-10-19 00:22:05 +02:00
|
|
|
Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix)),
|
2021-09-22 18:33:28 +02:00
|
|
|
existsAlmostCertainly
|
|
|
|
)
|
2021-09-30 18:25:36 +02:00
|
|
|
.handle(this::deserializeValue);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Void> putValue(T keySuffix, U value) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix)).single();
|
|
|
|
var valueMono = Mono.fromCallable(() -> serializeValue(value)).single();
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
|
|
|
.put(keyMono, valueMono, LLDictionaryResultType.VOID)
|
|
|
|
.doOnNext(Send::close)
|
|
|
|
.then();
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-02 19:18:15 +02:00
|
|
|
@Override
|
|
|
|
public Mono<UpdateMode> getUpdateMode() {
|
|
|
|
return dictionary.getUpdateMode();
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:21:31 +01:00
|
|
|
@Override
|
2021-05-08 03:09:00 +02:00
|
|
|
public Mono<U> updateValue(T keySuffix,
|
|
|
|
UpdateReturnMode updateReturnMode,
|
2021-03-18 16:19:41 +01:00
|
|
|
boolean existsAlmostCertainly,
|
2021-08-22 21:23:22 +02:00
|
|
|
SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
|
|
|
.update(keyMono, getSerializedUpdater(updater), updateReturnMode, existsAlmostCertainly)
|
2021-09-30 18:25:36 +02:00
|
|
|
.handle(this::deserializeValue);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Delta<U>> updateValueAndGetDelta(T keySuffix,
|
|
|
|
boolean existsAlmostCertainly,
|
2021-08-22 21:23:22 +02:00
|
|
|
SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
|
|
|
.updateAndGetDelta(keyMono, getSerializedUpdater(updater), existsAlmostCertainly)
|
2021-10-19 00:22:05 +02:00
|
|
|
.transform(mono -> LLUtils.mapLLDelta(mono, serializedToReceive -> {
|
|
|
|
try (var serialized = serializedToReceive.receive()) {
|
|
|
|
return valueSerializer.deserialize(serialized);
|
|
|
|
}
|
|
|
|
}));
|
2021-02-06 19:21:31 +01:00
|
|
|
}
|
|
|
|
|
2021-08-31 09:14:46 +02:00
|
|
|
public SerializationFunction<@Nullable Send<Buffer>, @Nullable Send<Buffer>> getSerializedUpdater(
|
|
|
|
SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2021-07-17 11:52:08 +02:00
|
|
|
return oldSerialized -> {
|
2021-08-31 09:14:46 +02:00
|
|
|
try (oldSerialized) {
|
2021-08-22 21:23:22 +02:00
|
|
|
U result;
|
|
|
|
if (oldSerialized == null) {
|
|
|
|
result = updater.apply(null);
|
|
|
|
} else {
|
2021-10-19 00:22:05 +02:00
|
|
|
try (var oldSerializedReceived = oldSerialized.receive()) {
|
|
|
|
result = updater.apply(valueSerializer.deserialize(oldSerializedReceived));
|
|
|
|
}
|
2021-08-22 21:23:22 +02:00
|
|
|
}
|
2021-07-17 11:52:08 +02:00
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2021-10-19 00:22:05 +02:00
|
|
|
return serializeValue(result);
|
2021-07-17 11:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-08 10:49:59 +01:00
|
|
|
public KVSerializationFunction<@NotNull T, @Nullable Send<Buffer>, @Nullable Send<Buffer>> getSerializedUpdater(
|
|
|
|
KVSerializationFunction<@NotNull T, @Nullable U, @Nullable U> updater) {
|
|
|
|
return (key, oldSerialized) -> {
|
2021-08-31 09:14:46 +02:00
|
|
|
try (oldSerialized) {
|
2021-08-22 21:23:22 +02:00
|
|
|
U result;
|
|
|
|
if (oldSerialized == null) {
|
2021-11-08 10:49:59 +01:00
|
|
|
result = updater.apply(key, null);
|
2021-08-22 21:23:22 +02:00
|
|
|
} else {
|
2021-10-19 00:22:05 +02:00
|
|
|
try (var oldSerializedReceived = oldSerialized.receive()) {
|
2021-11-08 10:49:59 +01:00
|
|
|
result = updater.apply(key, valueSerializer.deserialize(oldSerializedReceived));
|
2021-10-19 00:22:05 +02:00
|
|
|
}
|
2021-08-22 21:23:22 +02:00
|
|
|
}
|
2021-07-17 11:52:08 +02:00
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2021-10-19 00:22:05 +02:00
|
|
|
return serializeValue(result);
|
2021-07-17 11:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<U> putValueAndGetPrevious(T keySuffix, U value) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
|
|
|
var valueMono = Mono.fromCallable(() -> serializeValue(value));
|
|
|
|
return dictionary.put(keyMono, valueMono, LLDictionaryResultType.PREVIOUS_VALUE).handle(this::deserializeValue);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
2021-05-02 19:18:15 +02:00
|
|
|
public Mono<Boolean> putValueAndGetChanged(T keySuffix, U value) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
|
|
|
var valueMono = Mono.fromCallable(() -> serializeValue(value));
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
|
|
|
.put(keyMono, valueMono, LLDictionaryResultType.PREVIOUS_VALUE)
|
2021-09-30 18:25:36 +02:00
|
|
|
.handle(this::deserializeValue)
|
2021-08-31 09:14:46 +02:00
|
|
|
.map(oldValue -> !Objects.equals(oldValue, value))
|
|
|
|
.defaultIfEmpty(value != null);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Void> remove(T keySuffix) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
|
|
|
.remove(keyMono, LLDictionaryResultType.VOID)
|
|
|
|
.doOnNext(Send::close)
|
|
|
|
.then();
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<U> removeAndGetPrevious(T keySuffix) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
|
|
|
return dictionary.remove(keyMono, LLDictionaryResultType.PREVIOUS_VALUE).handle(this::deserializeValue);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:23:43 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Boolean> removeAndGetStatus(T keySuffix) {
|
2021-10-19 00:22:05 +02:00
|
|
|
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
|
2021-08-31 09:14:46 +02:00
|
|
|
return dictionary
|
|
|
|
.remove(keyMono, LLDictionaryResultType.PREVIOUS_VALUE_EXISTENCE)
|
|
|
|
.map(LLUtils::responseToBoolean);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-08 10:49:59 +01:00
|
|
|
public Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys, boolean existsAlmostCertainly) {
|
2021-08-31 09:14:46 +02:00
|
|
|
var mappedKeys = keys
|
2021-11-08 10:49:59 +01:00
|
|
|
.<Send<Buffer>>handle((keySuffix, sink) -> {
|
2021-08-28 22:42:51 +02:00
|
|
|
try {
|
2021-11-08 10:49:59 +01:00
|
|
|
var buf = serializeKeySuffixToKey(keySuffix);
|
|
|
|
sink.next(buf);
|
2021-10-19 00:22:05 +02:00
|
|
|
} catch (Throwable ex) {
|
2021-08-31 09:14:46 +02:00
|
|
|
sink.error(ex);
|
2021-08-22 21:23:22 +02:00
|
|
|
}
|
2021-08-31 09:14:46 +02:00
|
|
|
});
|
|
|
|
return dictionary
|
|
|
|
.getMulti(resolveSnapshot(snapshot), mappedKeys, existsAlmostCertainly)
|
2021-11-08 10:49:59 +01:00
|
|
|
.<Optional<U>>handle((valueBufOpt, sink) -> {
|
2021-08-31 09:14:46 +02:00
|
|
|
try {
|
|
|
|
Optional<U> valueOpt;
|
2021-11-08 10:49:59 +01:00
|
|
|
if (valueBufOpt.isPresent()) {
|
|
|
|
try (var buf = valueBufOpt.get().receive()) {
|
2021-10-19 00:22:05 +02:00
|
|
|
valueOpt = Optional.of(valueSerializer.deserialize(buf));
|
2021-08-31 09:14:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueOpt = Optional.empty();
|
|
|
|
}
|
2021-11-08 10:49:59 +01:00
|
|
|
sink.next(valueOpt);
|
2021-10-19 00:22:05 +02:00
|
|
|
} catch (Throwable ex) {
|
2021-08-31 09:14:46 +02:00
|
|
|
sink.error(ex);
|
2021-09-08 00:22:39 +02:00
|
|
|
} finally {
|
2021-11-08 10:49:59 +01:00
|
|
|
valueBufOpt.ifPresent(Send::close);
|
2021-08-31 09:14:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.transform(LLUtils::handleDiscard);
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 00:22:05 +02:00
|
|
|
private Send<LLEntry> serializeEntry(T keySuffix, U value) throws SerializationException {
|
|
|
|
try (var key = serializeKeySuffixToKey(keySuffix)) {
|
|
|
|
try (var serializedValue = serializeValue(value)) {
|
|
|
|
return LLEntry.of(key, serializedValue).send();
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 00:22:05 +02:00
|
|
|
private void serializeEntrySink(Entry<T,U> entry, SynchronousSink<Send<LLEntry>> sink) {
|
|
|
|
try {
|
|
|
|
sink.next(serializeEntry(entry.getKey(), entry.getValue()));
|
|
|
|
} catch (Throwable e) {
|
|
|
|
sink.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 02:21:53 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Void> putMulti(Flux<Entry<T, U>> entries) {
|
2021-05-02 19:18:15 +02:00
|
|
|
var serializedEntries = entries
|
2021-08-31 09:14:46 +02:00
|
|
|
.<Send<LLEntry>>handle((entry, sink) -> {
|
2021-08-28 22:42:51 +02:00
|
|
|
try {
|
|
|
|
sink.next(serializeEntry(entry.getKey(), entry.getValue()));
|
2021-10-19 00:22:05 +02:00
|
|
|
} catch (Throwable e) {
|
2021-08-28 22:42:51 +02:00
|
|
|
sink.error(e);
|
|
|
|
}
|
2021-09-08 00:22:39 +02:00
|
|
|
})
|
2021-11-08 12:06:32 +01:00
|
|
|
.doOnDiscard(Send.class, Send::close)
|
|
|
|
.doOnDiscard(Resource.class, Resource::close);
|
2021-02-01 02:21:53 +01:00
|
|
|
return dictionary
|
2021-05-02 19:18:15 +02:00
|
|
|
.putMulti(serializedEntries, false)
|
2021-08-28 22:42:51 +02:00
|
|
|
.then()
|
2021-09-08 00:22:39 +02:00
|
|
|
.doOnDiscard(Send.class, Send::close)
|
2021-11-08 12:06:32 +01:00
|
|
|
.doOnDiscard(Resource.class, Resource::close)
|
2021-09-08 00:22:39 +02:00
|
|
|
.doOnDiscard(LLEntry.class, ResourceSupport::close)
|
|
|
|
.doOnDiscard(List.class, list -> {
|
|
|
|
for (Object o : list) {
|
|
|
|
if (o instanceof Send send) {
|
|
|
|
send.close();
|
|
|
|
} else if (o instanceof Buffer buf) {
|
|
|
|
buf.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-02-01 02:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-07-17 11:52:08 +02:00
|
|
|
@Override
|
2021-11-08 10:49:59 +01:00
|
|
|
public Flux<Boolean> updateMulti(Flux<T> keys,
|
|
|
|
KVSerializationFunction<T, @Nullable U, @Nullable U> updater) {
|
|
|
|
var sharedKeys = keys.publish().refCount(2);
|
|
|
|
var serializedKeys = sharedKeys
|
|
|
|
.<Send<Buffer>>handle((key, sink) -> {
|
2021-08-31 09:14:46 +02:00
|
|
|
try {
|
2021-11-08 10:49:59 +01:00
|
|
|
Send<Buffer> serializedKey = serializeKeySuffixToKey(key);
|
|
|
|
sink.next(serializedKey);
|
2021-10-19 00:22:05 +02:00
|
|
|
} catch (Throwable ex) {
|
2021-08-31 09:14:46 +02:00
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
})
|
2021-07-23 22:18:08 +02:00
|
|
|
.doOnDiscard(Tuple2.class, uncastedEntry -> {
|
2021-08-29 23:18:03 +02:00
|
|
|
if (uncastedEntry.getT1() instanceof Buffer byteBuf) {
|
2021-08-31 09:14:46 +02:00
|
|
|
byteBuf.close();
|
2021-07-23 22:18:08 +02:00
|
|
|
}
|
2021-08-29 23:18:03 +02:00
|
|
|
if (uncastedEntry.getT2() instanceof Buffer byteBuf) {
|
2021-08-31 09:14:46 +02:00
|
|
|
byteBuf.close();
|
2021-07-23 22:18:08 +02:00
|
|
|
}
|
|
|
|
});
|
2021-07-17 11:52:08 +02:00
|
|
|
var serializedUpdater = getSerializedUpdater(updater);
|
2021-11-08 10:49:59 +01:00
|
|
|
return dictionary.updateMulti(sharedKeys, serializedKeys, serializedUpdater);
|
2021-07-17 11:52:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
@Override
|
2021-01-31 21:23:43 +01:00
|
|
|
public Flux<Entry<T, DatabaseStageEntry<U>>> getAllStages(@Nullable CompositeSnapshot snapshot) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return dictionary
|
|
|
|
.getRangeKeys(resolveSnapshot(snapshot), rangeMono)
|
2021-09-23 02:15:58 +02:00
|
|
|
.handle((keyBufToReceive, sink) -> {
|
|
|
|
try (var keyBuf = keyBufToReceive.receive()) {
|
|
|
|
assert keyBuf.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength;
|
|
|
|
// Remove prefix. Keep only the suffix and the ext
|
2021-10-19 00:22:05 +02:00
|
|
|
splitPrefix(keyBuf).close();
|
|
|
|
suffixKeyLengthConsistency(keyBuf.readableBytes());
|
|
|
|
T keySuffix;
|
|
|
|
try (var keyBufCopy = keyBuf.copy()) {
|
|
|
|
keySuffix = deserializeSuffix(keyBufCopy);
|
|
|
|
}
|
|
|
|
var subStage = new DatabaseSingle<>(dictionary, toKey(keyBuf.send()), valueSerializer, null);
|
|
|
|
sink.next(Map.entry(keySuffix, subStage));
|
|
|
|
} catch (Throwable ex) {
|
2021-08-31 09:14:46 +02:00
|
|
|
sink.error(ex);
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
2021-08-22 18:20:05 +02:00
|
|
|
});
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
2021-01-31 12:02:02 +01:00
|
|
|
|
2021-03-22 20:02:19 +01:00
|
|
|
@Override
|
|
|
|
public Flux<Entry<T, U>> getAllValues(@Nullable CompositeSnapshot snapshot) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return dictionary
|
|
|
|
.getRange(resolveSnapshot(snapshot), rangeMono)
|
2021-08-31 09:14:46 +02:00
|
|
|
.<Entry<T, U>>handle((serializedEntryToReceive, sink) -> {
|
2021-10-19 00:22:05 +02:00
|
|
|
try {
|
|
|
|
Entry<T, U> entry;
|
|
|
|
try (var serializedEntry = serializedEntryToReceive.receive()) {
|
|
|
|
try (var keyBuf = serializedEntry.getKey().receive()) {
|
|
|
|
assert keyBuf.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength;
|
|
|
|
// Remove prefix. Keep only the suffix and the ext
|
|
|
|
splitPrefix(keyBuf).close();
|
|
|
|
suffixKeyLengthConsistency(keyBuf.readableBytes());
|
|
|
|
T keySuffix = deserializeSuffix(keyBuf);
|
|
|
|
|
|
|
|
U value;
|
|
|
|
try (var valueBuf = serializedEntry.getValue().receive()) {
|
|
|
|
value = valueSerializer.deserialize(valueBuf);
|
|
|
|
}
|
|
|
|
entry = Map.entry(keySuffix, value);
|
|
|
|
}
|
2021-09-23 02:15:58 +02:00
|
|
|
}
|
2021-10-19 00:22:05 +02:00
|
|
|
sink.next(entry);
|
|
|
|
} catch (Throwable e) {
|
2021-08-22 21:23:22 +02:00
|
|
|
sink.error(e);
|
|
|
|
}
|
|
|
|
})
|
2021-07-23 22:18:08 +02:00
|
|
|
.doOnDiscard(Entry.class, uncastedEntry -> {
|
2021-08-29 23:18:03 +02:00
|
|
|
if (uncastedEntry.getKey() instanceof Buffer byteBuf) {
|
2021-08-31 09:14:46 +02:00
|
|
|
byteBuf.close();
|
2021-07-23 22:18:08 +02:00
|
|
|
}
|
2021-08-29 23:18:03 +02:00
|
|
|
if (uncastedEntry.getValue() instanceof Buffer byteBuf) {
|
2021-08-31 09:14:46 +02:00
|
|
|
byteBuf.close();
|
2021-07-23 22:18:08 +02:00
|
|
|
}
|
2021-08-22 18:20:05 +02:00
|
|
|
});
|
2021-03-22 20:02:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 15:47:48 +01:00
|
|
|
@Override
|
|
|
|
public Flux<Entry<T, U>> setAllValuesAndGetPrevious(Flux<Entry<T, U>> entries) {
|
2021-08-29 01:15:51 +02:00
|
|
|
return Flux.concat(
|
|
|
|
this.getAllValues(null),
|
2021-10-19 00:22:05 +02:00
|
|
|
dictionary.setRange(rangeMono, entries.handle(this::serializeEntrySink)).then(Mono.empty())
|
2021-08-22 21:23:22 +02:00
|
|
|
);
|
2021-01-31 15:47:48 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 03:13:19 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Void> clear() {
|
2021-08-31 09:14:46 +02:00
|
|
|
if (range.isAll()) {
|
|
|
|
return dictionary.clear();
|
|
|
|
} else if (range.isSingle()) {
|
|
|
|
return dictionary
|
|
|
|
.remove(Mono.fromCallable(range::getSingle), LLDictionaryResultType.VOID)
|
|
|
|
.doOnNext(Send::close)
|
|
|
|
.then();
|
|
|
|
} else {
|
|
|
|
return dictionary.setRange(rangeMono, Flux.empty());
|
|
|
|
}
|
2021-01-31 12:02:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|