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

94 lines
2.4 KiB
Java
Raw Normal View History

2021-02-04 00:48:49 +01:00
package it.cavallium.dbengine.database.collections;
import io.netty.buffer.ByteBuf;
2021-05-02 19:18:15 +02:00
import io.netty.util.ReferenceCounted;
2021-02-04 00:48:49 +01:00
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.collections.DatabaseEmpty.Nothing;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
2021-05-03 00:29:26 +02:00
import java.util.List;
2021-02-04 00:48:49 +01:00
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class SubStageGetterSet<T> implements SubStageGetter<Map<T, Nothing>, DatabaseSetDictionary<T>> {
private static final boolean assertsEnabled;
static {
boolean assertsEnabledTmp = false;
//noinspection AssertWithSideEffects
assert assertsEnabledTmp = true;
//noinspection ConstantConditions
assertsEnabled = assertsEnabledTmp;
}
private final SerializerFixedBinaryLength<T, ByteBuf> keySerializer;
2021-02-04 00:48:49 +01:00
public SubStageGetterSet(SerializerFixedBinaryLength<T, ByteBuf> keySerializer) {
2021-02-04 00:48:49 +01:00
this.keySerializer = keySerializer;
}
@Override
public Mono<DatabaseSetDictionary<T>> subStage(LLDictionary dictionary,
@Nullable CompositeSnapshot snapshot,
ByteBuf prefixKey,
2021-05-03 00:29:26 +02:00
List<ByteBuf> debuggingKeys) {
return Mono
.defer(() -> {
if (assertsEnabled) {
return checkKeyFluxConsistency(prefixKey.retain(), debuggingKeys);
} else {
return Mono
.fromCallable(() -> {
for (ByteBuf key : debuggingKeys) {
key.release();
}
return null;
});
}
})
.then(Mono
.fromSupplier(() -> DatabaseSetDictionary
.tail(
dictionary,
prefixKey.retain(),
keySerializer
)
)
)
.doFinally(s -> prefixKey.release());
2021-02-04 00:48:49 +01:00
}
@Override
2021-03-14 13:24:46 +01:00
public boolean isMultiKey() {
return true;
}
@Override
public boolean needsDebuggingKeyFlux() {
2021-02-04 00:48:49 +01:00
return assertsEnabled;
}
2021-05-03 00:29:26 +02:00
private Mono<Void> checkKeyFluxConsistency(ByteBuf prefixKey, List<ByteBuf> keys) {
return Mono
.fromCallable(() -> {
try {
for (ByteBuf key : keys) {
assert key.readableBytes() == prefixKey.readableBytes() + getKeyBinaryLength();
}
} finally {
prefixKey.release();
for (ByteBuf key : keys) {
key.release();
}
}
return null;
});
2021-02-04 00:48:49 +01:00
}
public int getKeyBinaryLength() {
return keySerializer.getSerializedBinaryLength();
}
}