109 lines
3.4 KiB
Nim
109 lines
3.4 KiB
Nim
# Nimbus - Types, data structures and shared utilities used in network sync
|
|
#
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
|
# 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
|
|
std/[algorithm, sequtils, sets, tables],
|
|
results,
|
|
".."/[aristo_desc, aristo_get, aristo_init, aristo_layers, aristo_utils]
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public generic iterators
|
|
# ------------------------------------------------------------------------------
|
|
|
|
iterator walkVtxBeImpl*[T](
|
|
db: AristoDbRef; # Database with optional backend filter
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
|
## Generic iterator
|
|
when T is VoidBackendRef:
|
|
let filter = if db.balancer.isNil: LayerRef() else: db.balancer
|
|
|
|
else:
|
|
mixin walkVtx
|
|
|
|
let filter = LayerRef()
|
|
if not db.balancer.isNil:
|
|
filter.sTab = db.balancer.sTab # copy table
|
|
|
|
for (rvid,vtx) in db.backend.T.walkVtx:
|
|
if filter.sTab.hasKey rvid:
|
|
let fVtx = filter.sTab.getOrVoid rvid
|
|
if fVtx.isValid:
|
|
yield (rvid,fVtx)
|
|
filter.sTab.del rvid
|
|
else:
|
|
yield (rvid,vtx)
|
|
|
|
for rvid in filter.sTab.keys.toSeq.sorted:
|
|
let vtx = filter.sTab.getOrVoid rvid
|
|
if vtx.isValid:
|
|
yield (rvid,vtx)
|
|
|
|
|
|
iterator walkKeyBeImpl*[T](
|
|
db: AristoDbRef; # Database with optional backend filter
|
|
): tuple[rvid: RootedVertexID, key: HashKey] =
|
|
## Generic iterator
|
|
when T is VoidBackendRef:
|
|
let filter = if db.balancer.isNil: LayerRef() else: db.balancer
|
|
|
|
else:
|
|
mixin walkKey
|
|
|
|
let filter = LayerRef()
|
|
if not db.balancer.isNil:
|
|
filter.kMap = db.balancer.kMap # copy table
|
|
|
|
for (rvid,key) in db.backend.T.walkKey:
|
|
if filter.kMap.hasKey rvid:
|
|
let fKey = filter.kMap.getOrVoid rvid
|
|
if fKey.isValid:
|
|
yield (rvid,fKey)
|
|
filter.kMap.del rvid
|
|
else:
|
|
yield (rvid,key)
|
|
|
|
for rvid in filter.kMap.keys.toSeq.sorted:
|
|
let key = filter.kMap.getOrVoid rvid
|
|
if key.isValid:
|
|
yield (rvid,key)
|
|
|
|
|
|
iterator walkPairsImpl*[T](
|
|
db: AristoDbRef; # Database with top layer & backend filter
|
|
): tuple[rvid: RootedVertexID, vtx: VertexRef] =
|
|
## Walk over all `(VertexID,VertexRef)` in the database. Note that entries
|
|
## are unsorted.
|
|
var seen: HashSet[VertexID]
|
|
for (rvid,vtx) in db.layersWalkVtx seen:
|
|
if vtx.isValid:
|
|
yield (rvid,vtx)
|
|
|
|
for (rvid,vtx) in walkVtxBeImpl[T](db):
|
|
if rvid.vid notin seen:
|
|
yield (rvid,vtx)
|
|
|
|
iterator replicateImpl*[T](
|
|
db: AristoDbRef; # Database with top layer & backend filter
|
|
): tuple[rvid: RootedVertexID, key: HashKey, vtx: VertexRef, node: NodeRef] =
|
|
## Variant of `walkPairsImpl()` for legacy applications.
|
|
for (rvid,vtx) in walkPairsImpl[T](db):
|
|
let node = block:
|
|
let rc = vtx.toNode(rvid.root, db)
|
|
if rc.isOk:
|
|
rc.value
|
|
else:
|
|
NodeRef(nil)
|
|
yield (rvid, db.getKey rvid, vtx, node)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|