convenience methods for node

This commit is contained in:
pippellia-btc
2025-05-30 15:36:38 +02:00
parent aeec9662c8
commit c4d54651f1
2 changed files with 14 additions and 11 deletions

View File

@@ -29,22 +29,25 @@ type Node struct {
Records []Record
}
// Added returns the time a node was added to the database.
func (n *Node) Added() (time.Time, bool) {
for _, rec := range n.Records {
if rec.Kind == Addition {
return rec.Timestamp, true
}
}
return time.Time{}, false
}
// Record contains the timestamp of a node update.
type Record struct {
Kind int // either [Addition], [Promotion] or [Demotion]
Timestamp time.Time
}
func (n *Node) Addition() (time.Time, bool) { return n.find(Addition) }
func (n *Node) Promotion() (time.Time, bool) { return n.find(Promotion) }
func (n *Node) Demotion() (time.Time, bool) { return n.find(Demotion) }
func (n *Node) find(kind int) (time.Time, bool) {
for _, record := range n.Records {
if record.Kind == kind {
return record.Timestamp, true
}
}
return time.Time{}, false
}
// Delta represents updates to apply to a Node.
// Add and Remove contain node IDs to add to or remove from the nodes relationships (e.g., follow list).
type Delta struct {

View File

@@ -135,7 +135,7 @@ func arbiterScan(ctx context.Context, config ArbiterConfig, db redb.RedisDB, sen
case graph.StatusInactive:
// inactive --> active
added, found := node.Added()
added, found := node.Addition()
if !found {
return promoted, demoted, fmt.Errorf("node %s doesn't have an addition record", node.ID)
}