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.
|
|
|
|
|
|
|
|
## Handle vertex IDs on the layered Aristo DB delta architecture
|
|
|
|
## =============================================================
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-12-19 12:39:23 +00:00
|
|
|
std/[algorithm, sequtils],
|
|
|
|
"."/[aristo_desc, aristo_layers]
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
proc vidFetch*(db: AristoDbRef; pristine = false): VertexID =
|
2023-06-30 22:22:33 +00:00
|
|
|
## Create a new `VertexID`. Reusable vertex *ID*s are kept in a list where
|
|
|
|
## the top entry *ID* has the property that any other *ID* larger is also not
|
2023-05-14 17:43:01 +00:00
|
|
|
## not used on the database.
|
2023-06-30 22:22:33 +00:00
|
|
|
##
|
|
|
|
## The function prefers to return recycled vertex *ID*s if there are any.
|
|
|
|
## When the argument `pristine` is set `true`, the function guarantees to
|
|
|
|
## return a non-recycled, brand new vertex *ID* which is the preferred mode
|
|
|
|
## when creating leaf vertices.
|
2023-12-19 12:39:23 +00:00
|
|
|
if db.vGen.len == 0:
|
2023-06-09 11:17:37 +00:00
|
|
|
# Note that `VertexID(1)` is the root of the main trie
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top.final.vGen = @[VertexID(3)]
|
2023-06-09 11:17:37 +00:00
|
|
|
result = VertexID(2)
|
2023-12-19 12:39:23 +00:00
|
|
|
elif db.vGen.len == 1 or pristine:
|
|
|
|
result = db.vGen[^1]
|
|
|
|
db.top.final.vGen[^1] = result + 1
|
2023-05-14 17:43:01 +00:00
|
|
|
else:
|
2023-12-19 12:39:23 +00:00
|
|
|
result = db.vGen[^2]
|
|
|
|
db.top.final.vGen[^2] = db.top.final.vGen[^1]
|
|
|
|
db.top.final.vGen.setLen(db.vGen.len-1)
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
proc vidPeek*(db: AristoDbRef): VertexID =
|
2023-05-14 17:43:01 +00:00
|
|
|
## Like `new()` without consuming this *ID*. It will return the *ID* that
|
|
|
|
## would be returned by the `new()` function.
|
2023-12-19 12:39:23 +00:00
|
|
|
case db.vGen.len:
|
2023-05-14 17:43:01 +00:00
|
|
|
of 0:
|
2023-06-09 11:17:37 +00:00
|
|
|
VertexID(2)
|
2023-05-14 17:43:01 +00:00
|
|
|
of 1:
|
2023-12-19 12:39:23 +00:00
|
|
|
db.vGen[^1]
|
2023-05-14 17:43:01 +00:00
|
|
|
else:
|
2023-12-19 12:39:23 +00:00
|
|
|
db.vGen[^2]
|
2023-05-14 17:43:01 +00:00
|
|
|
|
|
|
|
|
2023-07-04 18:24:03 +00:00
|
|
|
proc vidDispose*(db: AristoDbRef; vid: VertexID) =
|
2023-05-14 17:43:01 +00:00
|
|
|
## Recycle the argument `vtxID` which is useful after deleting entries from
|
|
|
|
## the vertex table to prevent the `VertexID` type key values small.
|
2023-06-09 11:17:37 +00:00
|
|
|
if VertexID(1) < vid:
|
2023-12-19 12:39:23 +00:00
|
|
|
if db.vGen.len == 0:
|
|
|
|
db.top.final.vGen = @[vid]
|
2023-06-09 11:17:37 +00:00
|
|
|
else:
|
2023-12-19 12:39:23 +00:00
|
|
|
let topID = db.vGen[^1]
|
2023-06-09 11:17:37 +00:00
|
|
|
# Only store smaller numbers: all numberts larger than `topID`
|
|
|
|
# are free numbers
|
|
|
|
if vid < topID:
|
2023-12-19 12:39:23 +00:00
|
|
|
db.top.final.vGen[^1] = vid
|
|
|
|
db.top.final.vGen.add topID
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
proc vidReorg*(vGen: seq[VertexID]): seq[VertexID] =
|
|
|
|
## Return a compacted version of the argument vertex ID generator state
|
|
|
|
## `vGen`. The function removes redundant items from the recycle queue.
|
|
|
|
if 1 < vGen.len:
|
|
|
|
let lst = vGen.mapIt(uint64(it)).sorted.mapIt(VertexID(it))
|
2023-06-09 11:17:37 +00:00
|
|
|
for n in (lst.len-1).countDown(1):
|
|
|
|
if lst[n-1].uint64 + 1 != lst[n].uint64:
|
2023-08-10 20:01:28 +00:00
|
|
|
# All elements larger than `lst[n-1]` are in increasing order. For
|
2023-06-09 11:17:37 +00:00
|
|
|
# the last continuously increasing sequence, only the smallest item
|
|
|
|
# is needed and the rest can be removed
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# ..3, 5, 6, 7 => ..3, 5
|
|
|
|
# ^
|
|
|
|
# |
|
|
|
|
# n
|
|
|
|
#
|
|
|
|
if n < lst.len-1:
|
2023-08-10 20:01:28 +00:00
|
|
|
return lst[0..n]
|
|
|
|
return vGen
|
2023-06-09 11:17:37 +00:00
|
|
|
# All entries are continuously increasing
|
2023-08-10 20:01:28 +00:00
|
|
|
return @[lst[0]]
|
|
|
|
|
|
|
|
vGen
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-05-14 17:43:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|