2023-05-14 17:43:01 +00:00
|
|
|
# nimbus-eth1
|
2024-06-07 21:39:58 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-14 17:43:01 +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.
|
|
|
|
|
|
|
|
## In-memory backend for Aristo DB
|
|
|
|
## ===============================
|
2023-06-20 13:26:25 +00:00
|
|
|
##
|
|
|
|
## The iterators provided here are currently available only by direct
|
|
|
|
## backend access
|
|
|
|
## ::
|
|
|
|
## import
|
|
|
|
## aristo/aristo_init,
|
|
|
|
## aristo/aristo_init/aristo_memory
|
|
|
|
##
|
2023-08-07 17:45:23 +00:00
|
|
|
## let rc = newAristoDbRef(BackendMemory)
|
2023-06-20 13:26:25 +00:00
|
|
|
## if rc.isOk:
|
|
|
|
## let be = rc.value.to(MemBackendRef)
|
|
|
|
## for (n, key, vtx) in be.walkVtx:
|
|
|
|
## ...
|
|
|
|
##
|
2023-05-14 17:43:01 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-08-21 14:58:30 +00:00
|
|
|
std/[algorithm, options, sequtils, tables],
|
2023-06-20 13:26:25 +00:00
|
|
|
eth/common,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2023-06-20 13:26:25 +00:00
|
|
|
../aristo_constants,
|
2023-06-12 13:48:47 +00:00
|
|
|
../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
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
const
|
|
|
|
extraTraceMessages = false or true
|
|
|
|
## Enabled additional logging noise
|
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
type
|
2024-03-14 22:17:43 +00:00
|
|
|
MemDbRef = ref object
|
|
|
|
## Database
|
2023-08-18 19:46:55 +00:00
|
|
|
sTab: Table[VertexID,Blob] ## Structural vertex table making up a trie
|
2023-06-12 18:16:03 +00:00
|
|
|
kMap: Table[VertexID,HashKey] ## Merkle hash key mapping
|
2024-06-04 15:05:13 +00:00
|
|
|
tUvi: Option[VertexID] ## Top used vertex ID
|
2024-06-07 21:39:58 +00:00
|
|
|
lSst: Opt[SavedState] ## Last saved state
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
MemBackendRef* = ref object of TypedBackendRef
|
|
|
|
## Inheriting table so access can be extended for debugging purposes
|
|
|
|
mdb: MemDbRef ## Database
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
MemPutHdlRef = ref object of TypedPutHdlRef
|
2023-08-18 19:46:55 +00:00
|
|
|
sTab: Table[VertexID,Blob]
|
2023-06-20 13:26:25 +00:00
|
|
|
kMap: Table[VertexID,HashKey]
|
2024-06-04 15:05:13 +00:00
|
|
|
tUvi: Option[VertexID]
|
2024-06-07 21:39:58 +00:00
|
|
|
lSst: Opt[SavedState]
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
import chronicles
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "aristo-backend"
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
template logTxt(info: static[string]): static[string] =
|
2024-04-16 20:39:11 +00:00
|
|
|
"MemoryDB/" & info
|
2023-06-30 22:22:33 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
proc newSession(db: MemBackendRef): MemPutHdlRef =
|
|
|
|
new result
|
|
|
|
result.TypedPutHdlRef.beginSession db
|
|
|
|
|
|
|
|
proc getSession(hdl: PutHdlRef; db: MemBackendRef): MemPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.verifySession db
|
|
|
|
hdl.MemPutHdlRef
|
|
|
|
|
|
|
|
proc endSession(hdl: PutHdlRef; db: MemBackendRef): MemPutHdlRef =
|
|
|
|
hdl.TypedPutHdlRef.finishSession db
|
|
|
|
hdl.MemPutHdlRef
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-20 13:26:25 +00:00
|
|
|
# Private functions: interface
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getVtxFn(db: MemBackendRef): GetVtxFn =
|
|
|
|
result =
|
|
|
|
proc(vid: VertexID): Result[VertexRef,AristoError] =
|
2023-08-18 19:46:55 +00:00
|
|
|
# Fetch serialised data record
|
2024-03-14 22:17:43 +00:00
|
|
|
let data = db.mdb.sTab.getOrDefault(vid, EmptyBlob)
|
2023-08-18 19:46:55 +00:00
|
|
|
if 0 < data.len:
|
2024-04-16 20:39:11 +00:00
|
|
|
let rc = data.deblobify(VertexRef)
|
|
|
|
when extraTraceMessages:
|
|
|
|
if rc.isErr:
|
|
|
|
trace logTxt "getVtxFn() failed", vid, error=rc.error
|
2023-08-18 19:46:55 +00:00
|
|
|
return rc
|
2023-06-20 13:26:25 +00:00
|
|
|
err(GetVtxNotFound)
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
proc getKeyFn(db: MemBackendRef): GetKeyFn =
|
|
|
|
result =
|
2023-06-12 18:16:03 +00:00
|
|
|
proc(vid: VertexID): Result[HashKey,AristoError] =
|
2024-03-14 22:17:43 +00:00
|
|
|
let key = db.mdb.kMap.getOrVoid vid
|
2023-06-22 11:13:24 +00:00
|
|
|
if key.isValid:
|
2023-05-30 21:21:15 +00:00
|
|
|
return ok key
|
2023-06-20 13:26:25 +00:00
|
|
|
err(GetKeyNotFound)
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
proc getTuvFn(db: MemBackendRef): GetTuvFn =
|
2023-06-09 11:17:37 +00:00
|
|
|
result =
|
2024-06-04 15:05:13 +00:00
|
|
|
proc(): Result[VertexID,AristoError]=
|
|
|
|
if db.mdb.tUvi.isSome:
|
|
|
|
return ok db.mdb.tUvi.unsafeGet
|
|
|
|
err(GetTuvNotFound)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
proc getLstFn(db: MemBackendRef): GetLstFn =
|
|
|
|
result =
|
|
|
|
proc(): Result[SavedState,AristoError]=
|
|
|
|
if db.mdb.lSst.isSome:
|
|
|
|
return ok db.mdb.lSst.unsafeGet
|
|
|
|
err(GetLstNotFound)
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc putBegFn(db: MemBackendRef): PutBegFn =
|
|
|
|
result =
|
|
|
|
proc(): PutHdlRef =
|
2023-06-20 13:26:25 +00:00
|
|
|
db.newSession()
|
2023-06-09 11:17:37 +00:00
|
|
|
|
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
proc putVtxFn(db: MemBackendRef): PutVtxFn =
|
|
|
|
result =
|
2023-06-09 11:17:37 +00:00
|
|
|
proc(hdl: PutHdlRef; vrps: openArray[(VertexID,VertexRef)]) =
|
2023-06-20 13:26:25 +00:00
|
|
|
let hdl = hdl.getSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if hdl.error.isNil:
|
|
|
|
for (vid,vtx) in vrps:
|
2023-08-18 19:46:55 +00:00
|
|
|
if vtx.isValid:
|
|
|
|
let rc = vtx.blobify()
|
2023-06-30 22:22:33 +00:00
|
|
|
if rc.isErr:
|
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: VtxPfx,
|
|
|
|
vid: vid,
|
|
|
|
code: rc.error)
|
|
|
|
return
|
2023-08-18 19:46:55 +00:00
|
|
|
hdl.sTab[vid] = rc.value
|
|
|
|
else:
|
|
|
|
hdl.sTab[vid] = EmptyBlob
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
proc putKeyFn(db: MemBackendRef): PutKeyFn =
|
|
|
|
result =
|
2023-06-12 18:16:03 +00:00
|
|
|
proc(hdl: PutHdlRef; vkps: openArray[(VertexID,HashKey)]) =
|
2023-06-20 13:26:25 +00:00
|
|
|
let hdl = hdl.getSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if hdl.error.isNil:
|
|
|
|
for (vid,key) in vkps:
|
|
|
|
hdl.kMap[vid] = key
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
proc putTuvFn(db: MemBackendRef): PutTuvFn =
|
2023-06-09 11:17:37 +00:00
|
|
|
result =
|
2024-06-04 15:05:13 +00:00
|
|
|
proc(hdl: PutHdlRef; vs: VertexID) =
|
2023-06-20 13:26:25 +00:00
|
|
|
let hdl = hdl.getSession db
|
2023-06-30 22:22:33 +00:00
|
|
|
if hdl.error.isNil:
|
2024-06-04 15:05:13 +00:00
|
|
|
hdl.tUvi = some(vs)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
proc putLstFn(db: MemBackendRef): PutLstFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; lst: SavedState) =
|
|
|
|
let hdl = hdl.getSession db
|
|
|
|
if hdl.error.isNil:
|
2024-06-05 18:17:50 +00:00
|
|
|
let rc = lst.blobify # test
|
|
|
|
if rc.isOk:
|
2024-06-07 21:39:58 +00:00
|
|
|
hdl.lSst = Opt.some(lst)
|
2024-06-05 18:17:50 +00:00
|
|
|
else:
|
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: AdmPfx,
|
|
|
|
aid: AdmTabIdLst,
|
|
|
|
code: rc.error)
|
2024-05-31 17:32:22 +00:00
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
proc putEndFn(db: MemBackendRef): 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 AdmPfx: trace logTxt "putEndFn: admin failed",
|
|
|
|
pfx=AdmPfx, aid=hdl.error.aid.uint64, error=hdl.error.code
|
|
|
|
of Oops: trace logTxt "putEndFn: failed",
|
|
|
|
pfx=hdl.error.pfx, error=hdl.error.code
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(hdl.error.code)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
for (vid,data) in hdl.sTab.pairs:
|
|
|
|
if 0 < data.len:
|
2024-03-14 22:17:43 +00:00
|
|
|
db.mdb.sTab[vid] = data
|
2023-06-20 13:26:25 +00:00
|
|
|
else:
|
2024-03-14 22:17:43 +00:00
|
|
|
db.mdb.sTab.del vid
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
for (vid,key) in hdl.kMap.pairs:
|
|
|
|
if key.isValid:
|
2024-03-14 22:17:43 +00:00
|
|
|
db.mdb.kMap[vid] = key
|
2023-06-20 13:26:25 +00:00
|
|
|
else:
|
2024-03-14 22:17:43 +00:00
|
|
|
db.mdb.kMap.del vid
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
let tuv = hdl.tUvi.get(otherwise = VertexID(0))
|
|
|
|
if tuv.isValid:
|
|
|
|
db.mdb.tUvi = some(tuv)
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
if hdl.lSst.isSome:
|
|
|
|
db.mdb.lSst = hdl.lSst
|
|
|
|
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-06-09 11:17:37 +00:00
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
proc guestDbFn(db: MemBackendRef): GuestDbFn =
|
|
|
|
result =
|
2024-05-07 19:59:27 +00:00
|
|
|
proc(instance: int): Result[RootRef,AristoError] =
|
2024-04-16 20:39:11 +00:00
|
|
|
ok(RootRef nil)
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
proc closeFn(db: MemBackendRef): CloseFn =
|
2023-05-14 17:43:01 +00:00
|
|
|
result =
|
2023-06-20 13:26:25 +00:00
|
|
|
proc(ignore: bool) =
|
|
|
|
discard
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
proc memoryBackend*(): BackendRef =
|
2023-09-05 13:57:20 +00:00
|
|
|
let db = MemBackendRef(
|
|
|
|
beKind: BackendMemory,
|
2024-03-14 22:17:43 +00:00
|
|
|
mdb: MemDbRef())
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
db.getVtxFn = getVtxFn db
|
|
|
|
db.getKeyFn = getKeyFn db
|
2024-06-04 15:05:13 +00:00
|
|
|
db.getTuvFn = getTuvFn db
|
2024-05-31 17:32:22 +00:00
|
|
|
db.getLstFn = getLstFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
db.putBegFn = putBegFn db
|
|
|
|
db.putVtxFn = putVtxFn db
|
|
|
|
db.putKeyFn = putKeyFn db
|
2024-06-04 15:05:13 +00:00
|
|
|
db.putTuvFn = putTuvFn db
|
2024-05-31 17:32:22 +00:00
|
|
|
db.putLstFn = putLstFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
db.putEndFn = putEndFn db
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2024-04-16 20:39:11 +00:00
|
|
|
db.guestDbFn = guestDbFn db
|
2023-06-20 13:26:25 +00:00
|
|
|
db.closeFn = closeFn db
|
|
|
|
db
|
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
proc dup*(db: MemBackendRef): MemBackendRef =
|
|
|
|
## Duplicate descriptor shell as needed for API debugging
|
|
|
|
new result
|
|
|
|
init_common.init(result[], db[])
|
|
|
|
result.mdb = db.mdb
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators (needs direct backend access)
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
iterator walkVtx*(
|
|
|
|
be: MemBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[vid: VertexID, vtx: VertexRef] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Iteration over the vertex sub-table.
|
2024-03-14 22:17:43 +00:00
|
|
|
for n,vid in be.mdb.sTab.keys.toSeq.mapIt(it).sorted:
|
|
|
|
let data = be.mdb.sTab.getOrDefault(vid, EmptyBlob)
|
2023-08-18 19:46:55 +00:00
|
|
|
if 0 < data.len:
|
|
|
|
let rc = data.deblobify VertexRef
|
|
|
|
if rc.isErr:
|
2024-04-16 20:39:11 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "walkVtxFn() skip", n, vid, error=rc.error
|
2023-08-18 19:46:55 +00:00
|
|
|
else:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid, rc.value)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
iterator walkKey*(
|
|
|
|
be: MemBackendRef;
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[vid: VertexID, key: HashKey] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Iteration over the Markle hash sub-table.
|
2024-03-14 22:17:43 +00:00
|
|
|
for vid in be.mdb.kMap.keys.toSeq.mapIt(it).sorted:
|
|
|
|
let key = be.mdb.kMap.getOrVoid vid
|
2023-06-20 13:26:25 +00:00
|
|
|
if key.isValid:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid, key)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
iterator walk*(
|
|
|
|
be: MemBackendRef;
|
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-06-04 15:05:13 +00:00
|
|
|
if be.mdb.tUvi.isSome:
|
|
|
|
yield(AdmPfx, AdmTabIdTuv.uint64, be.mdb.tUvi.unsafeGet.blobify)
|
2024-06-03 20:10:35 +00:00
|
|
|
if be.mdb.lSst.isSome:
|
2024-06-05 18:17:50 +00:00
|
|
|
yield(AdmPfx, AdmTabIdLst.uint64, be.mdb.lSst.unsafeGet.blobify.value)
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2024-03-14 22:17:43 +00:00
|
|
|
for vid in be.mdb.sTab.keys.toSeq.mapIt(it).sorted:
|
|
|
|
let data = be.mdb.sTab.getOrDefault(vid, EmptyBlob)
|
2023-08-18 19:46:55 +00:00
|
|
|
if 0 < data.len:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (VtxPfx, vid.uint64, data)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
for (vid,key) in be.walkKey:
|
2024-05-30 17:48:38 +00:00
|
|
|
yield (KeyPfx, vid.uint64, @(key.data))
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|