2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-03-05 04:54:42 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-09-12 18:44:45 +00: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 Kvt DB
|
|
|
|
## =========================================
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-13 18:15:11 +00:00
|
|
|
std/[sequtils, os],
|
2023-09-12 18:44:45 +00:00
|
|
|
rocksdb,
|
|
|
|
results,
|
2024-06-13 18:15:11 +00:00
|
|
|
../../../aristo/aristo_init/persistent,
|
|
|
|
../../../opts,
|
2023-09-12 18:44:45 +00:00
|
|
|
../../kvt_desc,
|
2024-06-13 18:15:11 +00:00
|
|
|
../../kvt_desc/desc_error as kdb,
|
2023-09-12 18:44:45 +00:00
|
|
|
./rdb_desc
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getCFInitOptions(opts: DbOptions): ColFamilyOptionsRef =
|
|
|
|
result = defaultColFamilyOptions()
|
|
|
|
if opts.writeBufferSize > 0:
|
|
|
|
result.setWriteBufferSize(opts.writeBufferSize)
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
proc getDbInitOptions(opts: DbOptions): DbOptionsRef =
|
|
|
|
result = defaultDbOptions()
|
|
|
|
result.setMaxOpenFiles(opts.maxOpenFiles)
|
|
|
|
result.setMaxBytesForLevelBase(opts.writeBufferSize)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
if opts.rowCacheSize > 0:
|
|
|
|
result.setRowCache(cacheCreateLRU(opts.rowCacheSize))
|
|
|
|
|
|
|
|
if opts.blockCacheSize > 0:
|
|
|
|
let tableOpts = defaultTableOptions()
|
|
|
|
tableOpts.setBlockCache(cacheCreateLRU(opts.rowCacheSize))
|
|
|
|
result.setBlockBasedTableFactory(tableOpts)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
rdb: var RdbInst;
|
|
|
|
basePath: string;
|
2024-06-13 18:15:11 +00:00
|
|
|
opts: DbOptions;
|
2023-09-12 18:44:45 +00:00
|
|
|
): Result[void,(KvtError,string)] =
|
2024-06-13 18:15:11 +00:00
|
|
|
## Database backend constructor for stand-alone version
|
|
|
|
##
|
|
|
|
const initFailed = "RocksDB/init() failed"
|
|
|
|
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.basePath = basePath
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
let
|
2023-11-20 20:22:27 +00:00
|
|
|
dataDir = rdb.dataDir
|
2023-09-12 18:44:45 +00:00
|
|
|
try:
|
|
|
|
dataDir.createDir
|
|
|
|
except OSError, IOError:
|
2024-06-13 18:15:11 +00:00
|
|
|
return err((kdb.RdbBeCantCreateDataDir, ""))
|
|
|
|
|
|
|
|
# Expand argument `opts` to rocksdb options
|
|
|
|
let (cfOpts, dbOpts) = (opts.getCFInitOptions, opts.getDbInitOptions)
|
|
|
|
|
|
|
|
# Column familiy names to allocate when opening the database.
|
|
|
|
let cfs = KvtCFs.mapIt(($it).initColFamilyDescriptor cfOpts)
|
|
|
|
|
|
|
|
# Open database for the extended family :)
|
|
|
|
let baseDb = openRocksDb(dataDir, dbOpts, columnFamilies=cfs).valueOr:
|
|
|
|
raiseAssert initFailed & " cannot create base descriptor: " & error
|
|
|
|
|
|
|
|
# Initialise column handlers (this stores implicitely `baseDb`)
|
|
|
|
for col in KvtCFs:
|
|
|
|
rdb.store[col] = baseDb.withColFamily($col).valueOr:
|
|
|
|
raiseAssert initFailed & " cannot initialise " &
|
|
|
|
$col & " descriptor: " & error
|
2023-09-12 18:44:45 +00:00
|
|
|
ok()
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc init*(
|
|
|
|
rdb: var RdbInst;
|
2024-06-13 18:15:11 +00:00
|
|
|
adb: AristoDbRef;
|
|
|
|
opts: DbOptions;
|
|
|
|
): Result[void,(KvtError,string)] =
|
|
|
|
## Initalise column handlers piggy-backing on the `Aristo` backend.
|
|
|
|
##
|
|
|
|
let
|
|
|
|
cfOpts = opts.getCFInitOptions()
|
|
|
|
iCfs = KvtCFs.toSeq.mapIt(initColFamilyDescriptor($it, cfOpts))
|
|
|
|
oCfs = adb.reinit(iCfs).valueOr:
|
|
|
|
return err((RdbBeHostError,$error))
|
|
|
|
|
|
|
|
# Collect column family descriptors (this stores implicitely `baseDb`)
|
|
|
|
for n in KvtCFs:
|
|
|
|
assert oCfs[n.ord].name != "" # debugging only
|
|
|
|
rdb.store[n] = oCfs[n.ord]
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
proc destroy*(rdb: var RdbInst; flush: bool) =
|
2024-04-16 20:39:11 +00:00
|
|
|
## Destructor (no need to do anything if piggybacked)
|
|
|
|
if 0 < rdb.basePath.len:
|
2024-06-13 18:15:11 +00:00
|
|
|
rdb.baseDb.close()
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
if flush:
|
2024-04-16 20:39:11 +00:00
|
|
|
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
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|