Skip to content

Commit

Permalink
Merge pull request #16 from manosriram/fix/reader_writer
Browse files Browse the repository at this point in the history
fix(os): separate out reader and writer
  • Loading branch information
manosriram authored May 16, 2024
2 parents a2a52cb + 91aee0a commit dab9636
Show file tree
Hide file tree
Showing 11 changed files with 1,041 additions and 165 deletions.
4 changes: 2 additions & 2 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (b *Batch) Get(k []byte) ([]byte, error) {
if err != nil {
return nil, err
}
return v.v, nil
return v.value, nil
}

func (b *Batch) Exists(k []byte) (bool, error) {
Expand All @@ -86,7 +86,7 @@ func (b *Batch) Exists(k []byte) (bool, error) {
if err != nil {
return false, err
}
return bytes.Equal(v.k, k), nil
return bytes.Equal(v.key, k), nil
}

func (b *Batch) Set(k []byte, v []byte) ([]byte, error) {
Expand Down
10 changes: 2 additions & 8 deletions benchmark/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,14 @@ func set(b *testing.B) {

func get(b *testing.B) {
for i := 0; i < 10000; i++ {
key := []byte(utils.GetTestKey(i))
value := []byte("testvalue")
_, err := db.Set(key, value)
_, err := db.Set([]byte(utils.GetTestKey(i)), []byte("testvalue"))
assert.Nil(b, err)
}
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
kv := &nimbusdb.KeyValuePair{
Key: []byte(utils.GetTestKey(rand.Int())),
Value: []byte("testvalue"),
}
_, err := db.Get(kv.Key)
_, err := db.Get([]byte(utils.GetTestKey(rand.Int())))
if err != nil && err != nimbusdb.ERROR_KEY_NOT_FOUND {
log.Fatal(err)
}
Expand Down
16 changes: 15 additions & 1 deletion btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ type item struct {
v KeyDirValue
}

var itemPool = sync.Pool{
New: func() interface{} {
return &item{}
},
}

func (it item) Less(i btree.Item) bool {
return bytes.Compare(it.key, i.(*item).key) < 0
}

func (b *BTree) Get(key []byte) *KeyDirValue {
i := b.tree.Get(&item{key: key})
it := itemPool.Get().(*item)
it.key = key
defer itemPool.Put(it)

i := b.tree.Get(it)
if i == nil {
return nil
}
Expand All @@ -40,6 +50,10 @@ func (b *BTree) Set(key []byte, value KeyDirValue) *KeyDirValue {
}

func (b *BTree) Delete(key []byte) *KeyValuePair {
it := itemPool.Get().(*item)
it.key = key
defer itemPool.Put(it)

i := b.tree.Delete(&item{key: key})
if i != nil {
x := i.(*item)
Expand Down
Loading

0 comments on commit dab9636

Please sign in to comment.