diff --git a/btree/btree.go b/btree/btree.go index 62942f9..291a068 100644 --- a/btree/btree.go +++ b/btree/btree.go @@ -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 diff --git a/btree/btree_test.go b/btree/btree_test.go index f4c2ac7..8e86f31 100644 --- a/btree/btree_test.go +++ b/btree/btree_test.go @@ -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) } @@ -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)) } @@ -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)) }