From 1b7866aad2e95acb83472b3e36f06d2f16ed10a8 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Wed, 27 Sep 2023 18:48:48 -0700 Subject: [PATCH] refactor --- datastore/backend.nim | 3 --- datastore/fsds.nim | 40 ++++++++++++++++---------------- datastore/threads/databuffer.nim | 4 ++-- tests/datastore/testfsds.nim | 2 +- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/datastore/backend.nim b/datastore/backend.nim index 5790b12..bb722b9 100644 --- a/datastore/backend.nim +++ b/datastore/backend.nim @@ -62,6 +62,3 @@ proc new*(tp: typedesc[KeyId], id: cstring): KeyId = proc new*(tp: typedesc[KeyId], id: string): KeyId = KeyId(data: DataBuffer.new(id)) - -template toOpenArray*(x: DbKey): openArray[char] = - x.data.toOpenArray(char) diff --git a/datastore/fsds.nim b/datastore/fsds.nim index 2b64975..8f2c770 100644 --- a/datastore/fsds.nim +++ b/datastore/fsds.nim @@ -110,8 +110,8 @@ proc readFile[V](self: FSDatastore, path: string): ?!V = when V is seq[byte]: var bytes = newSeq[byte](size) - elif V is DataBuffer: - var bytes = DataBuffer.new(size=size) + elif V is V: + var bytes = V.new(size=size) else: {.error: "unhandled result type".} var @@ -119,7 +119,7 @@ proc readFile[V](self: FSDatastore, path: string): ?!V = echo "BYTES: ", bytes.repr while read < size: - read += file.readBytes(bytes.toOpenArray(), read, size) + read += file.readBytes(bytes.toOpenArray(0, size-1), read, size) if read < size: return failure $read & " bytes were read from " & path & @@ -130,7 +130,7 @@ proc readFile[V](self: FSDatastore, path: string): ?!V = except CatchableError as e: return failure e -proc get*[K,V](self: FSDatastore[K,V], key: K): ?!DataBuffer = +proc get*[K,V](self: FSDatastore[K,V], key: K): ?!V = without path =? self.findPath(key), error: return failure error @@ -138,12 +138,12 @@ proc get*[K,V](self: FSDatastore[K,V], key: K): ?!DataBuffer = return failure( newException(DatastoreKeyNotFound, "Key doesn't exist")) - return readFile[DataBuffer](self, path) + return readFile[V](self, path) -proc put*[K,V]( - self: FSDatastore[K,V], - key: KeyId, - data: DataBuffer): ?!void = +proc put*[K,V](self: FSDatastore[K,V], + key: K, + data: V + ): ?!void = without path =? self.findPath(key), error: return failure error @@ -151,7 +151,7 @@ proc put*[K,V]( try: var data = data createDir(parentDir(path)) - writeFile(path, data.toOpenArray()) + writeFile(path, data.toOpenArray(0, data.len()-1)) except CatchableError as e: return failure e @@ -178,12 +178,12 @@ proc close*[K,V](self: FSDatastore[K,V]): ?!void = return success() type - FsQueryEnv*[K,V] = tuple[self: FSDatastore[K,V], basePath: DataBuffer] + FsQueryEnv*[K,V] = tuple[self: FSDatastore[K,V], basePath: V] -proc query*( - self: FSDatastore, - query: DbQuery[KeyId], -): Result[DbQueryHandle[KeyId, DataBuffer, FsQueryEnv], ref CatchableError] = +proc query*[K,V]( + self: FSDatastore[K,V], + query: DbQuery[K], +): Result[DbQueryHandle[KeyId, V, FsQueryEnv], ref CatchableError] = let key = query.key.toKey() without path =? self.findPath(key), error: @@ -199,10 +199,10 @@ proc query*( else: path.changeFileExt("") - let env: FsQueryEnv = (self: self, basePath: DataBuffer.new(basePath)) - success DbQueryHandle[KeyId, DataBuffer, FsQueryEnv](env: env) + let env: FsQueryEnv = (self: self, basePath: V.new(basePath)) + success DbQueryHandle[KeyId, V, FsQueryEnv](env: env) -iterator iter*[K, V](handle: var DbQueryHandle[K, V, DataBuffer]): ?!DbQueryResponse[K, V] = +iterator iter*[K, V](handle: var DbQueryHandle[K, V, V]): ?!DbQueryResponse[K, V] = let root = $(handle.env) for path in root.dirIter(): @@ -220,12 +220,12 @@ iterator iter*[K, V](handle: var DbQueryHandle[K, V, DataBuffer]): ?!DbQueryResp key = Key.init(keyPath).expect("should not fail") data = if query.value: - let res = readFile[DataBuffer](handle.env.self, fl) + let res = readFile[V](handle.env.self, fl) if res.isErr(): yield failure res.error() res.get() else: - DataBuffer.new() + V.new() yield success (key.some, data) diff --git a/datastore/threads/databuffer.nim b/datastore/threads/databuffer.nim index 3cb414a..5f80cbf 100644 --- a/datastore/threads/databuffer.nim +++ b/datastore/threads/databuffer.nim @@ -137,5 +137,5 @@ template toOpenArray*[T: byte | char](data: var DataBuffer, t: typedesc[T]): var var bf = cast[ptr UncheckedArray[T]](data[].buf) bf.toOpenArray(0, data[].size-1) -template toOpenArray*(data: var DataBuffer): var openArray[byte] = - toOpenArray(data, byte) +template toOpenArray*(data: var DataBuffer, first, last: int): var openArray[byte] = + toOpenArray(data, byte).toOpenArray(first, last) diff --git a/tests/datastore/testfsds.nim b/tests/datastore/testfsds.nim index 6a829b8..d1ad805 100644 --- a/tests/datastore/testfsds.nim +++ b/tests/datastore/testfsds.nim @@ -60,7 +60,7 @@ suite "Test Basic FSDatastore": createDir(basePathAbs) var - fsStore = newFSDatastore[KeyId, DataBuffer](root = basePathAbs, depth = 3).tryGet() + fsStore = newFSDatastore[Key, seq[byte]](root = basePathAbs, depth = 3).tryGet() testBasicBackend(fsStore, key, bytes, otherBytes, batch)