nim-datastore/datastore/backend.nim

62 lines
1.9 KiB
Nim
Raw Normal View History

2023-09-20 17:49:57 -07:00
import pkg/questionable/results
import pkg/upraises
2023-09-20 20:26:34 -07:00
import std/algorithm
import std/options
2023-09-20 17:49:57 -07:00
import ./threads/databuffer
import ./threads/threadresult
import ./threads/semaphore
2023-09-20 20:14:08 -07:00
import ./key
2023-09-20 17:49:57 -07:00
import ./types
export databuffer, threadresult, semaphore, types
2023-09-20 20:31:42 -07:00
export upraises, results, SortOrder
2023-09-20 20:14:08 -07:00
type
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-20 22:23:18 -07:00
DbBatchEntry* = tuple[key: string, data: seq[byte]] | tuple[key: KeyId, data: DataBuffer]
2023-09-20 20:26:34 -07:00
DbQuery* = object
key*: KeyId # 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-21 18:29:07 -07:00
DbQueryHandle* = ref object
cancel*: bool
2023-09-20 20:31:42 -07:00
DbQueryResponse* = tuple[key: Option[KeyId], val: DataBuffer]
2023-09-20 20:26:34 -07:00
2023-09-20 20:14:08 -07:00
proc `$`*(id: KeyId): string = $(id.data)
2023-09-20 20:57:47 -07:00
proc new*(tp: typedesc[KeyId], id: cstring): KeyId =
2023-09-20 23:07:52 -07:00
## copy cstring including null terminator
KeyId(data: DataBuffer.new(id.pointer, 0, id.len()))
proc new*(tp: typedesc[KeyId], id: string): KeyId =
## copy cstring including null terminator
KeyId(data: DataBuffer.new(id, {dbNullTerminate}))
proc toCString*(key: KeyId): cstring =
## copy cstring including null terminator
cast[cstring](baseAddr key.data)
2023-09-20 20:57:47 -07:00
2023-09-20 20:14:08 -07:00
proc toDb*(key: Key): DbKey {.inline, raises: [].} =
let id: string = key.id()
let db = DataBuffer.new(id.len()+1) # include room for null for cstring compat
db.setData(id)
DbKey(data: db)
2023-09-20 23:07:52 -07:00
proc toKey*(key: KeyId): Key {.inline, raises: [].} =
2023-09-20 20:14:08 -07:00
Key.init(key.data).expect("expected valid key here for but got `" & $key.data & "`")
template toOpenArray*(x: DbKey): openArray[char] =
x.data.toOpenArray(char)