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
|
|
|
|
## ===============================
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-06-09 11:17:37 +00:00
|
|
|
std/[sequtils, tables],
|
2023-05-14 17:43:01 +00:00
|
|
|
stew/results,
|
2023-06-12 13:48:47 +00:00
|
|
|
../aristo_desc,
|
|
|
|
../aristo_desc/aristo_types_backend
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
MemBackendRef = ref object
|
|
|
|
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]
|
|
|
|
txGen: uint ## Transaction ID generator (for debugging)
|
|
|
|
txId: uint ## Active transaction ID (for debugging)
|
|
|
|
|
|
|
|
MemPutHdlRef = ref object of PutHdlRef
|
|
|
|
txId: uint ## Transaction ID (for debugging)
|
|
|
|
|
|
|
|
const
|
|
|
|
VerifyIxId = true # for debugging
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getVtxFn(db: MemBackendRef): GetVtxFn =
|
|
|
|
result =
|
|
|
|
proc(vid: VertexID): Result[VertexRef,AristoError] =
|
2023-05-30 21:21:15 +00:00
|
|
|
let vtx = db.sTab.getOrDefault(vid, VertexRef(nil))
|
|
|
|
if vtx != VertexRef(nil):
|
|
|
|
return ok vtx
|
2023-05-14 17:43:01 +00:00
|
|
|
err(MemBeVtxNotFound)
|
|
|
|
|
|
|
|
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)
|
|
|
|
if key != VOID_HASH_KEY:
|
2023-05-30 21:21:15 +00:00
|
|
|
return ok key
|
2023-05-14 17:43:01 +00:00
|
|
|
err(MemBeKeyNotFound)
|
|
|
|
|
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 =
|
|
|
|
when VerifyIxId:
|
|
|
|
doAssert db.txId == 0
|
|
|
|
db.txGen.inc
|
|
|
|
MemPutHdlRef(txId: db.txGen)
|
|
|
|
|
|
|
|
|
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)]) =
|
|
|
|
when VerifyIxId:
|
|
|
|
doAssert db.txId == hdl.MemPutHdlRef.txId
|
2023-05-14 17:43:01 +00:00
|
|
|
for (vid,vtx) in vrps:
|
|
|
|
db.sTab[vid] = vtx
|
|
|
|
|
|
|
|
proc putKeyFn(db: MemBackendRef): PutKeyFn =
|
|
|
|
result =
|
2023-06-12 18:16:03 +00:00
|
|
|
proc(hdl: PutHdlRef; vkps: openArray[(VertexID,HashKey)]) =
|
2023-06-09 11:17:37 +00:00
|
|
|
when VerifyIxId:
|
|
|
|
doAssert db.txId == hdl.MemPutHdlRef.txId
|
2023-05-14 17:43:01 +00:00
|
|
|
for (vid,key) in vkps:
|
|
|
|
db.kMap[vid] = key
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
proc putIdgFn(db: MemBackendRef): PutIdgFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef; vs: openArray[VertexID]) =
|
|
|
|
when VerifyIxId:
|
|
|
|
doAssert db.txId == hdl.MemPutHdlRef.txId
|
|
|
|
db.vGen = vs.toSeq
|
|
|
|
|
|
|
|
|
|
|
|
proc putEndFn(db: MemBackendRef): PutEndFn =
|
|
|
|
result =
|
|
|
|
proc(hdl: PutHdlRef): AristoError =
|
|
|
|
when VerifyIxId:
|
|
|
|
doAssert db.txId == hdl.MemPutHdlRef.txId
|
|
|
|
db.txId = 0
|
|
|
|
AristoError(0)
|
|
|
|
|
|
|
|
# -------------
|
|
|
|
|
|
|
|
proc delVtxFn(db: MemBackendRef): DelVtxFn =
|
2023-05-14 17:43:01 +00:00
|
|
|
result =
|
|
|
|
proc(vids: openArray[VertexID]) =
|
|
|
|
for vid in vids:
|
|
|
|
db.sTab.del vid
|
2023-06-09 11:17:37 +00:00
|
|
|
|
|
|
|
proc delKeyFn(db: MemBackendRef): DelKeyFn =
|
|
|
|
result =
|
|
|
|
proc(vids: openArray[VertexID]) =
|
|
|
|
for vid in vids:
|
2023-05-14 17:43:01 +00:00
|
|
|
db.kMap.del vid
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc memoryBackend*(): AristoBackendRef =
|
|
|
|
let db = MemBackendRef()
|
|
|
|
|
|
|
|
AristoBackendRef(
|
|
|
|
getVtxFn: getVtxFn db,
|
|
|
|
getKeyFn: getKeyFn db,
|
2023-06-09 11:17:37 +00:00
|
|
|
getIdgFn: getIdgFn db,
|
|
|
|
|
|
|
|
putBegFn: putBegFn db,
|
2023-05-14 17:43:01 +00:00
|
|
|
putVtxFn: putVtxFn db,
|
|
|
|
putKeyFn: putKeyFn db,
|
2023-06-09 11:17:37 +00:00
|
|
|
putIdgFn: putIdgFn db,
|
|
|
|
putEndFn: putEndFn db,
|
|
|
|
|
|
|
|
delVtxFn: delVtxFn db,
|
|
|
|
delKeyFn: delKeyFn db)
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|