From 3ad9efe71fd18d0fccd565876dcbc12ede9c28f7 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 30 Jan 2021 10:52:14 +0100 Subject: [PATCH] Async LLInt and LLLong --- .../dbengine/database/LLSingleton.java | 6 ++--- .../database/disk/LLLocalSingleton.java | 27 ++++++++++--------- .../dbengine/database/structures/LLInt.java | 10 +++---- .../dbengine/database/structures/LLLong.java | 21 ++++++++------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/main/java/it/cavallium/dbengine/database/LLSingleton.java b/src/main/java/it/cavallium/dbengine/database/LLSingleton.java index 60a7b7d..65135c0 100644 --- a/src/main/java/it/cavallium/dbengine/database/LLSingleton.java +++ b/src/main/java/it/cavallium/dbengine/database/LLSingleton.java @@ -1,11 +1,11 @@ package it.cavallium.dbengine.database; -import java.io.IOException; import org.jetbrains.annotations.Nullable; +import reactor.core.publisher.Mono; public interface LLSingleton extends LLKeyValueDatabaseStructure { - byte[] get(@Nullable LLSnapshot snapshot) throws IOException; + Mono get(@Nullable LLSnapshot snapshot); - void set(byte[] value) throws IOException; + Mono set(byte[] value); } diff --git a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalSingleton.java b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalSingleton.java index 023f0c5..5a34a0f 100644 --- a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalSingleton.java +++ b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalSingleton.java @@ -10,6 +10,8 @@ import org.rocksdb.RocksDBException; import org.rocksdb.Snapshot; import it.cavallium.dbengine.database.LLSingleton; import it.cavallium.dbengine.database.LLSnapshot; +import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; public class LLLocalSingleton implements LLSingleton { @@ -44,21 +46,22 @@ public class LLLocalSingleton implements LLSingleton { } @Override - public byte[] get(@Nullable LLSnapshot snapshot) throws IOException { - try { - return db.get(cfh, resolveSnapshot(snapshot), name); - } catch (RocksDBException e) { - throw new IOException(e); - } + public Mono get(@Nullable LLSnapshot snapshot) { + return Mono + .fromCallable(() -> db.get(cfh, resolveSnapshot(snapshot), name)) + .onErrorMap(IOException::new) + .subscribeOn(Schedulers.boundedElastic()); } @Override - public void set(byte[] value) throws IOException { - try { - db.put(cfh, name, value); - } catch (RocksDBException e) { - throw new IOException(e); - } + public Mono set(byte[] value) { + return Mono + .fromCallable(() -> { + db.put(cfh, name, value); + return null; + }) + .onErrorMap(IOException::new) + .subscribeOn(Schedulers.boundedElastic()); } @Override diff --git a/src/main/java/it/cavallium/dbengine/database/structures/LLInt.java b/src/main/java/it/cavallium/dbengine/database/structures/LLInt.java index 030b0d8..48e9c16 100644 --- a/src/main/java/it/cavallium/dbengine/database/structures/LLInt.java +++ b/src/main/java/it/cavallium/dbengine/database/structures/LLInt.java @@ -4,8 +4,8 @@ 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 java.io.IOException; import org.jetbrains.annotations.Nullable; +import reactor.core.publisher.Mono; public class LLInt implements LLKeyValueDatabaseStructure { @@ -15,12 +15,12 @@ public class LLInt implements LLKeyValueDatabaseStructure { this.singleton = singleton; } - public int get(@Nullable LLSnapshot snapshot) throws IOException { - return Ints.fromByteArray(singleton.get(snapshot)); + public Mono get(@Nullable LLSnapshot snapshot) { + return singleton.get(snapshot).map(Ints::fromByteArray); } - public void set(int value) throws IOException { - singleton.set(Ints.toByteArray(value)); + public Mono set(int value) { + return singleton.set(Ints.toByteArray(value)); } @Override diff --git a/src/main/java/it/cavallium/dbengine/database/structures/LLLong.java b/src/main/java/it/cavallium/dbengine/database/structures/LLLong.java index 13682fb..573cdbc 100644 --- a/src/main/java/it/cavallium/dbengine/database/structures/LLLong.java +++ b/src/main/java/it/cavallium/dbengine/database/structures/LLLong.java @@ -5,8 +5,8 @@ import com.google.common.primitives.Longs; import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure; import it.cavallium.dbengine.database.LLSingleton; import it.cavallium.dbengine.database.LLSnapshot; -import java.io.IOException; import org.jetbrains.annotations.Nullable; +import reactor.core.publisher.Mono; public class LLLong implements LLKeyValueDatabaseStructure { @@ -16,17 +16,18 @@ public class LLLong implements LLKeyValueDatabaseStructure { this.singleton = singleton; } - public long get(@Nullable LLSnapshot snapshot) throws IOException { - var array = singleton.get(snapshot); - if (array.length == 4) { - return Ints.fromByteArray(array); - } else { - return Longs.fromByteArray(array); - } + public Mono get(@Nullable LLSnapshot snapshot) { + return singleton.get(snapshot).map(array -> { + if (array.length == 4) { + return (long) Ints.fromByteArray(array); + } else { + return Longs.fromByteArray(array); + } + }); } - public void set(long value) throws IOException { - singleton.set(Longs.toByteArray(value)); + public Mono set(long value) { + return singleton.set(Longs.toByteArray(value)); } @Override