implementing query type

This commit is contained in:
Jaremy Creechley 2023-08-29 16:40:11 -07:00 committed by Dmitriy Ryajov
parent 428b3e6a2e
commit 2979ae08fd
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 19 additions and 38 deletions

View File

@ -1,4 +1,5 @@
import std/options
import std/algorithm
import pkg/upraises
import pkg/chronos
import pkg/questionable
@ -7,12 +8,9 @@ import pkg/questionable/results
import ./key
import ./types
import ./databuffer
export options
export options, SortOrder
type
SortOrder* {.pure.} = enum
Assending,
Descending
Query* = object
key*: Key # Key to be queried
@ -45,7 +43,7 @@ proc init*(
T: type Query,
key: Key,
value = true,
sort = SortOrder.Assending,
sort = Ascending,
offset = 0,
limit = -1): T =
@ -57,16 +55,13 @@ proc init*(
limit: limit)
type
QSortOrder* {.pure.} = enum
Ascending,
Descending
QueryBuffer* = object
key*: KeyBuffer # Key to be queried
value*: bool # Flag to indicate if data should be returned
limit*: int # Max items to return - not available in all backends
offset*: int # Offset from which to start querying - not available in all backends
sort*: QSortOrder # Sort order - not available in all backends
sort*: SortOrder # Sort order - not available in all backends
QueryResponseBuffer* = object
key*: KeyBuffer
@ -85,10 +80,7 @@ proc toBuffer*(q: Query): QueryBuffer =
key: KeyBuffer.new(q.key),
value: q.value,
offset: q.offset,
sort:
case q.sort:
of SortOrder.Assending: QSortOrder.Ascending
of SortOrder.Descending: QSortOrder.Descending
sort: q.sort
)
proc toQuery*(qb: QueryBuffer): Query =
@ -98,10 +90,7 @@ proc toQuery*(qb: QueryBuffer): Query =
value: qb.value,
limit: qb.limit,
offset: qb.offset,
sort:
case qb.sort:
of QSortOrder.Ascending: SortOrder.Assending
of QSortOrder.Descending: SortOrder.Descending
sort: qb.sort
)
proc toBuffer*(q: QueryResponse): QueryResponseBuffer =
@ -133,4 +122,4 @@ proc toQueryResponse*(qb: QueryResponseBuffer): QueryResponse =
# result.ok(ret[].results.get().toString())
# else:
# let exc: ref CatchableError = ret[].results.error().toCatchable()
# result.err(exc)
# result.err(exc)

View File

@ -21,7 +21,6 @@ suite "Test Basic ThreadProxyDatastore":
var
sds: ThreadProxyDatastore
mem: MemoryDatastore
res: ThreadProxyDatastore
key1: Key
data: seq[byte]
@ -69,25 +68,18 @@ suite "Test Basic ThreadProxyDatastore":
basicStoreTests(ds, key, bytes, otherBytes)
# suite "Test Query":
# let
# path = currentSourcePath() # get this file's name
# basePath = "tests_data"
# basePathAbs = path.parentDir / basePath
suite "Test Query":
let
path = currentSourcePath() # get this file's name
basePath = "tests_data"
basePathAbs = path.parentDir / basePath
# var
# ds: FSDatastore
var
mem: MemoryDatastore
sds: ThreadProxyDatastore
# setup:
# removeDir(basePathAbs)
# require(not dirExists(basePathAbs))
# createDir(basePathAbs)
setup:
mem = MemoryDatastore.new()
sds = newThreadProxyDatastore(mem).expect("should work")
# ds = FSDatastore.new(root = basePathAbs, depth = 5).tryGet()
# teardown:
# removeDir(basePathAbs)
# require(not dirExists(basePathAbs))
# queryTests(ds, false)
queryTests(sds, false)