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 =
|
proc new*(tp: typedesc[KeyId], id: string): KeyId =
|
||||||
KeyId(data: DataBuffer.new(id))
|
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]:
|
when V is seq[byte]:
|
||||||
var bytes = newSeq[byte](size)
|
var bytes = newSeq[byte](size)
|
||||||
elif V is DataBuffer:
|
elif V is V:
|
||||||
var bytes = DataBuffer.new(size=size)
|
var bytes = V.new(size=size)
|
||||||
else:
|
else:
|
||||||
{.error: "unhandled result type".}
|
{.error: "unhandled result type".}
|
||||||
var
|
var
|
||||||
|
@ -119,7 +119,7 @@ proc readFile[V](self: FSDatastore, path: string): ?!V =
|
||||||
|
|
||||||
echo "BYTES: ", bytes.repr
|
echo "BYTES: ", bytes.repr
|
||||||
while read < size:
|
while read < size:
|
||||||
read += file.readBytes(bytes.toOpenArray(), read, size)
|
read += file.readBytes(bytes.toOpenArray(0, size-1), read, size)
|
||||||
|
|
||||||
if read < size:
|
if read < size:
|
||||||
return failure $read & " bytes were read from " & path &
|
return failure $read & " bytes were read from " & path &
|
||||||
|
@ -130,7 +130,7 @@ proc readFile[V](self: FSDatastore, path: string): ?!V =
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
return failure 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:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
|
|
||||||
|
@ -138,12 +138,12 @@ proc get*[K,V](self: FSDatastore[K,V], key: K): ?!DataBuffer =
|
||||||
return failure(
|
return failure(
|
||||||
newException(DatastoreKeyNotFound, "Key doesn't exist"))
|
newException(DatastoreKeyNotFound, "Key doesn't exist"))
|
||||||
|
|
||||||
return readFile[DataBuffer](self, path)
|
return readFile[V](self, path)
|
||||||
|
|
||||||
proc put*[K,V](
|
proc put*[K,V](self: FSDatastore[K,V],
|
||||||
self: FSDatastore[K,V],
|
key: K,
|
||||||
key: KeyId,
|
data: V
|
||||||
data: DataBuffer): ?!void =
|
): ?!void =
|
||||||
|
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
|
@ -151,7 +151,7 @@ proc put*[K,V](
|
||||||
try:
|
try:
|
||||||
var data = data
|
var data = data
|
||||||
createDir(parentDir(path))
|
createDir(parentDir(path))
|
||||||
writeFile(path, data.toOpenArray())
|
writeFile(path, data.toOpenArray(0, data.len()-1))
|
||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
return failure e
|
return failure e
|
||||||
|
|
||||||
|
@ -178,12 +178,12 @@ proc close*[K,V](self: FSDatastore[K,V]): ?!void =
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
type
|
type
|
||||||
FsQueryEnv*[K,V] = tuple[self: FSDatastore[K,V], basePath: DataBuffer]
|
FsQueryEnv*[K,V] = tuple[self: FSDatastore[K,V], basePath: V]
|
||||||
|
|
||||||
proc query*(
|
proc query*[K,V](
|
||||||
self: FSDatastore,
|
self: FSDatastore[K,V],
|
||||||
query: DbQuery[KeyId],
|
query: DbQuery[K],
|
||||||
): Result[DbQueryHandle[KeyId, DataBuffer, FsQueryEnv], ref CatchableError] =
|
): Result[DbQueryHandle[KeyId, V, FsQueryEnv], ref CatchableError] =
|
||||||
|
|
||||||
let key = query.key.toKey()
|
let key = query.key.toKey()
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
|
@ -199,10 +199,10 @@ proc query*(
|
||||||
else:
|
else:
|
||||||
path.changeFileExt("")
|
path.changeFileExt("")
|
||||||
|
|
||||||
let env: FsQueryEnv = (self: self, basePath: DataBuffer.new(basePath))
|
let env: FsQueryEnv = (self: self, basePath: V.new(basePath))
|
||||||
success DbQueryHandle[KeyId, DataBuffer, FsQueryEnv](env: env)
|
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)
|
let root = $(handle.env)
|
||||||
|
|
||||||
for path in root.dirIter():
|
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")
|
key = Key.init(keyPath).expect("should not fail")
|
||||||
data =
|
data =
|
||||||
if query.value:
|
if query.value:
|
||||||
let res = readFile[DataBuffer](handle.env.self, fl)
|
let res = readFile[V](handle.env.self, fl)
|
||||||
if res.isErr():
|
if res.isErr():
|
||||||
yield failure res.error()
|
yield failure res.error()
|
||||||
res.get()
|
res.get()
|
||||||
else:
|
else:
|
||||||
DataBuffer.new()
|
V.new()
|
||||||
|
|
||||||
yield success (key.some, data)
|
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)
|
var bf = cast[ptr UncheckedArray[T]](data[].buf)
|
||||||
bf.toOpenArray(0, data[].size-1)
|
bf.toOpenArray(0, data[].size-1)
|
||||||
|
|
||||||
template toOpenArray*(data: var DataBuffer): var openArray[byte] =
|
template toOpenArray*(data: var DataBuffer, first, last: int): var openArray[byte] =
|
||||||
toOpenArray(data, byte)
|
toOpenArray(data, byte).toOpenArray(first, last)
|
||||||
|
|
|
@ -60,7 +60,7 @@ suite "Test Basic FSDatastore":
|
||||||
createDir(basePathAbs)
|
createDir(basePathAbs)
|
||||||
|
|
||||||
var
|
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)
|
testBasicBackend(fsStore, key, bytes, otherBytes, batch)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue