2014-11-27 09:03:44 +03:00
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
2014-12-01 12:50:48 +03:00
|
|
|
|
"fmt"
|
2015-03-03 05:18:54 +03:00
|
|
|
|
goledis "github.com/siddontang/ledisdb/client/goledis"
|
2014-11-27 09:03:44 +03:00
|
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
2014-12-01 12:50:48 +03:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2014-11-27 09:03:44 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func dumpCommand(c *client) error {
|
|
|
|
|
if len(c.args) != 1 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := c.args[0]
|
|
|
|
|
if data, err := c.db.Dump(key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeBulk(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ldumpCommand(c *client) error {
|
|
|
|
|
if len(c.args) != 1 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := c.args[0]
|
|
|
|
|
if data, err := c.db.LDump(key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeBulk(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hdumpCommand(c *client) error {
|
|
|
|
|
if len(c.args) != 1 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := c.args[0]
|
|
|
|
|
if data, err := c.db.HDump(key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeBulk(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sdumpCommand(c *client) error {
|
|
|
|
|
if len(c.args) != 1 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := c.args[0]
|
|
|
|
|
if data, err := c.db.SDump(key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeBulk(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func zdumpCommand(c *client) error {
|
|
|
|
|
if len(c.args) != 1 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := c.args[0]
|
|
|
|
|
if data, err := c.db.ZDump(key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeBulk(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 12:50:48 +03:00
|
|
|
|
// unlike redis, restore will try to delete old key first
|
2014-11-27 09:03:44 +03:00
|
|
|
|
func restoreCommand(c *client) error {
|
|
|
|
|
args := c.args
|
|
|
|
|
if len(args) != 3 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := args[0]
|
|
|
|
|
ttl, err := ledis.StrInt64(args[1], nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
data := args[2]
|
|
|
|
|
|
|
|
|
|
if err = c.db.Restore(key, ttl, data); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeStatus(OK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 12:50:48 +03:00
|
|
|
|
func xdump(db *ledis.DB, tp string, key []byte) ([]byte, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var data []byte
|
2014-12-03 11:27:52 +03:00
|
|
|
|
switch strings.ToUpper(tp) {
|
|
|
|
|
case "KV":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
data, err = db.Dump(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "HASH":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
data, err = db.HDump(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "LIST":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
data, err = db.LDump(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "SET":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
data, err = db.SDump(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "ZSET":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
data, err = db.ZDump(key)
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("invalid key type %s", tp)
|
|
|
|
|
}
|
|
|
|
|
return data, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func xdel(db *ledis.DB, tp string, key []byte) error {
|
|
|
|
|
var err error
|
2014-12-03 11:27:52 +03:00
|
|
|
|
switch strings.ToUpper(tp) {
|
|
|
|
|
case "KV":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
_, err = db.Del(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "HASH":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
_, err = db.HClear(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "LIST":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
_, err = db.LClear(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "SET":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
_, err = db.SClear(key)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "ZSET":
|
2014-12-01 12:50:48 +03:00
|
|
|
|
_, err = db.ZClear(key)
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("invalid key type %s", tp)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
func xttl(db *ledis.DB, tp string, key []byte) (int64, error) {
|
|
|
|
|
switch strings.ToUpper(tp) {
|
|
|
|
|
case "KV":
|
|
|
|
|
return db.TTL(key)
|
|
|
|
|
case "HASH":
|
|
|
|
|
return db.HTTL(key)
|
|
|
|
|
case "LIST":
|
|
|
|
|
return db.LTTL(key)
|
|
|
|
|
case "SET":
|
|
|
|
|
return db.STTL(key)
|
|
|
|
|
case "ZSET":
|
|
|
|
|
return db.ZTTL(key)
|
|
|
|
|
default:
|
|
|
|
|
return 0, fmt.Errorf("invalid key type %s", tp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func xscan(db *ledis.DB, tp string, count int) ([][]byte, error) {
|
|
|
|
|
switch strings.ToUpper(tp) {
|
|
|
|
|
case "KV":
|
2015-03-02 06:10:54 +03:00
|
|
|
|
return db.Scan(KV, nil, count, false, "")
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "HASH":
|
2015-03-02 06:10:54 +03:00
|
|
|
|
return db.Scan(HASH, nil, count, false, "")
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "LIST":
|
2015-03-02 06:10:54 +03:00
|
|
|
|
return db.Scan(LIST, nil, count, false, "")
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "SET":
|
2015-03-02 06:10:54 +03:00
|
|
|
|
return db.Scan(SET, nil, count, false, "")
|
2014-12-03 11:27:52 +03:00
|
|
|
|
case "ZSET":
|
2015-03-02 06:10:54 +03:00
|
|
|
|
return db.Scan(ZSET, nil, count, false, "")
|
2014-12-03 11:27:52 +03:00
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("invalid key type %s", tp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 12:50:48 +03:00
|
|
|
|
func xdumpCommand(c *client) error {
|
|
|
|
|
args := c.args
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tp := string(args[0])
|
|
|
|
|
key := args[1]
|
|
|
|
|
|
|
|
|
|
if data, err := xdump(c.db, tp, key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
c.resp.writeBulk(data)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
func (app *App) getMigrateClient(addr string) *goledis.Client {
|
|
|
|
|
app.migrateM.Lock()
|
2014-12-01 12:50:48 +03:00
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
mc, ok := app.migrateClients[addr]
|
|
|
|
|
if !ok {
|
|
|
|
|
mc = goledis.NewClient(&goledis.Config{addr, 4, 0, 0})
|
|
|
|
|
app.migrateClients[addr] = mc
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.migrateM.Unlock()
|
|
|
|
|
|
|
|
|
|
return mc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//XMIGRATEDB host port tp count db timeout
|
|
|
|
|
//select count tp type keys and migrate
|
|
|
|
|
//will block any other write operations
|
|
|
|
|
//maybe only for xcodis
|
|
|
|
|
func xmigratedbCommand(c *client) error {
|
|
|
|
|
args := c.args
|
|
|
|
|
if len(args) != 6 {
|
2014-12-01 12:50:48 +03:00
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
addr := fmt.Sprintf("%s:%s", string(args[0]), string(args[1]))
|
|
|
|
|
if addr == c.app.cfg.Addr {
|
|
|
|
|
//same server, can not migrate
|
|
|
|
|
return fmt.Errorf("migrate in same server is not allowed")
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 12:50:48 +03:00
|
|
|
|
tp := string(args[2])
|
2014-12-03 11:27:52 +03:00
|
|
|
|
|
|
|
|
|
count, err := ledis.StrInt64(args[3], nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if count <= 0 {
|
|
|
|
|
count = 10
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 12:50:48 +03:00
|
|
|
|
db, err := ledis.StrUint64(args[4], nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2015-03-04 04:15:28 +03:00
|
|
|
|
} else if db >= uint64(c.app.cfg.Databases) {
|
|
|
|
|
return fmt.Errorf("invalid db index %d, must < %d", db, c.app.cfg.Databases)
|
2014-12-01 12:50:48 +03:00
|
|
|
|
}
|
2014-12-03 11:27:52 +03:00
|
|
|
|
|
|
|
|
|
timeout, err := ledis.StrInt64(args[5], nil)
|
2014-12-01 12:50:48 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if timeout < 0 {
|
|
|
|
|
return fmt.Errorf("invalid timeout %d", timeout)
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
m, err := c.db.Multi()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer m.Close()
|
|
|
|
|
|
|
|
|
|
keys, err := xscan(m.DB, tp, int(count))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if len(keys) == 0 {
|
|
|
|
|
c.resp.writeInteger(0)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mc := c.app.getMigrateClient(addr)
|
|
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
|
conn, err := mc.Get()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2014-12-03 11:27:52 +03:00
|
|
|
|
|
|
|
|
|
//timeout is milliseconds
|
|
|
|
|
t := time.Duration(timeout) * time.Millisecond
|
|
|
|
|
|
|
|
|
|
if _, err = conn.Do("select", db); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
data, err := xdump(m.DB, tp, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ttl, err := xttl(m.DB, tp, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn.SetReadDeadline(time.Now().Add(t))
|
|
|
|
|
|
|
|
|
|
//ttl is second, but restore need millisecond
|
|
|
|
|
if _, err = conn.Do("restore", key, ttl*1e3, data); err != nil {
|
|
|
|
|
return err
|
2014-12-01 12:50:48 +03:00
|
|
|
|
}
|
2014-12-03 11:27:52 +03:00
|
|
|
|
|
|
|
|
|
if err = xdel(m.DB, tp, key); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.resp.writeInteger(int64(len(keys)))
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//XMIGRATE host port type key destination-db timeout
|
|
|
|
|
//will block any other write operations
|
|
|
|
|
//maybe only for xcodis
|
|
|
|
|
func xmigrateCommand(c *client) error {
|
|
|
|
|
args := c.args
|
|
|
|
|
|
|
|
|
|
if len(args) != 6 {
|
|
|
|
|
return ErrCmdParams
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%s", string(args[0]), string(args[1]))
|
|
|
|
|
if addr == c.app.cfg.Addr {
|
|
|
|
|
//same server, can not migrate
|
|
|
|
|
return fmt.Errorf("migrate in same server is not allowed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tp := string(args[2])
|
|
|
|
|
key := args[3]
|
|
|
|
|
db, err := ledis.StrUint64(args[4], nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2015-03-04 04:15:28 +03:00
|
|
|
|
} else if db >= uint64(c.app.cfg.Databases) {
|
|
|
|
|
return fmt.Errorf("invalid db index %d, must < %d", db, c.app.cfg.Databases)
|
2014-12-01 12:50:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
timeout, err := ledis.StrInt64(args[5], nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if timeout < 0 {
|
|
|
|
|
return fmt.Errorf("invalid timeout %d", timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m, err := c.db.Multi()
|
|
|
|
|
if err != nil {
|
2014-12-01 12:50:48 +03:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer m.Close()
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
data, err := xdump(m.DB, tp, key)
|
2014-12-01 12:50:48 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if data == nil {
|
|
|
|
|
c.resp.writeStatus(NOKEY)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
ttl, err := xttl(m.DB, tp, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2014-12-01 12:50:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
mc := c.app.getMigrateClient(addr)
|
|
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
|
conn, err := mc.Get()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2014-12-03 11:27:52 +03:00
|
|
|
|
|
2014-12-01 12:50:48 +03:00
|
|
|
|
//timeout is milliseconds
|
2014-12-03 11:27:52 +03:00
|
|
|
|
t := time.Duration(timeout) * time.Millisecond
|
2014-12-01 12:50:48 +03:00
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
if _, err = conn.Do("select", db); err != nil {
|
2014-12-01 12:50:48 +03:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 11:27:52 +03:00
|
|
|
|
conn.SetReadDeadline(time.Now().Add(t))
|
|
|
|
|
|
|
|
|
|
//ttl is second, but restore need millisecond
|
|
|
|
|
if _, err = conn.Do("restore", key, ttl*1e3, data); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = xdel(m.DB, tp, key); err != nil {
|
|
|
|
|
return err
|
2014-12-01 12:50:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.resp.writeStatus(OK)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-27 09:03:44 +03:00
|
|
|
|
func init() {
|
|
|
|
|
register("dump", dumpCommand)
|
|
|
|
|
register("ldump", ldumpCommand)
|
|
|
|
|
register("hdump", hdumpCommand)
|
|
|
|
|
register("sdump", sdumpCommand)
|
|
|
|
|
register("zdump", zdumpCommand)
|
|
|
|
|
register("restore", restoreCommand)
|
2014-12-01 12:50:48 +03:00
|
|
|
|
register("xdump", xdumpCommand)
|
|
|
|
|
register("xmigrate", xmigrateCommand)
|
2014-12-03 11:27:52 +03:00
|
|
|
|
register("xmigratedb", xmigratedbCommand)
|
2014-11-27 09:03:44 +03:00
|
|
|
|
}
|