Code cleanup

This commit is contained in:
Andrea Cavalli 2022-05-21 23:49:06 +02:00
parent 52c216c0df
commit 2e58189015
9 changed files with 56 additions and 76 deletions

View File

@ -37,7 +37,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.publisher.SynchronousSink;
/** /**
* Optimized implementation of "DatabaseMapDictionary with SubStageGetterSingle" * Optimized implementation of "DatabaseMapDictionary with SubStageGetterSingle"
@ -206,7 +205,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
} }
@Override @Override
public Mono<Object2ObjectSortedMap<T, U>> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) { public Mono<Object2ObjectSortedMap<T, U>> get(@Nullable CompositeSnapshot snapshot) {
return dictionary return dictionary
.getRange(resolveSnapshot(snapshot), rangeMono, false, true) .getRange(resolveSnapshot(snapshot), rangeMono, false, true)
.map(entry -> { .map(entry -> {
@ -231,7 +230,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
@Override @Override
public Mono<Object2ObjectSortedMap<T, U>> setAndGetPrevious(Object2ObjectSortedMap<T, U> value) { public Mono<Object2ObjectSortedMap<T, U>> setAndGetPrevious(Object2ObjectSortedMap<T, U> value) {
return this return this
.get(null, false) .get(null)
.concatWith(dictionary.setRange(rangeMono, Flux .concatWith(dictionary.setRange(rangeMono, Flux
.fromIterable(Collections.unmodifiableMap(value).entrySet()) .fromIterable(Collections.unmodifiableMap(value).entrySet())
.map(this::serializeEntry), true).then(Mono.empty())) .map(this::serializeEntry), true).then(Mono.empty()))
@ -271,7 +270,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
} }
@Override @Override
public Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T keySuffix, boolean existsAlmostCertainly) { public Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T keySuffix) {
return Mono.usingWhen(dictionary return Mono.usingWhen(dictionary
.get(resolveSnapshot(snapshot), Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix))), .get(resolveSnapshot(snapshot), Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix))),
value -> Mono.fromCallable(() -> deserializeValue(keySuffix, value)), value -> Mono.fromCallable(() -> deserializeValue(keySuffix, value)),
@ -398,13 +397,17 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
} }
@Override @Override
public Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys, boolean existsAlmostCertainly) { public Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys) {
var mappedKeys = keys.map(this::serializeKeySuffixToKey); var mappedKeys = keys.map(this::serializeKeySuffixToKey);
return dictionary return dictionary
.getMulti(resolveSnapshot(snapshot), mappedKeys) .getMulti(resolveSnapshot(snapshot), mappedKeys)
.map(valueBufOpt -> { .map(valueBufOpt -> {
try (valueBufOpt) { try (valueBufOpt) {
return valueBufOpt.map(valueSerializer::deserialize); if (valueBufOpt.isPresent()) {
return Optional.of(valueSerializer.deserialize(valueBufOpt.get()));
} else {
return Optional.empty();
}
} }
}); });
} }
@ -491,29 +494,23 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
Mono<LLRange> sliceRangeMono, boolean reverse, boolean smallRange) { Mono<LLRange> sliceRangeMono, boolean reverse, boolean smallRange) {
return dictionary return dictionary
.getRangeKeys(resolveSnapshot(snapshot), sliceRangeMono, reverse, smallRange) .getRangeKeys(resolveSnapshot(snapshot), sliceRangeMono, reverse, smallRange)
.flatMapSequential(keyBuf -> Mono .map(keyBuf -> {
.<SubStageEntry<T, DatabaseStageEntry<U>>>fromCallable(() -> { try (keyBuf) {
assert keyBuf.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength; assert keyBuf.readableBytes() == keyPrefixLength + keySuffixLength + keyExtLength;
// Remove prefix. Keep only the suffix and the ext // Remove prefix. Keep only the suffix and the ext
splitPrefix(keyBuf).close(); splitPrefix(keyBuf).close();
suffixKeyLengthConsistency(keyBuf.readableBytes()); suffixKeyLengthConsistency(keyBuf.readableBytes());
T keySuffix; var bufSupplier = BufSupplier.ofOwned(toKey(keyBuf.copy()));
try (var keyBufCopy = keyBuf.copy()) { try {
keySuffix = deserializeSuffix(keyBufCopy); T keySuffix = deserializeSuffix(keyBuf);
}
var bufSupplier = BufSupplier.ofOwned(toKey(keyBuf));
var subStage = new DatabaseMapSingle<>(dictionary, bufSupplier, valueSerializer, null); var subStage = new DatabaseMapSingle<>(dictionary, bufSupplier, valueSerializer, null);
return new SubStageEntry<>(keySuffix, subStage); return new SubStageEntry<>(keySuffix, subStage);
}).doOnCancel(() -> { } catch (Throwable ex) {
if (keyBuf.isAccessible()) { bufSupplier.close();
keyBuf.close(); throw ex;
} }
}).doOnError(ex -> { }
if (keyBuf.isAccessible()) { });
keyBuf.close();
}
})
);
} }
@Override @Override

View File

@ -260,13 +260,6 @@ public class DatabaseMapDictionaryHashed<T, U, TH> extends
.map(this::deserializeMap); .map(this::deserializeMap);
} }
@Override
public Mono<Object2ObjectSortedMap<T, U>> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
return subDictionary
.get(snapshot, existsAlmostCertainly)
.map(this::deserializeMap);
}
@Override @Override
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) { public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return subDictionary.leavesCount(snapshot, fast); return subDictionary.leavesCount(snapshot, fast);

View File

@ -3,7 +3,6 @@ package it.cavallium.dbengine.database.collections;
import io.netty5.buffer.api.Buffer; import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.Drop; import io.netty5.buffer.api.Drop;
import io.netty5.buffer.api.Owned; import io.netty5.buffer.api.Owned;
import io.netty5.buffer.api.Send;
import io.netty5.buffer.api.internal.ResourceSupport; import io.netty5.buffer.api.internal.ResourceSupport;
import it.cavallium.dbengine.client.BadBlock; import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot; import it.cavallium.dbengine.client.CompositeSnapshot;
@ -19,13 +18,11 @@ import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializationFunction; import it.cavallium.dbengine.database.serialization.SerializationFunction;
import it.cavallium.dbengine.database.serialization.Serializer; import it.cavallium.dbengine.database.serialization.Serializer;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.publisher.SynchronousSink;
public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, DatabaseMapSingle<U>> implements public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, DatabaseMapSingle<U>> implements
DatabaseStageEntry<U> { DatabaseStageEntry<U> {
@ -113,7 +110,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
} }
@Override @Override
public Mono<U> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) { public Mono<U> get(@Nullable CompositeSnapshot snapshot) {
return Mono.usingWhen(dictionary.get(resolveSnapshot(snapshot), keyMono), return Mono.usingWhen(dictionary.get(resolveSnapshot(snapshot), keyMono),
buf -> Mono.fromSupplier(() -> deserializeValue(buf)), buf -> Mono.fromSupplier(() -> deserializeValue(buf)),
buf -> Mono.fromRunnable(buf::close) buf -> Mono.fromRunnable(buf::close)

View File

@ -82,8 +82,8 @@ public class DatabaseSingleBucket<K, V, TH>
} }
@Override @Override
public Mono<V> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) { public Mono<V> get(@Nullable CompositeSnapshot snapshot) {
return bucketStage.get(snapshot, existsAlmostCertainly).flatMap(this::extractValueTransformation); return bucketStage.get(snapshot).flatMap(this::extractValueTransformation);
} }
@Override @Override

View File

@ -8,7 +8,6 @@ import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot; import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.client.Mapper; import it.cavallium.dbengine.client.Mapper;
import it.cavallium.dbengine.database.Delta; import it.cavallium.dbengine.database.Delta;
import it.cavallium.dbengine.database.LLEntry;
import it.cavallium.dbengine.database.LLUtils; import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.UpdateReturnMode; import it.cavallium.dbengine.database.UpdateReturnMode;
import it.cavallium.dbengine.database.serialization.SerializationException; import it.cavallium.dbengine.database.serialization.SerializationException;
@ -79,8 +78,8 @@ public class DatabaseSingleMapped<A, B> extends ResourceSupport<DatabaseStage<A>
} }
@Override @Override
public Mono<A> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) { public Mono<A> get(@Nullable CompositeSnapshot snapshot) {
return serializedSingle.get(snapshot, existsAlmostCertainly).handle(this::deserializeSink); return serializedSingle.get(snapshot).handle(this::deserializeSink);
} }
@Override @Override

View File

@ -3,12 +3,10 @@ package it.cavallium.dbengine.database.collections;
import io.netty5.buffer.api.Buffer; import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.Drop; import io.netty5.buffer.api.Drop;
import io.netty5.buffer.api.Owned; import io.netty5.buffer.api.Owned;
import io.netty5.buffer.api.Send;
import io.netty5.buffer.api.internal.ResourceSupport; import io.netty5.buffer.api.internal.ResourceSupport;
import it.cavallium.dbengine.client.BadBlock; import it.cavallium.dbengine.client.BadBlock;
import it.cavallium.dbengine.client.CompositeSnapshot; import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.Delta; import it.cavallium.dbengine.database.Delta;
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLSingleton; import it.cavallium.dbengine.database.LLSingleton;
import it.cavallium.dbengine.database.LLSnapshot; import it.cavallium.dbengine.database.LLSnapshot;
import it.cavallium.dbengine.database.LLUtils; import it.cavallium.dbengine.database.LLUtils;
@ -16,7 +14,6 @@ import it.cavallium.dbengine.database.UpdateReturnMode;
import it.cavallium.dbengine.database.serialization.SerializationException; import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializationFunction; import it.cavallium.dbengine.database.serialization.SerializationFunction;
import it.cavallium.dbengine.database.serialization.Serializer; import it.cavallium.dbengine.database.serialization.Serializer;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -105,7 +102,7 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
} }
@Override @Override
public Mono<U> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) { public Mono<U> get(@Nullable CompositeSnapshot snapshot) {
return singleton.get(resolveSnapshot(snapshot)) return singleton.get(resolveSnapshot(snapshot))
.handle(this::deserializeValue); .handle(this::deserializeValue);
} }

View File

@ -8,23 +8,18 @@ import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.UpdateReturnMode; import it.cavallium.dbengine.database.UpdateReturnMode;
import it.cavallium.dbengine.database.serialization.SerializationFunction; import it.cavallium.dbengine.database.serialization.SerializationFunction;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
public interface DatabaseStage<T> extends DatabaseStageWithEntry<T>, Resource<DatabaseStage<T>> { public interface DatabaseStage<T> extends DatabaseStageWithEntry<T>, Resource<DatabaseStage<T>> {
default Mono<T> get(@Nullable CompositeSnapshot snapshot) { Mono<T> get(@Nullable CompositeSnapshot snapshot);
return get(snapshot, false);
}
Mono<T> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly);
default Mono<T> getOrDefault(@Nullable CompositeSnapshot snapshot, default Mono<T> getOrDefault(@Nullable CompositeSnapshot snapshot,
Mono<T> defaultValue, Mono<T> defaultValue,
boolean existsAlmostCertainly) { boolean existsAlmostCertainly) {
return get(snapshot, existsAlmostCertainly).switchIfEmpty(defaultValue).single(); return get(snapshot).switchIfEmpty(defaultValue).single();
} }
default Mono<T> getOrDefault(@Nullable CompositeSnapshot snapshot, Mono<T> defaultValue) { default Mono<T> getOrDefault(@Nullable CompositeSnapshot snapshot, Mono<T> defaultValue) {

View File

@ -36,17 +36,13 @@ public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends
); );
} }
default Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T key, boolean existsAlmostCertainly) { default Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T key) {
return Mono.usingWhen(this.at(snapshot, key), return Mono.usingWhen(this.at(snapshot, key),
stage -> stage.get(snapshot, existsAlmostCertainly), stage -> stage.get(snapshot),
LLUtils::finalizeResource LLUtils::finalizeResource
); );
} }
default Mono<U> getValue(@Nullable CompositeSnapshot snapshot, T key) {
return getValue(snapshot, key, false);
}
default Mono<U> getValueOrDefault(@Nullable CompositeSnapshot snapshot, T key, Mono<U> defaultValue) { default Mono<U> getValueOrDefault(@Nullable CompositeSnapshot snapshot, T key, Mono<U> defaultValue) {
return getValue(snapshot, key).switchIfEmpty(defaultValue).single(); return getValue(snapshot, key).switchIfEmpty(defaultValue).single();
} }
@ -110,21 +106,15 @@ public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends
return removeAndGetPrevious(key).map(o -> true).defaultIfEmpty(false); return removeAndGetPrevious(key).map(o -> true).defaultIfEmpty(false);
} }
/**
* GetMulti must return the elements in sequence!
*/
default Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys, boolean existsAlmostCertainly) {
return keys.flatMapSequential(key -> this
.getValue(snapshot, key, existsAlmostCertainly)
.map(Optional::of)
.defaultIfEmpty(Optional.empty()));
}
/** /**
* GetMulti must return the elements in sequence! * GetMulti must return the elements in sequence!
*/ */
default Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys) { default Flux<Optional<U>> getMulti(@Nullable CompositeSnapshot snapshot, Flux<T> keys) {
return getMulti(snapshot, keys, false); return keys.flatMapSequential(key -> this
.getValue(snapshot, key)
.map(Optional::of)
.defaultIfEmpty(Optional.empty())
);
} }
default Mono<Void> putMulti(Flux<Entry<T, U>> entries) { default Mono<Void> putMulti(Flux<Entry<T, U>> entries) {
@ -138,7 +128,7 @@ public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends
.getAllStages(snapshot, smallRange) .getAllStages(snapshot, smallRange)
.flatMapSequential(stage -> stage .flatMapSequential(stage -> stage
.getValue() .getValue()
.get(snapshot, true) .get(snapshot)
.map(value -> Map.entry(stage.getKey(), value)) .map(value -> Map.entry(stage.getKey(), value))
.doFinally(s -> stage.getValue().close()) .doFinally(s -> stage.getValue().close())
); );
@ -253,7 +243,7 @@ public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends
} }
@Override @Override
default Mono<Object2ObjectSortedMap<T, U>> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) { default Mono<Object2ObjectSortedMap<T, U>> get(@Nullable CompositeSnapshot snapshot) {
return this return this
.getAllValues(snapshot, true) .getAllValues(snapshot, true)
.collectMap(Entry::getKey, Entry::getValue, Object2ObjectLinkedOpenHashMap::new) .collectMap(Entry::getKey, Entry::getValue, Object2ObjectLinkedOpenHashMap::new)

View File

@ -33,6 +33,8 @@ import it.cavallium.dbengine.database.serialization.KVSerializationFunction;
import it.cavallium.dbengine.rpc.current.data.DatabaseOptions; import it.cavallium.dbengine.rpc.current.data.DatabaseOptions;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -260,14 +262,16 @@ public class LLLocalDictionary implements LLDictionary {
); );
} }
private Buffer getSync(LLSnapshot snapshot, Buffer key) throws Exception { private Buffer getSync(LLSnapshot snapshot, Buffer key) throws IOException {
logger.trace(MARKER_ROCKSDB, "Reading {}", () -> toStringSafe(key)); logger.trace(MARKER_ROCKSDB, "Reading {}", () -> toStringSafe(key));
try { try {
var readOptions = generateReadOptionsOrStatic(snapshot); var readOptions = generateReadOptionsOrStatic(snapshot);
Buffer result; Buffer result;
startedGet.increment(); startedGet.increment();
try { try {
result = getTime.recordCallable(() -> db.get(readOptions, key)); var initTime = System.nanoTime();
result = db.get(readOptions, key);
getTime.record(Duration.ofNanos(System.nanoTime() - initTime));
} finally { } finally {
endedGet.increment(); endedGet.increment();
if (readOptions != EMPTY_READ_OPTIONS) { if (readOptions != EMPTY_READ_OPTIONS) {
@ -543,7 +547,15 @@ public class LLLocalDictionary implements LLDictionary {
@Override @Override
public Flux<OptionalBuf> getMulti(@Nullable LLSnapshot snapshot, Flux<Buffer> keys) { public Flux<OptionalBuf> getMulti(@Nullable LLSnapshot snapshot, Flux<Buffer> keys) {
return keys.flatMapSequential(key -> runOnDb(false, () -> OptionalBuf.ofNullable(getSync(snapshot, key)))); return keys
.publishOn(dbRScheduler)
.handle((key, sink) -> {
try (key) {
sink.next(OptionalBuf.ofNullable(getSync(snapshot, key)));
} catch (IOException ex) {
sink.error(ex);
}
});
} }
@Override @Override