From 3366c52b246fabfeb64cb35eb2466069379c9289 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 24 Feb 2021 16:43:07 +0100 Subject: [PATCH] Better define count method --- .../java/it.cavallium.dbengine.client/SpeedExample.java | 6 +++--- .../database/collections/DatabaseMapDictionary.java | 2 +- .../database/collections/DatabaseMapDictionaryDeep.java | 5 +++++ .../dbengine/database/collections/DatabaseSingle.java | 2 +- .../database/collections/DatabaseSingleMapped.java | 4 ++-- .../dbengine/database/collections/DatabaseStage.java | 9 +++++++-- .../dbengine/database/collections/DatabaseStageMap.java | 2 +- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/example/java/it.cavallium.dbengine.client/SpeedExample.java b/src/example/java/it.cavallium.dbengine.client/SpeedExample.java index a4ae2eb..184fc5a 100644 --- a/src/example/java/it.cavallium.dbengine.client/SpeedExample.java +++ b/src/example/java/it.cavallium.dbengine.client/SpeedExample.java @@ -302,7 +302,7 @@ public class SpeedExample { numRepeats, tuple -> Mono .fromRunnable(() -> System.out.println("Calculating size")) - .then(tuple.getT2().size(null, false)) + .then(tuple.getT2().leavesCount(null, false)) .doOnNext(s -> System.out.println("Size after: " + s)) .then(tuple.getT1().close()) ); @@ -400,7 +400,7 @@ public class SpeedExample { numRepeats, tuple -> Mono .fromRunnable(() -> System.out.println("Calculating size")) - .then(tuple.getT2().size(null, false)) + .then(tuple.getT2().leavesCount(null, false)) .doOnNext(s -> System.out.println("Size after: " + s)) .then(tuple.getT1().close()) ); @@ -429,7 +429,7 @@ public class SpeedExample { numRepeats, tuple -> Mono .fromRunnable(() -> System.out.println("Calculating size")) - .then(tuple.getT2().size(null, false)) + .then(tuple.getT2().leavesCount(null, false)) .doOnNext(s -> System.out.println("Size after: " + s)) .then(tuple.getT1().close()) ); diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java index a2bffe8..44ad2d7 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionary.java @@ -87,7 +87,7 @@ public class DatabaseMapDictionary extends DatabaseMapDictionaryDeep size(@Nullable CompositeSnapshot snapshot, boolean fast) { + public Mono leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) { return dictionary.sizeRange(resolveSnapshot(snapshot), range, fast); } diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryDeep.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryDeep.java index b1071e8..6c28fff 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryDeep.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapDictionaryDeep.java @@ -174,6 +174,11 @@ public class DatabaseMapDictionaryDeep> implem return LLRange.of(first, end); } + @Override + public Mono leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) { + return dictionary.sizeRange(resolveSnapshot(snapshot), range, fast); + } + @SuppressWarnings("ReactiveStreamsUnusedPublisher") @Override public Mono at(@Nullable CompositeSnapshot snapshot, T keySuffix) { diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingle.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingle.java index 2c4bad8..217d773 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingle.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingle.java @@ -54,7 +54,7 @@ public class DatabaseSingle implements DatabaseStageEntry { } @Override - public Mono size(@Nullable CompositeSnapshot snapshot, boolean fast) { + public Mono leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) { return dictionary .isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key)) .map(empty -> empty ? 0L : 1L); diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingleMapped.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingleMapped.java index b382939..e3a24ed 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingleMapped.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingleMapped.java @@ -68,8 +68,8 @@ public class DatabaseSingleMapped implements DatabaseStageEntry { } @Override - public Mono size(@Nullable CompositeSnapshot snapshot, boolean fast) { - return serializedSingle.size(snapshot, fast); + public Mono leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) { + return serializedSingle.leavesCount(snapshot, fast); } @Override diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStage.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStage.java index f1cb19f..ae26303 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStage.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStage.java @@ -41,9 +41,14 @@ public interface DatabaseStage extends DatabaseStageWithEntry { return Mono.empty(); } - Mono size(@Nullable CompositeSnapshot snapshot, boolean fast); + /** + * Count all the elements. + * If it's a nested collection the count will include all the children recursively + * @param fast true to return an approximate value + */ + Mono leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast); default Mono isEmpty(@Nullable CompositeSnapshot snapshot) { - return size(snapshot, false).map(size -> size <= 0); + return leavesCount(snapshot, false).map(size -> size <= 0); } } diff --git a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStageMap.java b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStageMap.java index 8acb464..cfd759a 100644 --- a/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStageMap.java +++ b/src/main/java/it/cavallium/dbengine/database/collections/DatabaseStageMap.java @@ -136,7 +136,7 @@ public interface DatabaseStageMap> extends Dat } @Override - default Mono size(@Nullable CompositeSnapshot snapshot, boolean fast) { + default Mono leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) { return getAllStages(snapshot).count(); }