From f41bd9528e6df49db372c39919a1e4cfeb30b493 Mon Sep 17 00:00:00 2001 From: Tomasz Bekas Date: Mon, 4 Dec 2023 13:39:43 +0100 Subject: [PATCH] Remove methods taking non-async functions --- datastore/concurrentds.nim | 27 +++------------------------ datastore/sql/sqliteds.nim | 20 ++------------------ tests/datastore/concurrentdstests.nim | 8 ++++---- 3 files changed, 9 insertions(+), 46 deletions(-) diff --git a/datastore/concurrentds.nim b/datastore/concurrentds.nim index d530de4..7f7e9e4 100644 --- a/datastore/concurrentds.nim +++ b/datastore/concurrentds.nim @@ -14,39 +14,18 @@ push: {.upraises: [].} type Function*[T, U] = proc(value: T): U {.upraises: [CatchableError], gcsafe, closure.} - Modify* = Function[?seq[byte], ?seq[byte]] - ModifyGet* = Function[?seq[byte], (?seq[byte], seq[byte])] - ModifyAsync* = Function[?seq[byte], Future[?seq[byte]]] - ModifyGetAsync* = Function[?seq[byte], Future[(?seq[byte], seq[byte])]] + Modify* = Function[?seq[byte], Future[?seq[byte]]] + ModifyGet* = Function[?seq[byte], Future[(?seq[byte], seq[byte])]] method modify*(self: ConcurrentDatastore, key: Key, fn: Modify): Future[?!void] {.base, locks: "unknown".} = ## Concurrently safe way of modifying the value associated with the `key`. ## - ## Same as `modifyGet` with `fn: ModifyGetAsync` argument, but this takes non-async `fn` that doesn't - ## produce any auxillary value. - ## - - raiseAssert("Not implemented!") - -method modify*(self: ConcurrentDatastore, key: Key, fn: ModifyAsync): Future[?!void] {.base, locks: "unknown".} = - ## Concurrently safe way of modifying the value associated with the `key`. - ## - ## Same as `modifyGet` with `fn: ModifyGetAsync` argument, but this takes `fn` that doesn't produce - ## any auxillary value. + ## Same as `modifyGet`, but this takes `fn` that doesn't produce any auxillary value. ## raiseAssert("Not implemented!") method modifyGet*(self: ConcurrentDatastore, key: Key, fn: ModifyGet): Future[?!seq[byte]] {.base, locks: "unknown".} = - ## Concurrently safe way of updating value associated with the `key`. Returns auxillary value on - ## successful update. - ## - ## Same as `modifyGet` with `fn: ModifyGetAsync` argument, but this takes non-async `fn`. - ## - - raiseAssert("Not implemented!") - -method modifyGet*(self: ConcurrentDatastore, key: Key, fn: ModifyGetAsync): Future[?!seq[byte]] {.base, locks: "unknown".} = ## Concurrently safe way of updating value associated with the `key`. Returns auxillary value on ## successful update. ## diff --git a/datastore/sql/sqliteds.nim b/datastore/sql/sqliteds.nim index cb48e95..23445a2 100644 --- a/datastore/sql/sqliteds.nim +++ b/datastore/sql/sqliteds.nim @@ -31,7 +31,7 @@ proc timestamp*(t = epochTime()): int64 = const initVersion* = 0.int64 -method modifyGet*(self: SQLiteDatastore, key: Key, fn: ModifyGetAsync): Future[?!seq[byte]] {.async.} = +method modifyGet*(self: SQLiteDatastore, key: Key, fn: ModifyGet): Future[?!seq[byte]] {.async.} = var retriesLeft = 100 # allows reasonable concurrency, avoids infinite loop aux: seq[byte] @@ -117,13 +117,8 @@ method modifyGet*(self: SQLiteDatastore, key: Key, fn: ModifyGetAsync): Future[? return success(aux) -method modifyGet*(self: SQLiteDatastore, key: Key, fn: ModifyGet): Future[?!seq[byte]] {.async.} = - proc wrappedFn(maybeValue: ?seq[byte]): Future[(?seq[byte], seq[byte])] {.async.} = - return fn(maybeValue) - return await self.modifyGet(key, wrappedFn) - -method modify*(self: SQLiteDatastore, key: Key, fn: ModifyAsync): Future[?!void] {.async.} = +method modify*(self: SQLiteDatastore, key: Key, fn: Modify): Future[?!void] {.async.} = proc wrappedFn(maybeValue: ?seq[byte]): Future[(?seq[byte], seq[byte])] {.async.} = let res = await fn(maybeValue) let ignoredAux = newSeq[byte]() @@ -134,17 +129,6 @@ method modify*(self: SQLiteDatastore, key: Key, fn: ModifyAsync): Future[?!void] else: return success() -method modify*(self: SQLiteDatastore, key: Key, fn: Modify): Future[?!void] {.async.} = - proc wrappedFn(maybeValue: ?seq[byte]): Future[(?seq[byte], seq[byte])] {.async.} = - let maybeNewValue = fn(maybeValue) - let ignoredAux = newSeq[byte]() - return (maybeNewValue, ignoredAux) - - if err =? (await self.modifyGet(key, wrappedFn)).errorOption: - return failure(err) - else: - return success() - method has*(self: SQLiteDatastore, key: Key): Future[?!bool] {.async.} = var exists = false diff --git a/tests/datastore/concurrentdstests.nim b/tests/datastore/concurrentdstests.nim index 11b6a7c..13d8f72 100644 --- a/tests/datastore/concurrentdstests.nim +++ b/tests/datastore/concurrentdstests.nim @@ -80,7 +80,7 @@ proc concurrentStoreTests*( test "should put value": (await ds.delete(key)).tryGet() - proc returningSomeValue(_: ?seq[byte]): ?seq[byte] = + proc returningSomeValue(_: ?seq[byte]): Future[?seq[byte]] {.async.} = return @(123.uint64.toBytes).some (await ds.modify(key, returningSomeValue)).tryGet @@ -92,7 +92,7 @@ proc concurrentStoreTests*( test "should delete value": (await ds.put(key, @(0.uint64.toBytes))).tryGet - proc returningNone(_: ?seq[byte]): ?seq[byte] = + proc returningNone(_: ?seq[byte]): Future[?seq[byte]] {.async.} = return seq[byte].none (await ds.modify(key, returningNone)).tryGet @@ -102,7 +102,7 @@ proc concurrentStoreTests*( check not hasKey test "should return correct auxillary value": - proc returningAux(_: ?seq[byte]): (?seq[byte], seq[byte]) = + proc returningAux(_: ?seq[byte]): Future[(?seq[byte], seq[byte])] {.async.} = return (seq[byte].none, @[byte 123]) let result = await ds.modifyGet(key, returningAux) @@ -111,7 +111,7 @@ proc concurrentStoreTests*( result == success(@[byte 123]) test "should propagate exception as failure": - proc throwing(a: ?seq[byte]): ?seq[byte] = + proc throwing(a: ?seq[byte]): Future[?seq[byte]] {.async.} = raise newException(CatchableError, "some error msg") let result = await ds.modify(key, throwing)