-
Notifications
You must be signed in to change notification settings - Fork 640
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.