-
Notifications
You must be signed in to change notification settings - Fork 107
/
debug.go
51 lines (43 loc) · 1.33 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mkvs
import (
"context"
"fmt"
"io"
"strings"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/node"
)
// Implements Tree.
func (t *tree) DumpLocal(ctx context.Context, w io.Writer, maxDepth node.Depth) {
t.doDumpLocal(ctx, w, t.cache.pendingRoot, 0, maxDepth)
}
func (t *tree) doDumpLocal(ctx context.Context, w io.Writer, ptr *node.Pointer, depth, maxDepth node.Depth) {
prefix := strings.Repeat(" ", int(depth)*2)
if ptr == nil {
fmt.Fprint(w, prefix+"<nil>")
return
}
if maxDepth > 0 && depth > maxDepth {
fmt.Fprint(w, prefix+"<...>")
return
}
nd := ptr.Node
switch n := nd.(type) {
case nil:
fmt.Fprintf(w, prefix+"<nil> [%v/%s]", ptr.Clean, ptr.Hash)
case *node.InternalNode:
fmt.Fprintf(w, prefix+"* [%v/%q(%d)/%s]: {\n", n.Clean, n.Label, n.LabelBitLength, n.Hash)
// NB: depth+1 for LeafNode is purely for nicer indents. LeafNode should have the same depth as parent though.
t.doDumpLocal(ctx, w, n.LeafNode, depth+1, maxDepth)
fmt.Fprintln(w, ",")
t.doDumpLocal(ctx, w, n.Left, depth+1, maxDepth)
fmt.Fprintln(w, ",")
t.doDumpLocal(ctx, w, n.Right, depth+1, maxDepth)
fmt.Fprintln(w, "")
fmt.Fprint(w, prefix+"}")
case *node.LeafNode:
value := n.Value
fmt.Fprintf(w, "%s- %s -> %v [%v/%s]", prefix, n.Key, value, n.Clean, n.Hash)
default:
fmt.Fprintf(w, prefix+"<UNKNOWN>")
}
}