2021-01-30 20:16:14 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-09-23 20:57:28 +02:00
|
|
|
import io.net5.buffer.api.Resource;
|
2021-06-26 02:35:33 +02:00
|
|
|
import it.cavallium.dbengine.client.BadBlock;
|
2021-01-30 20:16:14 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
2021-05-08 03:09:00 +02:00
|
|
|
import it.cavallium.dbengine.database.Delta;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import it.cavallium.dbengine.database.UpdateReturnMode;
|
2021-08-22 21:23:22 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationFunction;
|
2021-01-30 20:16:14 +01:00
|
|
|
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;
|
2021-06-26 02:35:33 +02:00
|
|
|
import reactor.core.publisher.Flux;
|
2021-01-30 20:16:14 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-09-23 20:57:28 +02:00
|
|
|
public interface DatabaseStage<T> extends DatabaseStageWithEntry<T>, Resource<DatabaseStage<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-05-02 19:18:15 +02:00
|
|
|
return this
|
|
|
|
.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) {
|
2021-05-02 19:18:15 +02:00
|
|
|
return this
|
|
|
|
.setAndGetPrevious(value)
|
|
|
|
.map(oldValue -> !Objects.equals(oldValue, value))
|
|
|
|
.switchIfEmpty(Mono.fromSupplier(() -> value != null));
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<T> update(SerializationFunction<@Nullable T, @Nullable T> updater,
|
2021-05-08 03:09:00 +02:00
|
|
|
UpdateReturnMode updateReturnMode,
|
|
|
|
boolean existsAlmostCertainly) {
|
|
|
|
return this
|
|
|
|
.updateAndGetDelta(updater, existsAlmostCertainly)
|
|
|
|
.transform(prev -> LLUtils.resolveDelta(prev, updateReturnMode));
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<T> update(SerializationFunction<@Nullable T, @Nullable T> updater, UpdateReturnMode updateReturnMode) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return update(updater, updateReturnMode, false);
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
Mono<Delta<T>> updateAndGetDelta(SerializationFunction<@Nullable T, @Nullable T> updater,
|
2021-05-08 03:09:00 +02:00
|
|
|
boolean existsAlmostCertainly);
|
2021-03-18 16:19:41 +01:00
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
default Mono<Delta<T>> updateAndGetDelta(SerializationFunction<@Nullable T, @Nullable T> updater) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return updateAndGetDelta(updater, false);
|
2021-03-18 16:19:41 +01:00
|
|
|
}
|
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-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
|
|
|
}
|
2021-06-26 02:35:33 +02:00
|
|
|
|
|
|
|
Flux<BadBlock> badBlocks();
|
2021-01-30 20:16:14 +01:00
|
|
|
}
|