ledisdb/server/cmd_set.go

306 lines
5.0 KiB
Go
Raw Normal View History

2014-08-15 06:42:40 +04:00
package server
import (
"github.com/siddontang/ledisdb/ledis"
)
2014-08-25 10:18:23 +04:00
func saddCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) < 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SAdd(args[0], args[1:]...); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func soptGeneric(c *client, optType byte) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) < 1 {
return ErrCmdParams
}
var v [][]byte
var err error
switch optType {
case ledis.UnionType:
2014-08-25 10:18:23 +04:00
v, err = c.db.SUnion(args...)
2014-08-15 06:42:40 +04:00
case ledis.DiffType:
2014-08-25 10:18:23 +04:00
v, err = c.db.SDiff(args...)
2014-08-15 06:42:40 +04:00
case ledis.InterType:
2014-08-25 10:18:23 +04:00
v, err = c.db.SInter(args...)
2014-08-15 06:42:40 +04:00
}
if err != nil {
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeSliceArray(v)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func soptStoreGeneric(c *client, optType byte) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) < 2 {
return ErrCmdParams
}
var n int64
var err error
switch optType {
case ledis.UnionType:
2014-08-25 10:18:23 +04:00
n, err = c.db.SUnionStore(args[0], args[1:]...)
2014-08-15 06:42:40 +04:00
case ledis.DiffType:
2014-08-25 10:18:23 +04:00
n, err = c.db.SDiffStore(args[0], args[1:]...)
2014-08-15 06:42:40 +04:00
case ledis.InterType:
2014-08-25 10:18:23 +04:00
n, err = c.db.SInterStore(args[0], args[1:]...)
2014-08-15 06:42:40 +04:00
}
if err != nil {
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func scardCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SCard(args[0]); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func sdiffCommand(c *client) error {
return soptGeneric(c, ledis.DiffType)
2014-08-15 06:42:40 +04:00
}
2014-08-25 10:18:23 +04:00
func sdiffstoreCommand(c *client) error {
return soptStoreGeneric(c, ledis.DiffType)
2014-08-15 06:42:40 +04:00
}
2014-08-25 10:18:23 +04:00
func sinterCommand(c *client) error {
return soptGeneric(c, ledis.InterType)
2014-08-15 06:42:40 +04:00
}
2014-08-25 10:18:23 +04:00
func sinterstoreCommand(c *client) error {
return soptStoreGeneric(c, ledis.InterType)
2014-08-15 06:42:40 +04:00
}
2014-08-25 10:18:23 +04:00
func sismemberCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SIsMember(args[0], args[1]); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func smembersCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.SMembers(args[0]); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeSliceArray(v)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func sremCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) < 2 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SRem(args[0], args[1:]...); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func sunionCommand(c *client) error {
return soptGeneric(c, ledis.UnionType)
2014-08-15 06:42:40 +04:00
}
2014-08-25 10:18:23 +04:00
func sunionstoreCommand(c *client) error {
return soptStoreGeneric(c, ledis.UnionType)
2014-08-15 06:42:40 +04:00
}
2014-08-25 10:18:23 +04:00
func sclearCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SClear(args[0]); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func smclearCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) < 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SMclear(args...); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func sexpireCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 2 {
return ErrCmdParams
}
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return ErrValue
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.SExpire(args[0], duration); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func sexpireAtCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 2 {
return ErrCmdParams
}
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return ErrValue
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.SExpireAt(args[0], when); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func sttlCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if v, err := c.db.STTL(args[0]); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(v)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-25 10:18:23 +04:00
func spersistCommand(c *client) error {
args := c.args
2014-08-15 06:42:40 +04:00
if len(args) != 1 {
return ErrCmdParams
}
2014-08-25 10:18:23 +04:00
if n, err := c.db.SPersist(args[0]); err != nil {
2014-08-15 06:42:40 +04:00
return err
} else {
2014-08-25 10:18:23 +04:00
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
}
return nil
}
2014-08-26 19:21:45 +04:00
func sscanCommand(c *client) error {
2014-08-27 06:29:17 +04:00
key, match, count, err := parseScanArgs(c)
2014-08-26 19:21:45 +04:00
if err != nil {
return err
}
2014-08-27 06:29:17 +04:00
if ay, err := c.db.SScan(key, count, false, match); err != nil {
2014-08-26 19:21:45 +04:00
return err
} else {
data := make([]interface{}, 2)
if len(ay) < count {
2014-08-27 06:29:17 +04:00
data[0] = []byte("")
2014-08-26 19:21:45 +04:00
} else {
2014-08-27 06:29:17 +04:00
data[0] = ay[len(ay)-1]
2014-08-26 19:21:45 +04:00
}
data[1] = ay
c.resp.writeArray(data)
}
return nil
}
2014-08-15 06:42:40 +04:00
func init() {
register("sadd", saddCommand)
register("scard", scardCommand)
register("sdiff", sdiffCommand)
register("sdiffstore", sdiffstoreCommand)
register("sinter", sinterCommand)
register("sinterstore", sinterstoreCommand)
register("sismember", sismemberCommand)
register("smembers", smembersCommand)
register("srem", sremCommand)
register("sunion", sunionCommand)
register("sunionstore", sunionstoreCommand)
register("sclear", sclearCommand)
register("smclear", smclearCommand)
register("sexpire", sexpireCommand)
register("sexpireat", sexpireAtCommand)
register("sttl", sttlCommand)
register("spersist", spersistCommand)
2014-08-26 19:21:45 +04:00
register("sscan", sscanCommand)
2014-08-15 06:42:40 +04:00
}