mirror of https://github.com/status-im/nim-eth.git
Defect fixes in kvstore (#589)
* Defect fixes in kvstore * compile fixes
This commit is contained in:
parent
d2a3727c4c
commit
ade6d19691
|
@ -8,7 +8,12 @@
|
|||
## Simple Key-Value store database interface that allows creating multiple
|
||||
## tables within each store
|
||||
|
||||
when (NimMajor, NimMinor) < (1, 4):
|
||||
{.push raises: [Defect].}
|
||||
{.pragma: callback, gcsafe, raises: [Defect].}
|
||||
else:
|
||||
{.push raises: [].}
|
||||
{.pragma: callback, gcsafe, raises: [].}
|
||||
|
||||
import
|
||||
std/[tables, hashes, sets],
|
||||
|
@ -25,16 +30,16 @@ type
|
|||
|
||||
KvResult*[T] = Result[T, string]
|
||||
|
||||
DataProc* = proc(val: openArray[byte]) {.gcsafe, raises: [Defect].}
|
||||
KeyValueProc* = proc(key, val: openArray[byte]) {.gcsafe, raises: [Defect].}
|
||||
DataProc* = proc(val: openArray[byte]) {.callback.}
|
||||
KeyValueProc* = proc(key, val: openArray[byte]) {.callback.}
|
||||
|
||||
PutProc = proc (db: RootRef, key, val: openArray[byte]): KvResult[void] {.nimcall, gcsafe, raises: [Defect].}
|
||||
GetProc = proc (db: RootRef, key: openArray[byte], onData: DataProc): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].}
|
||||
FindProc = proc (db: RootRef, prefix: openArray[byte], onFind: KeyValueProc): KvResult[int] {.nimcall, gcsafe, raises: [Defect].}
|
||||
DelProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].}
|
||||
ClearProc = proc (db: RootRef): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].}
|
||||
ContainsProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, gcsafe, raises: [Defect].}
|
||||
CloseProc = proc (db: RootRef): 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, callback.}
|
||||
FindProc = proc (db: RootRef, prefix: openArray[byte], onFind: KeyValueProc): KvResult[int] {.nimcall, callback.}
|
||||
DelProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, callback.}
|
||||
ClearProc = proc (db: RootRef): KvResult[bool] {.nimcall, callback.}
|
||||
ContainsProc = proc (db: RootRef, key: openArray[byte]): KvResult[bool] {.nimcall, callback.}
|
||||
CloseProc = proc (db: RootRef): KvResult[void] {.nimcall, callback.}
|
||||
|
||||
KvStoreRef* = ref object
|
||||
## Key-Value store virtual interface
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
## Implementation of KvStore based on sqlite3
|
||||
|
||||
when (NimMajor, NimMinor) < (1, 4):
|
||||
{.push raises: [Defect].}
|
||||
{.pragma: callback, gcsafe, raises: [Defect].}
|
||||
else:
|
||||
{.push raises: [Defect].}
|
||||
{.pragma: callback, gcsafe, raises: [].}
|
||||
|
||||
import
|
||||
std/[os, options, strformat, typetraits],
|
||||
|
@ -22,7 +27,7 @@ type
|
|||
Sqlite* = ptr sqlite3
|
||||
SqliteStmt*[Params; Result] = distinct RawStmtPtr
|
||||
NoParams* = tuple # this is the empty tuple
|
||||
ResultHandler*[T] = proc(val: T) {.gcsafe, raises: [Defect].}
|
||||
ResultHandler*[T] = proc(val: T) {.callback.}
|
||||
|
||||
SqStoreRef* = ref object
|
||||
# Handle for a single database - from here, keyspaces and statements
|
||||
|
@ -48,7 +53,7 @@ type
|
|||
proc (
|
||||
a: 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) =
|
||||
discard sqlite3_close(db)
|
||||
|
@ -598,7 +603,7 @@ proc hasTable*(db: SqStoreRef, name: string): KvResult[bool] =
|
|||
let
|
||||
sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='" &
|
||||
name & "';"
|
||||
db.exec(sql, (), proc(_: openArray[byte]) = discard)
|
||||
db.exec(sql, (), proc(_: openArray[byte]) {.callback.} = discard)
|
||||
|
||||
proc openKvStore*(
|
||||
db: SqStoreRef, name = "kvstore", withoutRowid = false,
|
||||
|
|
|
@ -249,7 +249,7 @@ procSuite "SqStoreRef":
|
|||
|
||||
proc customSumFun(
|
||||
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 num2 = uint32.fromBytesBE(b)
|
||||
let sum = num1 + num2
|
||||
|
|
Loading…
Reference in New Issue