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:35:14 UTC

[incubator-tuweni] branch master updated: More cleanups of serializers. Added convenience methods to open proxy from java

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 b3e6775  More cleanups of serializers. Added convenience methods to open proxy from java
b3e6775 is described below

commit b3e6775299932f1d8e959d47f7ba1c2ba7abf548
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Thu Feb 13 18:34:58 2020 -0800

    More cleanups of serializers. Added convenience methods to open proxy from java
---
 .../org/apache/tuweni/kv/MapDBKeyValueStore.kt     | 13 +++++--
 .../org/apache/tuweni/kv/ProxyKeyValueStore.kt     | 40 +++++++++++++++++---
 .../org/apache/tuweni/kv/KeyValueStoreTest.java    | 43 ++++++++++++++++------
 .../org/apache/tuweni/kv/KeyValueStoreSpec.kt      |  2 +-
 4 files changed, 77 insertions(+), 21 deletions(-)

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 125f53d..2dae5fe 100644
--- a/kv/src/main/kotlin/org/apache/tuweni/kv/MapDBKeyValueStore.kt
+++ b/kv/src/main/kotlin/org/apache/tuweni/kv/MapDBKeyValueStore.kt
@@ -49,7 +49,7 @@ constructor(
   private val keySerializer: (K) -> Bytes,
   private val valueSerializer: (V) -> Bytes,
   private val keyDeserializer: (Bytes) -> K,
-  private val valueDeserializer: (Bytes?) -> V?,
+  private val valueDeserializer: (Bytes) -> V,
   override val coroutineContext: CoroutineContext = Dispatchers.IO
 ) : KeyValueStore<K, V> {
 
@@ -71,7 +71,7 @@ constructor(
       keySerializer: Function<K, Bytes>,
       valueSerializer: Function<V, Bytes>,
       keyDeserializer: Function<Bytes, K>,
-      valueDeserializer: Function<Bytes?, V?>
+      valueDeserializer: Function<Bytes, V>
     ) =
       MapDBKeyValueStore<K, V>(dbPath,
         keySerializer::apply,
@@ -110,7 +110,14 @@ constructor(
     ).createOrOpen()
   }
 
-  override suspend fun get(key: K): V? = valueDeserializer(storageData[keySerializer(key)])
+  override suspend fun get(key: K): V? {
+    val value = storageData[keySerializer(key)]
+    return if (value == null) {
+      value
+    } else {
+      return valueDeserializer(value)
+    }
+  }
 
   override suspend fun put(key: K, value: V) {
     storageData[keySerializer(key)] = valueSerializer(value)
diff --git a/kv/src/main/kotlin/org/apache/tuweni/kv/ProxyKeyValueStore.kt b/kv/src/main/kotlin/org/apache/tuweni/kv/ProxyKeyValueStore.kt
index 6d68683..4ab3933 100644
--- a/kv/src/main/kotlin/org/apache/tuweni/kv/ProxyKeyValueStore.kt
+++ b/kv/src/main/kotlin/org/apache/tuweni/kv/ProxyKeyValueStore.kt
@@ -17,6 +17,7 @@
 package org.apache.tuweni.kv
 
 import kotlin.coroutines.CoroutineContext
+import java.util.function.Function
 
 /**
  * A store used as a proxy for another store.
@@ -28,16 +29,45 @@ class ProxyKeyValueStore<K, V, E, R>(
   private val store: KeyValueStore<E, R>,
   private val unproxyKey: (E) -> K,
   private val proxyKey: (K) -> E,
-  private val unproxyValue: (R?) -> V?,
+  private val unproxyValue: (R) -> V,
   private val proxyValue: (V) -> R,
   override val coroutineContext: CoroutineContext = store.coroutineContext
 ) : KeyValueStore<K, V> {
 
-  override suspend fun get(key: K): V? = unproxyValue(store.get(proxyKey(key)))
+  companion object {
 
-  override suspend fun put(key: K, value: V) = store.put(proxyKey(key), proxyValue(value))
+    /**
+     * Opens a proxy store.
+     * @param store the store to proxy
+     * @param unproxyKey the function to convert a key from the underlying store into a key of this store
+     * @param proxyKey the function to convert a key from this store to the underlying store
+     * @param unproxyValue the function to convert a value from the underlying store into a value of this store
+     * @param proxyValue the function to convert a value from this store to the underlying store
+     * @return A key-value store proxying the store passed in.
+     */
+    @JvmStatic
+    fun <K, V, E, R> open(
+      store: KeyValueStore<E, R>,
+      unproxyKey: Function<E, K>,
+      proxyKey: Function<K, E>,
+      unproxyValue: Function<R, V>,
+      proxyValue: Function<V, R>
+    ) =
+      ProxyKeyValueStore(store, unproxyKey::apply, proxyKey::apply, unproxyValue::apply, proxyValue::apply)
+  }
 
-  override suspend fun keys(): Iterable<K> = store.keys().map(unproxyKey)
+  override suspend fun get(key: K): V? {
+    val value = store.get(proxyKey(key))
+    return if (value == null) {
+      null
+    } else {
+      unproxyValue(value)
+    }
+  }
 
-  override fun close() = store.close()
+    override suspend fun put(key: K, value: V) = store.put(proxyKey(key), proxyValue(value))
+
+    override suspend fun keys(): Iterable<K> = store.keys().map(unproxyKey)
+
+    override fun close() = store.close()
 }
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 0dd9056..3fc8285 100644
--- a/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java
+++ b/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java
@@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 
 import org.apache.tuweni.bytes.Bytes;
 import org.apache.tuweni.concurrent.AsyncCompletion;
+import org.apache.tuweni.io.Base64;
 import org.apache.tuweni.junit.TempDirectory;
 import org.apache.tuweni.junit.TempDirectoryExtension;
 
@@ -51,23 +52,41 @@ class KeyValueStoreTest {
 
   @Test
   void testPutAndGetMapDB(@TempDirectory Path tmpDir) throws Exception {
+    try (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 testPutAndGetProxied(@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);
+        .open(tmpDir.resolve("mapdbproxy"), bytesIdentityFn, bytesIdentityFn, bytesIdentityFn, bytesIdentityFn);
+    try (ProxyKeyValueStore<String, String, Bytes, Bytes> proxy =
+        ProxyKeyValueStore.open(store, Base64::encode, Base64::decode, Base64::encode, Base64::decode)) {
+      AsyncCompletion completion = proxy.putAsync(Base64.encode(Bytes.of(123)), Base64.encode(Bytes.of(10, 12, 13)));
+      completion.join();
+      String value = proxy.getAsync(Base64.encode(Bytes.of(123))).get();
+      assertNotNull(value);
+      assertEquals(Base64.encode(Bytes.of(10, 12, 13)), value);
+      assertEquals(Bytes.of(10, 12, 13), store.getAsync(Bytes.of(123)).get());
+    }
+
   }
 
   @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);
+    try (KeyValueStore<Bytes, Bytes> store = MapDBKeyValueStore.open(tmpDir.resolve("mapdbdirect"))) {
+      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
diff --git a/kv/src/test/kotlin/org/apache/tuweni/kv/KeyValueStoreSpec.kt b/kv/src/test/kotlin/org/apache/tuweni/kv/KeyValueStoreSpec.kt
index a69277b..b388a70 100644
--- a/kv/src/test/kotlin/org/apache/tuweni/kv/KeyValueStoreSpec.kt
+++ b/kv/src/test/kotlin/org/apache/tuweni/kv/KeyValueStoreSpec.kt
@@ -438,7 +438,7 @@ object ProxyKeyValueStoreSpec : Spek({
     kv,
     Base64::decode,
     Base64::encode,
-    { if (it == null) { null } else { Base64.decode(it) } },
+    Base64::decode,
     Base64::encode
   )
   afterGroup {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org