2023-05-14 17:43:01 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# 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
|
|
|
|
##
|
|
|
|
## let rc = AristoDb.init(BackendMemory)
|
|
|
|
## 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-06-20 13:26:25 +00:00
|
|
|
std/[algorithm, sequtils, tables],
|
2023-06-30 22:22:33 +00:00
|
|
|
chronicles,
|
2023-06-20 13:26:25 +00:00
|
|
|
eth/common,
|
2023-05-14 17:43:01 +00:00
|
|
|
stew/results,
|
2023-06-20 13:26:25 +00:00
|
|
|
../aristo_constants,
|
2023-06-12 13:48:47 +00:00
|
|
|
../aristo_desc,
|
2023-06-20 13:26:25 +00:00
|
|
|
../aristo_desc/aristo_types_backend,
|
|
|
|
../aristo_transcode,
|
|
|
|
./aristo_init_common
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
type
|
2023-06-30 22:22:33 +00:00
|
|
|
MemBackendRef* = ref object of TypedBackendRef
|
2023-06-20 13:26:25 +00:00
|
|
|
## Inheriting table so access can be extended for debugging purposes
|
2023-05-14 17:43:01 +00:00
|
|
|
sTab: Table[VertexID,VertexRef] ## Structural vertex table making up a trie
|
2023-06-12 18:16:03 +00:00
|
|
|
kMap: Table[VertexID,HashKey] ## Merkle hash key mapping
|
2023-06-09 11:17:37 +00:00
|
|
|
vGen: seq[VertexID]
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
MemPutHdlRef = ref object of TypedPutHdlRef
|
|
|
|
sTab: Table[VertexID,VertexRef]
|
|
|
|
kMap: Table[VertexID,HashKey]
|
|
|
|
vGen: seq[VertexID]
|
|
|
|
vGenOk: bool
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# 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] =
|
|
|
|
"MemoryDB " & info
|
|
|
|
|
|
|
|
|
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-06-22 11:13:24 +00:00
|
|
|
let vtx = db.sTab.getOrVoid vid
|
|
|
|
if vtx.isValid:
|
2023-06-22 19:21:33 +00:00
|
|
|
return ok vtx.dup
|
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] =
|
|
|
|
let key = db.kMap.getOrDefault(vid, VOID_HASH_KEY)
|
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
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
proc getIdgFn(db: MemBackendRef): GetIdgFn =
|
|
|
|
result =
|
|
|
|
proc(): Result[seq[VertexID],AristoError]=
|
|
|
|
ok db.vGen
|
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
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:
|
|
|
|
if not vtx.isNil:
|
|
|
|
let rc = vtx.blobify # verify data record
|
|
|
|
if rc.isErr:
|
|
|
|
hdl.error = TypedPutHdlErrRef(
|
|
|
|
pfx: VtxPfx,
|
|
|
|
vid: vid,
|
|
|
|
code: rc.error)
|
|
|
|
return
|
|
|
|
hdl.sTab[vid] = vtx.dup
|
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
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
proc putIdgFn(db: MemBackendRef): PutIdgFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vs: openArray[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:
|
|
|
|
hdl.vGen = vs.toSeq
|
|
|
|
hdl.vGenOk = true
|
2023-06-09 11:17:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc putEndFn(db: MemBackendRef): PutEndFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef): 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:
|
|
|
|
debug logTxt "putEndFn: failed",
|
|
|
|
pfx=hdl.error.pfx, vid=hdl.error.vid, error=hdl.error.code
|
|
|
|
return hdl.error.code
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
for (vid,vtx) in hdl.sTab.pairs:
|
|
|
|
if vtx.isValid:
|
|
|
|
db.sTab[vid] = vtx
|
|
|
|
else:
|
|
|
|
db.sTab.del vid
|
|
|
|
|
|
|
|
for (vid,key) in hdl.kMap.pairs:
|
|
|
|
if key.isValid:
|
|
|
|
db.kMap[vid] = key
|
|
|
|
else:
|
|
|
|
db.kMap.del vid
|
|
|
|
|
|
|
|
if hdl.vGenOk:
|
|
|
|
db.vGen = hdl.vGen
|
2023-06-09 11:17:37 +00:00
|
|
|
AristoError(0)
|
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc memoryBackend*(): AristoBackendRef =
|
2023-06-20 13:26:25 +00:00
|
|
|
let db = MemBackendRef(kind: BackendMemory)
|
|
|
|
|
|
|
|
db.getVtxFn = getVtxFn db
|
|
|
|
db.getKeyFn = getKeyFn db
|
|
|
|
db.getIdgFn = getIdgFn db
|
|
|
|
|
|
|
|
db.putBegFn = putBegFn db
|
|
|
|
db.putVtxFn = putVtxFn db
|
|
|
|
db.putKeyFn = putKeyFn db
|
|
|
|
db.putIdgFn = putIdgFn db
|
|
|
|
db.putEndFn = putEndFn db
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
db.closeFn = closeFn db
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
db
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators (needs direct backend access)
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
iterator walkIdg*(
|
|
|
|
be: MemBackendRef;
|
|
|
|
): tuple[n: int, vid: VertexID, vGen: seq[VertexID]] =
|
|
|
|
## Iteration over the ID generator sub-table (there is at most one instance).
|
|
|
|
if 0 < be.vGen.len:
|
|
|
|
yield(0, VertexID(0), be.vGen)
|
|
|
|
|
|
|
|
iterator walkVtx*(
|
|
|
|
be: MemBackendRef;
|
|
|
|
): tuple[n: int, vid: VertexID, vtx: VertexRef] =
|
|
|
|
## Iteration over the vertex sub-table.
|
|
|
|
for n,vid in be.sTab.keys.toSeq.mapIt(it.uint64).sorted.mapIt(it.VertexID):
|
|
|
|
let vtx = be.sTab.getOrVoid vid
|
|
|
|
if vtx.isValid:
|
|
|
|
yield (n, vid, vtx)
|
|
|
|
|
|
|
|
iterator walkKey*(
|
|
|
|
be: MemBackendRef;
|
|
|
|
): tuple[n: int, vid: VertexID, key: HashKey] =
|
|
|
|
## Iteration over the Markle hash sub-table.
|
|
|
|
for n,vid in be.kMap.keys.toSeq.mapIt(it.uint64).sorted.mapIt(it.VertexID):
|
|
|
|
let key = be.kMap.getOrDefault(vid, VOID_HASH_KEY)
|
|
|
|
if key.isValid:
|
|
|
|
yield (n, vid, key)
|
|
|
|
|
|
|
|
iterator walk*(
|
|
|
|
be: MemBackendRef;
|
|
|
|
): tuple[n: int, pfx: AristoStorageType, vid: VertexID, data: Blob] =
|
|
|
|
## 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.
|
|
|
|
for (n,vid,vGen) in be.walkIdg:
|
|
|
|
yield (n, IdgPfx, vid, vGen.blobify)
|
|
|
|
|
|
|
|
for (n,vid,vtx) in be.walkVtx:
|
|
|
|
let rc = vtx.blobify
|
|
|
|
if rc.isOk:
|
|
|
|
yield (n, VtxPfx, vid, rc.value)
|
|
|
|
|
|
|
|
for (n,vid,key) in be.walkKey:
|
|
|
|
yield (n, KeyPfx, vid, key.to(Blob))
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|