databuffer type

This commit is contained in:
Jaremy Creechley 2023-09-20 20:26:34 -07:00
parent ab5f8da736
commit 8b4f388bb3
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
3 changed files with 21 additions and 7 deletions

View File

@ -1,6 +1,8 @@
import pkg/questionable/results
import pkg/upraises
import std/algorithm
import std/options
import ./threads/databuffer
import ./threads/threadresult
import ./threads/semaphore
@ -13,11 +15,22 @@ export upraises, results
type
KeyId* = object
## serialized Key ID, equivalent to `key.id()`
data: DataBuffer
data*: DataBuffer
DbKey* = string | KeyId
DbVal* = seq[byte] | DataBuffer
DbBatchEntry* = tuple[key: string, data: seq[byte]] | tuple[key: KeyId, data: DataBuffer]
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
DbQueryResponse* = tuple[key: Option[string], data: seq[byte]] | tuple[key: Option[KeyId], data: DataBuffer]
proc `$`*(id: KeyId): string = $(id.data)
proc toDb*(key: Key): DbKey {.inline, raises: [].} =

View File

@ -26,7 +26,7 @@ proc readOnly*(self: SQLiteDatastore): bool = self.db.readOnly
proc timestamp*(t = epochTime()): int64 =
(t * 1_000_000).int64
proc has*(self: SQLiteDatastore, key: DbKey|string): ?!bool =
proc has*(self: SQLiteDatastore, key: DbKey): ?!bool =
var
exists = false
key = $key
@ -81,7 +81,7 @@ proc get*(self: SQLiteDatastore, key: DbKey): ?!seq[byte] =
proc put*(self: SQLiteDatastore, key: DbKey, data: seq[byte]): ?!void =
return self.db.putStmt.exec((key.id, data, timestamp()))
proc put*(self: SQLiteDatastore, batch: seq[BatchEntry]): ?!void =
proc put*(self: SQLiteDatastore, batch: iterator (): DbBatchEntry): ?!void =
if err =? self.db.beginStmt.exec().errorOption:
return failure err

View File

@ -8,6 +8,7 @@ import ../types
import ../query
import ../key
import ../backend
import ./databuffer
type
@ -19,7 +20,7 @@ type
ThreadTypes* = void | bool | SomeInteger | DataBuffer | tuple | Atomic
ThreadResErr* = (ErrorEnum, DataBuffer)
ThreadQueryRes* = (DataBuffer, DataBuffer)
# ThreadQueryRes* = tuple[key: KeyId, val: DataBuffer]
ThreadResult*[T: ThreadTypes] = Result[T, ThreadResErr]
@ -37,8 +38,8 @@ converter toExc*(e: ThreadResErr): ref CatchableError =
of ErrorEnum.DatastoreErr: (ref DatastoreError)(msg: $e[1])
of ErrorEnum.CatchableErr: (ref CatchableError)(msg: $e[1])
converter toQueryResponse*(r: ThreadQueryRes): QueryResponse =
if not r[0].isNil and r[0].len > 0 and key =? Key.init($r[0]):
(key.some, @(r[1]))
converter toQueryResponse*(r: DbQueryResponse): QueryResponse =
if not r.key.data.isNil and r.key.data.len > 0 and key =? Key.init($r.key.data):
(key.some, @(r.val))
else:
(Key.none, EmptyBytes)