53 lines
1.3 KiB
Nim
Raw Normal View History

2022-09-19 17:13:11 -06:00
import pkg/upraises
2023-09-13 14:42:37 -06:00
import std/algorithm
2022-09-19 17:13:11 -06:00
import pkg/chronos
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
import ./types
2023-08-29 16:40:11 -07:00
export options, SortOrder
2022-07-15 15:28:42 -05:00
type
2022-09-12 12:30:52 -06:00
Query* = object
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
QueryResponse* = tuple[key: ?Key, data: seq[byte]]
QueryEndedError* = object of DatastoreError
2022-09-19 17:13:11 -06:00
2023-09-13 14:42:37 -06:00
GetNext* = proc(): Future[?!QueryResponse] {.upraises: [], gcsafe.}
IterDispose* = proc(): Future[?!void] {.upraises: [], gcsafe.}
QueryIter* = ref object
finished*: bool
2022-09-19 17:13:11 -06:00
next*: GetNext
dispose*: IterDispose
2022-09-19 17:13:11 -06:00
2023-09-13 14:42:37 -06:00
iterator items*(q: QueryIter): Future[?!QueryResponse] =
while not q.finished:
yield q.next()
proc defaultDispose(): Future[?!void] {.upraises: [], gcsafe, async.} =
return success()
2023-09-13 14:42:37 -06:00
proc init*(T: type QueryIter, dispose = defaultDispose): T =
QueryIter(dispose: dispose)
2022-07-15 15:28:42 -05:00
proc init*(
T: type Query,
2022-09-12 12:30:52 -06:00
key: Key,
value = true,
2023-09-13 14:42:37 -06:00
sort = SortOrder.Ascending,
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,
offset: offset,
2022-09-12 12:30:52 -06:00
limit: limit)