2023-05-14 17:43:01 +00:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +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.
|
|
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-04-26 13:43:52 +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)
|
|
|
|
|
2024-04-26 13:43:52 +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
|
|
|
|
2024-04-26 13:43:52 +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
|
|
|
|
2024-04-26 13:43:52 +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] =
|
2024-04-26 13:43:52 +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
|
|
|
|
|
2024-04-26 13:43:52 +00:00
|
|
|
proc getFilUbe*(
|
2023-08-22 18:44:54 +00:00
|
|
|
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)
|
2024-04-26 13:43:52 +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.
|
2024-05-07 19:59:27 +00:00
|
|
|
if not db.roFilter.isNil:
|
|
|
|
db.roFilter.sTab.withValue(vid, w):
|
|
|
|
if w[].isValid:
|
|
|
|
return ok(w[])
|
|
|
|
return err(GetVtxNotFound)
|
2024-04-26 13:43:52 +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.
|
2024-05-07 19:59:27 +00:00
|
|
|
if not db.roFilter.isNil:
|
|
|
|
db.roFilter.kMap.withValue(vid, w):
|
|
|
|
if w[].isValid:
|
|
|
|
return ok(w[])
|
|
|
|
return err(GetKeyNotFound)
|
2024-04-26 13:43:52 +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
|
|
|
|
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
|
2024-03-22 17:31:56 +00:00
|
|
|
## backend. This function will never return a `VOID_HASH_KEY` but rather
|
|
|
|
## some `GetKeyNotFound` or `GetKeyUpdateNeeded` error.
|
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
|
2024-02-14 19:11:59 +00:00
|
|
|
# If there is a zero key value, the entry is either marked for being
|
2023-12-19 12:39:23 +00:00
|
|
|
# updated or for deletion on the database. So check below.
|
|
|
|
if key.isValid:
|
|
|
|
return ok key
|
|
|
|
|
2024-02-14 19:11:59 +00:00
|
|
|
# The zero key value does not refer to an update mark if there is no
|
2023-12-19 12:39:23 +00:00
|
|
|
# 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:
|
2024-02-14 19:11:59 +00:00
|
|
|
# The vertex is to be deleted. So is the value key.
|
2024-02-01 21:27:48 +00:00
|
|
|
return err(GetKeyNotFound)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
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
|
|
|
|
# ------------------------------------------------------------------------------
|