2023-09-20 20:26:34 -07:00
|
|
|
import std/algorithm
|
|
|
|
|
import std/options
|
2023-09-25 19:54:22 -07:00
|
|
|
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
2023-09-20 17:49:57 -07:00
|
|
|
import ./threads/databuffer
|
2023-09-25 19:55:12 -07:00
|
|
|
import ./types
|
2023-09-20 17:49:57 -07:00
|
|
|
|
2023-09-25 19:55:12 -07:00
|
|
|
export databuffer, types, SortOrder
|
2023-09-20 20:14:08 -07:00
|
|
|
|
|
|
|
|
type
|
2023-09-25 19:54:22 -07:00
|
|
|
DbQueryResponse*[K, V] = tuple[key: Option[K], data: V]
|
|
|
|
|
|
|
|
|
|
DbQuery*[K] = object
|
|
|
|
|
key*: K # 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
|
2023-09-25 19:14:10 -07:00
|
|
|
|
2023-09-20 20:14:08 -07:00
|
|
|
KeyId* = object
|
|
|
|
|
## serialized Key ID, equivalent to `key.id()`
|
2023-09-20 20:26:34 -07:00
|
|
|
data*: DataBuffer
|
2023-09-20 20:14:08 -07:00
|
|
|
|
|
|
|
|
DbKey* = string | KeyId
|
|
|
|
|
DbVal* = seq[byte] | DataBuffer
|
|
|
|
|
|
2023-09-25 17:49:17 -07:00
|
|
|
DbBatchEntry*[K, V] = tuple[key: K, data: V]
|
2023-09-20 20:26:34 -07:00
|
|
|
|
2023-09-25 18:12:48 -07:00
|
|
|
DbQueryHandle*[K, V, T] = object
|
2023-09-25 17:49:17 -07:00
|
|
|
query*: DbQuery[K]
|
2023-09-21 18:29:07 -07:00
|
|
|
cancel*: bool
|
2023-09-25 16:06:09 -07:00
|
|
|
closed*: bool
|
2023-09-25 15:43:27 -07:00
|
|
|
env*: T
|
2023-09-21 18:29:07 -07:00
|
|
|
|
2023-09-25 19:54:22 -07:00
|
|
|
proc dbQuery*[K](
|
|
|
|
|
key: K,
|
2023-09-25 19:58:03 -07:00
|
|
|
value = false,
|
2023-09-25 19:54:22 -07:00
|
|
|
sort = SortOrder.Ascending,
|
|
|
|
|
offset = 0,
|
|
|
|
|
limit = -1
|
|
|
|
|
): DbQuery[K] =
|
2023-09-25 21:27:35 -07:00
|
|
|
DbQuery[K](key: key, value: value, sort: sort, offset: offset, limit: limit)
|
2023-09-25 19:54:22 -07:00
|
|
|
|
2023-09-20 20:14:08 -07:00
|
|
|
proc `$`*(id: KeyId): string = $(id.data)
|
|
|
|
|
|
2023-09-25 17:49:17 -07:00
|
|
|
proc toKey*(tp: typedesc[KeyId], id: cstring): KeyId = KeyId.new(id)
|
|
|
|
|
proc toKey*(tp: typedesc[string], id: cstring): string = $(id)
|
|
|
|
|
|
2023-09-25 17:59:18 -07:00
|
|
|
template toVal*(tp: typedesc[DataBuffer], id: openArray[byte]): DataBuffer = DataBuffer.new(id)
|
|
|
|
|
template toVal*(tp: typedesc[seq[byte]], id: openArray[byte]): seq[byte] = @(id)
|
|
|
|
|
|
2023-09-20 20:57:47 -07:00
|
|
|
proc new*(tp: typedesc[KeyId], id: cstring): KeyId =
|
2023-09-25 15:31:22 -07:00
|
|
|
KeyId(data: DataBuffer.new(id.toOpenArray(0, id.len()-1)))
|
2023-09-20 23:07:52 -07:00
|
|
|
|
|
|
|
|
proc new*(tp: typedesc[KeyId], id: string): KeyId =
|
2023-09-25 15:31:22 -07:00
|
|
|
KeyId(data: DataBuffer.new(id))
|
2023-09-20 23:07:52 -07:00
|
|
|
|
2023-09-20 20:14:08 -07:00
|
|
|
template toOpenArray*(x: DbKey): openArray[char] =
|
|
|
|
|
x.data.toOpenArray(char)
|