Skip to content

Commit

Permalink
Merge branch 'main' into absl
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed May 28, 2024
2 parents 6e0b00e + fc67a9c commit ad3c788
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 31 deletions.
11 changes: 0 additions & 11 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) {
return nil, zero, false
}

node.incrementLazyRefCount(1)

currentNode := node.(Node[T])

i.pos = currentNode
Expand Down Expand Up @@ -126,7 +124,6 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) {
i.stack = newStack
}
}
node.incrementLazyRefCount(-1)
}
i.pos = nil
return nil, zero, false
Expand Down Expand Up @@ -158,10 +155,8 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{})

for {
// Check if the node matches the prefix
node.incrementLazyRefCount(1)

if node.isLeaf() {
node.incrementLazyRefCount(-1)
return watch
}

Expand All @@ -171,7 +166,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{})
mismatchIdx := prefixMismatch[T](node, prefix, len(prefix), depth)
if mismatchIdx < int(node.getPartialLen()) {
// If there's a mismatch, set the node to nil to break the loop
node.incrementLazyRefCount(-1)
if bytes.HasPrefix(node.getPartial(), prefix[depth:]) {
i.node = node
break
Expand All @@ -187,15 +181,13 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{})
child, _ := findChild[T](node, prefix[depth])
if child == nil {
// If the child node doesn't exist, break the loop
node.incrementLazyRefCount(-1)
node = nil
i.node = nil
break
}

if depth == len(prefix) {
// If the prefix is exhausted, break the loop
node.incrementLazyRefCount(-1)
i.node = nil
break
}
Expand All @@ -204,7 +196,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{})
i.node = node

// Move to the next level in the tree
node.incrementLazyRefCount(-1)
parent = node
watch = parent.getMutateCh()
node = child
Expand Down Expand Up @@ -274,7 +265,6 @@ func (i *Iterator[T]) SeekLowerBound(prefixKey []byte) {
if node == nil {
break
}
node.incrementLazyRefCount(1)

var prefixCmp int
if int(node.getPartialLen()) < len(prefix) {
Expand Down Expand Up @@ -341,7 +331,6 @@ func (i *Iterator[T]) SeekLowerBound(prefixKey []byte) {
}

// Move to the next level in the tree
node.incrementLazyRefCount(-1)
node = node.getChild(idx)
depth++
}
Expand Down
9 changes: 0 additions & 9 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ func (t *RadixTree[T]) Delete(key []byte) (*RadixTree[T], T, bool) {
func (t *RadixTree[T]) iterativeSearch(key []byte) (T, bool, <-chan struct{}) {
var zero T
n := t.root
n.incrementLazyRefCount(1)
watch := n.getMutateCh()
if t.root == nil {
n.incrementLazyRefCount(-1)
return zero, false, watch
}
var child Node[T]
Expand All @@ -178,7 +176,6 @@ func (t *RadixTree[T]) iterativeSearch(key []byte) (T, bool, <-chan struct{}) {
if isLeaf[T](n) {
// Check if the expanded path matches
if leafMatches(n.getKey(), key) == 0 {
n.incrementLazyRefCount(-1)
return n.getValue(), true, watch
}
break
Expand All @@ -188,29 +185,23 @@ func (t *RadixTree[T]) iterativeSearch(key []byte) (T, bool, <-chan struct{}) {
if n.getPartialLen() > 0 {
prefixLen := checkPrefix(n.getPartial(), int(n.getPartialLen()), key, depth)
if prefixLen != min(maxPrefixLen, int(n.getPartialLen())) {
n.incrementLazyRefCount(-1)
return zero, false, watch
}
depth += int(n.getPartialLen())
}

if depth >= len(key) {
n.incrementLazyRefCount(-1)
return zero, false, watch
}

// Recursively search
child, _ = t.findChild(n, key[depth])
if child == nil {
n.incrementLazyRefCount(-1)
return zero, false, watch
}
n.incrementLazyRefCount(-1)
n = child
n.incrementLazyRefCount(1)
depth++
}
n.incrementLazyRefCount(-1)
return zero, false, nil
}

Expand Down
15 changes: 4 additions & 11 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,13 @@ func (t *Txn[T]) recursiveInsert(node Node[T], key []byte, value T, depth int, o
nodeKey := node.getKey()
if len(key) == len(nodeKey) && bytes.Equal(nodeKey, key) {
*old = 1
doClone := node.getRefCount() > 1
if t.trackMutate {
t.trackChannel(node)
}
if doClone {
nl := t.makeLeaf(key, value)
node.incrementLazyRefCount(-1)
val := node.getValue()
return nl, val
}
oldV := node.getValue()
node.setValue(value)
node.processLazyRef()
return node, oldV
nl := t.makeLeaf(key, value)
node.incrementLazyRefCount(-1)
val := node.getValue()
return nl, val
}

// New value, we must split the leaf into a node4
Expand Down

0 comments on commit ad3c788

Please sign in to comment.