Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db namespaces added to newKvt API #2068

Closed
wants to merge 11 commits into from
11 changes: 5 additions & 6 deletions nimbus/core/clique/snapshot/snapshot_desc.nim
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Copyright (c) 2018-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)
Expand Down Expand Up @@ -147,7 +147,7 @@ proc loadSnapshot*(cfg: CliqueCfg; hash: Hash256):
## Load an existing snapshot from the database.
var s = Snapshot(cfg: cfg)
try:
let rc = s.cfg.db.newKvt(Shared).get(hash.cliqueSnapshotKey.toOpenArray)
let rc = s.cfg.db.newKvt(cliqueSnapshot, Shared).get(hash.data)
if rc.isOk:
s.data = rc.value.decode(SnapshotData)
else:
Expand All @@ -163,12 +163,11 @@ proc storeSnapshot*(cfg: CliqueCfg; s: Snapshot): CliqueOkResult =
## Insert the snapshot into the database.
try:
let
key = s.data.blockHash.cliqueSnapshotKey
val = rlp.encode(s.data)
db = s.cfg.db.newKvt(Companion)
db.put(key.toOpenArray, val).isOkOr:
kvt = s.cfg.db.newKvt(cliqueSnapshot, Companion)
kvt.put(s.data.blockHash.data, val).isOkOr:
error logTxt "put() failed", `error`=($$error)
db.persistent()
kvt.persistent()

cfg.nSnaps.inc
cfg.snapsData += val.len.uint
Expand Down
7 changes: 5 additions & 2 deletions nimbus/db/core_db/backend/aristo_db.nim
Expand Up @@ -16,7 +16,9 @@ import
"../.."/[aristo, aristo/aristo_walk],
"../.."/[kvt, kvt/kvt_init/memory_only, kvt/kvt_walk],
".."/[base, base/base_desc],
./aristo_db/[common_desc, handlers_aristo, handlers_kvt]
./aristo_db/[common_desc, handlers_aristo, handlers_kvt],
../../storage_types


import
../../aristo/aristo_init/memory_only as aristo_memory_only
Expand Down Expand Up @@ -139,7 +141,8 @@ proc baseMethods(
): CoreDbRc[CoreDbTrieRef] =
db.adbBase.getTrie(kind, root, address, "getTrieFn()"),

newKvtFn: proc(saveMode: CoreDbSaveFlags): CoreDbRc[CoreDxKvtRef] =
newKvtFn: proc(namespace: DbNamespace, saveMode: CoreDbSaveFlags): CoreDbRc[CoreDxKvtRef] =
# TODO: use namespace
db.kdbBase.gc()
db.kdbBase.newKvtHandler(saveMode, "newKvtFn()"),

Expand Down
30 changes: 21 additions & 9 deletions nimbus/db/core_db/backend/legacy_db.nim
Expand Up @@ -15,6 +15,7 @@ import
eth/[common, rlp, trie/db, trie/hexary],
stew/byteutils,
results,
../../storage_types,
../../../errors,
".."/[base, base/base_desc]

Expand Down Expand Up @@ -235,29 +236,37 @@ proc newRecorderRef(
# Private database method function tables
# ------------------------------------------------------------------------------

proc kvtMethods(db: LegacyDbRef): CoreDbKvtFns =
template mapKey(k: openArray[byte], namespace: DbNamespace): openArray[byte] =
if namespace == DbNamespace.default:
k
else:
k.toDbKey(namespace).toOpenArray()

proc kvtMethods(db: LegacyDbRef, namespace: DbNamespace): CoreDbKvtFns {.gcsafe.} =
## Key-value database table handlers

let tdb = db.tdb

CoreDbKvtFns(
backendFn: proc(): CoreDbKvtBackendRef =
db.bless(LegacyCoreDbKvtBE(tdb: tdb)),

getFn: proc(k: openArray[byte]): CoreDbRc[Blob] =
let data = tdb.get(k)
let data = tdb.get(k.mapKey(namespace))
if 0 < data.len:
return ok(data)
err(db.bless(KvtNotFound, LegacyCoreDbError(ctx: "getFn()"))),

delFn: proc(k: openArray[byte]): CoreDbRc[void] =
tdb.del(k)
tdb.del(k.mapKey(namespace))
ok(),

putFn: proc(k: openArray[byte]; v: openArray[byte]): CoreDbRc[void] =
tdb.put(k,v)
tdb.put(k.mapKey(namespace), v)
ok(),

hasKeyFn: proc(k: openArray[byte]): CoreDbRc[bool] =
ok(tdb.contains(k)),
ok(tdb.contains(k.mapKey(namespace))),

persistentFn: proc(): CoreDbRc[void] =
# Emulate `Kvt` behaviour
Expand All @@ -267,7 +276,10 @@ proc kvtMethods(db: LegacyDbRef): CoreDbKvtFns =
ok(),

forgetFn: proc(): CoreDbRc[void] =
ok())
ok(),

namespaceFn: proc(namespace: DbNamespace): CoreDxKvtRef {.gcsafe.} =
CoreDxKvtRef(methods: db.kvtMethods(namespace)))

proc mptMethods(mpt: HexaryChildDbRef; db: LegacyDbRef): CoreDbMptFns =
## Hexary trie database handlers
Expand Down Expand Up @@ -484,8 +496,8 @@ proc baseMethods(
trie.accPath = @(address.unsafeGet.keccakHash.data)
ok(db.bless trie),

newKvtFn: proc(saveMode: CoreDbSaveFlags): CoreDbRc[CoreDxKvtRef] =
ok(db.kvt),
newKvtFn: proc(namespace: DbNamespace, saveMode: CoreDbSaveFlags): CoreDbRc[CoreDxKvtRef] =
ok(db.kvt.namespace(namespace)),

newMptFn: proc(
trie: CoreDbTrieRef,
Expand Down Expand Up @@ -546,7 +558,7 @@ proc init*(

# Local extensions
db.tdb = tdb
db.kvt = db.bless CoreDxKvtRef(methods: db.kvtMethods())
db.kvt = db.bless CoreDxKvtRef(methods: db.kvtMethods(DbNamespace.default))

# Base descriptor
db.dbType = dbType
Expand Down
19 changes: 15 additions & 4 deletions nimbus/db/core_db/base.nim
Expand Up @@ -15,6 +15,7 @@ import
chronicles,
eth/common,
results,
../storage_types,
"../.."/[constants, errors],
./base/[api_new_desc, api_tracking, base_desc]

Expand Down Expand Up @@ -437,7 +438,7 @@ proc getTrie*(
# Public key-value table methods
# ------------------------------------------------------------------------------

proc newKvt*(db: CoreDbRef; saveMode = AutoSave): CoreDxKvtRef =
proc newKvt*(db: CoreDbRef; namespace: DbNamespace, saveMode = AutoSave): CoreDxKvtRef =
## Constructor, will defect on failure.
##
## Depending on the argument `saveMode`, the contructed object will have
Expand Down Expand Up @@ -476,10 +477,13 @@ proc newKvt*(db: CoreDbRef; saveMode = AutoSave): CoreDxKvtRef =
## function argument.
##
db.setTrackNewApi BaseNewKvtFn
result = db.methods.newKvtFn(saveMode).valueOr:
result = db.methods.newKvtFn(namespace, saveMode).valueOr:
raiseAssert error.prettyText()
db.ifTrackNewApi: debug newApiTxt, ctx, elapsed, saveMode

proc newDefaultKvt*(db: CoreDbRef; saveMode = AutoSave): CoreDxKvtRef =
newKvt(db, DbNamespace.default, saveMode)

proc get*(kvt: CoreDxKvtRef; key: openArray[byte]): CoreDbRc[Blob] =
## This function always returns a non-empty `Blob` or an error code.
kvt.setTrackNewApi KvtGetFn
Expand Down Expand Up @@ -555,6 +559,10 @@ proc forget*(dsc: CoreDxKvtRef): CoreDbRc[void] {.discardable.} =
result = dsc.methods.forgetFn()
dsc.ifTrackNewApi: debug newApiTxt, ctx, elapsed, result

proc namespace*(dsc: CoreDxKvtRef, namespace: DbNamespace): CoreDxKvtRef =
## TODO:
dsc.methods.namespaceFn(namespace)

# ------------------------------------------------------------------------------
# Public Merkle Patricia Tree, hexary trie constructors
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -1069,7 +1077,10 @@ when ProvideLegacyAPI:
proc kvt*(db: CoreDbRef): CoreDbKvtRef =
## Legacy pseudo constructor, see `toKvt()` for production constructor
db.setTrackLegaApi LegaNewKvtFn
result = db.newKvt().CoreDbKvtRef
db.ifTrackLegaApi: debug legaApiTxt, ctx, elapsed, result

result = db.newKvt(namespace = DbNamespace.default).CoreDbKvtRef

db.ifTrackLegaApi: debug legaApiTxt, ctx, elapsed, result

proc get*(kvt: CoreDbKvtRef; key: openArray[byte]): Blob =
Expand All @@ -1084,7 +1095,7 @@ when ProvideLegacyAPI:

proc put*(kvt: CoreDbKvtRef; key: openArray[byte]; val: openArray[byte]) =
kvt.setTrackLegaApi LegaKvtPutFn
kvt.distinctBase.parent.newKvt().put(key, val).expect $ctx
kvt.distinctBase.put(key, val).expect $ctx
kvt.ifTrackLegaApi:
debug legaApiTxt, ctx, elapsed, key=key.toStr, val=val.toLenStr

Expand Down
7 changes: 5 additions & 2 deletions nimbus/db/core_db/base/base_desc.nim
Expand Up @@ -14,6 +14,7 @@ import
std/tables,
eth/common,
results,
../../storage_types,
../../aristo/aristo_profile

# Annotation helpers
Expand Down Expand Up @@ -103,7 +104,7 @@ type
): CoreDbRc[CoreDbTrieRef] {.noRaise.}
CoreDbBaseLevelFn* = proc(): int {.noRaise.}
CoreDbBaseKvtFn* = proc(
saveMode: CoreDbSaveFlags): CoreDbRc[CoreDxKvtRef] {.noRaise.}
namespace: DbNamespace, saveMode: CoreDbSaveFlags): CoreDbRc[CoreDxKvtRef] {.noRaise.}
CoreDbBaseMptFn* = proc(
root: CoreDbTrieRef; prune: bool; saveMode: CoreDbSaveFlags;
): CoreDbRc[CoreDxMptRef] {.noRaise.}
Expand Down Expand Up @@ -147,13 +148,14 @@ type
# Sub-descriptor: KVT methods
# --------------------------------------------------
CoreDbKvtBackendFn* = proc(): CoreDbKvtBackendRef {.noRaise.}
CoreDbKvtGetFn* = proc(k: openArray[byte]): CoreDbRc[Blob] {.noRaise.}
CoreDbKvtGetFn* = proc(k: openArray[byte]): CoreDbRc[Blob] {.noRaise.}
CoreDbKvtDelFn* = proc(k: openArray[byte]): CoreDbRc[void] {.noRaise.}
CoreDbKvtPutFn* =
proc(k: openArray[byte]; v: openArray[byte]): CoreDbRc[void] {.noRaise.}
CoreDbKvtPersistentFn* = proc(): CoreDbRc[void] {.noRaise.}
CoreDbKvtForgetFn* = proc(): CoreDbRc[void] {.noRaise.}
CoreDbKvtHasKeyFn* = proc(k: openArray[byte]): CoreDbRc[bool] {.noRaise.}
CoreDbKvtNamespaceFn* = proc(ns: DbNamespace): CoreDxKvtRef {.noRaise.}

CoreDbKvtFns* = object
## Methods for key-value table
Expand All @@ -164,6 +166,7 @@ type
hasKeyFn*: CoreDbKvtHasKeyFn
persistentFn*: CoreDbKvtPersistentFn
forgetFn*: CoreDbKvtForgetFn
namespaceFn*: CoreDbKvtNamespaceFn


# --------------------------------------------------
Expand Down