Skip to content

Commit

Permalink
removed deep clone
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Jul 26, 2024
1 parent 7e6d5f3 commit a7ec115
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 193 deletions.
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Node[T any] interface {
matchPrefix([]byte) bool
getChild(int) Node[T]
setChild(int, Node[T])
clone(bool, bool) Node[T]
clone(bool) Node[T]
setMutateCh(chan struct{})
getKey() []byte
getValue() T
Expand Down
29 changes: 6 additions & 23 deletions node_128.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (n *Node128[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node128[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node128[T]) clone(keepWatch bool) Node[T] {
newNode := &Node128[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
Expand All @@ -105,32 +105,15 @@ func (n *Node128[T]) clone(keepWatch, deep bool) Node[T] {
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
copy(newNode.keys[:], n.keys[:])
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 128; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 128; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 128; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
29 changes: 6 additions & 23 deletions node_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (n *Node16[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node16[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node16[T]) clone(keepWatch bool) Node[T] {
newNode := &Node16[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
Expand All @@ -97,32 +97,15 @@ func (n *Node16[T]) clone(keepWatch, deep bool) Node[T] {
newNode.setMutateCh(n.getMutateCh())
}
newPartial := make([]byte, maxPrefixLen)
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
newNode.setId(n.getId())
copy(newNode.keys[:], n.keys[:])
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 16; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 16; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 16; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
29 changes: 6 additions & 23 deletions node_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,23 @@ func (n *Node256[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node256[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node256[T]) clone(keepWatch bool) Node[T] {
newNode := &Node256[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
}
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
newPartial := make([]byte, maxPrefixLen)
newNode.setId(n.getId())
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 256; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 256; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 256; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
29 changes: 6 additions & 23 deletions node_32.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (n *Node32[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node32[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node32[T]) clone(keepWatch bool) Node[T] {
newNode := &Node32[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
Expand All @@ -96,32 +96,15 @@ func (n *Node32[T]) clone(keepWatch, deep bool) Node[T] {
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
copy(newNode.keys[:], n.keys[:])
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 32; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 32; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 32; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
29 changes: 6 additions & 23 deletions node_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (n *Node4[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node4[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node4[T]) clone(keepWatch bool) Node[T] {
newNode := &Node4[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
Expand All @@ -96,32 +96,15 @@ func (n *Node4[T]) clone(keepWatch, deep bool) Node[T] {
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
copy(newNode.keys[:], n.keys[:])
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 4; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 4; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 4; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
29 changes: 6 additions & 23 deletions node_64.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (n *Node64[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node64[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node64[T]) clone(keepWatch bool) Node[T] {
newNode := &Node64[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
Expand All @@ -96,32 +96,15 @@ func (n *Node64[T]) clone(keepWatch, deep bool) Node[T] {
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
copy(newNode.keys[:], n.keys[:])
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 64; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 64; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 64; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
29 changes: 6 additions & 23 deletions node_8.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (n *Node8[T]) getChild(index int) Node[T] {
return n.children[index]
}

func (n *Node8[T]) clone(keepWatch, deep bool) Node[T] {
func (n *Node8[T]) clone(keepWatch bool) Node[T] {
newNode := &Node8[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
Expand All @@ -96,32 +96,15 @@ func (n *Node8[T]) clone(keepWatch, deep bool) Node[T] {
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
if deep {
if n.getNodeLeaf() != nil {
newNode.setNodeLeaf(n.getNodeLeaf().clone(true, true).(*NodeLeaf[T]))
}
} else {
newNode.setNodeLeaf(n.getNodeLeaf())
}
newNode.setNodeLeaf(n.getNodeLeaf())
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
copy(newNode.keys[:], n.keys[:])
if deep {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 8; i++ {
if cpy[i] == nil {
continue
}
newNode.setChild(i, cpy[i].clone(keepWatch, true))
}
} else {
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 8; i++ {
newNode.setChild(i, cpy[i])
}
cpy := make([]Node[T], len(n.children))
copy(cpy, n.children[:])
for i := 0; i < 8; i++ {
newNode.setChild(i, cpy[i])
}
return newNode
}
Expand Down
2 changes: 1 addition & 1 deletion node_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (n *NodeLeaf[T]) getChild(index int) Node[T] {
return nil
}

func (n *NodeLeaf[T]) clone(keepWatch, deep bool) Node[T] {
func (n *NodeLeaf[T]) clone(keepWatch bool) Node[T] {
n.processRefCount()
newNode := &NodeLeaf[T]{
key: make([]byte, len(n.getKey())),
Expand Down
2 changes: 1 addition & 1 deletion reverse_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func TestReverseIterator_SeekPrefixWatch(t *testing.T) {
ch := it.SeekPrefixWatch(key)

// Change prefix
tx := r.Txn(false)
tx := r.Txn()
tx.TrackMutate(true)
tx.Insert(key, "value")
tx.Commit()
Expand Down
Loading

0 comments on commit a7ec115

Please sign in to comment.