ledisdb/server/cmd_script.go

214 lines
3.3 KiB
Go
Raw Normal View History

2014-09-01 19:26:35 +04:00
package server
import (
2014-09-02 18:04:18 +04:00
"crypto/sha1"
2014-09-02 13:55:12 +04:00
"encoding/hex"
2014-09-01 19:26:35 +04:00
"fmt"
"github.com/aarzilli/golua/lua"
"github.com/siddontang/ledisdb/ledis"
2014-09-02 13:55:12 +04:00
"strconv"
"strings"
2014-09-01 19:26:35 +04:00
)
2014-09-02 13:55:12 +04:00
func parseEvalArgs(l *lua.State, c *client) error {
args := c.args
if len(args) < 2 {
return ErrCmdParams
}
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
args = args[1:]
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
n, err := strconv.Atoi(ledis.String(args[0]))
if err != nil {
return err
}
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
if n > len(args)-1 {
return ErrCmdParams
}
luaSetGlobalArray(l, "KEYS", args[1:n+1])
luaSetGlobalArray(l, "ARGV", args[n+1:])
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
return nil
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
func evalGenericCommand(c *client, evalSha1 bool) error {
m, err := c.db.Multi()
if err != nil {
return err
}
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
s := c.app.s
luaClient := s.c
l := s.l
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
s.Lock()
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
base := l.GetTop()
defer func() {
l.SetTop(base)
luaClient.db = nil
luaClient.script = nil
s.Unlock()
m.Close()
}()
luaClient.db = m.DB
luaClient.script = m
luaClient.remoteAddr = c.remoteAddr
if err := parseEvalArgs(l, c); err != nil {
return err
2014-09-01 19:26:35 +04:00
}
2014-09-02 18:04:18 +04:00
var key string
2014-09-02 13:55:12 +04:00
if !evalSha1 {
2014-09-02 18:04:18 +04:00
h := sha1.Sum(c.args[0])
key = hex.EncodeToString(h[0:20])
2014-09-02 13:55:12 +04:00
} else {
2014-09-02 18:04:18 +04:00
key = strings.ToLower(ledis.String(c.args[0]))
2014-09-01 19:26:35 +04:00
}
2014-09-02 18:04:18 +04:00
l.GetGlobal(key)
2014-09-02 13:55:12 +04:00
if l.IsNil(-1) {
l.Pop(1)
if evalSha1 {
2014-09-02 18:04:18 +04:00
return fmt.Errorf("missing %s script", key)
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
if r := l.LoadString(ledis.String(c.args[0])); r != 0 {
err := fmt.Errorf("%s", l.ToString(-1))
l.Pop(1)
return err
} else {
l.PushValue(-1)
2014-09-02 18:04:18 +04:00
l.SetGlobal(key)
2014-09-02 13:55:12 +04:00
2014-09-02 18:04:18 +04:00
s.chunks[key] = struct{}{}
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
}
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
if err := l.Call(0, lua.LUA_MULTRET); err != nil {
return err
} else {
r := luaReplyToLedisReply(l)
m.Close()
writeValue(c.resp, r)
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
return nil
}
func evalCommand(c *client) error {
return evalGenericCommand(c, false)
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
func evalshaCommand(c *client) error {
return evalGenericCommand(c, true)
}
func scriptCommand(c *client) error {
s := c.app.s
l := s.l
s.Lock()
base := l.GetTop()
defer func() {
l.SetTop(base)
s.Unlock()
}()
args := c.args
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
switch strings.ToLower(c.cmd) {
case "script load":
return scriptLoadCommand(c)
case "script exists":
return scriptExistsCommand(c)
case "script flush":
return scriptFlushCommand(c)
default:
2014-09-02 18:04:18 +04:00
return fmt.Errorf("invalid script cmd %s", args[0])
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
return nil
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
func scriptLoadCommand(c *client) error {
s := c.app.s
l := s.l
if len(c.args) != 1 {
return ErrCmdParams
2014-09-01 19:26:35 +04:00
}
2014-09-02 18:04:18 +04:00
h := sha1.Sum(c.args[0])
key := hex.EncodeToString(h[0:20])
2014-09-01 19:26:35 +04:00
2014-09-02 18:04:18 +04:00
if r := l.LoadString(ledis.String(c.args[0])); r != 0 {
2014-09-02 13:55:12 +04:00
err := fmt.Errorf("%s", l.ToString(-1))
l.Pop(1)
return err
} else {
l.PushValue(-1)
2014-09-02 18:04:18 +04:00
l.SetGlobal(key)
2014-09-02 13:55:12 +04:00
2014-09-02 18:04:18 +04:00
s.chunks[key] = struct{}{}
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
2014-09-02 18:04:18 +04:00
c.resp.writeBulk(ledis.Slice(key))
2014-09-02 13:55:12 +04:00
return nil
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
func scriptExistsCommand(c *client) error {
s := c.app.s
2014-09-01 19:26:35 +04:00
2014-09-02 18:04:18 +04:00
if len(c.args) < 1 {
return ErrCmdParams
}
ay := make([]interface{}, len(c.args))
for i, n := range c.args {
2014-09-02 13:55:12 +04:00
if _, ok := s.chunks[ledis.String(n)]; ok {
ay[i] = int64(1)
} else {
ay[i] = int64(0)
2014-09-01 19:26:35 +04:00
}
}
2014-09-02 13:55:12 +04:00
c.resp.writeArray(ay)
return nil
2014-09-01 19:26:35 +04:00
}
2014-09-02 13:55:12 +04:00
func scriptFlushCommand(c *client) error {
s := c.app.s
l := s.l
for n, _ := range s.chunks {
l.PushNil()
l.SetGlobal(n)
}
2014-09-02 18:04:18 +04:00
s.chunks = map[string]struct{}{}
2014-09-02 13:55:12 +04:00
c.resp.writeStatus(OK)
return nil
}
2014-09-01 19:26:35 +04:00
2014-09-02 13:55:12 +04:00
func init() {
register("eval", evalCommand)
register("evalsha", evalshaCommand)
register("script load", scriptCommand)
register("script flush", scriptCommand)
register("script exists", scriptCommand)
2014-09-01 19:26:35 +04:00
}