renamed to simple walker

This commit is contained in:
pippellia-btc
2025-06-02 11:43:04 +02:00
parent 5ef3dd08dd
commit 7d82354540
3 changed files with 69 additions and 62 deletions

View File

@@ -11,23 +11,23 @@ type Walker interface {
Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error)
} }
type MapWalker struct { type SimpleWalker struct {
follows map[graph.ID][]graph.ID follows map[graph.ID][]graph.ID
} }
func NewWalker(m map[graph.ID][]graph.ID) *MapWalker { func NewSimpleWalker(m map[graph.ID][]graph.ID) *SimpleWalker {
return &MapWalker{follows: m} return &SimpleWalker{follows: m}
} }
func (m *MapWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) { func (w *SimpleWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) {
return m.follows[node], nil return w.follows[node], nil
} }
func (m *MapWalker) Update(ctx context.Context, delta graph.Delta) { func (w *SimpleWalker) Update(ctx context.Context, delta graph.Delta) {
m.follows[delta.Node] = delta.New() w.follows[delta.Node] = delta.New()
} }
func NewCyclicWalker(n int) *MapWalker { func NewCyclicWalker(n int) *SimpleWalker {
follows := make(map[graph.ID][]graph.ID, n) follows := make(map[graph.ID][]graph.ID, n)
for i := range n { for i := range n {
node := graph.ID(strconv.Itoa(i)) node := graph.ID(strconv.Itoa(i))
@@ -35,18 +35,18 @@ func NewCyclicWalker(n int) *MapWalker {
follows[node] = []graph.ID{next} follows[node] = []graph.ID{next}
} }
return &MapWalker{follows: follows} return &SimpleWalker{follows: follows}
} }
// CachedWalker is a walker with optional fallback that stores follow relationships // CachedWalker is a walker with optional fallback that stores follow relationships
// in a compact format (uint32) for reduced memory footprint. // in a compact format (uint32) for reduced memory footprint.
type cachedWalker struct { type CachedWalker struct {
follows map[graph.ID][]graph.ID follows map[graph.ID][]graph.ID
fallback Walker fallback Walker
} }
func NewCachedWalker(nodes []graph.ID, follows [][]graph.ID, fallback Walker) *cachedWalker { func NewCachedWalker(nodes []graph.ID, follows [][]graph.ID, fallback Walker) *CachedWalker {
w := cachedWalker{ w := CachedWalker{
follows: make(map[graph.ID][]graph.ID, len(nodes)), follows: make(map[graph.ID][]graph.ID, len(nodes)),
fallback: fallback, fallback: fallback,
} }
@@ -58,7 +58,7 @@ func NewCachedWalker(nodes []graph.ID, follows [][]graph.ID, fallback Walker) *c
return &w return &w
} }
func (w *cachedWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) { func (w *CachedWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) {
follows, exists := w.follows[node] follows, exists := w.follows[node]
if !exists { if !exists {
var err error var err error

View File

@@ -102,12 +102,13 @@ func TestToRemove(t *testing.T) {
} }
func TestUpdateRemove(t *testing.T) { func TestUpdateRemove(t *testing.T) {
walker := NewWalker(map[graph.ID][]graph.ID{ walker := NewSimpleWalker(
"0": {"3"}, map[graph.ID][]graph.ID{
"1": {"2"}, "0": {"3"},
"2": {"0"}, "1": {"2"},
"3": {"2"}, "2": {"0"},
}) "3": {"2"},
})
delta := graph.Delta{ delta := graph.Delta{
Node: "0", Node: "0",

View File

@@ -8,7 +8,7 @@ import (
) )
type Setup struct { type Setup struct {
walker *walks.MapWalker walker *walks.SimpleWalker
deltas []graph.Delta deltas []graph.Delta
nodes []graph.ID nodes []graph.ID
@@ -54,7 +54,7 @@ func Dandlings(n int) Setup {
} }
return Setup{ return Setup{
walker: walks.NewWalker(make(map[graph.ID][]graph.ID)), walker: walks.NewSimpleWalker(make(map[graph.ID][]graph.ID)),
deltas: deltas, deltas: deltas,
nodes: nodes, nodes: nodes,
global: global, global: global,
@@ -93,13 +93,14 @@ func Cyclic(n int) Setup {
var Triangle = Cyclic(3) var Triangle = Cyclic(3)
var Acyclic1 = Setup{ var Acyclic1 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(
"0": {"1", "2"}, map[graph.ID][]graph.ID{
"1": {}, "0": {"1", "2"},
"2": {"3"}, "1": {},
"3": {"1"}, "2": {"3"},
"4": {}, "3": {"1"},
}), "4": {},
}),
deltas: []graph.Delta{ deltas: []graph.Delta{
// removals // removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}}, {Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -136,14 +137,15 @@ var Acyclic1 = Setup{
} }
var Acyclic2 = Setup{ var Acyclic2 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(
"0": {"1", "2"}, map[graph.ID][]graph.ID{
"1": {}, "0": {"1", "2"},
"2": {}, "1": {},
"3": {}, "2": {},
"4": {"3", "5"}, "3": {},
"5": {}, "4": {"3", "5"},
}), "5": {},
}),
deltas: []graph.Delta{ deltas: []graph.Delta{
// removals // removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}}, {Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -167,12 +169,13 @@ var Acyclic2 = Setup{
} }
var Acyclic3 = Setup{ var Acyclic3 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(
"0": {"1", "2"}, map[graph.ID][]graph.ID{
"1": {}, "0": {"1", "2"},
"2": {}, "1": {},
"3": {"1", "2"}, "2": {},
}), "3": {"1", "2"},
}),
deltas: []graph.Delta{ deltas: []graph.Delta{
// removals // removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}}, {Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -190,12 +193,13 @@ var Acyclic3 = Setup{
} }
var Acyclic4 = Setup{ var Acyclic4 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(
"0": {"1", "2"}, map[graph.ID][]graph.ID{
"1": {}, "0": {"1", "2"},
"2": {}, "1": {},
"3": {"1"}, "2": {},
}), "3": {"1"},
}),
deltas: []graph.Delta{ deltas: []graph.Delta{
// removals // removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}}, {Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -218,12 +222,13 @@ var Acyclic4 = Setup{
} }
var Acyclic5 = Setup{ var Acyclic5 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(
"0": {"3"}, map[graph.ID][]graph.ID{
"1": {"0"}, "0": {"3"},
"2": {}, "1": {"0"},
"3": {"2"}, "2": {},
}), "3": {"2"},
}),
deltas: []graph.Delta{ deltas: []graph.Delta{
// removals // removals
{Node: "0", Remove: []graph.ID{"3"}}, {Node: "0", Remove: []graph.ID{"3"}},
@@ -246,13 +251,14 @@ var Acyclic5 = Setup{
} }
var Acyclic6 = Setup{ var Acyclic6 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(
"0": {"4"}, map[graph.ID][]graph.ID{
"1": {"0"}, "0": {"4"},
"2": {}, "1": {"0"},
"3": {"1", "4"}, "2": {},
"4": {"2"}, "3": {"1", "4"},
}), "4": {"2"},
}),
deltas: []graph.Delta{ deltas: []graph.Delta{
// removals // removals
{Node: "0", Remove: []graph.ID{"4"}}, {Node: "0", Remove: []graph.ID{"4"}},
@@ -290,7 +296,7 @@ var Acyclic6 = Setup{
} }
var Acyclic7 = Setup{ var Acyclic7 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{ walker: walks.NewSimpleWalker(map[graph.ID][]graph.ID{
"0": {"1", "2", "3"}, "0": {"1", "2", "3"},
"1": {}, "1": {},
"2": {}, "2": {},