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/options
import std/algorithm
import pkg/upraises import pkg/upraises
import pkg/chronos import pkg/chronos
import pkg/questionable import pkg/questionable
@ -7,12 +8,9 @@ import pkg/questionable/results
import ./key import ./key
import ./types import ./types
import ./databuffer import ./databuffer
export options export options, SortOrder
type type
SortOrder* {.pure.} = enum
Assending,
Descending
Query* = object Query* = object
key*: Key # Key to be queried key*: Key # Key to be queried
@ -45,7 +43,7 @@ proc init*(
T: type Query, T: type Query,
key: Key, key: Key,
value = true, value = true,
sort = SortOrder.Assending, sort = Ascending,
offset = 0, offset = 0,
limit = -1): T = limit = -1): T =
@ -57,16 +55,13 @@ proc init*(
limit: limit) limit: limit)
type type
QSortOrder* {.pure.} = enum
Ascending,
Descending
QueryBuffer* = object QueryBuffer* = object
key*: KeyBuffer # Key to be queried key*: KeyBuffer # Key to be queried
value*: bool # Flag to indicate if data should be returned value*: bool # Flag to indicate if data should be returned
limit*: int # Max items to return - not available in all backends limit*: int # Max items to return - not available in all backends
offset*: int # Offset from which to start querying - 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 QueryResponseBuffer* = object
key*: KeyBuffer key*: KeyBuffer
@ -85,10 +80,7 @@ proc toBuffer*(q: Query): QueryBuffer =
key: KeyBuffer.new(q.key), key: KeyBuffer.new(q.key),
value: q.value, value: q.value,
offset: q.offset, offset: q.offset,
sort: sort: q.sort
case q.sort:
of SortOrder.Assending: QSortOrder.Ascending
of SortOrder.Descending: QSortOrder.Descending
) )
proc toQuery*(qb: QueryBuffer): Query = proc toQuery*(qb: QueryBuffer): Query =
@ -98,10 +90,7 @@ proc toQuery*(qb: QueryBuffer): Query =
value: qb.value, value: qb.value,
limit: qb.limit, limit: qb.limit,
offset: qb.offset, offset: qb.offset,
sort: sort: qb.sort
case qb.sort:
of QSortOrder.Ascending: SortOrder.Assending
of QSortOrder.Descending: SortOrder.Descending
) )
proc toBuffer*(q: QueryResponse): QueryResponseBuffer = proc toBuffer*(q: QueryResponse): QueryResponseBuffer =

View File

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