Skip to content

Commit

Permalink
rhashmap Get接口满足api约束 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jun 11, 2022
1 parent 0db8ef3 commit c255672
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
9 changes: 5 additions & 4 deletions rhashmap/rhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ package rhashmap
// https://github.com/redis/redis/blob/unstable/src/dict.c
import (
"errors"
"github.com/cespare/xxhash/v2"
"math"
"reflect"
"unsafe"

"github.com/cespare/xxhash/v2"
)

const (
Expand Down Expand Up @@ -303,7 +304,7 @@ func sizeMask(exp int8) uint64 {
}

// 获取
func (h *HashMap[K, V]) Get(key K) (v V, err error) {
func (h *HashMap[K, V]) GetWithErr(key K) (v V, err error) {
if h.Len() == 0 {
err = ErrNotFound
return
Expand Down Expand Up @@ -335,8 +336,8 @@ func (h *HashMap[K, V]) Get(key K) (v V, err error) {
}

// 获取
func (h *HashMap[K, V]) GetOrZero(key K) (v V) {
v, _ = h.Get(key)
func (h *HashMap[K, V]) Get(key K) (v V) {
v, _ = h.GetWithErr(key)
return
}

Expand Down
2 changes: 1 addition & 1 deletion rhashmap/rhashmap_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func BenchmarkGet(b *testing.B) {
b.ResetTimer()

for i := 0.0; i < max; i++ {
v := set.GetOrZero(i)
v := set.Get(i)
if v != i {
panic(fmt.Sprintf("need:%f, got:%f", i, v))
}
Expand Down
46 changes: 23 additions & 23 deletions rhashmap/rhashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func Test_SetGet_StringBool(t *testing.T) {
hm.Set("ni", true)
hm.Set("hao", true)

assert.True(t, hm.GetOrZero("hello"))
assert.True(t, hm.GetOrZero("world"))
assert.True(t, hm.GetOrZero("ni"))
assert.True(t, hm.GetOrZero("hao"))
assert.True(t, hm.Get("hello"))
assert.True(t, hm.Get("world"))
assert.True(t, hm.Get("ni"))
assert.True(t, hm.Get("hao"))
}

// 1. set get测试
Expand All @@ -31,10 +31,10 @@ func Test_SetGet_StringString(t *testing.T) {
hm.Set("ni", "ni")
hm.Set("hao", "hao")

assert.Equal(t, hm.GetOrZero("hello"), "hello")
assert.Equal(t, hm.GetOrZero("world"), "world")
assert.Equal(t, hm.GetOrZero("ni"), "ni")
assert.Equal(t, hm.GetOrZero("hao"), "hao")
assert.Equal(t, hm.Get("hello"), "hello")
assert.Equal(t, hm.Get("world"), "world")
assert.Equal(t, hm.Get("ni"), "ni")
assert.Equal(t, hm.Get("hao"), "hao")
}

// 1. set get测试
Expand All @@ -46,10 +46,10 @@ func Test_SetGet_IntString(t *testing.T) {
hm.Set(3, "ni")
hm.Set(4, "hao")

assert.Equal(t, hm.GetOrZero(1), "hello")
assert.Equal(t, hm.GetOrZero(2), "world")
assert.Equal(t, hm.GetOrZero(3), "ni")
assert.Equal(t, hm.GetOrZero(4), "hao")
assert.Equal(t, hm.Get(1), "hello")
assert.Equal(t, hm.Get(2), "world")
assert.Equal(t, hm.Get(3), "ni")
assert.Equal(t, hm.Get(4), "hao")
}

// 1. set get测试
Expand All @@ -60,10 +60,10 @@ func Test_SetGet_IntString_Lazyinit(t *testing.T) {
hm.Set(3, "ni")
hm.Set(4, "hao")

assert.Equal(t, hm.GetOrZero(1), "hello")
assert.Equal(t, hm.GetOrZero(2), "world")
assert.Equal(t, hm.GetOrZero(3), "ni")
assert.Equal(t, hm.GetOrZero(4), "hao")
assert.Equal(t, hm.Get(1), "hello")
assert.Equal(t, hm.Get(2), "world")
assert.Equal(t, hm.Get(3), "ni")
assert.Equal(t, hm.Get(4), "hao")
}

// 1. set get测试
Expand All @@ -73,19 +73,19 @@ func Test_SetGet_Replace_IntString(t *testing.T) {
hm.Set(1, "hello")
hm.Set(1, "world")

assert.Equal(t, hm.GetOrZero(1), "world")
assert.Equal(t, hm.Get(1), "world")
}

// 1. set get测试
// 获取空值数据
func Test_SetGet_Zero(t *testing.T) {
hm := New[int, int]()
for i := 0; i < 10; i++ {
assert.Equal(t, hm.GetOrZero(i), 0)
assert.Equal(t, hm.Get(i), 0)
}

for i := 0; i < 10; i++ {
v, err := hm.Get(i)
v, err := hm.GetWithErr(i)
assert.Error(t, err)
assert.Equal(t, v, 0)
}
Expand All @@ -98,10 +98,10 @@ func Test_SetGet_NotFound(t *testing.T) {
hm.Set(1, "hello")
hm.Set(1, "world")

_, err := hm.Get(3)
_, err := hm.GetWithErr(3)

assert.Error(t, err)
assert.Equal(t, hm.GetOrZero(1), "world")
assert.Equal(t, hm.Get(1), "world")
}

// 1. set get测试
Expand All @@ -114,10 +114,10 @@ func Test_SetGet_Rehashing(t *testing.T) {
hm.Set(4, "world")
hm.Set(5, "world")

_, err := hm.Get(7)
_, err := hm.GetWithErr(7)

assert.Error(t, err)
assert.Equal(t, hm.GetOrZero(1), "hello")
assert.Equal(t, hm.Get(1), "hello")

}

Expand Down

0 comments on commit c255672

Please # to comment.