mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-07 16:13:07 +00:00
update fsds
This commit is contained in:
parent
10bad7e70d
commit
2eb0ee6e4e
@ -23,15 +23,15 @@ type
|
|||||||
proc isRootSubdir*(root, path: string): bool =
|
proc isRootSubdir*(root, path: string): bool =
|
||||||
path.startsWith(root)
|
path.startsWith(root)
|
||||||
|
|
||||||
proc findPath*(root: string, depth: int, key: Key): ?!string =
|
proc validDepth(self: FSDatastore, key: Key): bool =
|
||||||
|
key.len <= self.depth
|
||||||
|
|
||||||
|
proc findPath*(self: FSDatastore, key: Key): ?!string =
|
||||||
## Return filename corresponding to the key
|
## Return filename corresponding to the key
|
||||||
## or failure if the key doesn't correspond to a valid filename
|
## or failure if the key doesn't correspond to a valid filename
|
||||||
##
|
##
|
||||||
|
let root = $self.root
|
||||||
proc validDepth(depth: int, key: Key): bool =
|
if not self.validDepth(key):
|
||||||
key.len <= depth
|
|
||||||
|
|
||||||
if not validDepth(depth, key):
|
|
||||||
return failure "Path has invalid depth!"
|
return failure "Path has invalid depth!"
|
||||||
|
|
||||||
var
|
var
|
||||||
@ -53,7 +53,6 @@ proc findPath*(root: string, depth: int, key: Key): ?!string =
|
|||||||
segments.add(ns.field / ns.value)
|
segments.add(ns.field / ns.value)
|
||||||
|
|
||||||
let
|
let
|
||||||
root = root
|
|
||||||
fullname = (root / segments.joinPath())
|
fullname = (root / segments.joinPath())
|
||||||
.absolutePath()
|
.absolutePath()
|
||||||
.catch()
|
.catch()
|
||||||
@ -65,9 +64,6 @@ proc findPath*(root: string, depth: int, key: Key): ?!string =
|
|||||||
|
|
||||||
return success fullname
|
return success fullname
|
||||||
|
|
||||||
proc findPath*(self: FSDatastore, key: Key): ?!string =
|
|
||||||
findPath($self.root, self.depth, key)
|
|
||||||
|
|
||||||
proc has*(self: FSDatastore, key: KeyId): ?!bool =
|
proc has*(self: FSDatastore, key: KeyId): ?!bool =
|
||||||
let key = key.toKey()
|
let key = key.toKey()
|
||||||
return self.findPath(key).?fileExists()
|
return self.findPath(key).?fileExists()
|
||||||
@ -95,7 +91,7 @@ proc delete*(self: FSDatastore, keys: openArray[KeyId]): ?!void =
|
|||||||
|
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
proc readFile*[V](self: FSDatastore, path: string): ?!V =
|
proc readFile[V](self: FSDatastore, path: string): ?!V =
|
||||||
var
|
var
|
||||||
file: File
|
file: File
|
||||||
|
|
||||||
@ -180,7 +176,7 @@ proc close*(self: FSDatastore): ?!void =
|
|||||||
return success()
|
return success()
|
||||||
|
|
||||||
type
|
type
|
||||||
FsQueryEnv* = tuple[path: DataBuffer, root: DataBuffer]
|
FsQueryEnv* = tuple[basePath: DataBuffer, self: FSDatastore]
|
||||||
|
|
||||||
proc query*(
|
proc query*(
|
||||||
self: FSDatastore,
|
self: FSDatastore,
|
||||||
@ -201,29 +197,14 @@ proc query*(
|
|||||||
else:
|
else:
|
||||||
path.changeFileExt("")
|
path.changeFileExt("")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
iterator iter*[K, V](handle: var DbQueryHandle[K, V, DataBuffer]): ?!DbQueryResponse[K, V] =
|
iterator iter*[K, V](handle: var DbQueryHandle[K, V, DataBuffer]): ?!DbQueryResponse[K, V] =
|
||||||
|
|
||||||
let root = $(handle.env)
|
let root = $(handle.env)
|
||||||
|
|
||||||
for path in root.dirIter():
|
for path in root.dirIter():
|
||||||
|
if handle.cancel:
|
||||||
|
return
|
||||||
|
|
||||||
let
|
var keyPath = handle.basePath
|
||||||
root = $self.root
|
|
||||||
path = walker()
|
|
||||||
|
|
||||||
if iter.finished:
|
|
||||||
return failure "iterator is finished"
|
|
||||||
|
|
||||||
# await lock.acquire()
|
|
||||||
|
|
||||||
if finished(walker):
|
|
||||||
iter.finished = true
|
|
||||||
return success (Key.none, EmptyBytes)
|
|
||||||
|
|
||||||
var
|
|
||||||
keyPath = basePath
|
|
||||||
|
|
||||||
keyPath.removePrefix(root)
|
keyPath.removePrefix(root)
|
||||||
keyPath = keyPath / path.changeFileExt("")
|
keyPath = keyPath / path.changeFileExt("")
|
||||||
@ -233,16 +214,13 @@ 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:
|
||||||
readFile[DataBuffer](self, (basePath / path).absolutePath)
|
let fl = (handle.env.basePath / path).absolutePath()
|
||||||
.expect("Should read file")
|
readFile[DataBuffer](handle.env.self, fl).expect("Should read file")
|
||||||
else:
|
else:
|
||||||
DataBuffer.new(0)
|
DataBuffer.new(0)
|
||||||
|
|
||||||
return success (key.some, data)
|
return success (key.some, data)
|
||||||
|
|
||||||
iter.next = next
|
|
||||||
return success iter
|
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type FSDatastore,
|
T: type FSDatastore,
|
||||||
root: string,
|
root: string,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user