aboutsummaryrefslogtreecommitdiff
path: root/node.go
diff options
context:
space:
mode:
Diffstat (limited to 'node.go')
-rw-r--r--node.go34
1 files changed, 32 insertions, 2 deletions
diff --git a/node.go b/node.go
index fd737ea..72145c4 100644
--- a/node.go
+++ b/node.go
@@ -2,6 +2,8 @@ package bolt
import (
"bytes"
+ "fmt"
+ "io"
"sort"
"unsafe"
)
@@ -191,6 +193,8 @@ func (n *node) write(p *page) {
copy(b[0:], item.value)
b = b[len(item.value):]
}
+
+ // DEBUG ONLY: n.dump(os.Stderr)
}
// split breaks up a node into smaller nodes, if appropriate.
@@ -261,6 +265,7 @@ func (n *node) spill() error {
// Add node's page to the freelist if it's not new.
if n.pgid > 0 {
tx.db.freelist.free(tx.id(), tx.page(n.pgid))
+ n.pgid = 0
}
// Spill nodes by deepest first.
@@ -273,8 +278,8 @@ func (n *node) spill() error {
}
// Write the node.
- node.write(p)
node.pgid = p.id
+ node.write(p)
// Insert into parent inodes.
if node.parent != nil {
@@ -302,8 +307,8 @@ func (n *node) spill() error {
}
// Write the new root.
- parent.write(p)
parent.pgid = p.id
+ parent.write(p)
}
return nil
@@ -477,6 +482,31 @@ func (n *node) free() {
}
}
+// dump writes the contents of the node for debugging purposes.
+func (n *node) dump(w io.Writer) {
+ // Write node header.
+ var typ = "branch"
+ if n.isLeaf {
+ typ = "leaf"
+ }
+ fmt.Fprintf(w, "[NODE %d {type=%s count=%d}]\n", n.pgid, typ, len(n.inodes))
+
+ // Write out abbreviated version of each item.
+ for _, item := range n.inodes {
+ if n.isLeaf {
+ if item.flags&bucketLeafFlag != 0 {
+ bucket := (*bucket)(unsafe.Pointer(&item.value[0]))
+ fmt.Fprintf(w, "[L \"%s\" -> (bucket root=%d)]\n", item.key, bucket.root)
+ } else {
+ fmt.Fprintf(w, "[L \"%s\" -> \"%s\"]\n", item.key, item.value)
+ }
+ } else {
+ fmt.Fprintf(w, "[B \"%s\" -> pgid=%d]\n", item.key, item.pgid)
+ }
+ }
+ fmt.Fprint(w, "\n")
+}
+
// inode represents an internal node inside of a node.
// It can be used to point to elements in a page or point
// to an element which hasn't been added to a page yet.