2021-01-30 22:14:48 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
import com.google.common.primitives.Ints;
|
|
|
|
import com.google.common.primitives.Longs;
|
|
|
|
import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure;
|
|
|
|
import it.cavallium.dbengine.database.LLSingleton;
|
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
2022-06-20 12:30:33 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-11-12 02:05:44 +01:00
|
|
|
import it.cavallium.dbengine.database.UpdateReturnMode;
|
2022-03-20 14:33:27 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationException;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-01-30 10:52:14 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
public class DatabaseLong implements LLKeyValueDatabaseStructure {
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
private final LLSingleton singleton;
|
2022-03-20 14:33:27 +01:00
|
|
|
private final SerializerFixedBinaryLength<Long> serializer;
|
|
|
|
private final SerializerFixedBinaryLength<Integer> bugSerializer;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
public DatabaseLong(LLSingleton singleton) {
|
2020-12-07 22:15:18 +01:00
|
|
|
this.singleton = singleton;
|
2022-03-20 14:33:27 +01:00
|
|
|
this.serializer = SerializerFixedBinaryLength.longSerializer(singleton.getAllocator());
|
|
|
|
this.bugSerializer = SerializerFixedBinaryLength.intSerializer(singleton.getAllocator());
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 10:52:14 +01:00
|
|
|
public Mono<Long> get(@Nullable LLSnapshot snapshot) {
|
2022-05-26 13:13:14 +02:00
|
|
|
var resultMono = singleton.get(snapshot);
|
|
|
|
return Mono.usingWhen(resultMono,
|
|
|
|
result -> Mono.fromSupplier(() -> {
|
|
|
|
if (result.readableBytes() == 4) {
|
|
|
|
return (long) (int) bugSerializer.deserialize(result);
|
|
|
|
} else {
|
|
|
|
return serializer.deserialize(result);
|
|
|
|
}
|
|
|
|
}),
|
2022-06-20 12:30:33 +02:00
|
|
|
LLUtils::finalizeResource
|
2022-05-26 13:13:14 +02:00
|
|
|
);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
public Mono<Long> incrementAndGet() {
|
2021-11-14 22:21:32 +01:00
|
|
|
return addAnd(1, UpdateReturnMode.GET_NEW_VALUE);
|
2021-11-12 02:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Mono<Long> getAndIncrement() {
|
2021-11-14 22:21:32 +01:00
|
|
|
return addAnd(1, UpdateReturnMode.GET_OLD_VALUE);
|
2021-11-12 02:05:44 +01:00
|
|
|
}
|
|
|
|
|
2021-11-14 22:21:32 +01:00
|
|
|
public Mono<Long> decrementAndGet() {
|
|
|
|
return addAnd(-1, UpdateReturnMode.GET_NEW_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Mono<Long> getAndDecrement() {
|
|
|
|
return addAnd(-1, UpdateReturnMode.GET_OLD_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Mono<Long> addAndGet(long count) {
|
|
|
|
return addAnd(count, UpdateReturnMode.GET_NEW_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Mono<Long> getAndAdd(long count) {
|
|
|
|
return addAnd(count, UpdateReturnMode.GET_OLD_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Mono<Long> addAnd(long count, UpdateReturnMode updateReturnMode) {
|
2022-05-26 13:13:14 +02:00
|
|
|
var resultMono = singleton.update(prev -> {
|
|
|
|
try (prev) {
|
|
|
|
if (prev != null) {
|
|
|
|
var prevLong = prev.readLong();
|
|
|
|
var alloc = singleton.getAllocator();
|
|
|
|
var buf = alloc.allocate(Long.BYTES);
|
|
|
|
buf.writeLong(prevLong + count);
|
|
|
|
return buf;
|
|
|
|
} else {
|
|
|
|
var alloc = singleton.getAllocator();
|
|
|
|
var buf = alloc.allocate(Long.BYTES);
|
|
|
|
buf.writeLong(count);
|
|
|
|
return buf;
|
|
|
|
}
|
2021-11-12 02:05:44 +01:00
|
|
|
}
|
2022-05-26 13:13:14 +02:00
|
|
|
}, updateReturnMode);
|
|
|
|
return Mono.usingWhen(resultMono,
|
|
|
|
result -> Mono.fromSupplier(result::readLong),
|
2022-06-20 12:30:33 +02:00
|
|
|
LLUtils::finalizeResource
|
2022-05-26 13:13:14 +02:00
|
|
|
).single();
|
2021-11-12 02:05:44 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 10:52:14 +01:00
|
|
|
public Mono<Void> set(long value) {
|
2022-03-20 14:33:27 +01:00
|
|
|
return singleton.set(Mono.fromCallable(() -> {
|
2022-05-20 10:20:00 +02:00
|
|
|
var buf = singleton.getAllocator().allocate(Long.BYTES);
|
|
|
|
try {
|
2022-03-20 14:33:27 +01:00
|
|
|
serializer.serialize(value, buf);
|
2022-05-20 10:20:00 +02:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
buf.close();
|
|
|
|
throw ex;
|
2022-03-20 14:33:27 +01:00
|
|
|
}
|
2022-05-20 10:20:00 +02:00
|
|
|
return buf;
|
2022-03-20 14:33:27 +01:00
|
|
|
}));
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDatabaseName() {
|
|
|
|
return singleton.getDatabaseName();
|
|
|
|
}
|
|
|
|
}
|