func (btree *Btree) Print() { if btree.rootblock == 0 { return } b := btree.Cache.GetBuf(btree.rootblock) if b == nil { fmt.Println("btree didn't find root") return } head := (*Head)(unsafe.Pointer(&b.Data[0])) switch head.kind { case LEAF: var leaf Leaf leaf.b = b leaf.head = head leaf.rec = leaf.recSlice() leaf.Print() case BRANCH: var branch Branch branch.b = b branch.head = head branch.twig = *(*[]Twig)(unsafe.Pointer(&b.Data[sizeHead])) branch.Print() default: bug.Pr("Not a leaf or branch", head.kind) } }
func (branch *Branch) store(i Len, key []byte, block int64) { bug.Pr("key", string(key), "block", block) for j := Len(0); j < LenBlockNum; j++ { branch.head.end-- branch.b.Data[branch.head.end] = byte(block) block >>= 8 } branch.storeSlice(key) branch.storeIndex(i, branch.head.end) }
func (btree *Btree) Insert(key, value []byte) { if len(key)+len(value) > MAX_KEY_VAL { bug.Pr("kay/value too big %d", len(key)+len(value)) return } if btree.rootblock == 0 { leaf := btree.newLeaf() btree.rootblock = leaf.head.block leaf.b.Put() } b := btree.Cache.GetBuf(btree.rootblock) if b == nil { fmt.Println("btree didn't find root") return } head := (*Head)(unsafe.Pointer(&b.Data[0])) if head.overflow != 0 { bug.Pr("Need to grow tree") return } switch head.kind { case LEAF: var leaf Leaf leaf.b = b leaf.head = head leaf.rec = leaf.recSlice() leaf.insert(key, value) case BRANCH: var branch Branch branch.b = b branch.head = head branch.twig = *(*[]Twig)(unsafe.Pointer(&b.Data[sizeHead])) branch.insert(key, value) default: bug.Pr("Not a leaf or branch", b.Data[0]) } }
func (leaf *Leaf) grow() *Branch { if !leaf.isSplit() { panic("trying to grow an unsplit leaf") } if leaf.head.block != leaf.btree.root { panic("leaf block not root") } branch := leaf.btree.newBranch() key := leaf.getKey(leaf.head.numrecs - 1) branch.store(0, key, leaf.head.block) branch.head.last = leaf.head.overflow leaf.head.overflow = 0 leaf.btree.root = branch.head.block branch.print(3) bug.Pr(branch.b.Data) return branch }