This commit is contained in:
Dmitriy Ryajov 2022-09-12 12:30:52 -06:00
parent ed6842b743
commit 78653d9dd4
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
4 changed files with 83 additions and 43 deletions

View File

@ -15,26 +15,26 @@ type
method contains*(
self: Datastore,
key: Key): Future[?!bool] {.async, base, locks: "unknown".} =
key: Key): Future[?!bool] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method delete*(
self: Datastore,
key: Key): Future[?!void] {.async, base, locks: "unknown".} =
key: Key): Future[?!void] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method get*(
self: Datastore,
key: Key): Future[?!(?seq[byte])] {.async, base, locks: "unknown".} =
key: Key): Future[?!(?seq[byte])] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method put*(
self: Datastore,
key: Key,
data: seq[byte]): Future[?!void] {.async, base, locks: "unknown".} =
data: seq[byte]): Future[?!void] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")

View File

@ -1,4 +1,6 @@
import std/os
import std/sequtils
import std/options
import pkg/chronos
import pkg/questionable
@ -14,30 +16,10 @@ push: {.upraises: [].}
type
FileSystemDatastore* = ref object of Datastore
root: string
root*: string
const
objExt* = ".dsobject"
proc new*(
T: type FileSystemDatastore,
root: string): ?!T =
try:
let
root = if root.isAbsolute: root
else: getCurrentDir() / root
if not dirExists(root):
failure "directory does not exist: " & root
else:
success T(root: root)
except OSError as e:
failure e
proc root*(self: FileSystemDatastore): string =
self.root
objExt* = ".obj"
proc path*(
self: FileSystemDatastore,
@ -160,3 +142,18 @@ method put*(
# query: ...): Future[?!(?...)] {.async, locks: "unknown".} =
#
# return success ....some
proc new*(
T: type FileSystemDatastore,
root: string,
caseSensitive = true): ?!T =
let root = ? (
block:
if root.isAbsolute: root
else: getCurrentDir() / root).catch
if not dirExists(root):
failure "directory does not exist: " & root
else:
success T(root: root)

View File

@ -1,18 +1,61 @@
import ./key
type
Query* = object
key: QueryKey
Node* = object of RootObj
next*: Node
prev*: Node
QueryKey* = Key
Filter* = object of Node
field*: string
value*: string
FilterBool* = object of Filter
a*, b*: Filter
FilterAnd = object of FilterBool
FilterOr = object of FilterBool
Eq = object of Filter
Lt = object of Filter
Gt = object of Filter
Not = object of Filter
SortOrder* {.pure.} = enum
Assending,
Descensing
Order* = object
field*: string
sort*: SortOrder
Query* = object
key*: Key
limit*: int
skip*: int
orders*: seq[Order]
filters*: seq[Filter]
QueryResponse* = tuple[key: Key, data: seq[byte]]
proc `==`*(a, b: Filter): Filter = discard
proc `!=`*(a, b: Filter): Filter = discard
proc `>`*(a, b: Filter): Filter = discard
proc `>=`*(a, b: Filter): Filter = discard
proc `<`*(a, b: Filter): Filter = discard
proc `<=`*(a, b: Filter): Filter = discard
proc init*(
T: type Query,
key: QueryKey): T =
key: Key,
orders: openArray[Order] = [],
filters: openArray[Filter] = [],
skip = 0,
limit = 0): T =
T(key: key)
proc key*(self: Query): QueryKey =
self.key
T(
key: key,
filters: @filters,
orders: @orders,
skip: skip,
limit: limit)

View File

@ -50,19 +50,19 @@ suite "FileSystemDatastore":
asyncTest "helpers":
let
ds = FileSystemDatastore.new(root).get
ds = FileSystemDatastore.new(root).tryGet()
check:
# see comment in ../../datastore/filesystem_datastore re: whether path
# equivalence of e.g. Key(/a:b) and Key(/a/b) is problematic
ds.path(Key.init("a").get) == rootAbs / "a" & objExt
ds.path(Key.init("a:b").get) == rootAbs / "a" / "b" & objExt
ds.path(Key.init("a/b").get) == rootAbs / "a" / "b" & objExt
ds.path(Key.init("a:b/c").get) == rootAbs / "a" / "b" / "c" & objExt
ds.path(Key.init("a/b/c").get) == rootAbs / "a" / "b" / "c" & objExt
ds.path(Key.init("a:b/c:d").get) == rootAbs / "a" / "b" / "c" / "d" & objExt
ds.path(Key.init("a/b/c:d").get) == rootAbs / "a" / "b" / "c" / "d" & objExt
ds.path(Key.init("a/b/c/d").get) == rootAbs / "a" / "b" / "c" / "d" & objExt
ds.path(Key.init("a").tryGet()) == rootAbs / "a" & objExt
ds.path(Key.init("a:b").tryGet()) == rootAbs / "a" / "b" & objExt
ds.path(Key.init("a/b").tryGet()) == rootAbs / "a" / "b" & objExt
ds.path(Key.init("a:b/c").tryGet()) == rootAbs / "a" / "b" / "c" & objExt
ds.path(Key.init("a/b/c").tryGet()) == rootAbs / "a" / "b" / "c" & objExt
ds.path(Key.init("a:b/c:d").tryGet()) == rootAbs / "a" / "b" / "c" / "d" & objExt
ds.path(Key.init("a/b/c:d").tryGet()) == rootAbs / "a" / "b" / "c" / "d" & objExt
ds.path(Key.init("a/b/c/d").tryGet()) == rootAbs / "a" / "b" / "c" / "d" & objExt
asyncTest "put":
let