2023-06-20 13:26:25 +00:00
|
|
|
# nimbus-eth1
|
2024-03-14 22:17:43 +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 backend for Aristo DB
|
|
|
|
## =============================
|
|
|
|
##
|
|
|
|
## The iterators provided here are currently available only by direct
|
|
|
|
## backend access
|
|
|
|
## ::
|
|
|
|
## import
|
|
|
|
## aristo/aristo_init,
|
|
|
|
## aristo/aristo_init/aristo_rocksdb
|
|
|
|
##
|
|
|
|
## let rc = AristoDb.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-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
eth/common,
|
|
|
|
rocksdb,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-06-22 11:13:24 +00:00
|
|
|
../aristo_constants,
|
|
|
|
../aristo_desc,
|
2023-08-25 22:53:59 +00:00
|
|
|
../aristo_desc/desc_backend,
|
2023-11-08 12:18:32 +00:00
|
|
|
../aristo_blobify,
|
2023-08-25 22:53:59 +00:00
|
|
|
./init_common,
|
|
|
|
./rocks_db/[rdb_desc, rdb_get, rdb_init, rdb_put, rdb_walk]
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
const
|
|
|
|
maxOpenFiles = 512 ## Rocks DB setup, open files limit
|
|
|
|
|
|
|
|
extraTraceMessages = false
|
|
|
|
## Enabled additional logging noise
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
type
|
2023-06-30 22:22:33 +00:00
|
|
|
RdbBackendRef* = ref object of TypedBackendRef
|
2023-06-20 13:26:25 +00:00
|
|
|
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-06-20 13:26:25 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
logScope:
|
|
|
|
topics = "aristo-backend"
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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 getVtxFn(db: RdbBackendRef): GetVtxFn =
|
|
|
|
result =
|
|
|
|
proc(vid: VertexID): Result[VertexRef,AristoError] =
|
|
|
|
|
|
|
|
# Fetch serialised data record
|
2024-04-22 19:02:22 +00:00
|
|
|
let data = db.rdb.getVtx(vid.uint64).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getVtxFn() failed", vid, error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# Decode data record
|
2024-04-16 20:39:11 +00:00
|
|
|
if 0 < data.len:
|
|
|
|
return data.deblobify VertexRef
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
err(GetVtxNotFound)
|
|
|
|
|
|
|
|
proc getKeyFn(db: RdbBackendRef): GetKeyFn =
|
|
|
|
result =
|
|
|
|
proc(vid: VertexID): Result[HashKey,AristoError] =
|
|
|
|
|
|
|
|
# Fetch serialised data record
|
2024-04-22 19:02:22 +00:00
|
|
|
let data = db.rdb.getKey(vid.uint64).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getKeyFn: failed", vid, error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# Decode data record
|
2024-04-16 20:39:11 +00:00
|
|
|
if 0 < data.len:
|
|
|
|
let lid = HashKey.fromBytes(data).valueOr:
|
2023-11-08 12:18:32 +00:00
|
|
|
return err(RdbHashKeyExpected)
|
|
|
|
return ok lid
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
err(GetKeyNotFound)
|
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
proc getFilFn(db: RdbBackendRef): GetFilFn =
|
2024-03-14 22:17:43 +00:00
|
|
|
if db.rdb.noFq:
|
2023-09-05 13:57:20 +00:00
|
|
|
result =
|
|
|
|
proc(qid: QueueID): Result[FilterRef,AristoError] =
|
|
|
|
err(FilQuSchedDisabled)
|
|
|
|
else:
|
|
|
|
result =
|
|
|
|
proc(qid: QueueID): Result[FilterRef,AristoError] =
|
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
# Fetch serialised data record.
|
|
|
|
let data = db.rdb.getByPfx(FilPfx, qid.uint64).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getFilFn: failed", qid, error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
# Decode data record
|
2024-04-16 20:39:11 +00:00
|
|
|
if 0 < data.len:
|
|
|
|
return data.deblobify FilterRef
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
err(GetFilNotFound)
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
proc getIdgFn(db: RdbBackendRef): GetIdgFn =
|
|
|
|
result =
|
|
|
|
proc(): Result[seq[VertexID],AristoError]=
|
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
# Fetch serialised data record.
|
|
|
|
let data = db.rdb.getByPfx(AdmPfx, AdmTabIdIdg.uint64).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getIdgFn: failed", error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
# Decode data record
|
|
|
|
if data.len == 0:
|
|
|
|
let w = EmptyVidSeq # Must be `let`
|
|
|
|
return ok w # Compiler error with `ok(EmptyVidSeq)`
|
2023-06-22 11:13:24 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# Decode data record
|
2024-04-16 20:39:11 +00:00
|
|
|
data.deblobify seq[VertexID]
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
proc getFqsFn(db: RdbBackendRef): GetFqsFn =
|
2024-03-14 22:17:43 +00:00
|
|
|
if db.rdb.noFq:
|
2023-09-05 13:57:20 +00:00
|
|
|
result =
|
|
|
|
proc(): Result[seq[(QueueID,QueueID)],AristoError] =
|
|
|
|
err(FilQuSchedDisabled)
|
|
|
|
else:
|
|
|
|
result =
|
|
|
|
proc(): Result[seq[(QueueID,QueueID)],AristoError]=
|
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
# Fetch serialised data record.
|
|
|
|
let data = db.rdb.getByPfx(AdmPfx, AdmTabIdFqs.uint64).valueOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "getFqsFn: failed", error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
2023-09-05 13:57:20 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
if data.len == 0:
|
|
|
|
let w = EmptyQidPairSeq # Must be `let`
|
|
|
|
return ok w # Compiler error with `ok(EmptyQidPairSeq)`
|
2023-09-05 13:57:20 +00:00
|
|
|
|
|
|
|
# Decode data record
|
2024-04-16 20:39:11 +00:00
|
|
|
data.deblobify seq[(QueueID,QueueID)]
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc putBegFn(db: RdbBackendRef): PutBegFn =
|
|
|
|
result =
|
|
|
|
proc(): PutHdlRef =
|
2024-04-16 20:39:11 +00:00
|
|
|
db.rdb.begin()
|
2023-06-20 13:26:25 +00:00
|
|
|
db.newSession()
|
|
|
|
|
|
|
|
proc putVtxFn(db: RdbBackendRef): PutVtxFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vrps: openArray[(VertexID,VertexRef)]) =
|
|
|
|
let hdl = hdl.getSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if hdl.error.isNil:
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Collect batch session arguments
|
|
|
|
var batch: seq[(uint64,Blob)]
|
2023-06-20 13:26:25 +00:00
|
|
|
for (vid,vtx) in vrps:
|
|
|
|
if vtx.isValid:
|
2023-08-18 19:46:55 +00:00
|
|
|
let rc = vtx.blobify()
|
2023-06-20 13:26:25 +00:00
|
|
|
if rc.isErr:
|
2023-06-30 22:22:33 +00:00
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: VtxPfx,
|
|
|
|
vid: vid,
|
|
|
|
code: rc.error)
|
2023-06-20 13:26:25 +00:00
|
|
|
return
|
2024-04-16 20:39:11 +00:00
|
|
|
batch.add (vid.uint64, rc.value)
|
2023-06-20 13:26:25 +00:00
|
|
|
else:
|
2024-04-16 20:39:11 +00:00
|
|
|
batch.add (vid.uint64, EmptyBlob)
|
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
# Stash batch session data via LRU cache
|
|
|
|
db.rdb.putVtx(batch).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: VtxPfx,
|
|
|
|
vid: VertexID(error[0]),
|
|
|
|
code: error[1],
|
|
|
|
info: error[2])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
proc putKeyFn(db: RdbBackendRef): PutKeyFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vkps: openArray[(VertexID,HashKey)]) =
|
|
|
|
let hdl = hdl.getSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if hdl.error.isNil:
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Collect batch session arguments
|
|
|
|
var batch: seq[(uint64,Blob)]
|
2023-06-20 13:26:25 +00:00
|
|
|
for (vid,key) in vkps:
|
|
|
|
if key.isValid:
|
2024-04-16 20:39:11 +00:00
|
|
|
batch.add (vid.uint64, @key)
|
2023-06-20 13:26:25 +00:00
|
|
|
else:
|
2024-04-16 20:39:11 +00:00
|
|
|
batch.add (vid.uint64, EmptyBlob)
|
|
|
|
|
2024-04-22 19:02:22 +00:00
|
|
|
# Stash batch session data via LRU cache
|
|
|
|
db.rdb.putKey(batch).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: KeyPfx,
|
|
|
|
vid: VertexID(error[0]),
|
|
|
|
code: error[1],
|
|
|
|
info: error[2])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
proc putFilFn(db: RdbBackendRef): PutFilFn =
|
2024-03-14 22:17:43 +00:00
|
|
|
if db.rdb.noFq:
|
2023-09-05 13:57:20 +00:00
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vf: openArray[(QueueID,FilterRef)]) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error.isNil:
|
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: FilPfx,
|
|
|
|
qid: (if 0 < vf.len: vf[0][0] else: QueueID(0)),
|
|
|
|
code: FilQuSchedDisabled)
|
|
|
|
else:
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vrps: openArray[(QueueID,FilterRef)]) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error.isNil:
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Collect batch session arguments
|
|
|
|
var batch: seq[(uint64,Blob)]
|
2023-09-05 13:57:20 +00:00
|
|
|
for (qid,filter) in vrps:
|
|
|
|
if filter.isValid:
|
|
|
|
let rc = filter.blobify()
|
|
|
|
if rc.isErr:
|
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: FilPfx,
|
|
|
|
qid: qid,
|
|
|
|
code: rc.error)
|
|
|
|
return
|
2024-04-16 20:39:11 +00:00
|
|
|
batch.add (qid.uint64, rc.value)
|
2023-09-05 13:57:20 +00:00
|
|
|
else:
|
2024-04-16 20:39:11 +00:00
|
|
|
batch.add (qid.uint64, EmptyBlob)
|
|
|
|
|
|
|
|
# Stash batch session data
|
2024-04-22 19:02:22 +00:00
|
|
|
db.rdb.putByPfx(FilPfx, batch).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: FilPfx,
|
|
|
|
qid: QueueID(error[0]),
|
|
|
|
code: error[1],
|
|
|
|
info: error[2])
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
proc putIdgFn(db: RdbBackendRef): PutIdgFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vs: openArray[VertexID]) =
|
|
|
|
let hdl = hdl.getSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if hdl.error.isNil:
|
2024-04-16 20:39:11 +00:00
|
|
|
let idg = if 0 < vs.len: vs.blobify else: EmptyBlob
|
2024-04-22 19:02:22 +00:00
|
|
|
db.rdb.putByPfx(AdmPfx, @[(AdmTabIdIdg.uint64, idg)]).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: AdmPfx,
|
|
|
|
aid: AdmTabIdIdg,
|
|
|
|
code: error[1],
|
|
|
|
info: error[2])
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
proc putFqsFn(db: RdbBackendRef): PutFqsFn =
|
2024-03-14 22:17:43 +00:00
|
|
|
if db.rdb.noFq:
|
2023-09-05 13:57:20 +00:00
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; fs: openArray[(QueueID,QueueID)]) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error.isNil:
|
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: AdmPfx,
|
|
|
|
code: FilQuSchedDisabled)
|
|
|
|
else:
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vs: openArray[(QueueID,QueueID)]) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error.isNil:
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Stash batch session data
|
|
|
|
let fqs = if 0 < vs.len: vs.blobify else: EmptyBlob
|
2024-04-22 19:02:22 +00:00
|
|
|
db.rdb.putByPfx(AdmPfx, @[(AdmTabIdFqs.uint64, fqs)]).isOkOr:
|
2024-04-16 20:39:11 +00:00
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: AdmPfx,
|
|
|
|
aid: AdmTabIdFqs,
|
|
|
|
code: error[1],
|
|
|
|
info: error[2])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc putEndFn(db: RdbBackendRef): PutEndFn =
|
|
|
|
result =
|
2023-09-12 18:45:12 +00:00
|
|
|
proc(hdl: PutHdlRef): Result[void,AristoError] =
|
2023-06-20 13:26:25 +00:00
|
|
|
let hdl = hdl.endSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if not hdl.error.isNil:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
case hdl.error.pfx:
|
|
|
|
of VtxPfx, KeyPfx: trace logTxt "putEndFn: vtx/key failed",
|
2023-08-18 19:46:55 +00:00
|
|
|
pfx=hdl.error.pfx, vid=hdl.error.vid, error=hdl.error.code
|
2024-04-16 20:39:11 +00:00
|
|
|
of FilPfx: trace logTxt "putEndFn: filter failed",
|
|
|
|
pfx=FilPfx, qid=hdl.error.qid, error=hdl.error.code
|
|
|
|
of AdmPfx: trace logTxt "putEndFn: admin failed",
|
|
|
|
pfx=AdmPfx, aid=hdl.error.aid.uint64, error=hdl.error.code
|
|
|
|
of Oops: trace logTxt "putEndFn: oops",
|
|
|
|
error=hdl.error.code
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(hdl.error.code)
|
2024-04-16 20:39:11 +00:00
|
|
|
|
|
|
|
# Commit session
|
|
|
|
db.rdb.commit().isOkOr:
|
2023-06-20 13:26:25 +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:45:12 +00:00
|
|
|
ok()
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc guestDbFn(db: RdbBackendRef): GuestDbFn =
|
|
|
|
result =
|
|
|
|
proc(): Result[RootRef,AristoError] =
|
|
|
|
let gdb = db.rdb.guestDb().valueOr:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "guestDbFn", error=error[0], info=error[1]
|
|
|
|
return err(error[0])
|
|
|
|
ok gdb
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
proc closeFn(db: RdbBackendRef): CloseFn =
|
|
|
|
result =
|
|
|
|
proc(flush: bool) =
|
|
|
|
db.rdb.destroy(flush)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
proc rocksDbBackend*(
|
|
|
|
path: string;
|
|
|
|
qidLayout: QidLayoutRef;
|
|
|
|
): Result[BackendRef,AristoError] =
|
|
|
|
let db = RdbBackendRef(
|
2024-03-14 22:17:43 +00:00
|
|
|
beKind: BackendRocksDB)
|
2023-09-05 13:57:20 +00:00
|
|
|
|
|
|
|
# Initialise RocksDB
|
|
|
|
block:
|
|
|
|
let rc = db.rdb.init(path, maxOpenFiles)
|
|
|
|
if rc.isErr:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace logTxt "constructor failed",
|
|
|
|
error=rc.error[0], info=rc.error[1]
|
|
|
|
return err(rc.error[0])
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
db.rdb.noFq = qidLayout.isNil
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
db.getVtxFn = getVtxFn db
|
|
|
|
db.getKeyFn = getKeyFn db
|
2023-08-22 18:44:54 +00:00
|
|
|
db.getFilFn = getFilFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
db.getIdgFn = getIdgFn db
|
2023-08-25 22:53:59 +00:00
|
|
|
db.getFqsFn = getFqsFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
db.putBegFn = putBegFn db
|
|
|
|
db.putVtxFn = putVtxFn db
|
|
|
|
db.putKeyFn = putKeyFn db
|
2023-08-22 18:44:54 +00:00
|
|
|
db.putFilFn = putFilFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
db.putIdgFn = putIdgFn db
|
2023-08-25 22:53:59 +00:00
|
|
|
db.putFqsFn = putFqsFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
db.putEndFn = putEndFn db
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
db.guestDbFn = guestDbFn db
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
db.closeFn = closeFn db
|
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
# Set up filter management table
|
2024-03-14 22:17:43 +00:00
|
|
|
if not db.rdb.noFq:
|
2024-04-19 18:37:27 +00:00
|
|
|
db.journal = QidSchedRef(ctx: qidLayout)
|
|
|
|
db.journal.state = block:
|
2023-09-05 13:57:20 +00:00
|
|
|
let rc = db.getFqsFn()
|
|
|
|
if rc.isErr:
|
|
|
|
db.closeFn(flush = false)
|
|
|
|
return err(rc.error)
|
|
|
|
rc.value
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
ok db
|
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
proc dup*(db: RdbBackendRef): RdbBackendRef =
|
|
|
|
## Duplicate descriptor shell as needed for API debugging
|
|
|
|
new result
|
|
|
|
init_common.init(result[], db[])
|
|
|
|
result.rdb = db.rdb
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators (needs direct backend access)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator walk*(
|
|
|
|
be: RdbBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[pfx: StorageType, xid: uint64, data: Blob] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Walk over all key-value pairs of the database.
|
|
|
|
##
|
|
|
|
## Non-decodable entries are stepped over while the counter `n` of the
|
|
|
|
## yield record is still incremented.
|
2024-03-14 22:17:43 +00:00
|
|
|
if be.rdb.noFq:
|
2023-09-05 13:57:20 +00:00
|
|
|
for w in be.rdb.walk:
|
|
|
|
case w.pfx:
|
|
|
|
of AdmPfx:
|
|
|
|
if w.xid == AdmTabIdFqs.uint64:
|
|
|
|
continue
|
|
|
|
of FilPfx:
|
|
|
|
break # last sub-table
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
yield w
|
|
|
|
else:
|
|
|
|
for w in be.rdb.walk:
|
|
|
|
yield w
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
iterator walkVtx*(
|
|
|
|
be: RdbBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[vid: VertexID, vtx: VertexRef] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Variant of `walk()` iteration over the vertex sub-table.
|
2023-12-20 16:19:00 +00:00
|
|
|
for (xid, data) in be.rdb.walk VtxPfx:
|
2023-06-20 13:26:25 +00:00
|
|
|
let rc = data.deblobify VertexRef
|
|
|
|
if rc.isOk:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (VertexID(xid), rc.value)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
iterator walkKey*(
|
2023-06-20 13:26:25 +00:00
|
|
|
be: RdbBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[vid: VertexID, key: HashKey] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Variant of `walk()` iteration over the Markle hash sub-table.
|
2023-12-20 16:19:00 +00:00
|
|
|
for (xid, data) in be.rdb.walk KeyPfx:
|
2023-11-08 12:18:32 +00:00
|
|
|
let lid = HashKey.fromBytes(data).valueOr:
|
|
|
|
continue
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (VertexID(xid), lid)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
iterator walkFil*(
|
|
|
|
be: RdbBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[qid: QueueID, filter: FilterRef] =
|
2023-08-22 18:44:54 +00:00
|
|
|
## Variant of `walk()` iteration over the filter sub-table.
|
2024-03-14 22:17:43 +00:00
|
|
|
if not be.rdb.noFq:
|
2023-12-20 16:19:00 +00:00
|
|
|
for (xid, data) in be.rdb.walk FilPfx:
|
2023-09-05 13:57:20 +00:00
|
|
|
let rc = data.deblobify FilterRef
|
|
|
|
if rc.isOk:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (QueueID(xid), rc.value)
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|