2014-11-27 09:03:44 +03:00
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
2014-12-01 12:50:48 +03:00
|
|
|
|
"fmt"
|
|
|
|
|
goledis "github.com/siddontang/ledisdb/client/go/ledis"
|
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":
|
|
|
|
|
return db.Scan(nil, count, false, "")
|
|
|
|
|
case "HASH":
|
|
|
|
|
return db.HScan(nil, count, false, "")
|
|
|
|
|
case "LIST":
|
|
|
|
|
return db.LScan(nil, count, false, "")
|
|
|
|
|
case "SET":
|
|
|
|
|
return db.SScan(nil, count, false, "")
|
|
|
|
|
case "ZSET":
|
|
|
|
|
return db.ZScan(nil, count, false, "")
|
|
|
|
|
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
|
|
|
|
|
} else if db >= uint64(ledis.MaxDBNumber) {
|
|
|
|
|
return fmt.Errorf("invalid db index %d, must < %d", db, ledis.MaxDBNumber)
|
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
|
|
conn := mc.Get()
|
|
|
|
|
|
|
|
|
|
//timeout is milliseconds
|
|
|
|
|
t := time.Duration(timeout) * time.Millisecond
|
|
|
|
|
conn.SetConnectTimeout(t)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
} else if db >= uint64(ledis.MaxDBNumber) {
|
|
|
|
|
return fmt.Errorf("invalid db index %d, must < %d", db, ledis.MaxDBNumber)
|
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)
|
|
|
|
|
|
|
|
|
|
conn := mc.Get()
|
|
|
|
|
|
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
|
|
|
|
|
conn.SetConnectTimeout(t)
|
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
|
|
|
|
}
|