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

add btree Ascend、Descend method and unitest. #257

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions index/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,23 @@ func (mt *MemoryBTree) Delete(key []byte) (*wal.ChunkPosition, bool) {
func (mt *MemoryBTree) Size() int {
return mt.tree.Len()
}

// Ascend calls f for each item in the tree in ascending order. If f returns false, iteration stops.
Copy link
Collaborator

@roseduan roseduan Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f -> handleFunc/handleFn maybe more clear?

The Caller can not use this function directly. We should add a function in db.go to get the value according to the position.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your suggestion, I have made further modifications to the code. Could you please review it to see if it meets your expectations? If not, please point out any issues, and I will make further adjustments. Due to my daytime commitments for company projects, I can only work on the open-source project during the evenings. I apologize for any delay in responding.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it`s so kind of you to tell me this, I also maintain the project in my spare time, so just feel free to contribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the project you've written, which has enabled me to further study the Bitcask model.

func (mt *MemoryBTree) Ascend(f func(k []byte, v *wal.ChunkPosition) bool) {
mt.lock.RLock()
defer mt.lock.RUnlock()

mt.tree.Ascend(func(i btree.Item) bool {
return f(i.(*item).key, i.(*item).pos)
})
}

// Descend calls f for each item in the tree in descending order. If f returns false, iteration stops.
func (mt *MemoryBTree) Descend(f func(k []byte, v *wal.ChunkPosition) bool) {
mt.lock.RLock()
defer mt.lock.RUnlock()

mt.tree.Descend(func(i btree.Item) bool {
return f(i.(*item).key, i.(*item).pos)
})
}
117 changes: 117 additions & 0 deletions index/btree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package index

import (
"bytes"
"testing"

"github.com/rosedblabs/wal"
)

func TestMemoryBTree_Put_Get(t *testing.T) {
mt := newBTree()
w, _ := wal.Open(wal.DefaultOptions)

key := []byte("testKey")
chunkPosition, _ := w.Write([]byte("some data 1"))

// Test Put
oldPos := mt.Put(key, chunkPosition)
if oldPos != nil {
t.Fatalf("expected nil, got %+v", oldPos)
}

// Test Get
gotPos := mt.Get(key)
if chunkPosition.ChunkOffset != gotPos.ChunkOffset {
t.Fatalf("expected %+v, got %+v", chunkPosition, gotPos)
}
}

func TestMemoryBTree_Delete(t *testing.T) {
mt := newBTree()
w, _ := wal.Open(wal.DefaultOptions)

key := []byte("testKey")
chunkPosition, _ := w.Write([]byte("some data 2"))

mt.Put(key, chunkPosition)

// Test Delete
delPos, ok := mt.Delete(key)
if !ok {
t.Fatal("expected item to be deleted")
}
if chunkPosition.ChunkOffset != delPos.ChunkOffset {
t.Fatalf("expected %+v, got %+v", chunkPosition, delPos)
}

// Ensure the key is deleted
if mt.Get(key) != nil {
t.Fatal("expected nil, got value")
}
}

func TestMemoryBTree_Size(t *testing.T) {
mt := newBTree()

if mt.Size() != 0 {
t.Fatalf("expected size to be 0, got %d", mt.Size())
}

w, _ := wal.Open(wal.DefaultOptions)
key := []byte("testKey")
chunkPosition, _ := w.Write([]byte("some data 3"))

mt.Put(key, chunkPosition)

if mt.Size() != 1 {
t.Fatalf("expected size to be 1, got %d", mt.Size())
}
}

func TestMemoryBTree_Ascend_Descend(t *testing.T) {
mt := newBTree()
w, _ := wal.Open(wal.DefaultOptions)

data := map[string][]byte{
"apple": []byte("some data 4"),
"banana": []byte("some data 5"),
"cherry": []byte("some data 6"),
}

positionMap := make(map[string]*wal.ChunkPosition)

for k, v := range data {
chunkPosition, _ := w.Write(v)
positionMap[k] = chunkPosition
mt.Put([]byte(k), chunkPosition)
}

// Test Ascend
prevKey := ""
mt.Ascend(func(key []byte, pos *wal.ChunkPosition) bool {
if prevKey != "" && bytes.Compare([]byte(prevKey), key) >= 0 {
t.Fatalf("items are not in ascending order")
}
expectedPos := positionMap[string(key)]
if expectedPos.ChunkOffset != pos.ChunkOffset {
t.Fatalf("expected position %+v, got %+v", expectedPos, pos)
}
prevKey = string(key)
return true
})

// Test Descend
prevKey = "zzzzzz"
mt.Descend(func(key []byte, pos *wal.ChunkPosition) bool {
if bytes.Compare([]byte(prevKey), key) <= 0 {
t.Fatalf("items are not in descending order")
}
expectedPos := positionMap[string(key)]
if expectedPos.ChunkOffset != pos.ChunkOffset {
t.Fatalf("expected position %+v, got %+v", expectedPos, pos)
}
prevKey = string(key)
return true
})
}
Loading