Skip to content

Commit

Permalink
add hkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Oct 30, 2023
1 parent b324f5d commit 5b052a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"time"

"github.com/bytedance/sonic"
cache "github.com/xgzlucario/GigaCache"
"github.com/xgzlucario/rotom/base"
)
Expand Down Expand Up @@ -90,6 +91,17 @@ func (c *Client) HLen(key string) (int, error) {
return base.ParseInt[int](args), nil
}

// HKeys
func (c *Client) HKeys(key string) ([]string, error) {
args, err := c.do(NewCodec(OpHKeys).Str(key))
if err != nil {
return nil, err

Check warning on line 98 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L98

Added line #L98 was not covered by tests
}
var keys []string
err = sonic.Unmarshal(args, &keys)
return keys, err
}

// HRemove
func (c *Client) HRemove(key, field string) (bool, error) {
args, err := c.do(NewCodec(OpHRemove).Str(key).Str(field))
Expand Down
15 changes: 15 additions & 0 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"time"

"github.com/bytedance/sonic"
"github.com/panjf2000/gnet/v2"
cache "github.com/xgzlucario/GigaCache"
"github.com/xgzlucario/rotom/base"
Expand All @@ -33,6 +34,7 @@ const (
OpHSet
OpHGet
OpHLen
OpHKeys
OpHRemove
// set
OpSAdd
Expand Down Expand Up @@ -172,6 +174,19 @@ var cmdTable = []Cmd{
_, err = w.Write(res)
return err
}},
{OpHKeys, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
m, err := e.fetchMap(*b2s(args[0]))
if err != nil {
return err

Check warning on line 181 in rotom.go

View check run for this annotation

Codecov / codecov/patch

rotom.go#L181

Added line #L181 was not covered by tests
}
src, err := sonic.Marshal(m.Keys())
if err != nil {
return err

Check warning on line 185 in rotom.go

View check run for this annotation

Codecov / codecov/patch

rotom.go#L185

Added line #L185 was not covered by tests
}
_, err = w.Write(src)
return err
}},
{OpHRemove, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// key, field
ok, err := e.HRemove(*b2s(args[0]), *b2s(args[1]))
Expand Down
10 changes: 5 additions & 5 deletions rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,14 @@ func TestClient(t *testing.T) {
assert.Nil(err)
assert.Equal(num, 1)

// HKeys
keys, err := cli.HKeys("exmap")
assert.Nil(err)
assert.ElementsMatch(keys, []string{key})

// HRemove
ok, err := cli.HRemove("exmap", key)
assert.Nil(err)
assert.True(ok)

// HLen
num, err = cli.HLen("exmap")
assert.Nil(err)
assert.Equal(num, 0)
}
}

0 comments on commit 5b052a7

Please sign in to comment.