You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/02/14 02:18:41 UTC
[incubator-tuweni] branch master updated: Add more convenience
methods to deal with stores
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/master by this push:
new 34f62f9 Add more convenience methods to deal with stores
34f62f9 is described below
commit 34f62f9d37a63c3d4fb3e0c0fdd290c5830e818e
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Thu Feb 13 18:18:29 2020 -0800
Add more convenience methods to deal with stores
---
.../org/apache/tuweni/kv/LevelDBKeyValueStore.kt | 18 +++++++++++
.../org/apache/tuweni/kv/MapDBKeyValueStore.kt | 35 +++++++++++++++++-----
.../org/apache/tuweni/kv/RedisKeyValueStore.kt | 20 +++++++++++++
.../org/apache/tuweni/kv/KeyValueStoreTest.java | 21 +++++++++++++
4 files changed, 87 insertions(+), 7 deletions(-)
diff --git a/kv/src/main/kotlin/org/apache/tuweni/kv/LevelDBKeyValueStore.kt b/kv/src/main/kotlin/org/apache/tuweni/kv/LevelDBKeyValueStore.kt
index f0e8985..e30a68f 100644
--- a/kv/src/main/kotlin/org/apache/tuweni/kv/LevelDBKeyValueStore.kt
+++ b/kv/src/main/kotlin/org/apache/tuweni/kv/LevelDBKeyValueStore.kt
@@ -110,6 +110,24 @@ constructor(
keyDeserializer::apply,
valueDeserializer::apply,
options)
+
+ /**
+ * Open a LevelDB-backed key-value store using Bytes keys and values.
+ *
+ * @param dbPath The path to the levelDB database.
+ * @return A key-value store dealing with bytes.
+ * @throws IOException If an I/O error occurs.
+ */
+ @JvmStatic
+ @Throws(IOException::class)
+ fun open(
+ dbPath: Path
+ ) =
+ LevelDBKeyValueStore<Bytes, Bytes>(dbPath,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply)
}
private val db: DB
diff --git a/kv/src/main/kotlin/org/apache/tuweni/kv/MapDBKeyValueStore.kt b/kv/src/main/kotlin/org/apache/tuweni/kv/MapDBKeyValueStore.kt
index 190108f..125f53d 100644
--- a/kv/src/main/kotlin/org/apache/tuweni/kv/MapDBKeyValueStore.kt
+++ b/kv/src/main/kotlin/org/apache/tuweni/kv/MapDBKeyValueStore.kt
@@ -19,6 +19,7 @@ package org.apache.tuweni.kv
import kotlinx.coroutines.Dispatchers
import org.apache.tuweni.bytes.Bytes
import org.mapdb.DB
+import org.mapdb.DB.Keys.valueSerializer
import org.mapdb.DBMaker
import org.mapdb.DataInput2
import org.mapdb.DataOutput2
@@ -26,6 +27,7 @@ import org.mapdb.HTreeMap
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
+import java.util.function.Function
import kotlin.coroutines.CoroutineContext
/**
@@ -42,7 +44,6 @@ import kotlin.coroutines.CoroutineContext
* @constructor Open a MapDB-backed key-value store.
*/
class MapDBKeyValueStore<K, V>
-@Throws(IOException::class)
constructor(
dbPath: Path,
private val keySerializer: (K) -> Bytes,
@@ -65,15 +66,35 @@ constructor(
* @throws IOException If an I/O error occurs.
*/
@JvmStatic
- @Throws(IOException::class)
fun <K, V> open(
dbPath: Path,
- keySerializer: (K) -> Bytes,
- valueSerializer: (V) -> Bytes,
- keyDeserializer: (Bytes) -> K,
- valueDeserializer: (Bytes?) -> V?
+ keySerializer: Function<K, Bytes>,
+ valueSerializer: Function<V, Bytes>,
+ keyDeserializer: Function<Bytes, K>,
+ valueDeserializer: Function<Bytes?, V?>
) =
- MapDBKeyValueStore<K, V>(dbPath, keySerializer, valueSerializer, keyDeserializer, valueDeserializer)
+ MapDBKeyValueStore<K, V>(dbPath,
+ keySerializer::apply,
+ valueSerializer::apply,
+ keyDeserializer::apply,
+ valueDeserializer::apply)
+
+ /**
+ * Open a MapDB-backed key-value store using Bytes keys and values.
+ *
+ * @param dbPath The path to the MapDB database.
+ * @return A key-value store dealing with bytes.
+ * @throws IOException If an I/O error occurs.
+ */
+ @JvmStatic
+ fun open(
+ dbPath: Path
+ ) =
+ MapDBKeyValueStore<Bytes, Bytes>(dbPath,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply)
}
private val db: DB
diff --git a/kv/src/main/kotlin/org/apache/tuweni/kv/RedisKeyValueStore.kt b/kv/src/main/kotlin/org/apache/tuweni/kv/RedisKeyValueStore.kt
index f22f0cd..c85f285 100644
--- a/kv/src/main/kotlin/org/apache/tuweni/kv/RedisKeyValueStore.kt
+++ b/kv/src/main/kotlin/org/apache/tuweni/kv/RedisKeyValueStore.kt
@@ -25,6 +25,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.await
import org.apache.tuweni.bytes.Bytes
import org.checkerframework.checker.units.qual.K
+import java.io.IOException
import java.net.InetAddress
import java.util.concurrent.CompletionStage
import java.util.function.Function
@@ -51,6 +52,25 @@ class RedisKeyValueStore<K, V>(
) : KeyValueStore<K, V> {
companion object {
+
+ /**
+ * Open a Redis-backed key-value store using Bytes keys and values.
+ *
+ * @param uri The uri to the Redis store.
+ * @return A key-value store dealing with bytes.
+ * @throws IOException If an I/O error occurs.
+ */
+ @JvmStatic
+ @Throws(IOException::class)
+ fun open(
+ uri: String
+ ) =
+ RedisKeyValueStore<Bytes, Bytes>(uri,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply,
+ Function.identity<Bytes>()::apply)
+
/**
* Open a Redis-backed key-value store.
*
diff --git a/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java b/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java
index 19e0f37..0dd9056 100644
--- a/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java
+++ b/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java
@@ -50,6 +50,27 @@ class KeyValueStoreTest {
}
@Test
+ void testPutAndGetMapDB(@TempDirectory Path tmpDir) throws Exception {
+ KeyValueStore<Bytes, Bytes> store = MapDBKeyValueStore
+ .open(tmpDir.resolve("mapdb"), bytesIdentityFn, bytesIdentityFn, bytesIdentityFn, bytesIdentityFn);
+ AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13));
+ completion.join();
+ Bytes value = store.getAsync(Bytes.of(123)).get();
+ assertNotNull(value);
+ assertEquals(Bytes.of(10, 12, 13), value);
+ }
+
+ @Test
+ void testPutAndGetMapDBDirect(@TempDirectory Path tmpDir) throws Exception {
+ KeyValueStore<Bytes, Bytes> store = MapDBKeyValueStore.open(tmpDir.resolve("mapdb"));
+ AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13));
+ completion.join();
+ Bytes value = store.getAsync(Bytes.of(123)).get();
+ assertNotNull(value);
+ assertEquals(Bytes.of(10, 12, 13), value);
+ }
+
+ @Test
void testNoValue() throws Exception {
Map<Bytes, Bytes> map = new HashMap<>();
KeyValueStore<Bytes, Bytes> store = MapKeyValueStore.open(map);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org