2014-05-16 11:03:23 +04:00
|
|
|
package server
|
2014-05-03 10:55:12 +04:00
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
import (
|
|
|
|
"errors"
|
2014-05-16 11:03:23 +04:00
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
2014-05-08 06:54:33 +04:00
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
//for simple implementation, we only support int64 score
|
|
|
|
|
|
|
|
var errScoreOverflow = errors.New("zset score overflow")
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zaddCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) < 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
if len(args[1:])%2 != 0 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[1:]
|
2014-05-16 04:56:32 +04:00
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
params := make([]ledis.ScorePair, len(args)/2)
|
2014-05-16 04:56:32 +04:00
|
|
|
for i := 0; i < len(params); i++ {
|
2014-05-16 11:03:23 +04:00
|
|
|
score, err := ledis.StrInt64(args[2*i], nil)
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-16 04:56:32 +04:00
|
|
|
params[i].Score = score
|
|
|
|
params[i].Member = args[2*i+1]
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZAdd(key, params...); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zcardCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZCard(args[0]); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zscoreCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if s, err := req.db.ZScore(args[0], args[1]); err != nil {
|
2014-06-05 06:20:50 +04:00
|
|
|
if err == ledis.ErrScoreMiss {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(nil)
|
2014-06-05 06:20:50 +04:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-08 06:54:33 +04:00
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(ledis.StrPutInt64(s))
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zremCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZRem(args[0], args[1:]...); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zincrbyCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
delta, err := ledis.StrInt64(args[1], nil)
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.ZIncrBy(key, delta, args[2]); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(ledis.StrPutInt64(v))
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func zparseScoreRange(minBuf []byte, maxBuf []byte) (min int64, max int64, err error) {
|
2014-05-16 11:03:23 +04:00
|
|
|
if strings.ToLower(ledis.String(minBuf)) == "-inf" {
|
2014-05-08 06:54:33 +04:00
|
|
|
min = math.MinInt64
|
|
|
|
} else {
|
|
|
|
var lopen bool = false
|
|
|
|
if minBuf[0] == '(' {
|
|
|
|
lopen = true
|
|
|
|
minBuf = minBuf[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(minBuf) == 0 {
|
|
|
|
err = ErrCmdParams
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
min, err = ledis.StrInt64(minBuf, nil)
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if min <= ledis.MinScore || min >= ledis.MaxScore {
|
2014-05-08 06:54:33 +04:00
|
|
|
err = errScoreOverflow
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if lopen {
|
|
|
|
min++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if strings.ToLower(ledis.String(maxBuf)) == "+inf" {
|
2014-05-08 06:54:33 +04:00
|
|
|
max = math.MaxInt64
|
|
|
|
} else {
|
|
|
|
var ropen = false
|
|
|
|
if maxBuf[0] == '(' {
|
|
|
|
ropen = true
|
|
|
|
maxBuf = maxBuf[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(maxBuf) == 0 {
|
|
|
|
err = ErrCmdParams
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
max, err = ledis.StrInt64(maxBuf, nil)
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if max <= ledis.MinScore || max >= ledis.MaxScore {
|
2014-05-08 06:54:33 +04:00
|
|
|
err = errScoreOverflow
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ropen {
|
|
|
|
max--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zcountCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
min, max, err := zparseScoreRange(args[1], args[2])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if min > max {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(0)
|
2014-05-08 06:54:33 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZCount(args[0], min, max); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrankCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZRank(args[0], args[1]); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else if n == -1 {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(nil)
|
2014-05-08 06:54:33 +04:00
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrevrankCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZRevRank(args[0], args[1]); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else if n == -1 {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeBulk(nil)
|
2014-05-08 06:54:33 +04:00
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zremrangebyrankCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
start, stop, err := zparseRange(req, args[1], args[2])
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZRemRangeByRank(key, start, stop); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zremrangebyscoreCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) != 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
min, max, err := zparseScoreRange(args[1], args[2])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZRemRangeByScore(key, min, max); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zparseRange(req *requestContext, a1 []byte, a2 []byte) (start int, stop int, err error) {
|
2014-05-16 11:03:23 +04:00
|
|
|
if start, err = strconv.Atoi(ledis.String(a1)); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if stop, err = strconv.Atoi(ledis.String(a2)); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrangeGeneric(req *requestContext, reverse bool) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) < 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
start, stop, err := zparseRange(req, args[1], args[2])
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[3:]
|
|
|
|
var withScores bool = false
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if len(args) > 0 && strings.ToLower(ledis.String(args[0])) == "withscores" {
|
2014-05-08 06:54:33 +04:00
|
|
|
withScores = true
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if datas, err := req.db.ZRangeGeneric(key, start, stop, reverse); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeScorePairArray(datas, withScores)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrangeCommand(req *requestContext) error {
|
|
|
|
return zrangeGeneric(req, false)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrevrangeCommand(req *requestContext) error {
|
|
|
|
return zrangeGeneric(req, true)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrangebyscoreGeneric(req *requestContext, reverse bool) error {
|
|
|
|
args := req.args
|
2014-05-08 06:54:33 +04:00
|
|
|
if len(args) < 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
key := args[0]
|
2014-07-01 03:55:34 +04:00
|
|
|
|
|
|
|
var minScore, maxScore []byte
|
|
|
|
|
|
|
|
if !reverse {
|
|
|
|
minScore, maxScore = args[1], args[2]
|
|
|
|
} else {
|
|
|
|
minScore, maxScore = args[2], args[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
min, max, err := zparseScoreRange(minScore, maxScore)
|
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[3:]
|
|
|
|
|
|
|
|
var withScores bool = false
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if len(args) > 0 && strings.ToLower(ledis.String(args[0])) == "withscores" {
|
2014-05-08 06:54:33 +04:00
|
|
|
withScores = true
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
var offset int = 0
|
2014-05-15 10:19:48 +04:00
|
|
|
var count int = -1
|
2014-05-08 06:54:33 +04:00
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
if len(args) != 3 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if strings.ToLower(ledis.String(args[0])) != "limit" {
|
2014-05-08 06:54:33 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if offset, err = strconv.Atoi(ledis.String(args[1])); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
if count, err = strconv.Atoi(ledis.String(args[2])); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
2014-06-22 06:39:23 +04:00
|
|
|
//for ledis, if offset < 0, a empty will return
|
2014-05-08 06:54:33 +04:00
|
|
|
//so here we directly return a empty array
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeArray([]interface{}{})
|
2014-05-08 06:54:33 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if datas, err := req.db.ZRangeByScoreGeneric(key, min, max, offset, count, reverse); err != nil {
|
2014-05-08 06:54:33 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeScorePairArray(datas, withScores)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrangebyscoreCommand(req *requestContext) error {
|
|
|
|
return zrangebyscoreGeneric(req, false)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zrevrangebyscoreCommand(req *requestContext) error {
|
|
|
|
return zrangebyscoreGeneric(req, true)
|
2014-05-08 06:54:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zclearCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-05-12 11:08:59 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZClear(args[0]); err != nil {
|
2014-05-12 11:08:59 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-05-12 11:08:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zmclearCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-27 12:28:26 +04:00
|
|
|
if len(args) < 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZMclear(args...); err != nil {
|
2014-06-27 12:28:26 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-06-27 12:28:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zexpireCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-30 07:23:14 +04:00
|
|
|
if len(args) != 2 {
|
2014-06-16 15:24:37 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
duration, err := ledis.StrInt64(args[1], nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.ZExpire(args[0], duration); err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(v)
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zexpireAtCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-30 07:23:14 +04:00
|
|
|
if len(args) != 2 {
|
2014-06-16 15:24:37 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
|
|
|
when, err := ledis.StrInt64(args[1], nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.ZExpireAt(args[0], when); err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(v)
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zttlCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-30 07:23:14 +04:00
|
|
|
if len(args) != 1 {
|
2014-06-16 15:24:37 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if v, err := req.db.ZTTL(args[0]); err != nil {
|
2014-06-16 15:24:37 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(v)
|
2014-06-16 15:24:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func zpersistCommand(req *requestContext) error {
|
|
|
|
args := req.args
|
2014-06-24 08:44:44 +04:00
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if n, err := req.db.ZPersist(args[0]); err != nil {
|
2014-06-24 08:44:44 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-07-31 14:28:19 +04:00
|
|
|
req.resp.writeInteger(n)
|
2014-06-24 08:44:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:55:12 +04:00
|
|
|
func init() {
|
|
|
|
register("zadd", zaddCommand)
|
|
|
|
register("zcard", zcardCommand)
|
|
|
|
register("zcount", zcountCommand)
|
|
|
|
register("zincrby", zincrbyCommand)
|
|
|
|
register("zrange", zrangeCommand)
|
|
|
|
register("zrangebyscore", zrangebyscoreCommand)
|
|
|
|
register("zrank", zrankCommand)
|
|
|
|
register("zrem", zremCommand)
|
|
|
|
register("zremrangebyrank", zremrangebyrankCommand)
|
|
|
|
register("zremrangebyscore", zremrangebyscoreCommand)
|
|
|
|
register("zrevrange", zrevrangeCommand)
|
|
|
|
register("zrevrank", zrevrankCommand)
|
|
|
|
register("zrevrangebyscore", zrevrangebyscoreCommand)
|
|
|
|
register("zscore", zscoreCommand)
|
2014-05-12 11:08:59 +04:00
|
|
|
|
|
|
|
//ledisdb special command
|
|
|
|
|
2014-06-16 15:24:37 +04:00
|
|
|
register("zclear", zclearCommand)
|
2014-06-27 12:28:26 +04:00
|
|
|
register("zmclear", zmclearCommand)
|
2014-06-16 15:24:37 +04:00
|
|
|
register("zexpire", zexpireCommand)
|
|
|
|
register("zexpireat", zexpireAtCommand)
|
|
|
|
register("zttl", zttlCommand)
|
2014-06-24 08:44:44 +04:00
|
|
|
register("zpersist", zpersistCommand)
|
2014-05-03 10:55:12 +04:00
|
|
|
}
|