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
|
|
|
|
std/os,
|
|
|
|
rocksdb,
|
2023-09-12 19:45:12 +01:00
|
|
|
results,
|
2023-06-20 14:26:25 +01:00
|
|
|
../../aristo_desc,
|
|
|
|
./rdb_desc
|
|
|
|
|
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;
|
|
|
|
openMax: int;
|
|
|
|
): 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
|
|
|
|
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))
|
2024-03-05 12:54:42 +08:00
|
|
|
|
2023-06-20 14:26:25 +01:00
|
|
|
ok()
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
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)
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|