2023-05-14 17:43:01 +00:00
|
|
|
# nimbus-eth1
|
2023-11-08 16:52:25 +00:00
|
|
|
# Copyright (c) 2023 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.
|
|
|
|
|
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,
|
2023-12-19 12:39:23 +00:00
|
|
|
"."/[aristo_desc, aristo_layers]
|
2023-12-04 20:39:26 +00:00
|
|
|
|
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-12-19 12:39:23 +00:00
|
|
|
## Get the leaf path from the cache layers and look up the database for a
|
|
|
|
## leaf node.
|
|
|
|
let vid = db.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-12-19 12:39:23 +00:00
|
|
|
block body:
|
|
|
|
let vtx = db.layersGetVtx(vid).valueOr:
|
|
|
|
break body
|
|
|
|
if vtx.isValid:
|
|
|
|
return ok(VidVtxPair(vid: vid, vtx: vtx))
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-12-19 12:39:23 +00:00
|
|
|
# The leaf node cannot be on the backend. It was produced by a `merge()`
|
|
|
|
# action. So this is a system problem.
|
|
|
|
err(GetLeafMissing)
|
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-12-19 12:39:23 +00:00
|
|
|
## Cascaded attempt to fetch a vertex from the cache layers or the backend.
|
2023-06-20 13:26:25 +00:00
|
|
|
##
|
2023-12-19 12:39:23 +00:00
|
|
|
block body:
|
|
|
|
# If the vertex marked is to be deleted on the backend, a `VertexRef(nil)`
|
|
|
|
# entry is kept in the local table in which case it isis returned as the
|
|
|
|
# error symbol `GetVtxNotFound`.
|
|
|
|
let vtx = db.layersGetVtx(vid).valueOr:
|
|
|
|
break body
|
2023-09-26 09:21:13 +00:00
|
|
|
if vtx.isValid:
|
2023-12-19 12:39:23 +00:00
|
|
|
return ok vtx
|
|
|
|
else:
|
|
|
|
return err(GetVtxNotFound)
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
db.getVtxBE vid
|
|
|
|
|
|
|
|
proc getVtx*(db: AristoDbRef; vid: VertexID): VertexRef =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Cascaded attempt to fetch a vertex from the cache layers or the backend.
|
2023-09-26 09:21:13 +00:00
|
|
|
## The function returns `nil` on error or failure.
|
|
|
|
##
|
2023-12-19 12:39:23 +00:00
|
|
|
db.getVtxRc(vid).valueOr: VertexRef(nil)
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
proc getKeyRc*(db: AristoDbRef; vid: VertexID): Result[HashKey,AristoError] =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Cascaded attempt to fetch a Merkle hash from the cache layers or the
|
|
|
|
## backend.
|
2023-06-20 13:26:25 +00:00
|
|
|
##
|
2023-12-19 12:39:23 +00:00
|
|
|
block body:
|
|
|
|
let key = db.layersGetKey(vid).valueOr:
|
|
|
|
break body
|
|
|
|
# If there is a zero value label, the entry is either marked for being
|
|
|
|
# updated or for deletion on the database. So check below.
|
|
|
|
if key.isValid:
|
|
|
|
return ok key
|
|
|
|
|
|
|
|
# The zero value label does not refer to an update mark if there is no
|
|
|
|
# valid vertex (either on the cache or the backend whatever comes first.)
|
|
|
|
let vtx = db.layersGetVtx(vid).valueOr:
|
|
|
|
# There was no vertex on the cache. So there must be one the backend (the
|
|
|
|
# reason for the key lable to exists, at all.)
|
|
|
|
return err(GetKeyUpdateNeeded)
|
|
|
|
if vtx.isValid:
|
|
|
|
return err(GetKeyUpdateNeeded)
|
|
|
|
else:
|
|
|
|
# The vertex is to be deleted. So is the value label.
|
|
|
|
return err(GetVtxNotFound)
|
|
|
|
|
2023-09-26 09:21:13 +00:00
|
|
|
db.getKeyBE vid
|
|
|
|
|
|
|
|
proc getKey*(db: AristoDbRef; vid: VertexID): HashKey =
|
2023-12-19 12:39:23 +00:00
|
|
|
## Cascaded attempt to fetch a vertex from the cache layers or the backend.
|
2023-09-26 09:21:13 +00:00
|
|
|
## The function returns `nil` on error or failure.
|
|
|
|
##
|
2023-12-19 12:39:23 +00:00
|
|
|
db.getKeyRc(vid).valueOr: VOID_HASH_KEY
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|