Skip to content

Commit

Permalink
add client oper for set
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Nov 1, 2023
1 parent 01843c4 commit 7bf4a0f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
57 changes: 57 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,63 @@ func (c *Client) HRemove(key, field string) (bool, error) {
return args[0] == _true, nil
}

// SAdd
func (c *Client) SAdd(key, item string) error {
_, err := c.do(NewCodec(OpSAdd).Str(key).Str(item))
return err
}

// SRemove
func (c *Client) SRemove(key, item string) error {
_, err := c.do(NewCodec(OpSRemove).Str(key).Str(item))
return err
}

// SHas
func (c *Client) SHas(key, item string) (bool, error) {
args, err := c.do(NewCodec(OpSHas).Str(key).Str(item))
if err != nil {
return false, err
}
return args[0] == _true, nil
}

// SCard
func (c *Client) SCard(key string) (int, error) {
args, err := c.do(NewCodec(OpSCard).Str(key))
if err != nil {
return 0, err
}
return base.ParseInt[int](args), nil
}

// SMembers
func (c *Client) SMembers(key string) ([]string, error) {
args, err := c.do(NewCodec(OpSMembers).Str(key))
if err != nil {
return nil, err
}
return base.ParseStrSlice(args), nil
}

// SUnion
func (c *Client) SUnion(dstKey string, srcKeys ...string) error {
_, err := c.do(NewCodec(OpSUnion).Str(dstKey).StrSlice(srcKeys))
return err
}

// SInter
func (c *Client) SInter(dstKey string, srcKeys ...string) error {
_, err := c.do(NewCodec(OpSInter).Str(dstKey).StrSlice(srcKeys))
return err
}

// SDiff
func (c *Client) SDiff(dstKey string, srcKeys ...string) error {
_, err := c.do(NewCodec(OpSDiff).Str(dstKey).StrSlice(srcKeys))
return err
}

// Close
func (c *Client) Close() error {
return c.c.Close()
Expand Down
35 changes: 32 additions & 3 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const (
// set
OpSAdd
OpSRemove
OpSHas
OpSCard
OpSMembers
OpSUnion
OpSInter
OpSDiff
Expand Down Expand Up @@ -202,17 +205,43 @@ var cmdTable = []Cmd{
// key, item
return e.SRemove(*b2s(args[0]), *b2s(args[1]))
}},
{OpSUnion, 2, func(e *Engine, args [][]byte, w base.Writer) error {
{OpSHas, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// key, item
ok, err := e.SHas(*b2s(args[0]), *b2s(args[1]))
if err != nil {
return err
}
return w.WriteByte(bool2byte(ok))
}},
{OpSCard, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
n, err := e.SCard(*b2s(args[0]))
if err != nil {
return err
}
_, err = w.Write(base.FormatInt(n))
return err
}},
{OpSMembers, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
s, err := e.fetchSet(*b2s(args[0]))
if err != nil {
return err
}
_, err = w.Write(base.FormatStrSlice(s.ToSlice()))
return err
}},
{OpSUnion, 2, func(e *Engine, args [][]byte, _ base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.SUnion(*b2s(args[0]), srcKeys...)
}},
{OpSInter, 2, func(e *Engine, args [][]byte, w base.Writer) error {
{OpSInter, 2, func(e *Engine, args [][]byte, _ base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.SInter(*b2s(args[0]), srcKeys...)
}},
{OpSDiff, 2, func(e *Engine, args [][]byte, w base.Writer) error {
{OpSDiff, 2, func(e *Engine, args [][]byte, _ base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.SDiff(*b2s(args[0]), srcKeys...)
Expand Down
32 changes: 31 additions & 1 deletion rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestDB(t *testing.T) {
assert.Nil(db)
}

func TestSet(t *testing.T) {
func TestCSet(t *testing.T) {
assert := assert.New(t)

cfg := DefaultConfig
Expand Down Expand Up @@ -285,6 +285,36 @@ func TestHMap(t *testing.T) {
}
}

func TestSet(t *testing.T) {
assert := assert.New(t)

cfg := DefaultConfig
cfg.Path = gofakeit.UUID() + ".db"

db, err := Open(cfg)
assert.Nil(err)

port := gofakeit.Number(10000, 20000)
addr := "localhost:" + strconv.Itoa(port)

// listen
go db.Listen(addr)
time.Sleep(time.Second / 10)

cli, err := NewClient(addr)
assert.Nil(err)

for i := 0; i < 1000; i++ {
assert.Nil(cli.SAdd("set", strconv.Itoa(i)))
}

for i := 200; i < 1200; i++ {
ok, err := cli.SHas("set", strconv.Itoa(i))
assert.Nil(err)
assert.Equal(ok, i < 1000, i)
}
}

func TestZSet(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 7bf4a0f

Please sign in to comment.