Defect fixes in kvstore (#589)

* Defect fixes in kvstore

* compile fixes
This commit is contained in:
Jacek Sieka 2023-02-21 18:54:30 +01:00 committed by GitHub
parent d2a3727c4c
commit ade6d19691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 15 deletions

View File

@ -8,7 +8,12 @@
## Simple Key-Value store database interface that allows creating multiple ## Simple Key-Value store database interface that allows creating multiple
## tables within each store ## tables within each store
{.push raises: [Defect].} when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
{.pragma: callback, gcsafe, raises: [Defect].}
else:
{.push raises: [].}
{.pragma: callback, gcsafe, raises: [].}
import import
std/[tables, hashes, sets], std/[tables, hashes, sets],
@ -25,16 +30,16 @@ type
KvResult*[T] = Result[T, string] KvResult*[T] = Result[T, string]
DataProc* = proc(val: openArray[byte]) {.gcsafe, raises: [Defect].} DataProc* = proc(val: openArray[byte]) {.callback.}
KeyValueProc* = proc(key, val: openArray[byte]) {.gcsafe, raises: [Defect].} KeyValueProc* = proc(key, val: openArray[byte]) {.callback.}
PutProc = proc (db: RootRef, key, val: openArray[byte]): KvResult[void] {.nimcall, gcsafe, raises: [Defect].} PutProc = proc (db: RootRef, key, val: openArray[byte]): KvResult[void] {.nimcall, callback.}
GetProc = proc (db: RootRef, key: openArray[byte], onData: DataProc): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].} GetProc = proc (db: RootRef, key: openArray[byte], onData: DataProc): KvResult[bool] {.nimcall, callback.}
FindProc = proc (db: RootRef, prefix: openArray[byte], onFind: KeyValueProc): KvResult[int] {.nimcall, gcsafe, raises: [Defect].} FindProc = proc (db: RootRef, prefix: openArray[byte], onFind: KeyValueProc): KvResult[int] {.nimcall, callback.}
DelProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].} DelProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, callback.}
ClearProc = proc (db: RootRef): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].} ClearProc = proc (db: RootRef): KvResult[bool] {.nimcall, callback.}
ContainsProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].} ContainsProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, callback.}
CloseProc = proc (db: RootRef): KvResult[void] {.nimcall, gcsafe, raises: [Defect].} CloseProc = proc (db: RootRef): KvResult[void] {.nimcall, callback.}
KvStoreRef* = ref object KvStoreRef* = ref object
## Key-Value store virtual interface ## Key-Value store virtual interface

View File

@ -1,6 +1,11 @@
## Implementation of KvStore based on sqlite3 ## Implementation of KvStore based on sqlite3
{.push raises: [Defect].} when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
{.pragma: callback, gcsafe, raises: [Defect].}
else:
{.push raises: [Defect].}
{.pragma: callback, gcsafe, raises: [].}
import import
std/[os, options, strformat, typetraits], std/[os, options, strformat, typetraits],
@ -22,7 +27,7 @@ type
Sqlite* = ptr sqlite3 Sqlite* = ptr sqlite3
SqliteStmt*[Params; Result] = distinct RawStmtPtr SqliteStmt*[Params; Result] = distinct RawStmtPtr
NoParams* = tuple # this is the empty tuple NoParams* = tuple # this is the empty tuple
ResultHandler*[T] = proc(val: T) {.gcsafe, raises: [Defect].} ResultHandler*[T] = proc(val: T) {.callback.}
SqStoreRef* = ref object SqStoreRef* = ref object
# Handle for a single database - from here, keyspaces and statements # Handle for a single database - from here, keyspaces and statements
@ -48,7 +53,7 @@ type
proc ( proc (
a: openArray[byte], a: openArray[byte],
b: openArray[byte] b: openArray[byte]
): Result[seq[byte], cstring] {.noSideEffect, gcsafe, cdecl, raises: [Defect].} ): Result[seq[byte], cstring] {.noSideEffect, cdecl, callback.}
template dispose(db: Sqlite) = template dispose(db: Sqlite) =
discard sqlite3_close(db) discard sqlite3_close(db)
@ -598,7 +603,7 @@ proc hasTable*(db: SqStoreRef, name: string): KvResult[bool] =
let let
sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='" & sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='" &
name & "';" name & "';"
db.exec(sql, (), proc(_: openArray[byte]) = discard) db.exec(sql, (), proc(_: openArray[byte]) {.callback.} = discard)
proc openKvStore*( proc openKvStore*(
db: SqStoreRef, name = "kvstore", withoutRowid = false, db: SqStoreRef, name = "kvstore", withoutRowid = false,

View File

@ -249,7 +249,7 @@ procSuite "SqStoreRef":
proc customSumFun( proc customSumFun(
a: openArray[byte], a: openArray[byte],
b: openArray[byte]): Result[seq[byte], cstring] {.noSideEffect, gcsafe, cdecl, raises: [Defect].} = b: openArray[byte]): Result[seq[byte], cstring] {.cdecl.} =
let num1 = uint32.fromBytesBE(a) let num1 = uint32.fromBytesBE(a)
let num2 = uint32.fromBytesBE(b) let num2 = uint32.fromBytesBE(b)
let sum = num1 + num2 let sum = num1 + num2