nimbus-eth1/nimbus/db/select_backend.nim

70 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:
2020-04-24 06:46:55 +00:00
proc newChainDB*(path: string): ChainDB =
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