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

85 lines
2.4 KiB
Java
Raw Normal View History

2021-01-30 22:14:48 +01:00
package it.cavallium.dbengine.database.collections;
2020-12-07 22:15:18 +01:00
import it.cavallium.dbengine.buffers.Buf;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
2020-12-07 22:15:18 +01:00
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.SerializerFixedBinaryLength;
2020-12-07 22:15:18 +01:00
import org.jetbrains.annotations.Nullable;
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
public DatabaseLong(LLSingleton singleton) {
2020-12-07 22:15:18 +01:00
this.singleton = singleton;
this.serializer = SerializerFixedBinaryLength.longSerializer();
this.bugSerializer = SerializerFixedBinaryLength.intSerializer();
2020-12-07 22:15:18 +01:00
}
public Long get(@Nullable LLSnapshot snapshot) {
var result = BufDataInput.create(singleton.get(snapshot));
if (result.available() == 4) {
return (long) (int) bugSerializer.deserialize(result);
} else {
return serializer.deserialize(result);
}
2020-12-07 22:15:18 +01:00
}
public 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 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
}
public Long decrementAndGet() {
2021-11-14 22:21:32 +01:00
return addAnd(-1, UpdateReturnMode.GET_NEW_VALUE);
}
public Long getAndDecrement() {
2021-11-14 22:21:32 +01:00
return addAnd(-1, UpdateReturnMode.GET_OLD_VALUE);
}
public Long addAndGet(long count) {
2021-11-14 22:21:32 +01:00
return addAnd(count, UpdateReturnMode.GET_NEW_VALUE);
}
public Long getAndAdd(long count) {
2021-11-14 22:21:32 +01:00
return addAnd(count, UpdateReturnMode.GET_OLD_VALUE);
}
private Long addAnd(long count, UpdateReturnMode updateReturnMode) {
var result = singleton.update(prev -> {
if (prev != null) {
var prevLong = prev.getLong(0);
var buf = Buf.createZeroes(Long.BYTES);
buf.setLong(0, prevLong + count);
return buf;
} else {
var buf = Buf.createZeroes(Long.BYTES);
buf.setLong(0, count);
return buf;
2021-11-12 02:05:44 +01:00
}
2022-05-26 13:13:14 +02:00
}, updateReturnMode);
return result.getLong(0);
2021-11-12 02:05:44 +01:00
}
public void set(long value) {
var buf = BufDataOutput.createLimited(Long.BYTES);
serializer.serialize(value, buf);
singleton.set(buf.asList());
2020-12-07 22:15:18 +01:00
}
@Override
public String getDatabaseName() {
return singleton.getDatabaseName();
}
}