2021-05-08 03:09:00 +02:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-06-26 02:35:33 +02:00
|
|
|
import it.cavallium.dbengine.client.BadBlock;
|
2021-05-08 03:09:00 +02:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
2021-06-26 02:35:33 +02:00
|
|
|
import it.cavallium.dbengine.database.Column;
|
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-05-08 03:09:00 +02:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
|
2021-05-19 22:51:55 +02:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectSets;
|
|
|
|
import java.util.HashSet;
|
2021-05-08 03:09:00 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
import java.util.function.Function;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.warp.commonutils.functional.TriFunction;
|
2021-06-26 02:35:33 +02:00
|
|
|
import reactor.core.publisher.Flux;
|
2021-05-08 03:09:00 +02:00
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public class DatabaseSingleBucket<K, V, TH> implements DatabaseStageEntry<V> {
|
|
|
|
|
2021-07-13 22:58:08 +02:00
|
|
|
private final DatabaseStageEntry<ObjectArraySet<Entry<K, V>>> bucketStage;
|
2021-05-08 03:09:00 +02:00
|
|
|
private final K key;
|
|
|
|
|
2021-07-13 22:58:08 +02:00
|
|
|
public DatabaseSingleBucket(DatabaseStageEntry<ObjectArraySet<Entry<K, V>>> bucketStage, K key) {
|
2021-05-08 03:09:00 +02:00
|
|
|
this.bucketStage = bucketStage;
|
|
|
|
this.key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<V> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
|
|
|
|
return bucketStage.get(snapshot, existsAlmostCertainly).flatMap(this::extractValueTransformation);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<V> getOrDefault(@Nullable CompositeSnapshot snapshot, Mono<V> defaultValue) {
|
|
|
|
return bucketStage.get(snapshot).flatMap(this::extractValueTransformation).switchIfEmpty(defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> set(V value) {
|
|
|
|
return this.update(prev -> value, UpdateReturnMode.NOTHING).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<V> setAndGetPrevious(V value) {
|
|
|
|
return this.update(prev -> value, UpdateReturnMode.GET_OLD_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> setAndGetChanged(V value) {
|
|
|
|
return this.updateAndGetDelta(prev -> value).map(LLUtils::isDeltaChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-22 21:23:22 +02:00
|
|
|
public Mono<V> update(SerializationFunction<@Nullable V, @Nullable V> updater,
|
2021-05-08 03:09:00 +02:00
|
|
|
UpdateReturnMode updateReturnMode,
|
|
|
|
boolean existsAlmostCertainly) {
|
|
|
|
return bucketStage
|
|
|
|
.update(oldBucket -> {
|
|
|
|
V oldValue = extractValue(oldBucket);
|
2021-06-19 12:14:14 +02:00
|
|
|
V newValue = updater.apply(oldValue);
|
2021-05-08 03:09:00 +02:00
|
|
|
|
|
|
|
if (newValue == null) {
|
|
|
|
return this.removeValueOrDelete(oldBucket);
|
|
|
|
} else {
|
|
|
|
return this.insertValueOrCreate(oldBucket, newValue);
|
|
|
|
}
|
|
|
|
}, updateReturnMode, existsAlmostCertainly)
|
|
|
|
.flatMap(this::extractValueTransformation);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-22 21:23:22 +02:00
|
|
|
public Mono<Delta<V>> updateAndGetDelta(SerializationFunction<@Nullable V, @Nullable V> updater, boolean existsAlmostCertainly) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return bucketStage
|
|
|
|
.updateAndGetDelta(oldBucket -> {
|
|
|
|
V oldValue = extractValue(oldBucket);
|
|
|
|
var result = updater.apply(oldValue);
|
|
|
|
if (result == null) {
|
|
|
|
return this.removeValueOrDelete(oldBucket);
|
|
|
|
} else {
|
|
|
|
return this.insertValueOrCreate(oldBucket, result);
|
|
|
|
}
|
|
|
|
}, existsAlmostCertainly)
|
|
|
|
.transform(mono -> LLUtils.mapDelta(mono, this::extractValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> clear() {
|
|
|
|
return this.update(prev -> null, UpdateReturnMode.NOTHING).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<V> clearAndGetPrevious() {
|
|
|
|
return this.update(prev -> null, UpdateReturnMode.GET_OLD_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> clearAndGetStatus() {
|
|
|
|
return this.updateAndGetDelta(prev -> null).map(LLUtils::isDeltaChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> close() {
|
|
|
|
return bucketStage.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
|
|
|
return this.get(snapshot).map(prev -> 1L).defaultIfEmpty(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return this.get(snapshot).map(prev -> true).defaultIfEmpty(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DatabaseStageEntry<V> entry() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-06-26 02:35:33 +02:00
|
|
|
@Override
|
|
|
|
public Flux<BadBlock> badBlocks() {
|
|
|
|
return bucketStage.badBlocks();
|
|
|
|
}
|
|
|
|
|
2021-05-08 03:09:00 +02:00
|
|
|
@Override
|
|
|
|
public void release() {
|
|
|
|
bucketStage.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Mono<V> extractValueTransformation(Set<Entry<K, V>> entries) {
|
|
|
|
return Mono.fromCallable(() -> extractValue(entries));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private V extractValue(Set<Entry<K, V>> entries) {
|
|
|
|
if (entries == null) return null;
|
|
|
|
for (Entry<K, V> entry : entries) {
|
|
|
|
if (Objects.equals(entry.getKey(), key)) {
|
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
2021-07-13 22:58:08 +02:00
|
|
|
private ObjectArraySet<Entry<K, V>> insertValueOrCreate(@Nullable ObjectArraySet<Entry<K, V>> entries, V value) {
|
2021-05-08 03:09:00 +02:00
|
|
|
if (entries != null) {
|
2021-07-13 22:58:08 +02:00
|
|
|
var clonedEntries = entries.clone();
|
|
|
|
var it = clonedEntries.iterator();
|
2021-05-08 03:09:00 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
var entry = it.next();
|
|
|
|
if (Objects.equals(entry.getKey(), key)) {
|
|
|
|
it.remove();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 22:58:08 +02:00
|
|
|
clonedEntries.add(Map.entry(key, value));
|
|
|
|
return clonedEntries;
|
2021-05-08 03:09:00 +02:00
|
|
|
} else {
|
2021-07-13 22:58:08 +02:00
|
|
|
var oas = new ObjectArraySet<Entry<K, V>>(1);
|
2021-05-17 04:10:41 +02:00
|
|
|
oas.add(Map.entry(key, value));
|
|
|
|
return oas;
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2021-07-13 22:58:08 +02:00
|
|
|
private ObjectArraySet<Entry<K, V>> removeValueOrDelete(@Nullable ObjectArraySet<Entry<K, V>> entries) {
|
2021-05-08 03:09:00 +02:00
|
|
|
if (entries != null) {
|
2021-07-13 22:58:08 +02:00
|
|
|
var clonedEntries = entries.clone();
|
|
|
|
var it = clonedEntries.iterator();
|
2021-05-08 03:09:00 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
var entry = it.next();
|
|
|
|
if (Objects.equals(entry.getKey(), key)) {
|
|
|
|
it.remove();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 22:58:08 +02:00
|
|
|
if (clonedEntries.size() == 0) {
|
2021-05-08 03:09:00 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
2021-07-13 22:58:08 +02:00
|
|
|
return clonedEntries;
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|