Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(key-revision): add revision for keys with GetUsingRevision function #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ build:
go build -v

clean:
rm -rf ./nimbusdb_temp* benchmark/nimbusdb_temp*
rm -rf ./nimbusdb_temp* benchmark/nimbusdb_temp* ~/nimbusdb/test_data
mkdir -p ~/nimbusdb/test_data

test:
go test -v -failfast
Expand Down
18 changes: 4 additions & 14 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nimbusdb
import (
"bytes"
"errors"
"sync"
"time"

"github.com/manosriram/nimbusdb/utils"
Expand All @@ -15,15 +14,6 @@ var (
ERROR_CANNOT_CLOSE_CLOSED_BATCH = errors.New("cannot close closed batch")
)

type Batch struct {
id ksuid.KSUID
db *Db
closed bool
batchlock sync.Mutex
mu sync.RWMutex
writeQueue []*KeyValuePair
}

func (db *Db) NewBatch() (*Batch, error) {
b := &Batch{
db: db,
Expand Down Expand Up @@ -73,11 +63,11 @@ func (b *Batch) Get(k []byte) ([]byte, error) {
return utils.Encode(record.Value), nil
}

v, err := b.db.getKeyDir(k) // else, search datafiles
v, err := b.db.getKeyDirUsingKey(k) // else, search datafiles
if err != nil {
return nil, err
}
return v.v, nil
return v.value, nil
}

func (b *Batch) Exists(k []byte) (bool, error) {
Expand All @@ -98,11 +88,11 @@ func (b *Batch) Exists(k []byte) (bool, error) {
return true, nil
}

v, err := b.db.getKeyDir(k) // else, search datafiles
v, err := b.db.getKeyDirUsingKey(k) // else, search datafiles
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
45 changes: 39 additions & 6 deletions btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nimbusdb

import (
"bytes"
"strings"
"sync"

"github.com/google/btree"
Expand All @@ -14,24 +15,46 @@ type BTree struct {
}

type item struct {
key []byte
v KeyDirValue
key []byte
v KeyDirValue
revision int64
}

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

if bytes.Equal(i.key, otherItem.key) {
switch i.revision {
case 0:
return i.revision > otherItem.revision
default:
return i.revision < otherItem.revision
}
}
return bytes.Compare(i.key, otherItem.key) < 0

}

func (b *BTree) Get(key []byte) *KeyDirValue {
i := b.tree.Get(&item{key: key})
if i == nil {
return nil
}
return &i.(*item).v
v := &i.(*item).v
return v
}

func (b *BTree) GetUsingRevision(key []byte, revision int64) *KeyDirValue {
i := b.tree.Get(&item{key: key, revision: revision})
if i == nil {
return nil
}
v := &i.(*item).v
return v
}

func (b *BTree) Set(key []byte, value KeyDirValue) *KeyDirValue {
i := b.tree.ReplaceOrInsert(&item{key: key, v: value})
i := b.tree.ReplaceOrInsert(&item{key: key, v: value, revision: value.revision})
if i != nil {
return &i.(*item).v
}
Expand Down Expand Up @@ -63,3 +86,13 @@ func (b *BTree) List() []*KeyValuePair {
})
return pairs
}

func (b *BTree) AscendWithPrefix(prefix string, handler func(k []byte)) {
b.tree.Ascend(func(it btree.Item) bool {
key := it.(*item).key
if strings.HasPrefix(string(key), prefix) {
handler(key)
}
return true
})
}
Loading
Loading