Jordan Hrycaj 5ac362fe6f
Aristo and kvt balancer management update (#2504)
* Aristo: Merge `delta_siblings` module into `deltaPersistent()`

* Aristo: Add `isEmpty()` for canonical checking whether a layer is empty

* Aristo: Merge `LayerDeltaRef` into `LayerObj`

why:
  No need to maintain nested object refs anymore. Previously the
 `LayerDeltaRef` object had a companion `LayerFinalRef` which held
  non-delta layer information.

* Kvt: Merge `LayerDeltaRef` into `LayerRef`

why:
  No need to maintain nested object refs (as with `Aristo`)

* Kvt: Re-write balancer logic similar to `Aristo`

why:
  Although `Kvt` was a cheap copy of `Aristo` it sort of got out of
  sync and the balancer code was wrong.

* Update iterator over forked peers

why:
  Yield additional field `isLast` indicating that the last iteration
  cycle was approached.

* Optimise balancer calculation.

why:
  One can often avoid providing a new object containing the merge of two
  layers for the balancer. This avoids copying tables. In some cases this
  is replaced by `hasKey()` look ups though. One uses one of the two
  to combine and merges the other into the first.

  Of course, this needs some checks for making sure that none of the
  components to merge is eventually shared with something else.

* Fix copyright year
2024-07-18 21:32:32 +00:00

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
# ------------------------------------------------------------------------------