2023-06-20 13:26:25 +00:00
|
|
|
# nimbus-eth1
|
2024-03-05 04:54:42 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-06-20 13:26:25 +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 Aristo DB
|
|
|
|
## ============================================
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/os,
|
|
|
|
chronicles,
|
2024-03-05 04:54:42 +00:00
|
|
|
rocksdb/lib/librocksdb,
|
2023-06-20 13:26:25 +00:00
|
|
|
rocksdb,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-06-20 13:26:25 +00:00
|
|
|
../../aristo_desc,
|
|
|
|
./rdb_desc
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "aristo-backend"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template logTxt(info: static[string]): static[string] =
|
|
|
|
"RocksDB/init " & info
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# 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 13:26:25 +00:00
|
|
|
let
|
2023-11-20 20:22:27 +00:00
|
|
|
dataDir = rdb.dataDir
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
dataDir.createDir
|
|
|
|
except OSError, IOError:
|
|
|
|
return err((RdbBeCantCreateDataDir, ""))
|
|
|
|
try:
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.cacheDir.createDir
|
2023-06-20 13:26:25 +00:00
|
|
|
except OSError, IOError:
|
|
|
|
return err((RdbBeCantCreateTmpDir, ""))
|
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
let dbOpts = defaultDbOptions()
|
|
|
|
dbOpts.setMaxOpenFiles(openMax)
|
|
|
|
|
|
|
|
let rc = openRocksDb(dataDir, dbOpts)
|
2023-06-20 13:26:25 +00:00
|
|
|
if rc.isErr:
|
|
|
|
let error = RdbBeDriverInitError
|
2024-03-12 03:04:46 +00:00
|
|
|
debug logTxt "driver failed", dataDir, openMax,
|
2023-06-20 13:26:25 +00:00
|
|
|
error, info=rc.error
|
|
|
|
return err((RdbBeDriverInitError, rc.error))
|
|
|
|
|
2024-03-05 04:54:42 +00:00
|
|
|
rdb.dbOpts = dbOpts
|
|
|
|
rdb.store = rc.get()
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# The following is a default setup (subject to change)
|
|
|
|
rdb.impOpt = rocksdb_ingestexternalfileoptions_create()
|
|
|
|
rdb.envOpt = rocksdb_envoptions_create()
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc destroy*(rdb: var RdbInst; flush: bool) =
|
|
|
|
## Destructor
|
|
|
|
rdb.envOpt.rocksdb_envoptions_destroy()
|
|
|
|
rdb.impOpt.rocksdb_ingestexternalfileoptions_destroy()
|
|
|
|
rdb.store.close()
|
|
|
|
|
|
|
|
try:
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.cacheDir.removeDir
|
2023-08-25 22:53:59 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
if flush:
|
2023-11-20 20:22:27 +00:00
|
|
|
rdb.dataDir.removeDir
|
2023-06-20 13:26:25 +00: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 13:26:25 +00: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 13:26:25 +00:00
|
|
|
|
|
|
|
except CatchableError:
|
|
|
|
discard
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|