Better define count method

This commit is contained in:
Andrea Cavalli 2021-02-24 16:43:07 +01:00
parent af47584803
commit 3366c52b24
7 changed files with 20 additions and 10 deletions

View File

@ -302,7 +302,7 @@ public class SpeedExample {
numRepeats, numRepeats,
tuple -> Mono tuple -> Mono
.fromRunnable(() -> System.out.println("Calculating size")) .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)) .doOnNext(s -> System.out.println("Size after: " + s))
.then(tuple.getT1().close()) .then(tuple.getT1().close())
); );
@ -400,7 +400,7 @@ public class SpeedExample {
numRepeats, numRepeats,
tuple -> Mono tuple -> Mono
.fromRunnable(() -> System.out.println("Calculating size")) .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)) .doOnNext(s -> System.out.println("Size after: " + s))
.then(tuple.getT1().close()) .then(tuple.getT1().close())
); );
@ -429,7 +429,7 @@ public class SpeedExample {
numRepeats, numRepeats,
tuple -> Mono tuple -> Mono
.fromRunnable(() -> System.out.println("Calculating size")) .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)) .doOnNext(s -> System.out.println("Size after: " + s))
.then(tuple.getT1().close()) .then(tuple.getT1().close())
); );

View File

@ -87,7 +87,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
} }
@Override @Override
public Mono<Long> size(@Nullable CompositeSnapshot snapshot, boolean fast) { public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return dictionary.sizeRange(resolveSnapshot(snapshot), range, fast); return dictionary.sizeRange(resolveSnapshot(snapshot), range, fast);
} }

View File

@ -174,6 +174,11 @@ public class DatabaseMapDictionaryDeep<T, U, US extends DatabaseStage<U>> implem
return LLRange.of(first, end); return LLRange.of(first, end);
} }
@Override
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return dictionary.sizeRange(resolveSnapshot(snapshot), range, fast);
}
@SuppressWarnings("ReactiveStreamsUnusedPublisher") @SuppressWarnings("ReactiveStreamsUnusedPublisher")
@Override @Override
public Mono<US> at(@Nullable CompositeSnapshot snapshot, T keySuffix) { public Mono<US> at(@Nullable CompositeSnapshot snapshot, T keySuffix) {

View File

@ -54,7 +54,7 @@ public class DatabaseSingle<U> implements DatabaseStageEntry<U> {
} }
@Override @Override
public Mono<Long> size(@Nullable CompositeSnapshot snapshot, boolean fast) { public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return dictionary return dictionary
.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key)) .isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key))
.map(empty -> empty ? 0L : 1L); .map(empty -> empty ? 0L : 1L);

View File

@ -68,8 +68,8 @@ public class DatabaseSingleMapped<U> implements DatabaseStageEntry<U> {
} }
@Override @Override
public Mono<Long> size(@Nullable CompositeSnapshot snapshot, boolean fast) { public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return serializedSingle.size(snapshot, fast); return serializedSingle.leavesCount(snapshot, fast);
} }
@Override @Override

View File

@ -41,9 +41,14 @@ public interface DatabaseStage<T> extends DatabaseStageWithEntry<T> {
return Mono.empty(); return Mono.empty();
} }
Mono<Long> 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<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast);
default Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) { default Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
return size(snapshot, false).map(size -> size <= 0); return leavesCount(snapshot, false).map(size -> size <= 0);
} }
} }

View File

@ -136,7 +136,7 @@ public interface DatabaseStageMap<T, U, US extends DatabaseStage<U>> extends Dat
} }
@Override @Override
default Mono<Long> size(@Nullable CompositeSnapshot snapshot, boolean fast) { default Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return getAllStages(snapshot).count(); return getAllStages(snapshot).count();
} }