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.
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
## Read vertex record on the layered Aristo DB delta architecture
|
|
|
|
## ==============================================================
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-08-18 19:46:55 +00:00
|
|
|
std/tables,
|
2023-08-10 20:01:28 +00:00
|
|
|
results,
|
|
|
|
./aristo_desc
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
type
|
|
|
|
VidVtxPair* = object
|
|
|
|
vid*: VertexID ## Table lookup vertex ID (if any)
|
|
|
|
vtx*: VertexRef ## Reference to vertex
|
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc getIdgUBE*(
|
2023-08-10 20:01:28 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[seq[VertexID],AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the ID generator state from the unfiltered backened if available.
|
2023-08-10 20:01:28 +00:00
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
|
|
|
return be.getIdgFn()
|
|
|
|
err(GetIdgNotFound)
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
proc getFqsUBE*(
|
2023-08-22 18:44:54 +00:00
|
|
|
db: AristoDbRef;
|
2023-08-25 22:53:59 +00:00
|
|
|
): Result[seq[(QueueID,QueueID)],AristoError] =
|
2023-08-22 18:44:54 +00:00
|
|
|
## Get the list of filter IDs unfiltered backened if available.
|
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
2023-08-25 22:53:59 +00:00
|
|
|
return be.getFqsFn()
|
|
|
|
err(GetFqsNotFound)
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc getVtxUBE*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef;
|
2023-05-14 17:43:01 +00:00
|
|
|
vid: VertexID;
|
|
|
|
): Result[VertexRef,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the vertex from the unfiltered backened if available.
|
2023-06-09 11:17:37 +00:00
|
|
|
let be = db.backend
|
2023-05-14 17:43:01 +00:00
|
|
|
if not be.isNil:
|
|
|
|
return be.getVtxFn vid
|
2023-08-10 20:01:28 +00:00
|
|
|
err GetVtxNotFound
|
2023-05-14 17:43:01 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc getKeyUBE*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef;
|
2023-06-09 11:17:37 +00:00
|
|
|
vid: VertexID;
|
2023-06-12 18:16:03 +00:00
|
|
|
): Result[HashKey,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the merkle hash/key from the unfiltered backend if available.
|
2023-06-20 13:26:25 +00:00
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
|
|
|
return be.getKeyFn vid
|
2023-08-10 20:01:28 +00:00
|
|
|
err GetKeyNotFound
|
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
proc getFilUBE*(
|
|
|
|
db: AristoDbRef;
|
2023-08-25 22:53:59 +00:00
|
|
|
qid: QueueID;
|
2023-08-22 18:44:54 +00:00
|
|
|
): Result[FilterRef,AristoError] =
|
|
|
|
## Get the filter from the unfiltered backened if available.
|
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
2023-08-25 22:53:59 +00:00
|
|
|
return be.getFilFn qid
|
2023-08-22 18:44:54 +00:00
|
|
|
err GetFilNotFound
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
# ------------------
|
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc getIdgBE*(
|
2023-08-10 20:01:28 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[seq[VertexID],AristoError] =
|
|
|
|
## Get the ID generator state the `backened` layer if available.
|
2023-08-18 19:46:55 +00:00
|
|
|
if not db.roFilter.isNil:
|
|
|
|
return ok(db.roFilter.vGen)
|
2023-08-11 17:23:57 +00:00
|
|
|
db.getIdgUBE()
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc getVtxBE*(
|
2023-08-10 20:01:28 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[VertexRef,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the vertex from the (filtered) backened if available.
|
2023-08-10 20:01:28 +00:00
|
|
|
if not db.roFilter.isNil and db.roFilter.sTab.hasKey vid:
|
|
|
|
let vtx = db.roFilter.sTab.getOrVoid vid
|
|
|
|
if vtx.isValid:
|
|
|
|
return ok(vtx)
|
|
|
|
return err(GetVtxNotFound)
|
2023-08-11 17:23:57 +00:00
|
|
|
db.getVtxUBE vid
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-11 17:23:57 +00:00
|
|
|
proc getKeyBE*(
|
2023-08-10 20:01:28 +00:00
|
|
|
db: AristoDbRef;
|
|
|
|
vid: VertexID;
|
|
|
|
): Result[HashKey,AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the merkle hash/key from the (filtered) backend if available.
|
2023-08-10 20:01:28 +00:00
|
|
|
if not db.roFilter.isNil and db.roFilter.kMap.hasKey vid:
|
|
|
|
let key = db.roFilter.kMap.getOrVoid vid
|
|
|
|
if key.isValid:
|
|
|
|
return ok(key)
|
|
|
|
return err(GetKeyNotFound)
|
2023-08-11 17:23:57 +00:00
|
|
|
db.getKeyUBE vid
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------
|
2023-06-09 11:17:37 +00:00
|
|
|
|
|
|
|
proc getLeaf*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef;
|
2023-06-12 13:48:47 +00:00
|
|
|
lty: LeafTie;
|
2023-05-30 11:47:47 +00:00
|
|
|
): Result[VidVtxPair,AristoError] =
|
2023-06-09 11:17:37 +00:00
|
|
|
## Get the vertex from the top layer by the `Patricia Trie` path. This
|
|
|
|
## function does not search on the `backend` layer.
|
2023-06-12 13:48:47 +00:00
|
|
|
let vid = db.top.lTab.getOrVoid lty
|
2023-06-20 13:26:25 +00:00
|
|
|
if not vid.isValid:
|
|
|
|
return err(GetLeafNotFound)
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
let vtx = db.top.sTab.getOrVoid vid
|
|
|
|
if not vtx.isValid:
|
|
|
|
return err(GetVtxNotFound)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
ok VidVtxPair(vid: vid, vtx: vtx)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
proc getLeafVtx*(db: AristoDbRef; lty: LeafTie): VertexRef =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Variant of `getLeaf()` returning `nil` on error (while ignoring the
|
|
|
|
## detailed error type information.)
|
|
|
|
##
|
2023-06-12 13:48:47 +00:00
|
|
|
let rc = db.getLeaf lty
|
2023-05-14 17:43:01 +00:00
|
|
|
if rc.isOk:
|
2023-06-09 11:17:37 +00:00
|
|
|
return rc.value.vtx
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# ------------------
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
proc getVtxRc*(db: AristoDbRef; vid: VertexID): Result[VertexRef,AristoError] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Cascaded attempt to fetch a vertex from the top layer or the backend.
|
|
|
|
##
|
|
|
|
if db.top.sTab.hasKey vid:
|
|
|
|
# If the vertex is to be deleted on the backend, a `VertexRef(nil)` entry
|
|
|
|
# is kept in the local table in which case it is OK to return this value.
|
2023-09-26 09:21:13 +00:00
|
|
|
let vtx = db.top.sTab.getOrVoid vid
|
|
|
|
if vtx.isValid:
|
|
|
|
return ok(vtx)
|
|
|
|
return err(GetVtxNotFound)
|
|
|
|
db.getVtxBE vid
|
|
|
|
|
|
|
|
proc getVtx*(db: AristoDbRef; vid: VertexID): VertexRef =
|
|
|
|
## Cascaded attempt to fetch a vertex from the top layer or the backend.
|
|
|
|
## The function returns `nil` on error or failure.
|
|
|
|
##
|
|
|
|
let rc = db.getVtxRc vid
|
2023-06-20 13:26:25 +00:00
|
|
|
if rc.isOk:
|
|
|
|
return rc.value
|
|
|
|
VertexRef(nil)
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
proc getKeyRc*(db: AristoDbRef; vid: VertexID): Result[HashKey,AristoError] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Cascaded attempt to fetch a Merkle hash from the top layer or the backend.
|
|
|
|
##
|
|
|
|
if db.top.kMap.hasKey vid:
|
|
|
|
# If the key is to be deleted on the backend, a `VOID_HASH_LABEL` entry
|
|
|
|
# is kept on the local table in which case it is OK to return this value.
|
2023-09-26 09:21:13 +00:00
|
|
|
let key = db.top.kMap.getOrVoid(vid).key
|
|
|
|
if key.isValid:
|
|
|
|
return ok(key)
|
2023-10-03 11:56:13 +00:00
|
|
|
return err(GetKeyTempLocked)
|
2023-09-26 09:21:13 +00:00
|
|
|
db.getKeyBE vid
|
|
|
|
|
|
|
|
proc getKey*(db: AristoDbRef; vid: VertexID): HashKey =
|
|
|
|
## Cascaded attempt to fetch a vertex from the top layer or the backend.
|
|
|
|
## The function returns `nil` on error or failure.
|
|
|
|
##
|
|
|
|
let rc = db.getKeyRc vid
|
2023-06-20 13:26:25 +00:00
|
|
|
if rc.isOk:
|
|
|
|
return rc.value
|
|
|
|
VOID_HASH_KEY
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|