CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/collections/DatabaseSingleBucket.java

258 lines
7.2 KiB
Java
Raw Normal View History

2021-05-08 03:09:00 +02:00
package it.cavallium.dbengine.database.collections;
2021-09-23 20:57:28 +02:00
import io.net5.buffer.api.Drop;
import io.net5.buffer.api.Owned;
import io.net5.buffer.api.Send;
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;
import it.cavallium.dbengine.database.Delta;
import it.cavallium.dbengine.database.LLUtils;
2021-10-17 17:15:57 +02:00
import io.net5.buffer.api.internal.ResourceSupport;
2021-05-08 03:09:00 +02:00
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;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-05-08 03:09:00 +02:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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")
2021-09-23 20:57:28 +02:00
public class DatabaseSingleBucket<K, V, TH>
2021-10-17 17:15:57 +02:00
extends ResourceSupport<DatabaseStage<V>, DatabaseSingleBucket<K, V, TH>>
2021-09-23 20:57:28 +02:00
implements DatabaseStageEntry<V> {
2021-05-08 03:09:00 +02:00
private static final Logger logger = LogManager.getLogger(DatabaseSingleBucket.class);
2021-10-01 19:17:33 +02:00
private static final Drop<DatabaseSingleBucket<?, ?, ?>> DROP = new Drop<>() {
@Override
public void drop(DatabaseSingleBucket<?, ?, ?> obj) {
try {
if (obj.bucketStage != null) {
obj.bucketStage.close();
}
} catch (Throwable ex) {
logger.error("Failed to close bucketStage", ex);
}
try {
if (obj.onClose != null) {
obj.onClose.run();
}
} catch (Throwable ex) {
logger.error("Failed to close onClose", ex);
}
}
@Override
public Drop<DatabaseSingleBucket<?, ?, ?>> fork() {
return this;
}
@Override
public void attach(DatabaseSingleBucket<?, ?, ?> obj) {
}
};
2021-05-08 03:09:00 +02:00
private final K key;
2021-09-23 20:57:28 +02:00
private DatabaseStageEntry<ObjectArraySet<Entry<K, V>>> bucketStage;
2021-10-01 19:17:33 +02:00
private Runnable onClose;
@SuppressWarnings({"unchecked", "rawtypes"})
public DatabaseSingleBucket(DatabaseStageEntry<ObjectArraySet<Entry<K, V>>> bucketStage, K key, Runnable onClose) {
super((Drop<DatabaseSingleBucket<K,V,TH>>) (Drop) DROP);
2021-09-23 20:57:28 +02:00
this.key = key;
2021-05-08 03:09:00 +02:00
this.bucketStage = bucketStage;
2021-10-01 19:17:33 +02:00
this.onClose = onClose;
2021-09-23 20:57:28 +02:00
}
2021-10-01 19:17:33 +02:00
@SuppressWarnings({"unchecked", "rawtypes"})
private DatabaseSingleBucket(Send<DatabaseStage<ObjectArraySet<Entry<K, V>>>> bucketStage, K key, Runnable onClose) {
super((Drop<DatabaseSingleBucket<K,V,TH>>) (Drop) DROP);
2021-05-08 03:09:00 +02:00
this.key = key;
2021-09-23 20:57:28 +02:00
this.bucketStage = (DatabaseStageEntry<ObjectArraySet<Entry<K, V>>>) bucketStage.receive();
2021-10-01 19:17:33 +02:00
this.onClose = onClose;
2021-05-08 03:09:00 +02:00
}
@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-09-23 20:57:28 +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<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
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;
}
}
2021-09-23 20:57:28 +02:00
@Override
protected RuntimeException createResourceClosedException() {
throw new IllegalStateException("Closed");
}
@Override
protected Owned<DatabaseSingleBucket<K, V, TH>> prepareSend() {
var bucketStage = this.bucketStage.send();
2021-10-01 19:17:33 +02:00
var onClose = this.onClose;
return drop -> {
var instance = new DatabaseSingleBucket<K, V, TH>(bucketStage, key, onClose);
drop.attach(instance);
return instance;
};
2021-09-23 20:57:28 +02:00
}
@Override
protected void makeInaccessible() {
this.bucketStage = null;
2021-10-01 19:17:33 +02:00
this.onClose = null;
2021-09-23 20:57:28 +02:00
}
2021-05-08 03:09:00 +02:00
}