refactor
This commit is contained in:
parent
4fa532e72a
commit
1b7866aad2
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue