mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-05-05 17:29:31 +00:00
reorg
This commit is contained in:
parent
82bf1f1a3c
commit
3e477f3461
@ -9,7 +9,7 @@ import pkg/upraises
|
|||||||
import pkg/chronos
|
import pkg/chronos
|
||||||
import pkg/taskpools
|
import pkg/taskpools
|
||||||
|
|
||||||
import ./threads/sqlbackend
|
import ./threads/fsbackend
|
||||||
import ./threads/threadproxyds
|
import ./threads/threadproxyds
|
||||||
import ./datastore
|
import ./datastore
|
||||||
|
|
||||||
@ -17,10 +17,9 @@ export datastore, Taskpool
|
|||||||
|
|
||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
FSDatastore* = ref object of Datastore
|
FSDatastore* = ref object of Datastore
|
||||||
db: ThreadDatastore[SQLiteBackend[KeyId, DataBuffer]]
|
db: ThreadDatastore[FSBackend[KeyId, DataBuffer]]
|
||||||
|
|
||||||
proc path*(self: FSDatastore): string =
|
proc path*(self: FSDatastore): string =
|
||||||
self.db.backend.path()
|
self.db.backend.path()
|
||||||
|
|||||||
@ -13,7 +13,14 @@ import ../datastore
|
|||||||
import ./threads/backend
|
import ./threads/backend
|
||||||
import ./threads/sqlbackend
|
import ./threads/sqlbackend
|
||||||
|
|
||||||
export datastore, sqlbackend
|
import pkg/chronos
|
||||||
|
import pkg/taskpools
|
||||||
|
|
||||||
|
import ./threads/sqlbackend
|
||||||
|
import ./threads/threadproxyds
|
||||||
|
import ./datastore
|
||||||
|
|
||||||
|
export datastore, Taskpool
|
||||||
|
|
||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
@ -64,25 +71,6 @@ method queryIter*(self: SQLiteDatastore,
|
|||||||
query: Query
|
query: Query
|
||||||
): ?!(iterator(): ?!QueryResponse) =
|
): ?!(iterator(): ?!QueryResponse) =
|
||||||
|
|
||||||
let dbquery = dbQuery(
|
|
||||||
key= KeyId.new query.key.id(),
|
|
||||||
value= query.value,
|
|
||||||
limit= query.limit,
|
|
||||||
offset= query.offset,
|
|
||||||
sort= query.sort,
|
|
||||||
)
|
|
||||||
var qhandle = ? self.db.query(dbquery)
|
|
||||||
|
|
||||||
let iter = iterator(): ?!QueryResponse =
|
|
||||||
for resp in qhandle.queryIter():
|
|
||||||
without qres =? resp, err:
|
|
||||||
yield QueryResponse.failure err
|
|
||||||
let k = qres.key.map() do(k: KeyId) -> Key:
|
|
||||||
Key.init($k).expect("valid key")
|
|
||||||
let v: seq[byte] = qres.data.toSeq()
|
|
||||||
yield success (k, v)
|
|
||||||
|
|
||||||
success iter
|
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type SQLiteDatastore,
|
T: type SQLiteDatastore,
|
||||||
|
|||||||
@ -15,7 +15,7 @@ export datastore
|
|||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
type
|
type
|
||||||
FSDatastore*[K, V] = object
|
FSBackend*[K, V] = object
|
||||||
root*: DataBuffer
|
root*: DataBuffer
|
||||||
ignoreProtected: bool
|
ignoreProtected: bool
|
||||||
depth: int
|
depth: int
|
||||||
@ -23,10 +23,10 @@ type
|
|||||||
proc isRootSubdir*(root, path: string): bool =
|
proc isRootSubdir*(root, path: string): bool =
|
||||||
path.startsWith(root)
|
path.startsWith(root)
|
||||||
|
|
||||||
proc validDepth*(self: FSDatastore, key: Key): bool =
|
proc validDepth*(self: FSBackend, key: Key): bool =
|
||||||
key.len <= self.depth
|
key.len <= self.depth
|
||||||
|
|
||||||
proc findPath*[K,V](self: FSDatastore[K,V], key: K): ?!string =
|
proc findPath*[K,V](self: FSBackend[K,V], key: K): ?!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
|
||||||
##
|
##
|
||||||
@ -65,15 +65,15 @@ proc findPath*[K,V](self: FSDatastore[K,V], key: K): ?!string =
|
|||||||
|
|
||||||
return success fullname
|
return success fullname
|
||||||
|
|
||||||
proc has*[K,V](self: FSDatastore[K,V], key: K): ?!bool =
|
proc has*[K,V](self: FSBackend[K,V], key: K): ?!bool =
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
success path.fileExists()
|
success path.fileExists()
|
||||||
|
|
||||||
proc contains*[K](self: FSDatastore, key: K): bool =
|
proc contains*[K](self: FSBackend, key: K): bool =
|
||||||
return self.has(key).get()
|
return self.has(key).get()
|
||||||
|
|
||||||
proc delete*[K,V](self: FSDatastore[K,V], key: K): ?!void =
|
proc delete*[K,V](self: FSBackend[K,V], key: K): ?!void =
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
|
|
||||||
@ -87,14 +87,14 @@ proc delete*[K,V](self: FSDatastore[K,V], key: K): ?!void =
|
|||||||
|
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
proc delete*[K,V](self: FSDatastore[K,V], keys: openArray[K]): ?!void =
|
proc delete*[K,V](self: FSBackend[K,V], keys: openArray[K]): ?!void =
|
||||||
for key in keys:
|
for key in keys:
|
||||||
if err =? self.delete(key).errorOption:
|
if err =? self.delete(key).errorOption:
|
||||||
return failure err
|
return failure err
|
||||||
|
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
proc readFile[V](self: FSDatastore, path: string): ?!V =
|
proc readFile[V](self: FSBackend, path: string): ?!V =
|
||||||
var
|
var
|
||||||
file: File
|
file: File
|
||||||
|
|
||||||
@ -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): ?!V =
|
proc get*[K,V](self: FSBackend[K,V], key: K): ?!V =
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ proc get*[K,V](self: FSDatastore[K,V], key: K): ?!V =
|
|||||||
|
|
||||||
return readFile[V](self, path)
|
return readFile[V](self, path)
|
||||||
|
|
||||||
proc put*[K,V](self: FSDatastore[K,V],
|
proc put*[K,V](self: FSBackend[K,V],
|
||||||
key: K,
|
key: K,
|
||||||
data: V
|
data: V
|
||||||
): ?!void =
|
): ?!void =
|
||||||
@ -158,7 +158,7 @@ proc put*[K,V](self: FSDatastore[K,V],
|
|||||||
return success()
|
return success()
|
||||||
|
|
||||||
proc put*[K,V](
|
proc put*[K,V](
|
||||||
self: FSDatastore,
|
self: FSBackend,
|
||||||
batch: seq[DbBatchEntry[K, V]]): ?!void =
|
batch: seq[DbBatchEntry[K, V]]): ?!void =
|
||||||
|
|
||||||
for entry in batch:
|
for entry in batch:
|
||||||
@ -174,7 +174,7 @@ iterator dirIter(path: string): string {.gcsafe.} =
|
|||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
raise newException(Defect, exc.msg)
|
raise newException(Defect, exc.msg)
|
||||||
|
|
||||||
proc close*[K,V](self: FSDatastore[K,V]): ?!void =
|
proc close*[K,V](self: FSBackend[K,V]): ?!void =
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -185,11 +185,11 @@ type
|
|||||||
env*: FsQueryEnv[K,V]
|
env*: FsQueryEnv[K,V]
|
||||||
|
|
||||||
FsQueryEnv*[K,V] = object
|
FsQueryEnv*[K,V] = object
|
||||||
self: FSDatastore[K,V]
|
self: FSBackend[K,V]
|
||||||
basePath: DataBuffer
|
basePath: DataBuffer
|
||||||
|
|
||||||
proc query*[K,V](
|
proc query*[K,V](
|
||||||
self: FSDatastore[K,V],
|
self: FSBackend[K,V],
|
||||||
query: DbQuery[K],
|
query: DbQuery[K],
|
||||||
): Result[FsQueryHandle[K, V], ref CatchableError] =
|
): Result[FsQueryHandle[K, V], ref CatchableError] =
|
||||||
|
|
||||||
@ -253,11 +253,11 @@ iterator queryIter*[K, V](
|
|||||||
yield success (key.some, data)
|
yield success (key.some, data)
|
||||||
handle.close()
|
handle.close()
|
||||||
|
|
||||||
proc newFSDatastore*[K,V](root: string,
|
proc newFSBackend*[K,V](root: string,
|
||||||
depth = 2,
|
depth = 2,
|
||||||
caseSensitive = true,
|
caseSensitive = true,
|
||||||
ignoreProtected = false
|
ignoreProtected = false
|
||||||
): ?!FSDatastore[K,V] =
|
): ?!FSBackend[K,V] =
|
||||||
|
|
||||||
let root = ? (
|
let root = ? (
|
||||||
block:
|
block:
|
||||||
@ -267,7 +267,7 @@ proc newFSDatastore*[K,V](root: string,
|
|||||||
if not dirExists(root):
|
if not dirExists(root):
|
||||||
return failure "directory does not exist: " & root
|
return failure "directory does not exist: " & root
|
||||||
|
|
||||||
success FSDatastore[K,V](
|
success FSBackend[K,V](
|
||||||
root: DataBuffer.new root,
|
root: DataBuffer.new root,
|
||||||
ignoreProtected: ignoreProtected,
|
ignoreProtected: ignoreProtected,
|
||||||
depth: depth)
|
depth: depth)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user