From 36e6679a799dc10c5407fb828f5ce1f84f2fedc0 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Tue, 26 Sep 2023 17:01:21 -0700 Subject: [PATCH] fixup backend types --- datastore/query.nim | 2 +- datastore/sql/sqliteds.nim | 5 ++--- datastore/threads/databuffer.nim | 4 ++-- datastore/threads/threadproxyds.nim | 26 +++++++------------------- tests/datastore/testthreadproxyds.nim | 22 +++++++++++----------- 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/datastore/query.nim b/datastore/query.nim index 1295ff9..477d04d 100644 --- a/datastore/query.nim +++ b/datastore/query.nim @@ -44,4 +44,4 @@ proc init*(T: type Query, dbQuery[Key](key, value, sort, offset, limit) proc toKey*(key: KeyId): Key {.inline, raises: [].} = - Key.init(key.data).expect("expected valid key here for but got `" & $key.data & "`") + Key.init($key.data).expect("expected valid key here for but got `" & $key.data & "`") diff --git a/datastore/sql/sqliteds.nim b/datastore/sql/sqliteds.nim index 8b07c39..feb0785 100644 --- a/datastore/sql/sqliteds.nim +++ b/datastore/sql/sqliteds.nim @@ -58,13 +58,12 @@ proc delete*[K,V](self: SQLiteBackend[K,V], keys: openArray[K]): ?!void = return success() -proc get*[K,V](self: SQLiteBackend[K,V], key: K): ?!seq[byte] = +proc get*[K,V](self: SQLiteBackend[K,V], key: K): ?!V = # see comment in ./filesystem_datastore re: finer control of memory # allocation in `proc get`, could apply here as well if bytes were read # incrementally with `sqlite3_blob_read` - var - bytes: seq[byte] + var bytes: V proc onData(s: RawStmtPtr) = bytes = dataCol[V](self.db.getDataCol) diff --git a/datastore/threads/databuffer.nim b/datastore/threads/databuffer.nim index 9e192e7..5ea7b4b 100644 --- a/datastore/threads/databuffer.nim +++ b/datastore/threads/databuffer.nim @@ -87,7 +87,7 @@ proc setData*[T: byte | char](db: DataBuffer, data: openArray[T]) = copyMem(db[].buf, baseAddr data, data.len()) db[].size = data.len() -converter toSeq*(self: DataBuffer): seq[byte] = +proc toSeq*(self: DataBuffer): seq[byte] = ## convert buffer to a seq type using copy and either a byte or char ## @@ -102,7 +102,7 @@ proc `@`*(self: DataBuffer): seq[byte] = self.toSeq() -converter toString*(data: DataBuffer): string = +proc toString*(data: DataBuffer): string = ## convert buffer to string type using copy ## diff --git a/datastore/threads/threadproxyds.nim b/datastore/threads/threadproxyds.nim index 0387781..6582c0e 100644 --- a/datastore/threads/threadproxyds.nim +++ b/datastore/threads/threadproxyds.nim @@ -122,14 +122,11 @@ template dispatchTaskWrap[T](self: ThreadDatastore, try: case self.backend.kind: of Sqlite: - echo "dispatchTask:sql:" var ds {.used, inject.} = self.backend.sql proc runTask() = `blk` runTask() - echo "dispatchTask:wait:start" await wait(ctx[].signal) - echo "dispatchTask:wait:done" except CancelledError as exc: trace "Cancelling thread future!", exc = exc.msg @@ -191,14 +188,8 @@ proc putTask[T, DB](ctx: TaskCtx[T], ds: DB; key: KeyId, data: DataBuffer) {.gcsafe, nimcall.} = ## run backend command - echo "\n\nputTask:start " executeTask(ctx): - echo "putTask:key: ", key - echo "putTask:data: ", data - echo "putTask:ctx: ", ctx.repr() - echo "" put(ds, key, data) - echo "putTask:done" method put*(self: ThreadDatastore, key: Key, @@ -209,14 +200,8 @@ method put*(self: ThreadDatastore, let key = KeyId.new key.id() let data = DataBuffer.new data - dispatchTask[void](self, signal): - echo "put:key: ", key - echo "put:data: ", data - echo "put:ctx: ", ctx.repr() - echo "" self.tp.spawn putTask(ctx, ds, key, data) - return ctx[].res method put*( @@ -227,12 +212,14 @@ method put*( if err =? (await self.put(entry.key, entry.data)).errorOption: return failure err - -proc getTask[T, DB](ctx: TaskCtx[T], ds: DB; +proc getTask[DB](ctx: TaskCtx[DataBuffer], ds: DB; key: KeyId) {.gcsafe, nimcall.} = ## run backend command executeTask(ctx): - get(ds, key) + let res = get(ds, key) + static: + echo "getTask:type: ", res.typeof + res method get*(self: ThreadDatastore, key: Key, @@ -242,8 +229,9 @@ method get*(self: ThreadDatastore, return failure err let key = KeyId.new key.id() - dispatchTask[void](self, signal): + dispatchTask[DataBuffer](self, signal): self.tp.spawn getTask(ctx, ds, key) + # return ctx[].res method close*(self: ThreadDatastore): Future[?!void] {.async.} = await self.semaphore.closeAll() diff --git a/tests/datastore/testthreadproxyds.nim b/tests/datastore/testthreadproxyds.nim index 93eb84b..a9ae843 100644 --- a/tests/datastore/testthreadproxyds.nim +++ b/tests/datastore/testthreadproxyds.nim @@ -46,17 +46,17 @@ suite "Test Basic ThreadProxyDatastore": echo "res1: ", res1.repr check res1.isOk - # test "check get": - # echo "\n\n=== get ===" - # echo "get send key: ", key.repr - # let res2 = await ds.get(key) - # echo "get key post: ", key.repr - # echo "get res2: ", res2.repr - # echo res2.get() == data - # var val = "" - # for c in res2.get(): - # val &= char(c) - # echo "get res2: ", $val + test "check get": + echo "\n\n=== get ===" + echo "get send key: ", key.repr + let res2 = await ds.get(key) + echo "get key post: ", key.repr + echo "get res2: ", res2.repr + echo res2.get() == data + var val = "" + for c in res2.get(): + val &= char(c) + echo "get res2: ", $val # suite "Test Basic ThreadDatastore with SQLite":