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-06-04 15:05:13 +00:00
|
|
|
proc getTuvUbe*(
|
2023-08-10 20:01:28 +00:00
|
|
|
db: AristoDbRef;
|
2024-06-04 15:05:13 +00:00
|
|
|
): Result[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:
|
2024-06-04 15:05:13 +00:00
|
|
|
return be.getTuvFn()
|
|
|
|
err(GetTuvNotFound)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
proc getLstUbe*(
|
|
|
|
db: AristoDbRef;
|
|
|
|
): Result[SavedState,AristoError] =
|
|
|
|
## Get the last saved state
|
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
|
|
|
return be.getLstFn()
|
|
|
|
err(GetLstNotFound)
|
|
|
|
|
2024-04-26 13:43:52 +00:00
|
|
|
proc getVtxUbe*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
2024-09-20 05:43:53 +00:00
|
|
|
flags: set[GetVtxFlag] = {};
|
2023-05-14 17:43:01 +00:00
|
|
|
): 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:
|
2024-09-20 05:43:53 +00:00
|
|
|
return be.getVtxFn(rvid, flags)
|
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;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
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:
|
2024-07-04 13:46:52 +00:00
|
|
|
return be.getKeyFn rvid
|
2023-08-10 20:01:28 +00:00
|
|
|
err GetKeyNotFound
|
|
|
|
|
|
|
|
# ------------------
|
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
proc getTuvBE*(
|
2023-08-10 20:01:28 +00:00
|
|
|
db: AristoDbRef;
|
2024-06-04 15:05:13 +00:00
|
|
|
): Result[VertexID,AristoError] =
|
2023-08-10 20:01:28 +00:00
|
|
|
## Get the ID generator state the `backened` layer if available.
|
2024-06-03 20:10:35 +00:00
|
|
|
if not db.balancer.isNil:
|
2024-06-04 15:05:13 +00:00
|
|
|
return ok(db.balancer.vTop)
|
|
|
|
db.getTuvUbe()
|
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;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
2024-09-20 05:43:53 +00:00
|
|
|
flags: set[GetVtxFlag] = {};
|
2024-07-18 07:13:56 +00:00
|
|
|
): Result[(VertexRef, int),AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the vertex from the (filtered) backened if available.
|
2024-06-03 20:10:35 +00:00
|
|
|
if not db.balancer.isNil:
|
2024-07-04 13:46:52 +00:00
|
|
|
db.balancer.sTab.withValue(rvid, w):
|
2024-05-07 19:59:27 +00:00
|
|
|
if w[].isValid:
|
2024-07-18 07:13:56 +00:00
|
|
|
return ok (w[], -1)
|
2024-05-07 19:59:27 +00:00
|
|
|
return err(GetVtxNotFound)
|
2024-09-20 05:43:53 +00:00
|
|
|
ok (? db.getVtxUbe(rvid, flags), -2)
|
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;
|
2024-07-04 13:46:52 +00:00
|
|
|
rvid: RootedVertexID;
|
2024-07-18 07:13:56 +00:00
|
|
|
): Result[(HashKey, int),AristoError] =
|
2023-08-11 17:23:57 +00:00
|
|
|
## Get the merkle hash/key from the (filtered) backend if available.
|
2024-06-03 20:10:35 +00:00
|
|
|
if not db.balancer.isNil:
|
2024-07-04 13:46:52 +00:00
|
|
|
db.balancer.kMap.withValue(rvid, w):
|
2024-05-07 19:59:27 +00:00
|
|
|
if w[].isValid:
|
2024-07-18 07:13:56 +00:00
|
|
|
return ok((w[], -1))
|
2024-05-07 19:59:27 +00:00
|
|
|
return err(GetKeyNotFound)
|
2024-07-18 07:13:56 +00:00
|
|
|
ok ((?db.getKeyUbe rvid), -2)
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
proc getVtxRc*(
|
|
|
|
db: AristoDbRef;
|
2024-09-20 05:43:53 +00:00
|
|
|
rvid: RootedVertexID;
|
|
|
|
flags: set[GetVtxFlag] = {};
|
2024-07-18 07:13:56 +00:00
|
|
|
): Result[(VertexRef, int),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)`
|
2024-07-29 20:15:17 +00:00
|
|
|
# entry is kept in the local table in which case it is returned as the
|
2023-12-19 12:39:23 +00:00
|
|
|
# error symbol `GetVtxNotFound`.
|
2024-07-04 13:46:52 +00:00
|
|
|
let vtx = db.layersGetVtx(rvid).valueOr:
|
2023-12-19 12:39:23 +00:00
|
|
|
break body
|
2024-07-18 07:13:56 +00:00
|
|
|
if vtx[0].isValid:
|
2023-12-19 12:39:23 +00:00
|
|
|
return ok vtx
|
|
|
|
else:
|
|
|
|
return err(GetVtxNotFound)
|
|
|
|
|
2024-09-20 05:43:53 +00:00
|
|
|
db.getVtxBE(rvid, flags)
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2024-09-20 05:43:53 +00:00
|
|
|
proc getVtx*(db: AristoDbRef; rvid: RootedVertexID, flags: set[GetVtxFlag] = {}): 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.
|
|
|
|
##
|
2024-07-18 07:13:56 +00:00
|
|
|
db.getVtxRc(rvid).valueOr((VertexRef(nil), 0))[0]
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-07-18 07:13:56 +00:00
|
|
|
proc getKeyRc*(db: AristoDbRef; rvid: RootedVertexID): Result[(HashKey, int),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:
|
2024-07-04 13:46:52 +00:00
|
|
|
let key = db.layersGetKey(rvid).valueOr:
|
2023-12-19 12:39:23 +00:00
|
|
|
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.
|
2024-07-18 07:13:56 +00:00
|
|
|
if key[0].isValid:
|
2023-12-19 12:39:23 +00:00
|
|
|
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.)
|
2024-07-04 13:46:52 +00:00
|
|
|
let vtx = db.layersGetVtx(rvid).valueOr:
|
2023-12-19 12:39:23 +00:00
|
|
|
# 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)
|
2024-07-18 07:13:56 +00:00
|
|
|
if vtx[0].isValid:
|
2023-12-19 12:39:23 +00:00
|
|
|
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
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
db.getKeyBE rvid
|
2023-09-26 09:21:13 +00:00
|
|
|
|
2024-07-04 13:46:52 +00:00
|
|
|
proc getKey*(db: AristoDbRef; rvid: RootedVertexID): 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.
|
|
|
|
##
|
2024-07-18 07:13:56 +00:00
|
|
|
(db.getKeyRc(rvid).valueOr((VOID_HASH_KEY, 0)))[0]
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|