2021-01-30 20:16:14 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
|
|
|
import java.util.Objects;
|
2021-02-06 19:21:31 +01:00
|
|
|
import java.util.function.Function;
|
2021-01-30 20:16:14 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-02-03 14:37:02 +01:00
|
|
|
public interface DatabaseStage<T> extends DatabaseStageWithEntry<T> {
|
2021-01-30 20:16:14 +01:00
|
|
|
|
2021-03-18 16:19:41 +01:00
|
|
|
default 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,
|
|
|
|
Mono<T> defaultValue,
|
|
|
|
boolean existsAlmostCertainly) {
|
|
|
|
return get(snapshot, existsAlmostCertainly).switchIfEmpty(defaultValue).single();
|
|
|
|
}
|
2021-01-30 20:16:14 +01:00
|
|
|
|
|
|
|
default Mono<T> getOrDefault(@Nullable CompositeSnapshot snapshot, Mono<T> defaultValue) {
|
2021-03-18 16:19:41 +01:00
|
|
|
return getOrDefault(snapshot, defaultValue, false);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default Mono<Void> set(T value) {
|
2021-04-30 19:15:04 +02:00
|
|
|
return setAndGetChanged(value).then();
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Mono<T> setAndGetPrevious(T value);
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
default Mono<Boolean> setAndGetChanged(T value) {
|
|
|
|
return setAndGetPrevious(value).map(oldValue -> !Objects.equals(oldValue, value)).defaultIfEmpty(value != null);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
Mono<Boolean> update(Function<@Nullable T, @Nullable T> updater, boolean existsAlmostCertainly);
|
2021-03-18 16:19:41 +01:00
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
default Mono<Boolean> update(Function<@Nullable T, @Nullable T> updater) {
|
2021-03-18 16:19:41 +01:00
|
|
|
return update(updater, false);
|
|
|
|
}
|
2021-02-06 19:21:31 +01:00
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
default Mono<Void> clear() {
|
|
|
|
return clearAndGetStatus().then();
|
|
|
|
}
|
|
|
|
|
|
|
|
Mono<T> clearAndGetPrevious();
|
|
|
|
|
|
|
|
default Mono<Boolean> clearAndGetStatus() {
|
|
|
|
return clearAndGetPrevious().map(Objects::nonNull).defaultIfEmpty(false);
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
void release();
|
|
|
|
|
2021-01-30 20:16:14 +01:00
|
|
|
default Mono<Void> close() {
|
|
|
|
return Mono.empty();
|
|
|
|
}
|
|
|
|
|
2021-02-24 16:43:07 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
2021-01-30 20:16:14 +01:00
|
|
|
|
|
|
|
default Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
|
2021-02-24 16:43:07 +01:00
|
|
|
return leavesCount(snapshot, false).map(size -> size <= 0);
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
}
|