// TestKeyMatch tests key consistency func (s *PtreeSuite) TestKeyMatch(c *gc.C) { tree1 := new(MemPrefixTree) tree1.Init() for i := 1; i < 100; i++ { tree1.Insert(cf.Zi(cf.P_SKS, 65537*i+i)) } // Some extra samples for i := 1; i < 50; i++ { tree1.Insert(cf.Zi(cf.P_SKS, 68111*i)) } tree2 := new(MemPrefixTree) tree2.Init() for i := 1; i < 100; i++ { tree2.Insert(cf.Zi(cf.P_SKS, 65537*i)) } // One extra sample for i := 1; i < 20; i++ { tree2.Insert(cf.Zi(cf.P_SKS, 70001*i)) } for i := 1; i < 100; i++ { zi := cf.Zi(cf.P_SKS, 65537*i) bs := cf.NewZpBitstring(zi) node1, err := Find(tree1, zi) c.Assert(err, gc.IsNil) node2, err := Find(tree2, zi) c.Assert(err, gc.IsNil) c.Logf("node1=%v, node2=%v (%b) full=%v", node1.Key(), node2.Key(), zi.Int64(), bs) // If keys are different, one must prefix the other. c.Assert(strings.HasPrefix(node1.Key().String(), node2.Key().String()) || strings.HasPrefix(node2.Key().String(), node1.Key().String()), gc.Equals, true) } }
func (n *MemPrefixNode) split(depth int) error { // Create child nodes numChildren := 1 << uint(n.BitQuantum) for i := 0; i < numChildren; i++ { child := &MemPrefixNode{parent: n} child.key = i child.init(n.MemPrefixTree) n.children = append(n.children, child) } // Move elements into child nodes for _, element := range n.elements { bs := cf.NewZpBitstring(element) childIndex := NextChild(n, bs, depth) child := n.children[childIndex] marray, err := AddElementArray(n.MemPrefixTree, element) if err != nil { return err } err = child.insert(element, marray, bs, depth+1) if err != nil { return err } } n.elements = nil return nil }
// Remove a Z/Zp integer from the prefix tree func (t *MemPrefixTree) Remove(z *cf.Zp) error { bs := cf.NewZpBitstring(z) err := t.root.remove(z, DelElementArray(t, z), bs, 0) if err != nil { return err } t.allElements.Remove(z) return nil }
// Insert a Z/Zp integer into the prefix tree func (t *MemPrefixTree) Insert(z *cf.Zp) error { if t.allElements.Has(z) { return fmt.Errorf("duplicate: %q", z.String()) } bs := cf.NewZpBitstring(z) marray, err := AddElementArray(t, z) if err != nil { return err } err = t.root.insert(z, marray, bs, 0) if err != nil { return err } t.allElements.Add(z) return nil }
func (t *prefixTree) Remove(z *cf.Zp) error { _, lookupErr := t.db.Get(z.Bytes(), nil) if lookupErr != nil { return lookupErr } bs := cf.NewZpBitstring(z) root, err := t.Root() if err != nil { return err } marray := recon.DelElementArray(t, z) err = root.(*prefixNode).remove(z, marray, bs, 0) if err != nil { return err } return t.db.Delete(z.Bytes(), nil) }
func (t *prefixTree) Insert(z *cf.Zp) error { _, lookupErr := t.db.Get(z.Bytes(), nil) if lookupErr == nil { return ErrDuplicateElement(z) } else if lookupErr != leveldb.ErrNotFound { return lookupErr } bs := cf.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) }
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 := cf.Zb(cf.P_SKS, element) bs := cf.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 }
func Find(t PrefixTree, z *cf.Zp) (PrefixNode, error) { bs := cf.NewZpBitstring(z) return t.Node(bs) }