Skip to content

Commit

Permalink
Dev to Main (#18)
Browse files Browse the repository at this point in the history
* reverse iterator init

* some fixes

* fix reverse iterator seaklowerbound

* fix tests

* fix track channels

* longest prefix on txn

* some fixes

* revisit

* todo revisit

* major code refactor

* some minor fixes

* add lru

* fix bugs

* added walk func

* add more tests

* some prog

* some progress track mutate

* some progress

* fix tests

* some optimizations

* some memory optimizations

* fix tests

* minor fixes

* some minor fixes

* some fixes

* fix delete prefix

* some fixes

* init stack

* fix seek lower bound

* clone

* fix watch

* fix clone bug

* some fixes for go mem db

* merge absl

* Fix race

* fix go mod

* fix race in add child

* fix last memdb test

* clone

* fix memdb last tests

* fix race

* Fix race without impacting memory and performance. Used node level lock

* Add fix in seeklowerbound

* minor check

* fix race

* Make immutable and fix race

* removed unwanted locks

* Revert "removed unwanted locks"

This reverts commit daa5671.

* fix go mod

* returning  node clone

* wip

* some fixes

* fix go mod
  • Loading branch information
absolutelightning authored May 23, 2024
1 parent 08d2267 commit e3ae4b9
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 223 deletions.
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (t *Txn[T]) removeChild4(n *Node4[T], c byte) Node[T] {
// Concatenate the prefixes
prefix := int(n.getPartialLen())
if prefix < maxPrefixLen {
n.partial[prefix] = n.keys[0]
n.getPartial()[prefix] = n.keys[0]
prefix++
}
if prefix < maxPrefixLen {
Expand Down
5 changes: 2 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package adaptive

import "sync"

type Node[T any] interface {
getId() uint64
setId(uint64)
getPartialLen() uint32
setPartialLen(uint32)
getArtNodeType() nodeType
Expand All @@ -17,7 +17,6 @@ type Node[T any] interface {
matchPrefix([]byte) bool
getChild(int) Node[T]
setChild(int, Node[T])
setMutex(*sync.RWMutex)
clone(bool, bool) Node[T]
getKey() []byte
getValue() T
Expand Down
32 changes: 12 additions & 20 deletions node_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@ package adaptive
import (
"bytes"
"sort"
"sync"
)

type Node16[T any] struct {
id uint64
partialLen uint32
numChildren uint8
partial []byte
keys [16]byte
children [16]Node[T]
mutateCh chan struct{}
mu *sync.RWMutex
}

func (n *Node16[T]) getId() uint64 {
return n.id
}

func (n *Node16[T]) setId(id uint64) {
n.id = id
}

func (n *Node16[T]) getPartialLen() uint32 {
n.mu.Lock()
defer n.mu.Unlock()
return n.partialLen
}

func (n *Node16[T]) setPartialLen(partialLen uint32) {
n.mu.Lock()
defer n.mu.Unlock()
n.partialLen = partialLen
}

Expand All @@ -44,14 +47,10 @@ func (n *Node16[T]) setNumChildren(numChildren uint8) {
}

func (n *Node16[T]) getPartial() []byte {
n.mu.Lock()
defer n.mu.Unlock()
return n.partial
}

func (n *Node16[T]) setPartial(partial []byte) {
n.mu.Lock()
defer n.mu.Unlock()
n.partial = partial
}

Expand Down Expand Up @@ -85,18 +84,17 @@ func (n *Node16[T]) matchPrefix(prefix []byte) bool {
}

func (n *Node16[T]) getChild(index int) Node[T] {
n.mu.Lock()
defer n.mu.Unlock()
return n.children[index]
}

func (n *Node16[T]) clone(keepWatch, deep bool) Node[T] {
newNode := &Node16[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
partial: n.getPartial(),
mu: n.mu,
}
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
if keepWatch {
newNode.mutateCh = n.getMutateCh()
} else {
Expand Down Expand Up @@ -125,8 +123,6 @@ func (n *Node16[T]) setKeyLen(keyLen uint32) {
}

func (n *Node16[T]) setChild(index int, child Node[T]) {
n.mu.Lock()
defer n.mu.Unlock()
n.children[index] = child
}
func (n *Node16[T]) getKey() []byte {
Expand Down Expand Up @@ -164,10 +160,6 @@ func (n *Node16[T]) setMutateCh(ch chan struct{}) {
n.mutateCh = ch
}

func (n *Node16[T]) setMutex(mu *sync.RWMutex) {
n.mu = mu
}

func (n *Node16[T]) setValue(T) {

}
Expand Down
33 changes: 12 additions & 21 deletions node_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@

package adaptive

import "sync"

type Node256[T any] struct {
id uint64
partialLen uint32
numChildren uint8
partial []byte
children [256]Node[T]
mutateCh chan struct{}
mu *sync.RWMutex
}

func (n *Node256[T]) getId() uint64 {
return n.id
}

func (n *Node256[T]) getPartialLen() uint32 {
n.mu.Lock()
defer n.mu.Unlock()
return n.partialLen
}

func (n *Node256[T]) setId(id uint64) {
n.id = id
}

func (n *Node256[T]) setPartialLen(partialLen uint32) {
n.mu.Lock()
defer n.mu.Unlock()
n.partialLen = partialLen
}

Expand All @@ -47,21 +49,13 @@ func (n *Node256[T]) setNumChildren(numChildren uint8) {
}

func (n *Node256[T]) getPartial() []byte {
n.mu.Lock()
defer n.mu.Unlock()
return n.partial
}

func (n *Node256[T]) setPartial(partial []byte) {
n.mu.Lock()
defer n.mu.Unlock()
n.partial = partial
}

func (n *Node256[T]) setMutex(mu *sync.RWMutex) {
n.mu = mu
}

func (n *Node256[T]) isLeaf() bool {
return false
}
Expand Down Expand Up @@ -95,18 +89,17 @@ func (n *Node256[T]) getChild(index int) Node[T] {
if index < 0 || index >= 256 {
return nil
}
n.mu.Lock()
defer n.mu.Unlock()
return n.children[index]
}

func (n *Node256[T]) clone(keepWatch bool, deep bool) Node[T] {
newNode := &Node256[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
partial: n.getPartial(),
mu: n.mu,
}
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
if keepWatch {
newNode.mutateCh = n.getMutateCh()
} else {
Expand All @@ -125,8 +118,6 @@ func (n *Node256[T]) clone(keepWatch bool, deep bool) Node[T] {
}

func (n *Node256[T]) setChild(index int, child Node[T]) {
n.mu.Lock()
defer n.mu.Unlock()
n.children[index] = child
}

Expand Down
32 changes: 12 additions & 20 deletions node_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@ package adaptive
import (
"bytes"
"sort"
"sync"
)

type Node4[T any] struct {
id uint64
partialLen uint32
numChildren uint8
partial []byte
keys [4]byte
children [4]Node[T]
mutateCh chan struct{}
mu *sync.RWMutex
}

func (n *Node4[T]) getId() uint64 {
return n.id
}

func (n *Node4[T]) setId(id uint64) {
n.id = id
}

func (n *Node4[T]) getPartialLen() uint32 {
n.mu.Lock()
defer n.mu.Unlock()
return n.partialLen
}

func (n *Node4[T]) setPartialLen(partialLen uint32) {
n.mu.Lock()
defer n.mu.Unlock()
n.partialLen = partialLen
}

Expand All @@ -44,14 +47,10 @@ func (n *Node4[T]) setNumChildren(numChildren uint8) {
}

func (n *Node4[T]) getPartial() []byte {
n.mu.Lock()
defer n.mu.Unlock()
return n.partial
}

func (n *Node4[T]) setPartial(partial []byte) {
n.mu.Lock()
defer n.mu.Unlock()
n.partial = partial
}

Expand Down Expand Up @@ -84,18 +83,17 @@ func (n *Node4[T]) matchPrefix(prefix []byte) bool {
}

func (n *Node4[T]) getChild(index int) Node[T] {
n.mu.Lock()
defer n.mu.Unlock()
return n.children[index]
}

func (n *Node4[T]) clone(keepWatch, deep bool) Node[T] {
newNode := &Node4[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
partial: n.getPartial(),
mu: n.mu,
}
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
if keepWatch {
newNode.mutateCh = n.getMutateCh()
} else {
Expand Down Expand Up @@ -123,15 +121,9 @@ func (n *Node4[T]) setKeyLen(keyLen uint32) {
}

func (n *Node4[T]) setChild(index int, child Node[T]) {
n.mu.Lock()
defer n.mu.Unlock()
n.children[index] = child
}

func (n *Node4[T]) setMutex(mu *sync.RWMutex) {
n.mu = mu
}

func (n *Node4[T]) getKey() []byte {
//no op
return []byte{}
Expand Down
32 changes: 12 additions & 20 deletions node_48.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@ package adaptive

import (
"bytes"
"sync"
)

type Node48[T any] struct {
id uint64
partialLen uint32
artNodeType uint8
numChildren uint8
partial []byte
keys [256]byte
children [48]Node[T]
mutateCh chan struct{}
mu *sync.RWMutex
}

func (n *Node48[T]) getId() uint64 {
return n.id
}

func (n *Node48[T]) setId(id uint64) {
n.id = id
}

func (n *Node48[T]) getPartialLen() uint32 {
n.mu.Lock()
defer n.mu.Unlock()
return n.partialLen
}

func (n *Node48[T]) setPartialLen(partialLen uint32) {
n.mu.Lock()
defer n.mu.Unlock()
n.partialLen = partialLen
}

Expand All @@ -44,18 +47,10 @@ func (n *Node48[T]) setNumChildren(numChildren uint8) {
}

func (n *Node48[T]) getPartial() []byte {
n.mu.Lock()
defer n.mu.Unlock()
return n.partial
}

func (n *Node48[T]) setMutex(mu *sync.RWMutex) {
n.mu = mu
}

func (n *Node48[T]) setPartial(partial []byte) {
n.mu.Lock()
defer n.mu.Unlock()
n.partial = partial
}

Expand Down Expand Up @@ -98,18 +93,17 @@ func (n *Node48[T]) matchPrefix(prefix []byte) bool {
}

func (n *Node48[T]) getChild(index int) Node[T] {
n.mu.Lock()
defer n.mu.Unlock()
return n.children[index]
}

func (n *Node48[T]) clone(keepWatch, deep bool) Node[T] {
newNode := &Node48[T]{
partialLen: n.getPartialLen(),
numChildren: n.getNumChildren(),
partial: n.getPartial(),
mu: n.mu,
}
newPartial := make([]byte, maxPrefixLen)
copy(newPartial, n.partial)
newNode.setPartial(newPartial)
if keepWatch {
newNode.mutateCh = n.getMutateCh()
} else {
Expand Down Expand Up @@ -137,8 +131,6 @@ func (n *Node48[T]) setKeyLen(keyLen uint32) {
}

func (n *Node48[T]) setChild(index int, child Node[T]) {
n.mu.Lock()
defer n.mu.Unlock()
n.children[index] = child
}

Expand Down
Loading

0 comments on commit e3ae4b9

Please sign in to comment.