nimbus-eth1/nimbus/db/select_backend.nim

71 lines
2.3 KiB
Nim
Raw Normal View History

2020-04-24 06:46:55 +00:00
import strutils, eth/db/kvstore
export kvstore
# Database access layer that turns errors in the database into Defects as the
# code that uses it isn't equipped to handle errors of that kind - this should
# be reconsidered when making more changes here.
2018-12-27 02:59:06 +00:00
type DbBackend* = enum
2022-04-20 07:57:50 +00:00
none,
sqlite,
rocksdb,
lmdb
const
nimbus_db_backend* {.strdefine.} = "rocksdb"
dbBackend* = parseEnum[DbBackend](nimbus_db_backend)
when dbBackend == sqlite:
import eth/db/kvstore_sqlite3 as database_backend
elif dbBackend == rocksdb:
import ./kvstore_rocksdb as database_backend
2020-04-24 06:46:55 +00:00
type
ChainDB* = ref object of RootObj
kv*: KvStoreRef
when dbBackend == rocksdb:
rdb*: RocksStoreRef
2020-04-24 06:46:55 +00:00
# TODO KvStore is a virtual interface and TrieDB is a virtual interface - one
# will be enough eventually - unless the TrieDB interface gains operations
# that are not typical to KvStores
proc get*(db: ChainDB, key: openArray[byte]): seq[byte] =
var res: seq[byte]
proc onData(data: openArray[byte]) = res = @data
if db.kv.get(key, onData).expect("working database"):
return res
proc put*(db: ChainDB, key, value: openArray[byte]) =
db.kv.put(key, value).expect("working database")
proc contains*(db: ChainDB, key: openArray[byte]): bool =
db.kv.contains(key).expect("working database")
proc del*(db: ChainDB, key: openArray[byte]): bool =
2020-04-24 06:46:55 +00:00
db.kv.del(key).expect("working database")
when dbBackend == sqlite:
2020-04-24 06:46:55 +00:00
proc newChainDB*(path: string): ChainDB =
let db = SqStoreRef.init(path, "nimbus").expect("working database")
ChainDB(kv: kvStore db.openKvStore().expect("working database"))
elif dbBackend == rocksdb:
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
proc newChainDB*(path: string): ChainDB
{.gcsafe, deprecated: "use newCoreDbRef(LegacyDbPersistent,<path>)".} =
let rdb = RocksStoreRef.init(path, "nimbus").tryGet()
ChainDB(kv: kvStore rdb, rdb: rdb)
elif dbBackend == lmdb:
2020-04-24 06:46:55 +00:00
# TODO This implementation has several issues on restricted platforms, possibly
# due to mmap restrictions - see:
# https://github.com/status-im/nim-beacon-chain/issues/732
# https://github.com/status-im/nim-beacon-chain/issues/688
# It also has other issues, including exception safety:
# https://github.com/status-im/nim-beacon-chain/pull/809
{.error: "lmdb deprecated, needs reimplementing".}
2022-04-20 07:57:50 +00:00
elif dbBackend == none:
discard
2018-12-27 02:59:06 +00:00
2022-04-20 07:57:50 +00:00
when dbBackend != none:
export database_backend