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;
|
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-03-20 14:33:27 +01:00
|
|
|
return singleton.get(snapshot).handle((dataSend, sink) -> {
|
|
|
|
try (var data = dataSend.receive()) {
|
|
|
|
if (data.readableBytes() == 4) {
|
|
|
|
sink.next((long) (int) bugSerializer.deserialize(data));
|
|
|
|
} else {
|
|
|
|
sink.next(serializer.deserialize(data));
|
|
|
|
}
|
|
|
|
} catch (SerializationException e) {
|
|
|
|
sink.error(e);
|
2021-01-30 10:52:14 +01: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) {
|
2021-11-12 02:05:44 +01:00
|
|
|
return singleton.update(prev -> {
|
|
|
|
if (prev != null) {
|
2022-04-01 01:30:56 +02:00
|
|
|
var prevLong = prev.readLong();
|
|
|
|
var alloc = singleton.getAllocator();
|
|
|
|
var buf = alloc.allocate(Long.BYTES);
|
|
|
|
buf.writeLong(prevLong + count);
|
|
|
|
return buf;
|
2021-11-12 02:05:44 +01:00
|
|
|
} else {
|
|
|
|
var alloc = singleton.getAllocator();
|
2021-11-12 02:52:42 +01:00
|
|
|
var buf = alloc.allocate(Long.BYTES);
|
2021-11-14 22:21:32 +01:00
|
|
|
buf.writeLong(count);
|
2021-11-12 02:52:42 +01:00
|
|
|
return buf;
|
2021-11-12 02:05:44 +01:00
|
|
|
}
|
|
|
|
}, updateReturnMode).map(send -> {
|
|
|
|
try (var buf = send.receive()) {
|
|
|
|
return buf.readLong();
|
|
|
|
}
|
|
|
|
}).single();
|
|
|
|
}
|
|
|
|
|
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(() -> {
|
|
|
|
try (var buf = singleton.getAllocator().allocate(Long.BYTES)) {
|
|
|
|
serializer.serialize(value, buf);
|
|
|
|
return buf.send();
|
|
|
|
}
|
|
|
|
}));
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDatabaseName() {
|
|
|
|
return singleton.getDatabaseName();
|
|
|
|
}
|
|
|
|
}
|