Example #1
0
func (t *prefixTree) Insert(z *Zp) error {
	_, lookupErr := t.db.Get(z.Bytes(), nil)
	if lookupErr == nil {
		return ErrDuplicateElement(z)
	} else if lookupErr != leveldb.ErrNotFound {
		return lookupErr
	}
	bs := NewZpBitstring(z)
	root, err := t.Root()
	if err != nil {
		return err
	}
	marray, err := recon.AddElementArray(t, z)
	if err != nil {
		return err
	}
	err = root.(*prefixNode).insert(z, marray, bs, 0)
	if err != nil {
		return err
	}
	return t.db.Put(z.Bytes(), []byte{}, nil)
}
Example #2
0
func (n *prefixNode) split(depth int) (err error) {
	splitElements := n.NodeElements
	n.Leaf = false
	n.NodeElements = nil
	err = n.upsertNode()
	if err != nil {
		return err
	}
	// Create child nodes
	numChildren := 1 << uint(n.BitQuantum())
	var children []*prefixNode
	for i := 0; i < numChildren; i++ {
		// Create new empty child node
		child := n.newChildNode(n, i)
		err = child.upsertNode()
		if err != nil {
			return err
		}
		children = append(children, child)
	}
	// Move elements into child nodes
	for _, element := range splitElements {
		z := Zb(P_SKS, element)
		bs := NewZpBitstring(z)
		childIndex := recon.NextChild(n, bs, depth)
		child := children[childIndex]
		marray, err := recon.AddElementArray(child, z)
		if err != nil {
			return err
		}
		err = child.insert(z, marray, bs, depth+1)
		if err != nil {
			return err
		}
	}
	return nil
}