mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-30 22:16:25 +00:00
d6a4205324
* Aristo+RocksDB: Update backend drivers why: RocksDB update allows use some of the newly provided methods which were previously implemented by using the very C backend (for the lack of NIM methods.) * Aristo+RocksDB: Simplify drivers wrapper * Kvt: Update backend drivers and wrappers similar to `Aristo` * Aristo+Kvm: Use column families for RocksDB * Aristo+MemoryDB: Code cosmetics * Aristo: Provide guest column family for export why: So `Kvt` can piggyback on `Aristo` so there avoiding to run a second DBMS system in parallel. * Kvt: Provide import mechanism for RoksDB guest column family why: So `Kvt` can piggyback on `Aristo` so there avoiding to run a second DBMS system in parallel. * CoreDb+Aristo: Run persistent `Kvt` DB piggybacked on `Aristo` why: Avoiding to run two DBMS systems in parallel. * Fix copyright year * Ditto
111 lines
3.0 KiB
Nim
111 lines
3.0 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/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,
|
|
initColFamilyDescriptor GuestFamily]
|
|
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 guestDb*(rdb: RdbInst): Result[RootRef,(AristoError,string)] =
|
|
# Initialise `Guest` family
|
|
let guestDb = rdb.store.db.withColFamily(GuestFamily).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
|
|
# ------------------------------------------------------------------------------
|