ledisdb/server/cmd_set.go

285 lines
4.7 KiB
Go
Raw Normal View History

2014-08-15 06:42:40 +04:00
package server
import (
2020-04-24 09:10:03 +03:00
"github.com/ledisdb/ledisdb/ledis"
2014-08-15 06:42:40 +04:00
)
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
}
n, err := c.db.SAdd(args[0], args[1:]...)
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
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
}
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
}
n, err := c.db.SCard(args[0])
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
n, err := c.db.SIsMember(args[0], args[1])
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
v, err := c.db.SMembers(args[0])
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
n, err := c.db.SRem(args[0], args[1:]...)
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
n, err := c.db.SClear(args[0])
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
n, err := c.db.SMclear(args...)
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
v, err := c.db.SExpire(args[0], duration)
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
v, err := c.db.SExpireAt(args[0], when)
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
v, err := c.db.STTL(args[0])
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
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
}
n, err := c.db.SPersist(args[0])
if err != nil {
2014-08-15 06:42:40 +04:00
return err
}
c.resp.writeInteger(n)
2014-08-15 06:42:40 +04:00
return nil
}
func skeyexistsCommand(c *client) error {
2015-02-01 12:54:52 +03:00
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
n, err := c.db.SKeyExists(args[0])
if err != nil {
2015-02-01 12:54:52 +03:00
return err
}
c.resp.writeInteger(n)
2015-02-01 12:54:52 +03:00
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)
2014-08-15 06:42:40 +04:00
register("sclear", sclearCommand)
register("smclear", smclearCommand)
register("sexpire", sexpireCommand)
register("sexpireat", sexpireAtCommand)
register("sttl", sttlCommand)
register("spersist", spersistCommand)
register("skeyexists", skeyexistsCommand)
2015-02-01 12:54:52 +03:00
2014-08-15 06:42:40 +04:00
}