2023-08-10 20:01:28 +00:00
|
|
|
# Nimbus - Types, data structures and shared utilities used in network sync
|
|
|
|
#
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-08-10 20:01:28 +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.
|
|
|
|
|
|
|
|
import
|
2024-02-01 21:27:48 +00:00
|
|
|
std/[algorithm, sequtils, sets, tables],
|
2023-09-05 13:57:20 +00:00
|
|
|
results,
|
2023-12-20 16:19:00 +00:00
|
|
|
".."/[aristo_desc, aristo_get, aristo_init, aristo_layers, aristo_utils]
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public generic iterators
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator walkVtxBeImpl*[T](
|
|
|
|
db: AristoDbRef; # Database with optional backend filter
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[vid: VertexID, vtx: VertexRef] =
|
2023-08-10 20:01:28 +00:00
|
|
|
## Generic iterator
|
2023-09-15 15:23:53 +00:00
|
|
|
when T is VoidBackendRef:
|
2023-08-18 19:46:55 +00:00
|
|
|
let filter = if db.roFilter.isNil: FilterRef() else: db.roFilter
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
mixin walkVtx
|
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
let filter = FilterRef()
|
2023-08-10 20:01:28 +00:00
|
|
|
if not db.roFilter.isNil:
|
|
|
|
filter.sTab = db.roFilter.sTab # copy table
|
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
for (vid,vtx) in db.backend.T.walkVtx:
|
2023-08-10 20:01:28 +00:00
|
|
|
if filter.sTab.hasKey vid:
|
|
|
|
let fVtx = filter.sTab.getOrVoid vid
|
|
|
|
if fVtx.isValid:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid,fVtx)
|
2023-08-10 20:01:28 +00:00
|
|
|
filter.sTab.del vid
|
|
|
|
else:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid,vtx)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
for vid in filter.sTab.keys.toSeq.mapIt(it.uint64).sorted.mapIt(it.VertexID):
|
|
|
|
let vtx = filter.sTab.getOrVoid vid
|
|
|
|
if vtx.isValid:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid,vtx)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
iterator walkKeyBeImpl*[T](
|
|
|
|
db: AristoDbRef; # Database with optional backend filter
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[vid: VertexID, key: HashKey] =
|
2023-08-10 20:01:28 +00:00
|
|
|
## Generic iterator
|
2023-09-15 15:23:53 +00:00
|
|
|
when T is VoidBackendRef:
|
2023-08-18 19:46:55 +00:00
|
|
|
let filter = if db.roFilter.isNil: FilterRef() else: db.roFilter
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
mixin walkKey
|
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
let filter = FilterRef()
|
2023-08-10 20:01:28 +00:00
|
|
|
if not db.roFilter.isNil:
|
|
|
|
filter.kMap = db.roFilter.kMap # copy table
|
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
for (vid,key) in db.backend.T.walkKey:
|
2023-08-10 20:01:28 +00:00
|
|
|
if filter.kMap.hasKey vid:
|
|
|
|
let fKey = filter.kMap.getOrVoid vid
|
|
|
|
if fKey.isValid:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid,fKey)
|
2023-08-10 20:01:28 +00:00
|
|
|
filter.kMap.del vid
|
|
|
|
else:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid,key)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
|
|
|
for vid in filter.kMap.keys.toSeq.mapIt(it.uint64).sorted.mapIt(it.VertexID):
|
|
|
|
let key = filter.kMap.getOrVoid vid
|
|
|
|
if key.isValid:
|
2023-12-20 16:19:00 +00:00
|
|
|
yield (vid,key)
|
2023-08-10 20:01:28 +00:00
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
|
|
|
|
iterator walkFilBeImpl*[T](
|
|
|
|
be: T; # Backend descriptor
|
2023-12-20 16:19:00 +00:00
|
|
|
): tuple[qid: QueueID, filter: FilterRef] =
|
2023-08-22 18:44:54 +00:00
|
|
|
## Generic filter iterator
|
2023-09-15 15:23:53 +00:00
|
|
|
when T isnot VoidBackendRef:
|
2023-08-22 18:44:54 +00:00
|
|
|
mixin walkFil
|
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
for (qid,filter) in be.walkFil:
|
|
|
|
yield (qid,filter)
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
|
|
|
|
iterator walkFifoBeImpl*[T](
|
|
|
|
be: T; # Backend descriptor
|
2023-09-15 15:23:53 +00:00
|
|
|
): tuple[qid: QueueID, fid: FilterRef] =
|
2023-09-05 13:57:20 +00:00
|
|
|
## Generic filter iterator walking slots in fifo order. This iterator does
|
|
|
|
## not depend on the backend type but may be type restricted nevertheless.
|
2023-09-15 15:23:53 +00:00
|
|
|
when T isnot VoidBackendRef:
|
2023-09-05 13:57:20 +00:00
|
|
|
proc kvp(chn: int, qid: QueueID): (QueueID,FilterRef) =
|
|
|
|
let cid = QueueID((chn.uint64 shl 62) or qid.uint64)
|
|
|
|
(cid, be.getFilFn(cid).get(otherwise = FilterRef(nil)))
|
|
|
|
|
|
|
|
if not be.isNil:
|
2024-04-19 18:37:27 +00:00
|
|
|
let scd = be.journal
|
2023-09-05 13:57:20 +00:00
|
|
|
if not scd.isNil:
|
|
|
|
for i in 0 ..< scd.state.len:
|
|
|
|
let (left, right) = scd.state[i]
|
|
|
|
if left == 0:
|
|
|
|
discard
|
|
|
|
elif left <= right:
|
|
|
|
for j in right.countDown left:
|
|
|
|
yield kvp(i, j)
|
|
|
|
else:
|
|
|
|
for j in right.countDown QueueID(1):
|
|
|
|
yield kvp(i, j)
|
|
|
|
for j in scd.ctx.q[i].wrap.countDown left:
|
|
|
|
yield kvp(i, j)
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
|
|
|
|
iterator walkPairsImpl*[T](
|
|
|
|
db: AristoDbRef; # Database with top layer & backend filter
|
|
|
|
): tuple[vid: VertexID, vtx: VertexRef] =
|
|
|
|
## Walk over all `(VertexID,VertexRef)` in the database. Note that entries
|
|
|
|
## are unsorted.
|
2023-12-19 12:39:23 +00:00
|
|
|
var seen: HashSet[VertexID]
|
|
|
|
for (vid,vtx) in db.layersWalkVtx seen:
|
2023-09-15 15:23:53 +00:00
|
|
|
if vtx.isValid:
|
|
|
|
yield (vid,vtx)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
for (vid,vtx) in walkVtxBeImpl[T](db):
|
2023-12-19 12:39:23 +00:00
|
|
|
if vid notin seen:
|
2023-09-15 15:23:53 +00:00
|
|
|
yield (vid,vtx)
|
|
|
|
|
2023-09-18 20:20:28 +00:00
|
|
|
iterator replicateImpl*[T](
|
|
|
|
db: AristoDbRef; # Database with top layer & backend filter
|
|
|
|
): tuple[vid: VertexID, key: HashKey, vtx: VertexRef, node: NodeRef] =
|
|
|
|
## Variant of `walkPairsImpl()` for legacy applications.
|
|
|
|
for (vid,vtx) in walkPairsImpl[T](db):
|
|
|
|
let node = block:
|
|
|
|
let rc = vtx.toNode(db)
|
|
|
|
if rc.isOk:
|
|
|
|
rc.value
|
|
|
|
else:
|
|
|
|
NodeRef(nil)
|
|
|
|
yield (vid, db.getKey vid, vtx, node)
|
|
|
|
|
2023-08-10 20:01:28 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|