func (btree *Btree) Insert(key, val []byte) { if btree.root == 0 { leaf := btree.newLeaf() btree.root = leaf.head.block leaf.b.Put() } b := btree.cache.GetBuf(btree.root) if b == nil { bug.Fatal("Couldn't find root") } head := (*head)(unsafe.Pointer(&b.Data[0])) switch head.kind { case BRANCH: branch := Branch{b, head, btree} branch.insert(key, val) if branch.isSplit() { branch := branch.grow() branch.insert(key, val) } else { branch.insert(key, val) } case LEAF: leaf := Leaf{b, head, btree} if leaf.isSplit() { branch := leaf.grow() branch.insert(key, val) } else { leaf.insert(key, val) } default: bug.Fatal("Btree bad root node") } }
func (btree *Btree) Find(key []byte) []byte { if btree.root == 0 { return nil } b := btree.cache.GetBuf(btree.root) if b == nil { bug.Fatal("Couldn't find root") } head := (*head)(unsafe.Pointer(&b.Data[0])) switch head.kind { case BRANCH: case LEAF: return (&Leaf{b, head, btree}).find(key) default: bug.Fatal("Btree bad root node") } return nil }
func (leaf *Leaf) storeRec(i Len, rec []byte) { n := Len(len(rec)) if n >= leaf.head.free { bug.Fatal("Leaf full") } leaf.head.free -= n leaf.head.end -= n copy(leaf.b[leaf.head.end:], rec) leaf.storeIndex(i, leaf.head.end) }
func (leaf *Leaf) storeSlice(s []byte) { n := Len(len(s)) if leaf.head.free < n+LenLen { bug.Fatal("No free space") } leaf.head.free -= n + LenLen leaf.head.end -= n + LenLen offset := leaf.storeLen(leaf.head.end, n) copy(leaf.b[offset:], s) }
func (branch *Branch) storeRec(i Len, rec []byte) { n := Len(len(rec)) if n >= branch.head.available { bug.Fatal("branch full") } branch.head.available -= n branch.head.end -= n copy(branch.b.Data[branch.head.end:], rec) branch.storeIndex(i, branch.head.end) }
func (branch *Branch) storeSlice(s []byte) { n := Len(len(s)) if branch.head.available < n+LenLen { bug.Fatal("No available space") } branch.head.available -= n + LenLen branch.head.end -= n + LenLen offset := branch.storeLen(branch.head.end, n) copy(branch.b.Data[offset:], s) }
func (leaf *Leaf) storeSlice(s []byte) { n := Len(len(s)) if leaf.head.available < n+LenLen { bug.Fatal("No available space") } leaf.head.available -= n + LenLen leaf.head.end -= n + LenLen offset := leaf.storeLen(leaf.head.end, n) copy(leaf.b.Data[offset:], s) }
func (btree *Btree) Print() { fmt.Println("Print B-tree") if btree.root == 0 { fmt.Println("Empty B-tree") return } b := btree.cache.GetBuf(btree.root) if b == nil { bug.Fatal("Couldn't find root") } head := (*head)(unsafe.Pointer(&b.Data[0])) switch head.kind { case BRANCH: (&Branch{b, head, btree}).print(0) case LEAF: (&Leaf{b, head, btree}).print(0) default: bug.Fatal("Btree bad root node") } }
func (leaf *Leaf) storeIndex(i, offset Len) { if i > leaf.head.numrecs { bug.Fatal("numrecs=%d i=%d", leaf.head.numrecs, i) return } start := leaf.ithRec(i) if i < leaf.head.numrecs { end := leaf.ithRec(leaf.head.numrecs) copy(leaf.b[start+LenLen:end+LenLen], leaf.b[start:end]) } leaf.storeLen(start, offset) leaf.head.numrecs++ leaf.head.free -= LenLen }
func (branch *Branch) storeIndex(i, offset Len) { if i > branch.head.numrecs { bug.Fatal("numrecs =", branch.head.numrecs, "i =", i) return } start := branch.ithRec(i) if i < branch.head.numrecs { end := branch.ithRec(branch.head.numrecs) copy(branch.b.Data[start+LenLen:end+LenLen], branch.b.Data[start:end]) } branch.storeLen(start, offset) branch.head.numrecs++ branch.head.available -= LenLen }
func (leaf *Leaf) ithRec(i Len) Len { if i > leaf.head.numrecs { bug.Fatal("i=%d numrecs=%d", i, leaf.head.numrecs) } return LenHead + LenLen*i }
func (branch *Branch) ithRec(i Len) Len { if i > branch.head.numrecs { bug.Fatal("numrecs =", branch.head.numrecs, "i =", i) } return LenHead + LenLen*i }
func (leaf *Leaf) ithRec(i Len) Len { if i > leaf.head.numrecs { bug.Fatal("numrecs =", leaf.head.numrecs, "i =", i) } return LenHead + LenLen*i }