2024-02-29 21:10:24 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2024 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
## Stackable API for `Kvt`
|
|
|
|
## =======================
|
|
|
|
|
|
|
|
import
|
|
|
|
std/times,
|
|
|
|
results,
|
|
|
|
../aristo/aristo_profile,
|
2024-03-14 22:17:43 +00:00
|
|
|
./kvt_desc/desc_backend,
|
|
|
|
./kvt_init/memory_db,
|
|
|
|
"."/[kvt_desc, kvt_init, kvt_tx, kvt_utils]
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-03-07 19:24:05 +00:00
|
|
|
const
|
|
|
|
AutoValidateApiHooks = defined(release).not
|
|
|
|
## No validatinon needed for production suite.
|
|
|
|
|
2024-04-19 18:37:27 +00:00
|
|
|
KvtPersistentBackendOk = AutoValidateApiHooks # and false
|
2024-03-14 22:17:43 +00:00
|
|
|
## Set true for persistent backend profiling (which needs an extra
|
|
|
|
## link library.)
|
|
|
|
|
|
|
|
when KvtPersistentBackendOk:
|
|
|
|
import ./kvt_init/rocks_db
|
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
# Annotation helper(s)
|
|
|
|
{.pragma: noRaise, gcsafe, raises: [].}
|
|
|
|
|
|
|
|
type
|
|
|
|
KvtDbProfListRef* = AristoDbProfListRef
|
|
|
|
## Borrowed from `aristo_profile`
|
|
|
|
|
|
|
|
KvtDbProfData* = AristoDbProfData
|
|
|
|
## Borrowed from `aristo_profile`
|
|
|
|
|
|
|
|
KvtApiCommitFn* = proc(tx: KvtTxRef): Result[void,KvtError] {.noRaise.}
|
|
|
|
KvtApiDelFn* = proc(db: KvtDbRef,
|
|
|
|
key: openArray[byte]): Result[void,KvtError] {.noRaise.}
|
2024-06-14 11:19:48 +00:00
|
|
|
KvtApiFinishFn* = proc(db: KvtDbRef, eradicate = false) {.noRaise.}
|
2024-02-29 21:10:24 +00:00
|
|
|
KvtApiForgetFn* = proc(db: KvtDbRef): Result[void,KvtError] {.noRaise.}
|
|
|
|
KvtApiGetFn* = proc(db: KvtDbRef,
|
2024-10-01 21:03:10 +00:00
|
|
|
key: openArray[byte]): Result[seq[byte],KvtError] {.noRaise.}
|
2024-06-21 07:44:10 +00:00
|
|
|
KvtApiLenFn* = proc(db: KvtDbRef,
|
|
|
|
key: openArray[byte]): Result[int,KvtError] {.noRaise.}
|
2024-08-30 11:18:36 +00:00
|
|
|
KvtApiHasKeyRcFn* = proc(db: KvtDbRef,
|
2024-02-29 21:10:24 +00:00
|
|
|
key: openArray[byte]): Result[bool,KvtError] {.noRaise.}
|
|
|
|
KvtApiIsTopFn* = proc(tx: KvtTxRef): bool {.noRaise.}
|
2024-12-18 16:03:51 +00:00
|
|
|
KvtApiTxFrameLevelFn* = proc(db: KvtDbRef): int {.noRaise.}
|
2024-02-29 21:10:24 +00:00
|
|
|
KvtApiPutFn* = proc(db: KvtDbRef,
|
|
|
|
key, data: openArray[byte]): Result[void,KvtError] {.noRaise.}
|
|
|
|
KvtApiRollbackFn* = proc(tx: KvtTxRef): Result[void,KvtError] {.noRaise.}
|
2024-05-07 19:59:27 +00:00
|
|
|
KvtApiPersistFn* = proc(db: KvtDbRef): Result[void,KvtError] {.noRaise.}
|
2024-04-03 15:48:35 +00:00
|
|
|
KvtApiToKvtDbRefFn* = proc(tx: KvtTxRef): KvtDbRef {.noRaise.}
|
2024-12-18 16:03:51 +00:00
|
|
|
KvtApiTxFrameBeginFn* = proc(db: KvtDbRef): Result[KvtTxRef,KvtError] {.noRaise.}
|
|
|
|
KvtApiTxFrameTopFn* =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(db: KvtDbRef): Result[KvtTxRef,KvtError] {.noRaise.}
|
|
|
|
|
|
|
|
KvtApiRef* = ref KvtApiObj
|
|
|
|
KvtApiObj* = object of RootObj
|
|
|
|
## Useful set of `Kvt` fuctions that can be filtered, stacked etc. Note
|
|
|
|
## that this API is modelled after a subset of the `Aristo` API.
|
|
|
|
commit*: KvtApiCommitFn
|
|
|
|
del*: KvtApiDelFn
|
|
|
|
finish*: KvtApiFinishFn
|
|
|
|
get*: KvtApiGetFn
|
2024-06-21 07:44:10 +00:00
|
|
|
len*: KvtApiLenFn
|
2024-08-30 11:18:36 +00:00
|
|
|
hasKeyRc*: KvtApiHasKeyRcFn
|
2024-02-29 21:10:24 +00:00
|
|
|
isTop*: KvtApiIsTopFn
|
2024-12-18 16:03:51 +00:00
|
|
|
txFrameLevel*: KvtApiTxFrameLevelFn
|
2024-02-29 21:10:24 +00:00
|
|
|
put*: KvtApiPutFn
|
|
|
|
rollback*: KvtApiRollbackFn
|
2024-05-07 19:59:27 +00:00
|
|
|
persist*: KvtApiPersistFn
|
2024-04-03 15:48:35 +00:00
|
|
|
toKvtDbRef*: KvtApiToKvtDbRefFn
|
2024-12-18 16:03:51 +00:00
|
|
|
txFrameBegin*: KvtApiTxFrameBeginFn
|
|
|
|
txFrameTop*: KvtApiTxFrameTopFn
|
2024-02-29 21:10:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
KvtApiProfNames* = enum
|
|
|
|
## index/name mapping for profile slots
|
|
|
|
KvtApiProfTotal = "total"
|
|
|
|
|
|
|
|
KvtApiProfCommitFn = "commit"
|
|
|
|
KvtApiProfDelFn = "del"
|
|
|
|
KvtApiProfFinishFn = "finish"
|
|
|
|
KvtApiProfGetFn = "get"
|
2024-06-21 07:44:10 +00:00
|
|
|
KvtApiProfLenFn = "len"
|
2024-08-30 11:18:36 +00:00
|
|
|
KvtApiProfHasKeyRcFn = "hasKeyRc"
|
2024-02-29 21:10:24 +00:00
|
|
|
KvtApiProfIsTopFn = "isTop"
|
|
|
|
KvtApiProfLevelFn = "level"
|
|
|
|
KvtApiProfPutFn = "put"
|
|
|
|
KvtApiProfRollbackFn = "rollback"
|
2024-05-07 19:59:27 +00:00
|
|
|
KvtApiProfPersistFn = "persist"
|
2024-04-03 15:48:35 +00:00
|
|
|
KvtApiProfToKvtDbRefFn = "toKvtDbRef"
|
2024-12-18 16:03:51 +00:00
|
|
|
KvtApiProfTxFrameBeginFn = "txFrameBegin"
|
|
|
|
KvtApiProfTxFrameTopFn = "txFrameTop"
|
2024-02-29 21:10:24 +00:00
|
|
|
|
|
|
|
KvtApiProfBeGetKvpFn = "be/getKvp"
|
2024-06-21 07:44:10 +00:00
|
|
|
KvtApiProfBeLenKvpFn = "be/lenKvp"
|
2024-04-22 19:02:22 +00:00
|
|
|
KvtApiProfBePutKvpFn = "be/putKvp"
|
2024-02-29 21:10:24 +00:00
|
|
|
KvtApiProfBePutEndFn = "be/putEnd"
|
|
|
|
|
|
|
|
KvtApiProfRef* = ref object of KvtApiRef
|
|
|
|
## Profiling API extension of `KvtApiObj`
|
|
|
|
data*: KvtDbProfListRef
|
|
|
|
be*: BackendRef
|
|
|
|
|
2024-03-07 19:24:05 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
when AutoValidateApiHooks:
|
|
|
|
proc validate(api: KvtApiObj|KvtApiRef) =
|
|
|
|
doAssert not api.commit.isNil
|
|
|
|
doAssert not api.del.isNil
|
|
|
|
doAssert not api.finish.isNil
|
|
|
|
doAssert not api.get.isNil
|
2024-08-30 11:18:36 +00:00
|
|
|
doAssert not api.hasKeyRc.isNil
|
2024-03-07 19:24:05 +00:00
|
|
|
doAssert not api.isTop.isNil
|
2024-12-18 16:03:51 +00:00
|
|
|
doAssert not api.txFrameLevel.isNil
|
2024-03-07 19:24:05 +00:00
|
|
|
doAssert not api.put.isNil
|
|
|
|
doAssert not api.rollback.isNil
|
2024-05-07 19:59:27 +00:00
|
|
|
doAssert not api.persist.isNil
|
2024-04-03 15:48:35 +00:00
|
|
|
doAssert not api.toKvtDbRef.isNil
|
2024-12-18 16:03:51 +00:00
|
|
|
doAssert not api.txFrameBegin.isNil
|
|
|
|
doAssert not api.txFrameTop.isNil
|
2024-03-07 19:24:05 +00:00
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
proc validate(prf: KvtApiProfRef) =
|
2024-03-07 19:24:05 +00:00
|
|
|
prf.KvtApiRef.validate
|
|
|
|
doAssert not prf.data.isNil
|
2024-03-14 22:17:43 +00:00
|
|
|
|
|
|
|
proc dup(be: BackendRef): BackendRef =
|
|
|
|
case be.kind:
|
|
|
|
of BackendMemory:
|
|
|
|
return MemBackendRef(be).dup
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
of BackendRocksDB, BackendRdbTriggered:
|
2024-03-14 22:17:43 +00:00
|
|
|
when KvtPersistentBackendOk:
|
|
|
|
return RdbBackendRef(be).dup
|
|
|
|
|
|
|
|
of BackendVoid:
|
|
|
|
discard
|
2024-03-07 19:24:05 +00:00
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public API constuctors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func init*(api: var KvtApiObj) =
|
2024-03-07 19:24:05 +00:00
|
|
|
when AutoValidateApiHooks:
|
|
|
|
api.reset
|
|
|
|
api.commit = commit
|
|
|
|
api.del = del
|
|
|
|
api.finish = finish
|
|
|
|
api.get = get
|
2024-06-21 07:44:10 +00:00
|
|
|
api.len = len
|
2024-08-30 11:18:36 +00:00
|
|
|
api.hasKeyRc = hasKeyRc
|
2024-03-07 19:24:05 +00:00
|
|
|
api.isTop = isTop
|
2024-12-18 16:03:51 +00:00
|
|
|
api.txFrameLevel = txFrameLevel
|
2024-03-07 19:24:05 +00:00
|
|
|
api.put = put
|
|
|
|
api.rollback = rollback
|
2024-05-07 19:59:27 +00:00
|
|
|
api.persist = persist
|
2024-04-03 15:48:35 +00:00
|
|
|
api.toKvtDbRef = toKvtDbRef
|
2024-12-18 16:03:51 +00:00
|
|
|
api.txFrameBegin = txFrameBegin
|
|
|
|
api.txFrameTop = txFrameTop
|
2024-03-07 19:24:05 +00:00
|
|
|
when AutoValidateApiHooks:
|
|
|
|
api.validate
|
2024-02-29 21:10:24 +00:00
|
|
|
|
|
|
|
func init*(T: type KvtApiRef): T =
|
|
|
|
result = new T
|
|
|
|
result[].init()
|
|
|
|
|
|
|
|
func dup*(api: KvtApiRef): KvtApiRef =
|
2024-03-07 19:24:05 +00:00
|
|
|
result = KvtApiRef(
|
2024-04-03 15:48:35 +00:00
|
|
|
commit: api.commit,
|
|
|
|
del: api.del,
|
|
|
|
finish: api.finish,
|
|
|
|
get: api.get,
|
2024-06-21 07:44:10 +00:00
|
|
|
len: api.len,
|
2024-08-30 11:18:36 +00:00
|
|
|
hasKeyRc: api.hasKeyRc,
|
2024-04-03 15:48:35 +00:00
|
|
|
isTop: api.isTop,
|
2024-12-18 16:03:51 +00:00
|
|
|
txFrameLevel: api.txFrameLevel,
|
2024-04-03 15:48:35 +00:00
|
|
|
put: api.put,
|
|
|
|
rollback: api.rollback,
|
2024-05-07 19:59:27 +00:00
|
|
|
persist: api.persist,
|
2024-04-03 15:48:35 +00:00
|
|
|
toKvtDbRef: api.toKvtDbRef,
|
2024-12-18 16:03:51 +00:00
|
|
|
txFrameBegin: api.txFrameBegin,
|
|
|
|
txFrameTop: api.txFrameTop)
|
2024-03-07 19:24:05 +00:00
|
|
|
when AutoValidateApiHooks:
|
2024-07-12 13:12:25 +00:00
|
|
|
result.validate
|
2024-02-29 21:10:24 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public profile API constuctor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
T: type KvtApiProfRef;
|
|
|
|
api: KvtApiRef;
|
|
|
|
be = BackendRef(nil);
|
|
|
|
): T =
|
|
|
|
## This constructor creates a profiling API descriptor to be derived from
|
|
|
|
## an initialised `api` argument descriptor. For profiling the DB backend,
|
|
|
|
## the field `.be` of the result descriptor must be assigned to the
|
|
|
|
## `.backend` field of the `KvtDbRef` descriptor.
|
|
|
|
##
|
|
|
|
## The argument desctiptors `api` and `be` will not be modified and can be
|
|
|
|
## used to restore the previous set up.
|
|
|
|
##
|
|
|
|
let
|
|
|
|
data = KvtDbProfListRef(
|
|
|
|
list: newSeq[KvtDbProfData](1 + high(KvtApiProfNames).ord))
|
|
|
|
profApi = T(data: data)
|
|
|
|
|
|
|
|
template profileRunner(n: KvtApiProfNames, code: untyped): untyped =
|
|
|
|
let start = getTime()
|
|
|
|
code
|
|
|
|
data.update(n.ord, getTime() - start)
|
|
|
|
|
|
|
|
profApi.commit =
|
|
|
|
proc(a: KvtTxRef): auto =
|
|
|
|
KvtApiProfCommitFn.profileRunner:
|
|
|
|
result = api.commit(a)
|
|
|
|
|
|
|
|
profApi.del =
|
|
|
|
proc(a: KvtDbRef; b: openArray[byte]): auto =
|
|
|
|
KvtApiProfDelFn.profileRunner:
|
|
|
|
result = api.del(a, b)
|
|
|
|
|
|
|
|
profApi.finish =
|
|
|
|
proc(a: KvtDbRef; b = false) =
|
|
|
|
KvtApiProfFinishFn.profileRunner:
|
|
|
|
api.finish(a, b)
|
|
|
|
|
|
|
|
profApi.get =
|
|
|
|
proc(a: KvtDbRef, b: openArray[byte]): auto =
|
|
|
|
KvtApiProfGetFn.profileRunner:
|
|
|
|
result = api.get(a, b)
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
profApi.len =
|
|
|
|
proc(a: KvtDbRef, b: openArray[byte]): auto =
|
|
|
|
KvtApiProfLenFn.profileRunner:
|
|
|
|
result = api.len(a, b)
|
|
|
|
|
2024-08-30 11:18:36 +00:00
|
|
|
profApi.hasKeyRc =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(a: KvtDbRef, b: openArray[byte]): auto =
|
2024-08-30 11:18:36 +00:00
|
|
|
KvtApiProfHasKeyRcFn.profileRunner:
|
|
|
|
result = api.hasKeyRc(a, b)
|
2024-02-29 21:10:24 +00:00
|
|
|
|
|
|
|
profApi.isTop =
|
|
|
|
proc(a: KvtTxRef): auto =
|
|
|
|
KvtApiProfIsTopFn.profileRunner:
|
|
|
|
result = api.isTop(a)
|
|
|
|
|
|
|
|
profApi.level =
|
|
|
|
proc(a: KvtDbRef): auto =
|
|
|
|
KvtApiProfLevelFn.profileRunner:
|
|
|
|
result = api.level(a)
|
|
|
|
|
|
|
|
profApi.put =
|
|
|
|
proc(a: KvtDbRef; b, c: openArray[byte]): auto =
|
|
|
|
KvtApiProfPutFn.profileRunner:
|
|
|
|
result = api.put(a, b, c)
|
|
|
|
|
|
|
|
profApi.rollback =
|
|
|
|
proc(a: KvtTxRef): auto =
|
|
|
|
KvtApiProfRollbackFn.profileRunner:
|
|
|
|
result = api.rollback(a)
|
|
|
|
|
2024-05-07 19:59:27 +00:00
|
|
|
profApi.persist =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(a: KvtDbRef): auto =
|
2024-05-07 19:59:27 +00:00
|
|
|
KvtApiProfPersistFn.profileRunner:
|
|
|
|
result = api.persist(a)
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-04-03 15:48:35 +00:00
|
|
|
profApi.toKvtDbRef =
|
|
|
|
proc(a: KvtTxRef): auto =
|
|
|
|
KvtApiProfToKvtDbRefFn.profileRunner:
|
|
|
|
result = api.toKvtDbRef(a)
|
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
profApi.txFrameBegin =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(a: KvtDbRef): auto =
|
2024-12-18 16:03:51 +00:00
|
|
|
KvtApiProfTxFrameBeginFn.profileRunner:
|
|
|
|
result = api.txFrameBegin(a)
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-12-18 16:03:51 +00:00
|
|
|
profApi.txFrameTop =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(a: KvtDbRef): auto =
|
2024-12-18 16:03:51 +00:00
|
|
|
KvtApiProfTxFrameTopFn.profileRunner:
|
|
|
|
result = api.txFrameTop(a)
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-04-19 18:37:27 +00:00
|
|
|
let beDup = be.dup()
|
|
|
|
if beDup.isNil:
|
|
|
|
profApi.be = be
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-04-19 18:37:27 +00:00
|
|
|
else:
|
|
|
|
beDup.getKvpFn =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(a: openArray[byte]): auto =
|
|
|
|
KvtApiProfBeGetKvpFn.profileRunner:
|
|
|
|
result = be.getKvpFn(a)
|
2024-04-19 18:37:27 +00:00
|
|
|
data.list[KvtApiProfBeGetKvpFn.ord].masked = true
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
beDup.lenKvpFn =
|
|
|
|
proc(a: openArray[byte]): auto =
|
|
|
|
KvtApiProfBeLenKvpFn.profileRunner:
|
|
|
|
result = be.lenKvpFn(a)
|
|
|
|
data.list[KvtApiProfBeLenKvpFn.ord].masked = true
|
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
beDup.putKvpFn =
|
2024-08-07 11:30:55 +00:00
|
|
|
proc(a: PutHdlRef; b, c: openArray[byte]) =
|
|
|
|
KvtApiProfBePutKvpFn.profileRunner:
|
|
|
|
be.putKvpFn(a, b, c)
|
2024-04-22 19:02:22 +00:00
|
|
|
data.list[KvtApiProfBePutKvpFn.ord].masked = true
|
|
|
|
|
2024-04-19 18:37:27 +00:00
|
|
|
beDup.putEndFn =
|
2024-02-29 21:10:24 +00:00
|
|
|
proc(a: PutHdlRef): auto =
|
|
|
|
KvtApiProfBePutEndFn.profileRunner:
|
|
|
|
result = be.putEndFn(a)
|
2024-04-19 18:37:27 +00:00
|
|
|
data.list[KvtApiProfBePutEndFn.ord].masked = true
|
|
|
|
|
|
|
|
profApi.be = beDup
|
2024-02-29 21:10:24 +00:00
|
|
|
|
2024-03-07 19:24:05 +00:00
|
|
|
when AutoValidateApiHooks:
|
2024-03-14 22:17:43 +00:00
|
|
|
profApi.validate
|
2024-03-07 19:24:05 +00:00
|
|
|
|
2024-02-29 21:10:24 +00:00
|
|
|
profApi
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|