2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-03-14 22:17:43 +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 backend for Kvt DB
|
|
|
|
## ==========================
|
|
|
|
##
|
|
|
|
## The iterators provided here are currently available only by direct
|
|
|
|
## backend access
|
|
|
|
## ::
|
|
|
|
## import
|
|
|
|
## kvt/kvt_init,
|
|
|
|
## kvt/kvt_init/kvt_rocksdb
|
|
|
|
##
|
|
|
|
## let rc = KvtDb.init(BackendRocksDB, "/var/tmp")
|
|
|
|
## if rc.isOk:
|
|
|
|
## let be = rc.value.to(RdbBackendRef)
|
|
|
|
## for (n, key, vtx) in be.walkVtx:
|
|
|
|
## ...
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
2024-03-14 22:17:43 +00:00
|
|
|
{.warning: "*** importing rocks DB which needs a linker library".}
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
eth/common,
|
|
|
|
rocksdb,
|
|
|
|
results,
|
|
|
|
../kvt_desc,
|
|
|
|
../kvt_desc/desc_backend,
|
|
|
|
./init_common,
|
|
|
|
./rocks_db/[rdb_desc, rdb_get, rdb_init, rdb_put, rdb_walk]
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
maxOpenFiles = 512 ## Rocks DB setup, open files limit
|
|
|
|
|
|
|
|
extraTraceMessages = false or true
|
|
|
|
## Enabled additional logging noise
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RdbBackendRef* = ref object of TypedBackendRef
|
|
|
|
rdb: RdbInst ## Allows low level access to database
|
|
|
|
|
|
|
|
RdbPutHdlRef = ref object of TypedPutHdlRef
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
import chronicles
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
logScope:
|
|
|
|
topics = "aristo-backend"
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template logTxt(info: static[string]): static[string] =
|
|
|
|
"RocksDB " & info
|
|
|
|
|
|
|
|
|
|
|
|
proc newSession(db: RdbBackendRef): RdbPutHdlRef =
|
|
|
|
new result
|
|
|
|
result.TypedPutHdlRef.beginSession db
|
|
|
|
|
|
|
|
proc getSession(hdl: PutHdlRef; db: RdbBackendRef): RdbPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.verifySession db
|
|
|
|
hdl.RdbPutHdlRef
|
|
|
|
|
|
|
|
proc endSession(hdl: PutHdlRef; db: RdbBackendRef): RdbPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.finishSession db
|
|
|
|
hdl.RdbPutHdlRef
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: interface
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getKvpFn(db: RdbBackendRef): GetKvpFn =
|
|
|
|
result =
|
2023-09-15 15:23:53 +00:00
|
|
|
proc(key: openArray[byte]): Result[Blob,KvtError] =
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Get data record
|
2024-05-24 09:27:17 +00:00
|
|
|
var data = db.rdb.get(key).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "getKvpFn() failed", key, error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
|
|
|
|
|
|
|
# Return if non-empty
|
|
|
|
if 0 < data.len:
|
2024-05-24 09:27:17 +00:00
|
|
|
return ok(move(data))
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
err(GetNotFound)
|
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc putBegFn(db: RdbBackendRef): PutBegFn =
|
|
|
|
result =
|
|
|
|
proc(): PutHdlRef =
|
2024-04-16 20:39:11 +00:00
|
|
|
db.rdb.begin()
|
2023-09-12 18:44:45 +00:00
|
|
|
db.newSession()
|
|
|
|
|
|
|
|
proc putKvpFn(db: RdbBackendRef): PutKvpFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; kvps: openArray[(Blob,Blob)]) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error == KvtError(0):
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Collect batch session arguments
|
|
|
|
db.rdb.put(kvps).isOkOr:
|
|
|
|
hdl.error = error[1]
|
|
|
|
hdl.info = error[2]
|
|
|
|
return
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
proc putEndFn(db: RdbBackendRef): PutEndFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef): Result[void,KvtError] =
|
|
|
|
let hdl = hdl.endSession db
|
|
|
|
if hdl.error != KvtError(0):
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "putEndFn: failed", error=hdl.error, info=hdl.info
|
2023-09-12 18:44:45 +00:00
|
|
|
return err(hdl.error)
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Commit session
|
|
|
|
db.rdb.commit().isOkOr:
|
2023-09-12 18:44:45 +00:00
|
|
|
when extraTraceMessages:
|
2024-04-16 20:39:11 +00:00
|
|
|
trace logTxt "putEndFn: failed", error=($error[0]), info=error[1]
|
|
|
|
return err(error[0])
|
2023-09-12 18:44:45 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
proc closeFn(db: RdbBackendRef): CloseFn =
|
|
|
|
result =
|
|
|
|
proc(flush: bool) =
|
|
|
|
db.rdb.destroy(flush)
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
# --------------
|
|
|
|
|
|
|
|
proc setup(db: RdbBackendRef) =
|
|
|
|
db.getKvpFn = getKvpFn db
|
|
|
|
|
|
|
|
db.putBegFn = putBegFn db
|
|
|
|
db.putKvpFn = putKvpFn db
|
|
|
|
db.putEndFn = putEndFn db
|
|
|
|
|
|
|
|
db.closeFn = closeFn db
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc rocksDbBackend*(path: string): Result[BackendRef,KvtError] =
|
2023-09-12 18:44:45 +00:00
|
|
|
let db = RdbBackendRef(
|
|
|
|
beKind: BackendRocksDB)
|
|
|
|
|
|
|
|
# Initialise RocksDB
|
2024-04-16 20:39:11 +00:00
|
|
|
db.rdb.init(path, maxOpenFiles).isOkOr:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "constructor failed", error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
db.setup()
|
|
|
|
ok db
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc rocksDbBackend*(store: ColFamilyReadWrite): Result[BackendRef,KvtError] =
|
|
|
|
let db = RdbBackendRef(
|
|
|
|
beKind: BackendRocksDB)
|
|
|
|
db.rdb.init(store)
|
|
|
|
db.setup()
|
2023-09-12 18:44:45 +00:00
|
|
|
ok db
|
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
proc dup*(db: RdbBackendRef): RdbBackendRef =
|
|
|
|
new result
|
|
|
|
init_common.init(result[], db[])
|
|
|
|
result.rdb = db.rdb
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators (needs direct backend access)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator walk*(
|
|
|
|
be: RdbBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[key: Blob, data: Blob] =
|
2023-09-12 18:44:45 +00:00
|
|
|
## Walk over all key-value pairs of the database.
|
|
|
|
##
|
|
|
|
for (k,v) in be.rdb.walk:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (k,v)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|