2022-09-19 17:13:11 -06:00
|
|
|
import pkg/upraises
|
|
|
|
|
import pkg/chronos
|
2022-09-20 16:41:54 -04:00
|
|
|
import pkg/questionable
|
|
|
|
|
import pkg/questionable/results
|
2022-09-19 17:13:11 -06:00
|
|
|
|
2022-07-15 15:28:42 -05:00
|
|
|
import ./key
|
2022-09-20 16:41:54 -04:00
|
|
|
import ./types
|
2022-07-15 15:28:42 -05:00
|
|
|
|
|
|
|
|
type
|
2022-09-12 12:30:52 -06:00
|
|
|
SortOrder* {.pure.} = enum
|
|
|
|
|
Assending,
|
2022-09-20 16:41:54 -04:00
|
|
|
Descending
|
2022-07-15 15:28:42 -05:00
|
|
|
|
2022-09-12 12:30:52 -06:00
|
|
|
Query* = object
|
2022-11-22 15:23:23 -06:00
|
|
|
key*: Key # 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*: SortOrder # Sort order - not available in all backends
|
2022-07-15 15:28:42 -05:00
|
|
|
|
2022-09-20 16:41:54 -04:00
|
|
|
QueryResponse* = tuple[key: ?Key, data: seq[byte]]
|
|
|
|
|
QueryEndedError* = object of DatastoreError
|
2022-09-19 17:13:11 -06:00
|
|
|
|
2022-09-20 16:41:54 -04:00
|
|
|
GetNext* = proc(): Future[?!QueryResponse] {.upraises: [], gcsafe, closure.}
|
|
|
|
|
IterDispose* = proc(): Future[?!void] {.upraises: [], gcsafe.}
|
|
|
|
|
QueryIter* = ref object
|
|
|
|
|
finished*: bool
|
2022-09-19 17:13:11 -06:00
|
|
|
next*: GetNext
|
2022-09-20 16:41:54 -04:00
|
|
|
dispose*: IterDispose
|
2022-09-19 17:13:11 -06:00
|
|
|
|
2022-09-20 16:41:54 -04:00
|
|
|
iterator items*(q: QueryIter): Future[?!QueryResponse] =
|
2022-09-19 17:13:11 -06:00
|
|
|
while not q.finished:
|
|
|
|
|
yield q.next()
|
2022-09-12 12:30:52 -06:00
|
|
|
|
2022-09-20 20:18:33 -04:00
|
|
|
proc defaultDispose(): Future[?!void] {.upraises: [], gcsafe, async.} =
|
|
|
|
|
return success()
|
|
|
|
|
|
2022-11-22 15:23:23 -06:00
|
|
|
proc new*(T: type QueryIter, dispose = defaultDispose): T =
|
|
|
|
|
QueryIter(dispose: dispose)
|
2022-09-20 20:18:33 -04:00
|
|
|
|
2022-07-15 15:28:42 -05:00
|
|
|
proc init*(
|
|
|
|
|
T: type Query,
|
2022-09-12 12:30:52 -06:00
|
|
|
key: Key,
|
2022-11-22 15:23:23 -06:00
|
|
|
value = true,
|
2022-09-20 16:41:54 -04:00
|
|
|
sort = SortOrder.Assending,
|
|
|
|
|
offset = 0,
|
|
|
|
|
limit = -1): T =
|
2022-07-15 15:28:42 -05:00
|
|
|
|
2022-09-12 12:30:52 -06:00
|
|
|
T(
|
|
|
|
|
key: key,
|
2022-09-19 15:52:34 -06:00
|
|
|
value: value,
|
|
|
|
|
sort: sort,
|
2022-09-20 16:41:54 -04:00
|
|
|
offset: offset,
|
2022-09-12 12:30:52 -06:00
|
|
|
limit: limit)
|