Skip to content

Commit

Permalink
reordered cmdTable
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Oct 29, 2023
1 parent bf0f31c commit 14547e4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 64 deletions.
29 changes: 22 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *Client) Rename(key, newKey string) (bool, error) {

// Get
func (c *Client) Get(key string) ([]byte, error) {
args, err := c.do(NewCodec(ReqGet).Str(key))
args, err := c.do(NewCodec(OpGet).Str(key))
if err != nil {
return nil, err
}
Expand All @@ -71,29 +71,44 @@ func (c *Client) Get(key string) ([]byte, error) {

// Len
func (c *Client) Len() (uint64, error) {
args, err := c.do(NewCodec(ReqLen))
args, err := c.do(NewCodec(OpLen))
if err != nil {
return 0, err
}
return base.ParseInt[uint64](args), nil
}

// HSet
func (c *Client) HSet(key, field string, val []byte) ([]byte, error) {
args, err := c.do(NewCodec(OpHSet).Str(key).Str(field).Bytes(val))
func (c *Client) HSet(key, field string, val []byte) error {
_, err := c.do(NewCodec(OpHSet).Str(key).Str(field).Bytes(val))
return err
}

// HGet
func (c *Client) HGet(key, field string) ([]byte, error) {
args, err := c.do(NewCodec(OpHGet).Str(key).Str(field))
if err != nil {
return nil, err
}
return args, nil
}

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

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

// Close
Expand Down
125 changes: 70 additions & 55 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ import (
type Operation byte

const (
OpSetTx Operation = iota
Response Operation = iota
OpSetTx
OpGet
OpRemove
OpRename
OpLen
// map
OpHSet
OpHGet
OpHLen
OpHRemove
// set
OpSAdd
Expand All @@ -40,6 +45,7 @@ const (
OpLPop
OpRPush
OpRPop
OpLLen
// bitmap
OpBitSet
OpBitFlip
Expand All @@ -50,15 +56,9 @@ const (
OpZAdd
OpZIncr
OpZRemove
// marshal
// others
OpMarshalBytes
// request
Response
ReqPing
ReqGet
ReqLen
ReqHLen
ReqLLen
OpPing
)

// Cmd
Expand All @@ -70,6 +70,9 @@ type Cmd struct {

// cmdTable defines the argsNum and callback function required for the operation.
var cmdTable = []Cmd{
{Response, 2, func(_ *Engine, _ [][]byte, _ base.Writer) error {
return nil
}},
{OpSetTx, 4, func(e *Engine, args [][]byte, _ base.Writer) error {
// type, key, ts, val
ts := base.ParseInt[int64](args[2]) * timeCarry
Expand Down Expand Up @@ -117,6 +120,15 @@ var cmdTable = []Cmd{

return nil
}},
{OpGet, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
val, _, err := e.GetBytes(*b2s(args[0]))
if err != nil {
return err
}
_, err = w.Write(val)
return err
}},
{OpRemove, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
ok := e.Remove(*b2s(args[0]))
Expand All @@ -127,14 +139,46 @@ var cmdTable = []Cmd{
ok := e.Rename(*b2s(args[0]), *b2s(args[1]))
return w.WriteByte(bool2byte(ok))
}},
{OpLen, 0, func(e *Engine, args [][]byte, w base.Writer) error {
res := base.FormatInt[uint64](e.Stat().Len)
_, err := w.Write(res)
return err
}},
// map
{OpHSet, 3, func(e *Engine, args [][]byte, _ base.Writer) error {
// key, field, val
return e.HSet(*b2s(args[0]), *b2s(args[1]), args[2])
}},
{OpHRemove, 2, func(e *Engine, args [][]byte, _ base.Writer) error {
{OpHGet, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// key, field
return e.HRemove(*b2s(args[0]), *b2s(args[1]))
m, err := e.fetchMap(*b2s(args[0]))
if err != nil {
return err
}
val, ok := m.Get(*b2s(args[1]))
if !ok {
return base.ErrFieldNotFound
}
_, err = w.Write(val)
return err
}},
{OpHLen, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
m, err := e.fetchMap(*b2s(args[0]))
if err != nil {
return err
}
res := base.FormatInt[int](m.Len())
_, err = w.Write(res)
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]))
if err != nil {
return err
}
return w.WriteByte(bool2byte(ok))
}},
// set
{OpSAdd, 2, func(e *Engine, args [][]byte, _ base.Writer) error {
Expand Down Expand Up @@ -177,6 +221,16 @@ var cmdTable = []Cmd{
_, err := e.RPop(*b2s(args[0]))
return err
}},
{OpLLen, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
l, err := e.fetchList(*b2s(args[0]))
if err != nil {
return err
}
str := strconv.Itoa(l.Len())
_, err = w.Write(s2b(&str))
return err
}},
// bitmap
{OpBitSet, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key, offset, val
Expand Down Expand Up @@ -221,53 +275,15 @@ var cmdTable = []Cmd{
// key, val
return e.ZRemove(*b2s(args[0]), *b2s(args[1]))
}},
// marshal
// others
{OpMarshalBytes, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// val
return e.m.UnmarshalBytes(args[0])
}},
// request
{Response, 2, func(e *Engine, args [][]byte, w base.Writer) error {
return nil
}},
{ReqPing, 0, func(e *Engine, args [][]byte, w base.Writer) error {
{OpPing, 0, func(_ *Engine, _ [][]byte, w base.Writer) error {
_, err := w.Write([]byte("pong"))
return err
}},
{ReqGet, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
val, _, err := e.GetBytes(*b2s(args[0]))
if err != nil {
return err
}
_, err = w.Write(val)
return err
}},
{ReqLen, 0, func(e *Engine, args [][]byte, w base.Writer) error {
res := base.FormatInt[uint64](e.Stat().Len)
_, err := w.Write(res)
return err
}},
{ReqHLen, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
m, err := e.fetchMap(*b2s(args[0]))
if err != nil {
return err
}
str := strconv.Itoa(m.Len())
_, err = w.Write(s2b(&str))
return err
}},
{ReqLLen, 1, func(e *Engine, args [][]byte, w base.Writer) error {
// key
l, err := e.fetchList(*b2s(args[0]))
if err != nil {
return err
}
str := strconv.Itoa(l.Len())
_, err = w.Write(s2b(&str))
return err
}},
}

// VType is value type for Set Operation.
Expand Down Expand Up @@ -556,15 +572,14 @@ func (e *Engine) HSet(key, field string, val []byte) error {
}

// HRemove
func (e *Engine) HRemove(key, field string) error {
func (e *Engine) HRemove(key, field string) (bool, error) {
m, err := e.fetchMap(key)
if err != nil {
return err
return false, err
}
e.encode(NewCodec(OpHRemove).Str(key).Str(field))
m.Delete(field)

return nil
return m.Delete(field), nil
}

// HKeys
Expand Down
33 changes: 31 additions & 2 deletions rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ func TestHMap(t *testing.T) {
assert.Nil(err)

if op%3 == 0 {
err = db.HRemove(key, field)
ok, err := db.HRemove(key, field)
assert.Nil(err)
assert.True(ok)

res, err = db.HGet(key, field)
assert.Equal(res, nilBytes)
Expand Down Expand Up @@ -267,8 +268,9 @@ func TestHMap(t *testing.T) {
}
{
// remove
err := db.HRemove("str", "foo")
ok, err := db.HRemove("str", "foo")
assert.Equal(err, base.ErrWrongType)
assert.False(ok)
}
{
// keys
Expand Down Expand Up @@ -571,4 +573,31 @@ func TestClient(t *testing.T) {
assert.Nil(err)
assert.Equal(num, uint64(i+1))
}

for i := 0; i < 10000; i++ {
// HSet
key := fmt.Sprintf("key-%d", i)
err := cli.HSet("exmap", key, []byte(key))
assert.Nil(err)

// HGet
res, err := cli.HGet("exmap", key)
assert.Nil(err)
assert.Equal(res, []byte(key))

// HLen
num, err := cli.HLen("exmap")
assert.Nil(err)
assert.Equal(num, 1)

// 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 14547e4

Please sign in to comment.