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
commit 6562aa545a4dfc24da2f6c33ea25e43b23350493 Author: Antoine Toulme <[email protected]> AuthorDate: Sat Jun 6 15:56:50 2020 -0700 add tests for containsKey, make sure it's well supported --- .../apache/tuweni/kv/EntityManagerKeyValueStore.kt | 11 +--- .../kotlin/org/apache/tuweni/kv/KeyValueStore.kt | 2 +- .../org/apache/tuweni/kv/KeyValueStoreTest.java | 7 +++ .../org/apache/tuweni/kv/KeyValueStoreSpec.kt | 64 ++++++++++++++++++++++ 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/kv/src/main/kotlin/org/apache/tuweni/kv/EntityManagerKeyValueStore.kt b/kv/src/main/kotlin/org/apache/tuweni/kv/EntityManagerKeyValueStore.kt index ab66f37..a0ce034 100644 --- a/kv/src/main/kotlin/org/apache/tuweni/kv/EntityManagerKeyValueStore.kt +++ b/kv/src/main/kotlin/org/apache/tuweni/kv/EntityManagerKeyValueStore.kt @@ -50,16 +50,7 @@ class EntityManagerKeyValueStore<K, V> ) = EntityManagerKeyValueStore(entityManagerProvider::get, entityClass, idAccessor::apply) } - override suspend fun containsKey(key: K): Boolean { - val em = entityManagerProvider() - em.transaction.begin() - try { - return em.contains(key) - } finally { - em.transaction.commit() - em.close() - } - } + override suspend fun containsKey(key: K): Boolean = get(key) != null override suspend fun get(key: K): V? { val em = entityManagerProvider() diff --git a/kv/src/main/kotlin/org/apache/tuweni/kv/KeyValueStore.kt b/kv/src/main/kotlin/org/apache/tuweni/kv/KeyValueStore.kt index 5aaab72..ff2fff9 100644 --- a/kv/src/main/kotlin/org/apache/tuweni/kv/KeyValueStore.kt +++ b/kv/src/main/kotlin/org/apache/tuweni/kv/KeyValueStore.kt @@ -42,7 +42,7 @@ interface KeyValueStore<K, V> : Closeable, CoroutineScope { * @param key The key for the content. * @return An [AsyncResult] that will complete with a boolean result. */ - suspend fun containsKeyAsync(key: K): AsyncResult<Boolean> = asyncResult { containsKey(key) } + fun containsKeyAsync(key: K): AsyncResult<Boolean> = asyncResult { containsKey(key) } /** * Retrieves data from the 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 8f8ec44..8163526 100644 --- a/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java +++ b/kv/src/test/java/org/apache/tuweni/kv/KeyValueStoreTest.java @@ -12,6 +12,8 @@ */ package org.apache.tuweni.kv; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -44,6 +46,7 @@ class KeyValueStoreTest { KeyValueStore<Bytes, Bytes> store = MapKeyValueStore.open(map); AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13)); completion.join(); + assertTrue(store.containsKeyAsync(Bytes.of(123)).get()); Bytes value = store.getAsync(Bytes.of(123)).get(); assertNotNull(value); assertEquals(Bytes.of(10, 12, 13), value); @@ -56,6 +59,7 @@ class KeyValueStoreTest { .open(tmpDir.resolve("mapdb"), bytesIdentityFn, bytesIdentityFn, bytesIdentityFn, bytesIdentityFn)) { AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13)); completion.join(); + assertTrue(store.containsKeyAsync(Bytes.of(123)).get()); Bytes value = store.getAsync(Bytes.of(123)).get(); assertNotNull(value); assertEquals(Bytes.of(10, 12, 13), value); @@ -70,6 +74,7 @@ class KeyValueStoreTest { .open(store, Base64::encode, Base64::decode, Base64::encode, (key, value) -> Base64.decode(value))) { AsyncCompletion completion = proxy.putAsync(Base64.encode(Bytes.of(123)), Base64.encode(Bytes.of(10, 12, 13))); completion.join(); + assertTrue(store.containsKeyAsync(Bytes.of(123)).get()); String value = proxy.getAsync(Base64.encode(Bytes.of(123))).get(); assertNotNull(value); assertEquals(Base64.encode(Bytes.of(10, 12, 13)), value); @@ -83,6 +88,7 @@ class KeyValueStoreTest { try (KeyValueStore<Bytes, Bytes> store = MapDBKeyValueStore.open(tmpDir.resolve("mapdbdirect"))) { AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13)); completion.join(); + assertTrue(store.containsKeyAsync(Bytes.of(123)).get()); Bytes value = store.getAsync(Bytes.of(123)).get(); assertNotNull(value); assertEquals(Bytes.of(10, 12, 13), value); @@ -94,6 +100,7 @@ class KeyValueStoreTest { Map<Bytes, Bytes> map = new HashMap<>(); KeyValueStore<Bytes, Bytes> store = MapKeyValueStore.open(map); assertNull(store.getAsync(Bytes.of(123)).get()); + assertFalse(store.containsKeyAsync(Bytes.of(123)).get()); } @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 6a05b3c..e2a3ab5 100644 --- a/kv/src/test/kotlin/org/apache/tuweni/kv/KeyValueStoreSpec.kt +++ b/kv/src/test/kotlin/org/apache/tuweni/kv/KeyValueStoreSpec.kt @@ -64,6 +64,14 @@ object KeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put(foobar, foo) + kv.containsKey(foobar).should.be.`true` + kv.containsKey(bar).should.be.`false` + } + } + it("should return null when no value is present") { runBlocking { kv.get(Bytes.wrap("foofoobar".toByteArray())).should.be.`null` @@ -110,6 +118,14 @@ object InfinispanKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put(foobar, foo) + kv.containsKey(foobar).should.be.`true` + kv.containsKey(bar).should.be.`false` + } + } + it("should return null when no value is present") { runBlocking { kv.get(Bytes.wrap("foofoobar".toByteArray())).should.be.`null` @@ -154,6 +170,14 @@ object MapDBKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put(foobar, foo) + kv.containsKey(foobar).should.be.`true` + kv.containsKey(bar).should.be.`false` + } + } + it("should return null when no value is present") { runBlocking { kv.get(Bytes.wrap("foofoobar".toByteArray())).should.be.`null` @@ -225,6 +249,14 @@ object LevelDBKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put(foobar, foo) + kv.containsKey(foobar).should.be.`true` + kv.containsKey(bar).should.be.`false` + } + } + it("should return null when no value is present") { runBlocking { kv.get(Bytes.wrap("foofoobar".toByteArray())).should.be.`null` @@ -289,6 +321,14 @@ object RocksDBKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put(foobar, foo) + kv.containsKey(foobar).should.be.`true` + kv.containsKey(bar).should.be.`false` + } + } + it("should return null when no value is present") { runBlocking { kv.get(Bytes.wrap("foofoobar".toByteArray())).should.be.`null` @@ -371,6 +411,14 @@ object SQLKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put(foobar, foo) + kv.containsKey(foobar).should.be.`true` + kv.containsKey(bar).should.be.`false` + } + } + it("should allow to update values") { runBlocking { kv.put(foobar, foo) @@ -446,6 +494,14 @@ object EntityManagerKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + kv.put("foo", Store("foo", "bar")) + kv.containsKey("foo").should.be.`true` + kv.containsKey("foobar").should.be.`false` + } + } + it("should allow to update values") { runBlocking { kv.put("foo", Store("foo", "bar")) @@ -510,6 +566,14 @@ object ProxyKeyValueStoreSpec : Spek({ } } + it("should allow to see if a key is present") { + runBlocking { + proxy.put(foo, bar) + proxy.containsKey(foo).should.be.`true` + proxy.containsKey(foobar).should.be.`false` + } + } + it("should allow to update values") { runBlocking { proxy.put(foo, bar) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
