Skip to content

Commit

Permalink
btree Get接口满足api约束 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jun 11, 2022
1 parent f5d4bbf commit 2e9f61d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion btree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,15 @@ func (b *Btree[K, V]) SetWithPrev(k K, v V) (prev V, replaced bool) {
return
}

// 获取值, 忽略找不到的情况
func (b *Btree[K, V]) Get(k K) (v V) {
v, _ = b.GetWithErr(k)
return
}

// 找到err为nil
// 找不到err为ErrNotFound
func (b *Btree[K, V]) Get(k K) (v V, err error) {
func (b *Btree[K, V]) GetWithErr(k K) (v V, err error) {
if b.root == nil {
err = ErrNotFound
return
Expand Down
6 changes: 3 additions & 3 deletions btree/btree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Test_Btree_SetAndGet(t *testing.T) {
}

for i := 0; i < max; i++ {
v, err := b.Get(i)
v, err := b.GetWithErr(i)
assert.NoError(t, err)
assert.Equal(t, v, i)
}
Expand All @@ -36,7 +36,7 @@ func Test_Btree_SetAndGet_Split(t *testing.T) {
}

for i := 0; i < max; i++ {
v, err := b.Get(i)
v, err := b.GetWithErr(i)
assert.NoError(t, err, fmt.Sprintf("index:%d", i))
assert.Equal(t, v, i, fmt.Sprintf("index:%d", i))
}
Expand All @@ -53,7 +53,7 @@ func Test_Btree_SetAndGet_Split_Big(t *testing.T) {
}

for i := 0; i < max; i++ {
v, err := b.Get(i)
v, err := b.GetWithErr(i)
assert.NoError(t, err, fmt.Sprintf("index:%d", i))
assert.Equal(t, v, i, fmt.Sprintf("index:%d", i))
}
Expand Down

0 comments on commit 2e9f61d

Please # to comment.