Jordan Hrycaj 54f784bef1
Kvt remodel tx and forked descriptors (#2168)
* Aristo: Generalise alien/guest interface for piggiback on database

* Aristo: Code cosmetics

* CoreDb+Kvt: Update transaction API

why:
  Use single addressable function `forkTx(backLevel: int)` as used
  in `Aristo`. So `Kvt` can be synced simultaneously to `Aristo`.

also:
  Refactored `kvt_tx.nim` in a similar fashion to `Aristo`.

* Kvt: Replace `LayerDelta` object by reference

why:
  Will be needed when introducing filters

* Kvt: Remodel backend filter facility similar to `Aristo`

why:
  This allows to operate on several KVT instances simultaneously.

* CoreDb+Kvt: Fix on-disk storage

why:
  Overlooked name change: `stow()` => `persist()` for permanent storage

* Fix copyright headers
2024-05-07 19:59:27 +00:00

119 lines
3.2 KiB
Nim

# nimbus-eth1
# Copyright (c) 2023-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.
## Rocksdb constructor/destructor for Aristo DB
## ============================================
{.push raises: [].}
import
std/[sequtils, os],
rocksdb,
results,
../../aristo_desc,
./rdb_desc
const
extraTraceMessages = false
## Enable additional logging noise
when extraTraceMessages:
import
chronicles
logScope:
topics = "aristo-rocksdb"
# ------------------------------------------------------------------------------
# Public constructor
# ------------------------------------------------------------------------------
proc init*(
rdb: var RdbInst;
basePath: string;
openMax: int;
): Result[void,(AristoError,string)] =
## Constructor c ode inspired by `RocksStoreRef.init()` from
## kvstore_rocksdb.nim
rdb.basePath = basePath
let
dataDir = rdb.dataDir
try:
dataDir.createDir
except OSError, IOError:
return err((RdbBeCantCreateDataDir, ""))
let
cfs = @[initColFamilyDescriptor AristoFamily] &
RdbGuest.mapIt(initColFamilyDescriptor $it)
opts = defaultDbOptions()
opts.setMaxOpenFiles(openMax)
# Reserve a family corner for `Aristo` on the database
let baseDb = openRocksDb(dataDir, opts, columnFamilies=cfs).valueOr:
let errSym = RdbBeDriverInitError
when extraTraceMessages:
trace logTxt "init failed", dataDir, openMax, error=errSym, info=error
return err((errSym, error))
# Initialise `Aristo` family
rdb.store = baseDb.withColFamily(AristoFamily).valueOr:
let errSym = RdbBeDriverInitError
when extraTraceMessages:
trace logTxt "init failed", dataDir, openMax, error=errSym, info=error
return err((errSym, error))
ok()
proc initGuestDb*(
rdb: RdbInst;
instance: int;
): Result[RootRef,(AristoError,string)] =
# Initialise `Guest` family
if high(RdbGuest).ord < instance:
return err((RdbGuestInstanceUnsupported,""))
let
guestSym = $RdbGuest(instance)
guestDb = rdb.store.db.withColFamily(guestSym).valueOr:
let errSym = RdbBeDriverGuestError
when extraTraceMessages:
trace logTxt "guestDb failed", error=errSym, info=error
return err((errSym, error))
ok RdbGuestDbRef(
beKind: BackendRocksDB,
guestDb: guestDb)
proc destroy*(rdb: var RdbInst; flush: bool) =
## Destructor
rdb.store.db.close()
if flush:
try:
rdb.dataDir.removeDir
# Remove the base folder if it is empty
block done:
for w in rdb.baseDir.walkDirRec:
# Ignore backup files
if 0 < w.len and w[^1] != '~':
break done
rdb.baseDir.removeDir
except CatchableError:
discard
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------