Skip to content

Commit

Permalink
Dev to Main (#47)
Browse files Browse the repository at this point in the history
* fix go mod

* Dev to Main (#39)

* try fixing race

* Revert "try fixing race"

This reverts commit 02ff901.

* using lock for set child and get child

* using locks only for set child and get child

* fix nil mu

* fix delete

* using locks to set attribute of node

* fix memdb tests

* tracking channel of child changes

* removed process lazy redundant

* fix nil check

* Fix get watch bug

* go fmt

* some minor fixes

* merge main

* fix go mod

* fix go mod

* fmt

* Dev to Main (#40)

* try fixing race

* Revert "try fixing race"

This reverts commit 02ff901.

* using lock for set child and get child

* using locks only for set child and get child

* fix nil mu

* fix delete

* using locks to set attribute of node

* fix memdb tests

* tracking channel of child changes

* removed process lazy redundant

* fix nil check

* Fix get watch bug

* go fmt

* some minor fixes

* merge main

* fix go mod

* fix go mod

* some minor fixes

* some minor fixes

* fmt

* Dev to main (#41)

* try fixing race

* Revert "try fixing race"

This reverts commit 02ff901.

* using lock for set child and get child

* using locks only for set child and get child

* fix nil mu

* fix delete

* using locks to set attribute of node

* fix memdb tests

* tracking channel of child changes

* removed process lazy redundant

* fix nil check

* Fix get watch bug

* go fmt

* some minor fixes

* merge main

* fix go mod

* fix go mod

* some minor fixes

* some minor fixes

* fmt

* some fixes

* Dev to Main (#42)

* try fixing race

* Revert "try fixing race"

This reverts commit 02ff901.

* using lock for set child and get child

* using locks only for set child and get child

* fix nil mu

* fix delete

* using locks to set attribute of node

* fix memdb tests

* tracking channel of child changes

* removed process lazy redundant

* fix nil check

* Fix get watch bug

* go fmt

* some minor fixes

* merge main

* fix go mod

* fix go mod

* some minor fixes

* some minor fixes

* fmt

* some fixes

* fix seek prefix watch

* SeekPrefix Watch Fix (#43)

* try fixing race

* Revert "try fixing race"

This reverts commit 02ff901.

* using lock for set child and get child

* using locks only for set child and get child

* fix nil mu

* fix delete

* using locks to set attribute of node

* fix memdb tests

* tracking channel of child changes

* removed process lazy redundant

* fix nil check

* Fix get watch bug

* go fmt

* some minor fixes

* merge main

* fix go mod

* fix go mod

* some minor fixes

* some minor fixes

* fmt

* some fixes

* fix seek prefix watch

* fix ref count

* fix removed unused var

* bug fix
  • Loading branch information
absolutelightning committed May 29, 2024
1 parent 597224a commit 77d7658
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
5 changes: 1 addition & 4 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{})
i.stack = []Node[T]{node}
i.node = node

parent := node

for {
// Check if the node matches the prefix

Expand Down Expand Up @@ -196,8 +194,7 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{})
i.node = node

// Move to the next level in the tree
parent = node
watch = parent.getMutateCh()
watch = node.getMutateCh()
node = child
depth++
}
Expand Down
3 changes: 2 additions & 1 deletion tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func TestARTree_InsertAndSearchWords(t *testing.T) {
// optionally, resize scanner's capacity for lines over 64K, see next example
lineNumber := 1
for scanner.Scan() {
art, _, _ = art.Insert(scanner.Bytes(), lineNumber)
line := scanner.Text()
art, _, _ = art.Insert([]byte(line), lineNumber)
lineNumber += 1
lines = append(lines, scanner.Text())
}
Expand Down
49 changes: 32 additions & 17 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ func (t *Txn[T]) recursiveInsert(node Node[T], key []byte, value T, depth int, o

node.processLazyRef()

oldRef := node

if node.isLeaf() {
// This means node is nil
if node.getKeyLen() == 0 {
nl := t.makeLeaf(key, value)
nl.processLazyRef()
node.incrementLazyRefCount(-1)
t.trackChannel(node)
return nl, zero
}
Expand All @@ -146,7 +149,14 @@ func (t *Txn[T]) recursiveInsert(node Node[T], key []byte, value T, depth int, o
doClone := node.getRefCount() > 1

if doClone {
oldRef.incrementLazyRefCount(-1)
node = t.writeNode(node)
} else {
defer func() {
oldRef.incrementLazyRefCount(-1)
oldRef.processLazyRef()
}()
node.incrementLazyRefCount(1)
}

// Determine longest prefix
Expand All @@ -169,13 +179,19 @@ func (t *Txn[T]) recursiveInsert(node Node[T], key []byte, value T, depth int, o
return newNode, zero
}

oldRef := node
oldRef.processLazyRef()
// Check if given node has a prefix
if node.getPartialLen() > 0 {
doClone := node.getRefCount() > 1
if doClone {
oldRef.incrementLazyRefCount(-1)
node = t.writeNode(node)
} else {
defer func() {
oldRef.incrementLazyRefCount(-1)
oldRef.processLazyRef()
}()
node.incrementLazyRefCount(1)
}
// Determine if the prefixes differ, since we need to split
prefixDiff := prefixMismatch[T](node, key, len(key), depth)
Expand Down Expand Up @@ -243,7 +259,14 @@ func (t *Txn[T]) recursiveInsert(node Node[T], key []byte, value T, depth int, o

doClone := node.getRefCount() > 1
if doClone {
oldRef.incrementLazyRefCount(-1)
node = t.writeNode(node)
} else {
defer func() {
oldRef.incrementLazyRefCount(-1)
oldRef.processLazyRef()
}()
node.incrementLazyRefCount(1)
}

if depth < len(key) {
Expand Down Expand Up @@ -306,8 +329,6 @@ func (t *Txn[T]) recursiveDelete(node Node[T], key []byte, depth int) (Node[T],
return nil, nil
}

doClone := node.getRefCount() > 1

node.processLazyRef()

// Handle hitting a leaf node
Expand All @@ -319,7 +340,7 @@ func (t *Txn[T]) recursiveDelete(node Node[T], key []byte, depth int) (Node[T],
return node, nil
}

node.incrementLazyRefCount(1)
oldRef := node

// Bail if the prefix does not match
if node.getPartialLen() > 0 {
Expand All @@ -333,40 +354,34 @@ func (t *Txn[T]) recursiveDelete(node Node[T], key []byte, depth int) (Node[T],
// Find child node
child, idx := t.findChild(node, key[depth])
if child == nil {
node.incrementLazyRefCount(-1)
return nil, nil
}

oldRef := node

// Recurse
newChild, val := t.recursiveDelete(child, key, depth+1)

if newChild != child {
doClone := node.getRefCount() > 1

if doClone {
oldRef.incrementLazyRefCount(-1)
node = t.writeNode(node)
} else {
defer func() {
oldRef.incrementLazyRefCount(-1)
}()
node.incrementLazyRefCount(1)
t.trackChannel(oldRef)
}
node.setChild(idx, newChild)
}

if newChild == nil {
t.trackChannel(child)

if doClone {
node = t.writeNode(node)
} else {
t.trackChannel(oldRef)
}
if doClone {
oldRef.incrementLazyRefCount(-1)
}
node = t.removeChild(node, key[depth])
}

oldRef.processLazyRef()
oldRef.incrementLazyRefCount(-1)
return node, val
}

Expand Down

0 comments on commit 77d7658

Please sign in to comment.