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
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# 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
|
2024-06-10 12:04:22 +00:00
|
|
|
const initFailed = "RocksDB/init() failed"
|
|
|
|
|
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
|
2024-06-10 12:04:22 +00:00
|
|
|
cfs = @[initColFamilyDescriptor(AdmCF, cfOpts),
|
|
|
|
initColFamilyDescriptor(VtxCF, cfOpts),
|
|
|
|
initColFamilyDescriptor(KeyCF, cfOpts)] &
|
2024-06-05 17:08:29 +02:00
|
|
|
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-06-10 12:04:22 +00:00
|
|
|
raiseAssert initFailed & " cannot create base descriptor: " & error
|
|
|
|
|
|
|
|
# Initialise column handlers (this stores implicitely `baseDb`)
|
|
|
|
rdb.admCol = baseDb.withColFamily(AdmCF).valueOr:
|
|
|
|
raiseAssert initFailed & " cannot initialise AdmCF descriptor: " & error
|
|
|
|
rdb.vtxCol = baseDb.withColFamily(VtxCF).valueOr:
|
|
|
|
raiseAssert initFailed & " cannot initialise VtxCF descriptor: " & error
|
|
|
|
rdb.keyCol = baseDb.withColFamily(KeyCF).valueOr:
|
|
|
|
raiseAssert initFailed & " cannot initialise KeyCF descriptor: " & 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-06-10 12:04:22 +00:00
|
|
|
## Initialise `Guest` family
|
|
|
|
##
|
|
|
|
## Thus was a worth a try, but there are better solutions and this item
|
|
|
|
## will be removed in future.
|
|
|
|
##
|
2024-05-07 20:59:27 +01:00
|
|
|
if high(RdbGuest).ord < instance:
|
|
|
|
return err((RdbGuestInstanceUnsupported,""))
|
|
|
|
let
|
|
|
|
guestSym = $RdbGuest(instance)
|
2024-06-10 12:04:22 +00:00
|
|
|
guestDb = rdb.baseDb.withColFamily(guestSym).valueOr:
|
|
|
|
raiseAssert "RocksDb/initGuestDb() failed: " & 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-06-10 12:04:22 +00:00
|
|
|
rdb.baseDb.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
|
|
|
|
# ------------------------------------------------------------------------------
|