From 9918ef3258aed7b0190219264129da60b843146b Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sun, 31 Jan 2021 12:02:02 +0100 Subject: [PATCH] Update pom.xml, DatabaseMapDictionary.java, and 6 more files... --- pom.xml | 5 ++ ...Parent.java => DatabaseMapDictionary.java} | 29 ++++++---- .../DatabaseMapDictionaryRange.java | 2 +- .../collections/FixedLengthSerializer.java | 24 +++++++++ .../database/collections/MapBuilder.java | 54 ------------------- .../collections/SubStageGetterMapDeep.java | 2 +- .../cavallium/dbengine/client/Database.java | 3 ++ 7 files changed, 54 insertions(+), 65 deletions(-) rename src/main/java/it/cavallium/dbengine/database/collections/{DatabaseMapDictionaryParent.java => DatabaseMapDictionary.java} (82%) create mode 100644 src/main/java/it/cavallium/dbengine/database/collections/FixedLengthSerializer.java delete mode 100644 src/main/java/it/cavallium/dbengine/database/collections/MapBuilder.java create mode 100644 src/test/java/it/cavallium/dbengine/client/Database.java diff --git a/pom.xml b/pom.xml index 0262793..ee2081f 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,11 @@ common-utils 1.1.2 + + io.netty + netty-buffer + 4.1.58.Final + javax.xml.bind jaxb-api diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryParent.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java similarity index 82% rename from src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryParent.java rename to src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java index 4289972..a77ab74 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryParent.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java @@ -13,11 +13,12 @@ import reactor.core.publisher.GroupedFlux; import reactor.core.publisher.Mono; // todo: implement optimized methods -public class DatabaseMapDictionaryParent> implements DatabaseStageMap { +public class DatabaseMapDictionary> implements DatabaseStageMap { public static final byte[] EMPTY_BYTES = new byte[0]; private final LLDictionary dictionary; private final SubStageGetter subStageGetter; + private final FixedLengthSerializer suffixKeySerializer; private final byte[] keyPrefix; private final int keySuffixLength; private final int keyExtLength; @@ -65,13 +66,14 @@ public class DatabaseMapDictionaryParent> impleme } @SuppressWarnings("unused") - public DatabaseMapDictionaryParent(LLDictionary dictionary, SubStageGetter subStageGetter, int keyLength, int keyExtLength) { - this(dictionary, subStageGetter, EMPTY_BYTES, keyLength, keyExtLength); + public DatabaseMapDictionary(LLDictionary dictionary, SubStageGetter subStageGetter, FixedLengthSerializer keySerializer, int keyLength, int keyExtLength) { + this(dictionary, subStageGetter, keySerializer, EMPTY_BYTES, keyLength, keyExtLength); } - public DatabaseMapDictionaryParent(LLDictionary dictionary, SubStageGetter subStageGetter, byte[] prefixKey, int keySuffixLength, int keyExtLength) { + public DatabaseMapDictionary(LLDictionary dictionary, SubStageGetter subStageGetter, FixedLengthSerializer suffixKeySerializer, byte[] prefixKey, int keySuffixLength, int keyExtLength) { this.dictionary = dictionary; this.subStageGetter = subStageGetter; + this.suffixKeySerializer = suffixKeySerializer; this.keyPrefix = prefixKey; this.keySuffixLength = keySuffixLength; this.keyExtLength = keyExtLength; @@ -152,23 +154,32 @@ public class DatabaseMapDictionaryParent> impleme } @Override - public Mono at(@Nullable CompositeSnapshot snapshot, byte[] keySuffix) { + public Mono at(@Nullable CompositeSnapshot snapshot, T keySuffix) { + byte[] keySuffixData = serializeSuffix(keySuffix); Flux rangeKeys = this - .dictionary.getRangeKeys(resolveSnapshot(snapshot), toExtRange(keySuffix) + .dictionary.getRangeKeys(resolveSnapshot(snapshot), toExtRange(keySuffixData) ); return this.subStageGetter - .subStage(dictionary, snapshot, toKeyWithoutExt(keySuffix), rangeKeys); + .subStage(dictionary, snapshot, toKeyWithoutExt(keySuffixData), rangeKeys); } @Override - public Flux> getAllStages(@Nullable CompositeSnapshot snapshot) { + public Flux> getAllStages(@Nullable CompositeSnapshot snapshot) { Flux> groupedFlux = dictionary .getRangeKeys(resolveSnapshot(snapshot), range) .groupBy(this::removeExtFromFullKey); return groupedFlux .flatMap(rangeKeys -> this.subStageGetter .subStage(dictionary, snapshot, rangeKeys.key(), rangeKeys) - .map(us -> Map.entry(rangeKeys.key(), us)) + .map(us -> Map.entry(this.deserializeSuffix(this.stripPrefix(rangeKeys.key())), us)) ); } + + private T deserializeSuffix(byte[] suffix) { + return (T) new Object(); + } + + private byte[] serializeSuffix(T keySuffix) { + return new byte[0]; + } } diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryRange.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryRange.java index 8409f2c..0e63a24 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryRange.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryRange.java @@ -13,7 +13,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** - * @deprecated Use DatabaseMapDictionaryParent with SubStageGetterSingle + * @deprecated Use DatabaseMapDictionary with SubStageGetterSingle */ @Deprecated public class DatabaseMapDictionaryRange implements DatabaseStageMap> { diff --git a/src/main/java/it/cavallium/dbengine/database/collections/FixedLengthSerializer.java b/src/main/java/it/cavallium/dbengine/database/collections/FixedLengthSerializer.java new file mode 100644 index 0000000..29546d8 --- /dev/null +++ b/src/main/java/it/cavallium/dbengine/database/collections/FixedLengthSerializer.java @@ -0,0 +1,24 @@ +package it.cavallium.dbengine.database.collections; + +import io.netty.buffer.ByteBuf; + +public interface FixedLengthSerializer { + + B deserialize(ByteBuf serialized, int length); + + void serialize(B deserialized, ByteBuf output, int length); + + static FixedLengthSerializer noop() { + return new FixedLengthSerializer<>() { + @Override + public ByteBuf deserialize(ByteBuf serialized, int length) { + return serialized.readSlice(length); + } + + @Override + public void serialize(ByteBuf deserialized, ByteBuf output, int length) { + output.writeBytes(deserialized, length); + } + }; + } +} diff --git a/src/main/java/it/cavallium/dbengine/database/collections/MapBuilder.java b/src/main/java/it/cavallium/dbengine/database/collections/MapBuilder.java deleted file mode 100644 index 95dd530..0000000 --- a/src/main/java/it/cavallium/dbengine/database/collections/MapBuilder.java +++ /dev/null @@ -1,54 +0,0 @@ -package it.cavallium.dbengine.database.collections; - -import it.cavallium.dbengine.database.LLDictionary; - -public class MapBuilder { - - private final LLDictionary dictionary; - - public MapBuilder(LLDictionary dictionary) { - this.dictionary = dictionary; - } - - public static MapBuilder of(LLDictionary dictionary) { - return new MapBuilder(dictionary); - } - - public MapBuilder2 map() { - return MapBuilder2.fromDictionary(dictionary); - } - - public static class MapBuilder2 { - - private final LLDictionary dictionary; - private final MapBuilder2 parent; - private final SerializationOptions serializationOptions; - - public MapBuilder2(LLDictionary dictionary, SerializationOptions serializationOptions) { - this.dictionary = dictionary; - this.parent = null; - this.serializationOptions = serializationOptions; - } - - private MapBuilder2(MapBuilder2 parent, SerializationOptions serializationOptions) { - this.dictionary = null; - this.parent = parent; - this.serializationOptions = serializationOptions; - } - - public static MapBuilder2 fromDictionary(LLDictionary dictionary) { - return new MapBuilder2<>(dictionary, SerializationOptions.noop()); - } - - public MapBuilder2 serialize(SerializationOptions serializationOptions) { - return new MapBuilder2<>(this, serializationOptions); - } - - public static class SerializationOptions { - - public static SerializationOptions noop() { - return new SerializationOptions<>(); - } - } - } -} diff --git a/src/main/java/it/cavallium/dbengine/database/collections/SubStageGetterMapDeep.java b/src/main/java/it/cavallium/dbengine/database/collections/SubStageGetterMapDeep.java index e4c4adb..f6dcd14 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/SubStageGetterMapDeep.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/SubStageGetterMapDeep.java @@ -25,6 +25,6 @@ public class SubStageGetterMapDeep> implements @Nullable CompositeSnapshot snapshot, byte[] prefixKey, Flux keyFlux) { - return Mono.just(new DatabaseMapDictionaryParent<>(dictionary, subStageGetter, prefixKey, keyLength, keyExtLength)); + return Mono.just(new DatabaseMapDictionary<>(dictionary, subStageGetter, prefixKey, keyLength, keyExtLength)); } } diff --git a/src/test/java/it/cavallium/dbengine/client/Database.java b/src/test/java/it/cavallium/dbengine/client/Database.java new file mode 100644 index 0000000..ff3b02f --- /dev/null +++ b/src/test/java/it/cavallium/dbengine/client/Database.java @@ -0,0 +1,3 @@ +package it.cavallium.dbengine.client; + +public class Database {}