From 08bc1892dfe8e0e3f80a9d2616ed36c7ac5cbc49 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Thu, 18 Nov 2021 17:05:03 +0800 Subject: [PATCH 1/3] chore(proto): update proto --- pkg/proto/stringx.pb.go | 69 +++++++++++++++++------------------------ protobuf/stringx.proto | 1 - 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/pkg/proto/stringx.pb.go b/pkg/proto/stringx.pb.go index 787af75..83f24d9 100644 --- a/pkg/proto/stringx.pb.go +++ b/pkg/proto/stringx.pb.go @@ -580,8 +580,6 @@ type SetBitResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - - Result string `protobuf:"bytes,2,opt,name=result,proto3" json:"result,omitempty"` } func (x *SetBitResponse) Reset() { @@ -616,13 +614,6 @@ func (*SetBitResponse) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{11} } -func (x *SetBitResponse) GetResult() string { - if x != nil { - return x.Result - } - return "" -} - type GetBitRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -1071,37 +1062,35 @@ var file_stringx_proto_rawDesc = []byte{ 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x22, - 0x28, 0x0a, 0x0e, 0x53, 0x65, 0x74, 0x42, 0x69, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x41, 0x0a, 0x0d, 0x47, 0x65, 0x74, - 0x42, 0x69, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, - 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, - 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x22, 0x22, 0x0a, 0x0e, - 0x47, 0x65, 0x74, 0x42, 0x69, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x10, - 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x76, 0x61, 0x6c, - 0x22, 0x55, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, - 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, - 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x64, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x03, 0x65, 0x6e, 0x64, 0x22, 0x2a, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x52, 0x61, - 0x6e, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x72, 0x65, 0x73, - 0x75, 0x6c, 0x74, 0x22, 0x3d, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x53, 0x65, 0x74, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, - 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x76, - 0x61, 0x6c, 0x22, 0x28, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x53, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2b, 0x0a, 0x0d, - 0x53, 0x74, 0x72, 0x4c, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, - 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, - 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x28, 0x0a, 0x0e, 0x53, 0x74, 0x72, - 0x4c, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6c, - 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6c, 0x65, 0x6e, - 0x67, 0x74, 0x68, 0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x10, 0x0a, 0x0e, 0x53, 0x65, 0x74, 0x42, 0x69, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x22, 0x41, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x42, 0x69, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, + 0x0a, 0x05, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6f, + 0x66, 0x66, 0x65, 0x72, 0x22, 0x22, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x42, 0x69, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x22, 0x55, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x52, + 0x61, 0x6e, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, + 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x10, 0x0a, + 0x03, 0x65, 0x6e, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x65, 0x6e, 0x64, 0x22, + 0x2a, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x52, 0x61, 0x6e, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x3d, 0x0a, 0x0d, 0x47, + 0x65, 0x74, 0x53, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, + 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, + 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x22, 0x28, 0x0a, 0x0e, 0x47, 0x65, + 0x74, 0x53, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, + 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x72, 0x65, + 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2b, 0x0a, 0x0d, 0x53, 0x74, 0x72, 0x4c, 0x65, 0x6e, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, + 0x79, 0x22, 0x28, 0x0a, 0x0e, 0x53, 0x74, 0x72, 0x4c, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x42, 0x0b, 0x5a, 0x09, 0x70, + 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/protobuf/stringx.proto b/protobuf/stringx.proto index cd0d175..c62dd92 100644 --- a/protobuf/stringx.proto +++ b/protobuf/stringx.proto @@ -53,7 +53,6 @@ message SetBitRequest { } message SetBitResponse { - string result = 2; } message GetBitRequest { From 40afd8057d6d1b7541512bc06557517fba6a6320 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Thu, 18 Nov 2021 18:58:35 +0800 Subject: [PATCH 2/3] fix(hashx): fix HIncrBy err catch --- pkg/structure/hashx/hashx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/structure/hashx/hashx.go b/pkg/structure/hashx/hashx.go index 0cb7f36..9c234de 100644 --- a/pkg/structure/hashx/hashx.go +++ b/pkg/structure/hashx/hashx.go @@ -98,7 +98,7 @@ func (h HashX) Add(renewal int, keys ...string) (count int, result []string, err if v, ok := h[key]; ok { res, err := v.Incr(int32(renewal)) if err != nil { - return count, result, errorx.New("option key err, key:%s, err:%v", key, err) + continue } count += 1 From cdd26736a52df4efdbf34092f500b145549f1d47 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Thu, 18 Nov 2021 18:59:15 +0800 Subject: [PATCH 3/3] feat(dao-hashx): add dao hashx test --- storage/dao/hashx_test.go | 160 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 storage/dao/hashx_test.go diff --git a/storage/dao/hashx_test.go b/storage/dao/hashx_test.go new file mode 100644 index 0000000..430f7db --- /dev/null +++ b/storage/dao/hashx_test.go @@ -0,0 +1,160 @@ +package dao + +import ( + "testing" + + "gitee.com/wheat-os/wheatCache/pkg/lru" + "gitee.com/wheat-os/wheatCache/pkg/proto" + "github.com/stretchr/testify/require" +) + +func initData(t *testing.T, key *proto.BaseKey, dao Interface) { + _, err := dao.HSet(key, map[string]string{ + "test": "test", + "test1": "1", + "test2": "1.1", + }) + require.NoError(t, err) +} + +func TestDao_HSet(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + + initData(t, baseKey, dao) + + res, err := dao.HGet(baseKey, []string{"test", "test1"}) + require.NoError(t, err) + + require.Equal(t, res.Items, map[string]string{ + "test": "test", + "test1": "1", + }) + + res, err = dao.HGet(baseKey, []string{"test", "test1", "test2"}) + require.NoError(t, err) + + require.Equal(t, res.Items, map[string]string{ + "test": "test", + "test1": "1", + "test2": "1.10", // 默认2个小数点 + }) +} + +func TestDao_HGet(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + res, err := dao.HGet(baseKey, []string{"test", "test1", "test2"}) + require.NoError(t, err) + + require.Equal(t, res.Items, map[string]string{ + "test": "test", + "test1": "1", + "test2": "1.10", // 默认2个小数点 + }) + + _, err = dao.HGet(baseKey, []string{"test", "test1", "test2", "a"}) + require.Error(t, err) +} + +func TestDao_HExists(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + + resp, err := dao.HExists(baseKey, "test") + require.NoError(t, err) + require.Equal(t, resp.Exists, true) + + resp, err = dao.HExists(baseKey, "kjd") + require.NoError(t, err) + require.Equal(t, resp.Exists, false) +} + +func TestDao_HDel(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + _, err := dao.HDel(baseKey, []string{"test", "test1"}) + require.NoError(t, err) + + _, err = dao.HDel(baseKey, []string{"test"}) + require.Error(t, err) + + res, err := dao.HExists(baseKey, "test") + require.NoError(t, err) + require.Equal(t, res.Exists, false) + + res, err = dao.HExists(baseKey, "test2") + require.NoError(t, err) + require.Equal(t, res.Exists, true) +} + +func TestDao_HGetAll(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + resp, err := dao.HGetAll(baseKey) + require.NoError(t, err) + + require.Equal(t, resp.Items, map[string]string{ + "test": "test", + "test1": "1", + "test2": "1.10", + }) +} + +func TestDao_HSetX(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + res, err := dao.HSetX(baseKey, map[string]string{ + "test": "test", + "test1": "1", + "test2": "1.10", // 默认2个小数点 + }) + require.NoError(t, err) + require.Equal(t, res.Count, 0) +} + +func TestDao_HLen(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + resp, err := dao.HLen(baseKey) + require.NoError(t, err) + + require.Equal(t, resp.Length, int32(3)) +} + +func TestDao_HKeys(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + + res, err := dao.HKeys(baseKey) + require.NoError(t, err) + require.Equal(t, res.Keys, []string{"test", "test1", "test2"}) +} + +func TestDao_HIncrBy(t *testing.T) { + lru := lru.NewLRUCache() + dao := NewDao(lru) + baseKey := proto.NewBaseKey("apple") + initData(t, baseKey, dao) + + resp, err := dao.HIncrBy(baseKey, []string{"test", "test1"}, 10) + require.NoError(t, err) + require.Equal(t, resp.Count, int32(1)) + require.Equal(t, resp.Values, []string{"11"}) + +}