Skip to content

Commit ef9df7f

Browse files
committed
chore: add printHamt() for debugging
1 parent 58f187c commit ef9df7f

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

hamt_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ func addAndRemoveKeys(t *testing.T, keys []string, extraKeys []string, options .
129129
}
130130
}
131131

132-
fmt.Println("start flush")
133-
bef := time.Now()
132+
// fmt.Println("start flush")
133+
// bef := time.Now()
134134
if err := begn.Flush(ctx); err != nil {
135135
t.Fatal(err)
136136
}
137-
fmt.Println("flush took: ", time.Since(bef))
137+
// fmt.Println("flush took: ", time.Since(bef))
138138
c, err := cs.Put(ctx, begn)
139139
if err != nil {
140140
t.Fatal(err)
@@ -158,6 +158,8 @@ func addAndRemoveKeys(t *testing.T, keys []string, extraKeys []string, options .
158158
}
159159
}
160160

161+
printHamt(begn)
162+
161163
// create second hamt by adding and deleting the extra keys
162164
for i := 0; i < len(extraKeys); i++ {
163165
begn.Set(ctx, extraKeys[i], randValue())
@@ -188,6 +190,37 @@ func addAndRemoveKeys(t *testing.T, keys []string, extraKeys []string, options .
188190
}
189191
}
190192

193+
func printHamt(hamt *Node) {
194+
ctx := context.Background()
195+
196+
var printNode func(n *Node, depth int)
197+
198+
printNode = func(n *Node, depth int) {
199+
c, err := n.store.Put(ctx, n)
200+
if err != nil {
201+
panic(err)
202+
}
203+
fmt.Printf("%s‣ %v:\n", strings.Repeat(" ", depth), c)
204+
for _, p := range n.Pointers {
205+
if p.isShard() {
206+
child, err := p.loadChild(ctx, n.store, n.bitWidth, n.hash)
207+
if err != nil {
208+
panic(err)
209+
}
210+
printNode(child, depth+1)
211+
} else {
212+
var keys []string
213+
for _, pt := range p.KVs {
214+
keys = append(keys, string(pt.Key))
215+
}
216+
fmt.Printf("%s⇶ [ %s ]\n", strings.Repeat(" ", depth+1), strings.Join(keys, ", "))
217+
}
218+
}
219+
}
220+
221+
printNode(hamt, 0)
222+
}
223+
191224
func dotGraphRec(n *Node, name *int) {
192225
cur := *name
193226
for _, p := range n.Pointers {

0 commit comments

Comments
 (0)