Skip to content

Commit

Permalink
separate out iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Jun 15, 2024
1 parent 6f16901 commit ed321a7
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 77 deletions.
7 changes: 7 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,10 @@ func (t *Txn[T]) removeChild256(n Node[T], c uint8) Node[T] {
}
return n
}

func hasPrefix(key []byte, prefix []byte) bool {
if len(prefix) == 0 {
return true
}
return bytes.HasPrefix(key, prefix)
}
63 changes: 12 additions & 51 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ import (
// down to a specified path. This will iterate over the same values that
// the Node.WalkPath method will.
type Iterator[T any] struct {
path []byte
node Node[T]
stack []Node[T]
depth int
pos Node[T]
lowerBound bool
reverseLowerBound bool
seeKPrefixWatch bool
seenMismatch bool
iterPath []byte
stackItrSet bool
path []byte
node Node[T]
stack []Node[T]
depth int
pos Node[T]
seenMismatch bool
iterPath []byte
stackItrSet bool
}

func (i *Iterator[T]) GetIterPath() []byte {
Expand Down Expand Up @@ -56,29 +53,18 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) {
for itr := int(n4.numChildren) - 1; itr >= 0; itr-- {
i.stack = append(i.stack, n4.children[itr])
}
if i.seeKPrefixWatch && n4L != nil {
if n4L != nil {
return getKey(n4L.key), n4L.value, true
}
if i.lowerBound && n4L != nil {
if bytes.Compare(n4L.key, i.path) >= 0 {
return getKey(n4L.key), n4L.value, true
}
continue
}
case *Node16[T]:
n16 := node.(*Node16[T])
n16L := n16.leaf
for itr := int(n16.numChildren) - 1; itr >= 0; itr-- {
i.stack = append(i.stack, n16.children[itr])
}
if i.seeKPrefixWatch && n16L != nil {
if n16L != nil {
return getKey(n16L.key), n16L.value, true
}
if i.lowerBound && n16L != nil {
if bytes.Compare(n16L.key, i.path) >= 0 {
return getKey(n16.leaf.key), n16.leaf.value, true
}
}
case *Node48[T]:
n48 := node.(*Node48[T])
n48L := n48.leaf
Expand All @@ -93,14 +79,9 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) {
}
i.stack = append(i.stack, nodeCh)
}
if i.seeKPrefixWatch && n48L != nil {
if n48L != nil {
return getKey(n48L.key), n48L.value, true
}
if i.lowerBound && n48L != nil {
if bytes.Compare(n48L.key, i.path) >= 0 {
return getKey(n48L.key), n48L.value, true
}
}
case *Node256[T]:
n256 := node.(*Node256[T])
n256L := n256.leaf
Expand All @@ -111,22 +92,11 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) {
}
i.stack = append(i.stack, nodeCh)
}
if i.seeKPrefixWatch && n256L != nil {
if n256L != nil {
return getKey(n256L.key), n256L.value, true
}
if i.lowerBound && n256L != nil {
if bytes.Compare(n256L.key, i.path) >= 0 {
return getKey(n256L.key), n256L.value, true
}
}
case *NodeLeaf[T]:
leafCh := node.(*NodeLeaf[T])
if i.lowerBound {
if bytes.Compare(getKey(leafCh.key), getKey(i.path)) >= 0 {
return getKey(leafCh.key), leafCh.value, true
}
continue
}
if !leafCh.matchPrefix([]byte(i.Path())) {
continue
}
Expand All @@ -136,16 +106,8 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) {
return nil, zero, false
}

func hasPrefix(key []byte, prefix []byte) bool {
if len(prefix) == 0 {
return true
}
return bytes.HasPrefix(key, prefix)
}

func (i *Iterator[T]) SeekPrefixWatch(prefix []byte) (watch <-chan struct{}) {
// Start from the node
i.seeKPrefixWatch = true

node := i.node

Expand Down Expand Up @@ -285,7 +247,6 @@ func (i *Iterator[T]) SeekLowerBound(prefixKey []byte) {
node := i.node

i.stack = []Node[T]{}
i.lowerBound = true

if len(prefixKey) == 0 {
i.stack = []Node[T]{node}
Expand Down
4 changes: 2 additions & 2 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestIterateLowerBoundFuzz(t *testing.T) {
t.Log("NewKey: ", newKey, "SearchKey: ", searchKey)

// Now iterate the tree from searchKey to the end
it := r.Root().Iterator()
it := r.Root().LowerBoundIterator()
var result []string
it.SeekLowerBound([]byte(searchKey))
for {
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestIterateLowerBound(t *testing.T) {
}
// Get and seek iterator
root := r.root
iter := root.Iterator()
iter := root.LowerBoundIterator()
iter.SeekLowerBound([]byte(test.search))

// Consume all the keys
Expand Down
Loading

0 comments on commit ed321a7

Please sign in to comment.