2023-06-20 14:26:25 +01:00
|
|
|
# nimbus-eth1
|
2024-03-05 12:54:42 +08:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-20 14:26:25 +01:00
|
|
|
# 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
|
2024-05-07 20:59:27 +01:00
|
|
|
std/[sequtils, os],
|
2023-06-20 14:26:25 +01:00
|
|
|
rocksdb,
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2023-06-20 14:26:25 +01:00
|
|
|
../../aristo_desc,
|
2024-06-05 17:08:29 +02:00
|
|
|
./rdb_desc,
|
|
|
|
../../../opts
|
2023-06-20 14:26:25 +01:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
const
|
|
|
|
extraTraceMessages = false
|
|
|
|
## Enable additional logging noise
|
2023-06-20 14:26:25 +01:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
import
|
|
|
|
chronicles
|
2023-06-20 14:26:25 +01:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
logScope:
|
|
|
|
topics = "aristo-rocksdb"
|
2023-06-20 14:26:25 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
rdb: var RdbInst;
|
|
|
|
basePath: string;
|
2024-06-05 17:08:29 +02:00
|
|
|
opts: DbOptions;
|
2023-06-20 14:26:25 +01:00
|
|
|
): Result[void,(AristoError,string)] =
|
|
|
|
## Constructor c ode inspired by `RocksStoreRef.init()` from
|
|
|
|
## kvstore_rocksdb.nim
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.basePath = basePath
|
|
|
|
|
2023-06-20 14:26:25 +01:00
|
|
|
let
|
2023-11-20 20:22:27 +00:00
|
|
|
dataDir = rdb.dataDir
|
2023-06-20 14:26:25 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
dataDir.createDir
|
|
|
|
except OSError, IOError:
|
|
|
|
return err((RdbBeCantCreateDataDir, ""))
|
2024-03-05 12:54:42 +08:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
let
|
2024-06-05 17:08:29 +02:00
|
|
|
cfOpts = defaultColFamilyOptions()
|
|
|
|
|
|
|
|
if opts.writeBufferSize > 0:
|
|
|
|
cfOpts.setWriteBufferSize(opts.writeBufferSize)
|
|
|
|
|
|
|
|
let
|
|
|
|
cfs = @[initColFamilyDescriptor(AristoFamily, cfOpts)] &
|
|
|
|
RdbGuest.mapIt(initColFamilyDescriptor($it, cfOpts))
|
|
|
|
dbOpts = defaultDbOptions()
|
|
|
|
|
|
|
|
dbOpts.setMaxOpenFiles(opts.maxOpenFiles)
|
|
|
|
dbOpts.setMaxBytesForLevelBase(opts.writeBufferSize)
|
|
|
|
|
|
|
|
if opts.rowCacheSize > 0:
|
|
|
|
dbOpts.setRowCache(cacheCreateLRU(opts.rowCacheSize))
|
|
|
|
|
|
|
|
if opts.blockCacheSize > 0:
|
|
|
|
let tableOpts = defaultTableOptions()
|
|
|
|
tableOpts.setBlockCache(cacheCreateLRU(opts.rowCacheSize))
|
|
|
|
dbOpts.setBlockBasedTableFactory(tableOpts)
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Reserve a family corner for `Aristo` on the database
|
2024-06-05 17:08:29 +02:00
|
|
|
let baseDb = openRocksDb(dataDir, dbOpts, columnFamilies=cfs).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
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))
|
2024-03-05 12:54:42 +08:00
|
|
|
|
2023-06-20 14:26:25 +01:00
|
|
|
ok()
|
|
|
|
|
2024-05-07 20:59:27 +01:00
|
|
|
proc initGuestDb*(
|
|
|
|
rdb: RdbInst;
|
|
|
|
instance: int;
|
|
|
|
): Result[RootRef,(AristoError,string)] =
|
2024-04-16 20:39:11 +00:00
|
|
|
# Initialise `Guest` family
|
2024-05-07 20:59:27 +01:00
|
|
|
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))
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
ok RdbGuestDbRef(
|
|
|
|
beKind: BackendRocksDB,
|
|
|
|
guestDb: guestDb)
|
2023-06-20 14:26:25 +01:00
|
|
|
|
2024-05-07 20:59:27 +01:00
|
|
|
|
2023-06-20 14:26:25 +01:00
|
|
|
proc destroy*(rdb: var RdbInst; flush: bool) =
|
|
|
|
## Destructor
|
2024-04-16 20:39:11 +00:00
|
|
|
rdb.store.db.close()
|
2023-08-25 23:53:59 +01:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
if flush:
|
|
|
|
try:
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.dataDir.removeDir
|
2023-06-20 14:26:25 +01:00
|
|
|
|
|
|
|
# Remove the base folder if it is empty
|
|
|
|
block done:
|
2023-11-20 20:22:27 +00:00
|
|
|
for w in rdb.baseDir.walkDirRec:
|
2023-06-20 14:26:25 +01:00
|
|
|
# Ignore backup files
|
|
|
|
if 0 < w.len and w[^1] != '~':
|
|
|
|
break done
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.baseDir.removeDir
|
2023-06-20 14:26:25 +01:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
except CatchableError:
|
|
|
|
discard
|
2023-06-20 14:26:25 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|