Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Nov 4, 2023
1 parent d33eaa8 commit 188feb5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
37 changes: 37 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,43 @@ func (c *Client) SDiff(dstKey string, srcKeys ...string) error {
return c.doNoRes(NewCodec(OpSDiff).Str(dstKey).StrSlice(srcKeys))
}

// LPush
func (c *Client) LPush(key, item string) error {
return c.doNoRes(NewCodec(OpLPush).Str(key).Str(item))
}

// LPop
func (c *Client) LPop(key string) (string, error) {
res, err := c.do(NewCodec(OpLPop).Str(key))
if err != nil {
return "", err
}
return string(res), nil
}

// RPush
func (c *Client) RPush(key, item string) error {
return c.doNoRes(NewCodec(OpRPush).Str(key).Str(item))
}

// RPop
func (c *Client) RPop(key string) (string, error) {
res, err := c.do(NewCodec(OpRPop).Str(key))
if err != nil {
return "", err
}
return string(res), nil
}

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

// BitSet
func (c *Client) BitSet(key string, offset uint32, val bool) error {
return c.doNoRes(NewCodec(OpBitSet).Str(key).Uint(offset).Bool(val))
Expand Down
18 changes: 12 additions & 6 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,25 +273,31 @@ var cmdTable = []Cmd{
}},
{OpLPop, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
_, err := e.LPop(string(args[0]))
return err
res, err := e.LPop(string(args[0]))
if err != nil {
return err
}
return w.Write(s2b(&res))
}},
{OpRPush, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// key, item
return e.RPush(string(args[0]), string(args[1]))
}},
{OpRPop, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
_, err := e.RPop(string(args[0]))
return err
res, err := e.RPop(string(args[0]))
if err != nil {
return err
}
return w.Write(s2b(&res))
}},
{OpLLen, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
l, err := e.fetchList(string(args[0]))
num, err := e.LLen(string(args[0]))
if err != nil {
return err
}
return w.Write(base.FormatInt(l.Len()))
return w.Write(base.FormatInt(num))
}},
// bitmap
{OpBitSet, 3, func(e *Engine, args [][]byte, w base.Writer) error {
Expand Down
40 changes: 24 additions & 16 deletions rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,27 +323,24 @@ func TestHmap(t *testing.T) {
func TestList(t *testing.T) {
assert := assert.New(t)

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

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

for i := 0; i < 1000; i++ {
key := gofakeit.UUID()

for j := 0; j < 100; j++ {
err := db.RPush(key, strconv.Itoa(j))
err := cli.RPush(key, strconv.Itoa(j))
assert.Nil(err)
}
for j := 0; j < 100; j++ {
res, err := db.LPop(key)
res, err := cli.LPop(key)
assert.Nil(err)
assert.Equal(res, strconv.Itoa(j))
}

// LLen
num, err := db.LLen(key)
num, err := cli.LLen(key)
assert.Nil(err)
assert.Equal(num, 0)
}
Expand All @@ -352,41 +349,43 @@ func TestList(t *testing.T) {
key := gofakeit.UUID()

for j := 0; j < 100; j++ {
err := db.LPush(key, strconv.Itoa(j))
err := cli.LPush(key, strconv.Itoa(j))
assert.Nil(err)
}
for j := 0; j < 100; j++ {
res, err := db.RPop(key)
res, err := cli.RPop(key)
assert.Nil(err)
assert.Equal(res, strconv.Itoa(j))
}

// LLen
num, err := db.LLen(key)
num, err := cli.LLen(key)
assert.Nil(err)
assert.Equal(num, 0)
}

// Error
db.HSet("map", "key", []byte("value"))
cli.HSet("map", "key", []byte("value"))

err = db.LPush("map", "1")
err = cli.LPush("map", "1")
assert.ErrorContains(err, base.ErrWrongType.Error())

err = db.RPush("map", "1")
err = cli.RPush("map", "1")
assert.ErrorContains(err, base.ErrWrongType.Error())

res, err := db.LPop("map")
res, err := cli.LPop("map")
assert.Equal(res, "")
assert.ErrorContains(err, base.ErrWrongType.Error())

res, err = db.RPop("map")
res, err = cli.RPop("map")
assert.Equal(res, "")
assert.ErrorContains(err, base.ErrWrongType.Error())

n, err := db.LLen("map")
n, err := cli.LLen("map")
assert.Equal(n, 0)
assert.ErrorContains(err, base.ErrWrongType.Error())

db.Close()
}

func TestSet(t *testing.T) {
Expand Down Expand Up @@ -486,6 +485,15 @@ func TestSet(t *testing.T) {
assert.Equal(m, nilStrings)
assert.ErrorContains(err, base.ErrWrongType.Error())

err = cli.SUnion("map", "map")
assert.ErrorContains(err, base.ErrWrongType.Error())

err = cli.SDiff("map", "map")
assert.ErrorContains(err, base.ErrWrongType.Error())

err = cli.SInter("map", "map")
assert.ErrorContains(err, base.ErrWrongType.Error())

db.Shrink()
db.Close()

Expand Down

0 comments on commit 188feb5

Please sign in to comment.