package it.cavallium.dbengine.database.collections; import com.google.common.primitives.Ints; import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure; import it.cavallium.dbengine.database.LLSingleton; import it.cavallium.dbengine.database.LLSnapshot; import it.cavallium.dbengine.database.serialization.SerializationException; import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength; import org.jetbrains.annotations.Nullable; import reactor.core.publisher.Mono; public class DatabaseInt implements LLKeyValueDatabaseStructure { private final LLSingleton singleton; private final SerializerFixedBinaryLength serializer; public DatabaseInt(LLSingleton singleton) { this.singleton = singleton; this.serializer = SerializerFixedBinaryLength.intSerializer(singleton.getAllocator()); } public Mono get(@Nullable LLSnapshot snapshot) { return singleton.get(snapshot).handle((data, sink) -> { try (data) { sink.next(serializer.deserialize(data)); } catch (SerializationException e) { sink.error(e); } }); } public Mono set(int value) { return singleton.set(Mono.fromCallable(() -> { var buf = singleton.getAllocator().allocate(Integer.BYTES); try { serializer.serialize(value, buf); return buf; } catch (Throwable ex) { buf.close(); throw ex; } })); } @Override public String getDatabaseName() { return singleton.getDatabaseName(); } }