use goredis client

This commit is contained in:
siddontang 2015-03-11 11:54:02 +08:00
parent ce8e0d4fdc
commit 186ed2f84f
31 changed files with 504 additions and 453 deletions

6
Godeps/Godeps.json generated
View File

@ -1,6 +1,6 @@
{ {
"ImportPath": "github.com/siddontang/ledisdb", "ImportPath": "github.com/siddontang/ledisdb",
"GoVersion": "go1.4", "GoVersion": "go1.4.2",
"Packages": [ "Packages": [
"./..." "./..."
], ],
@ -58,6 +58,10 @@
"ImportPath": "github.com/siddontang/go/sync2", "ImportPath": "github.com/siddontang/go/sync2",
"Rev": "c2b33271306fcb7c6532efceac33ec45ee2439e0" "Rev": "c2b33271306fcb7c6532efceac33ec45ee2439e0"
}, },
{
"ImportPath": "github.com/siddontang/goredis",
"Rev": "6d2857b0488d1e8b9f96b46802eacb68e29fb003"
},
{ {
"ImportPath": "github.com/siddontang/rdb", "ImportPath": "github.com/siddontang/rdb",
"Rev": "fc89ed2e418d27e3ea76e708e54276d2b44ae9cf" "Rev": "fc89ed2e418d27e3ea76e708e54276d2b44ae9cf"

View File

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2014 siddontang Copyright (c) 2015 siddontang
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.

View File

@ -1,4 +1,4 @@
package ledis package goredis
import ( import (
"container/list" "container/list"
@ -7,17 +7,23 @@ import (
"sync" "sync"
) )
type Config struct { type PoolConn struct {
Addr string *Conn
MaxIdleConns int c *Client
ReadBufferSize int }
WriteBufferSize int
func (c *PoolConn) Close() {
c.c.put(c.Conn)
} }
type Client struct { type Client struct {
sync.Mutex sync.Mutex
cfg *Config addr string
maxIdleConns int
readBufferSize int
writeBufferSize int
password string
conns *list.List conns *list.List
} }
@ -30,22 +36,36 @@ func getProto(addr string) string {
} }
} }
func NewClient(cfg *Config) *Client { func NewClient(addr string, password string) *Client {
c := new(Client) c := new(Client)
c.cfg = cfg c.addr = addr
if c.cfg.ReadBufferSize == 0 { c.maxIdleConns = 4
c.cfg.ReadBufferSize = 4096 c.readBufferSize = 1024
} c.writeBufferSize = 1024
if c.cfg.WriteBufferSize == 0 { c.password = password
c.cfg.WriteBufferSize = 4096
}
c.conns = list.New() c.conns = list.New()
return c return c
} }
func (c *Client) SetPassword(pass string) {
c.password = pass
}
func (c *Client) SetReadBufferSize(s int) {
c.readBufferSize = s
}
func (c *Client) SetWriteBufferSize(s int) {
c.writeBufferSize = s
}
func (c *Client) SetMaxIdleConns(n int) {
c.maxIdleConns = n
}
func (c *Client) Do(cmd string, args ...interface{}) (interface{}, error) { func (c *Client) Do(cmd string, args ...interface{}) (interface{}, error) {
var co *Conn var co *Conn
var err error var err error
@ -59,7 +79,7 @@ func (c *Client) Do(cmd string, args ...interface{}) (interface{}, error) {
r, err = co.Do(cmd, args...) r, err = co.Do(cmd, args...)
if err != nil { if err != nil {
co.finalize() co.Close()
if e, ok := err.(*net.OpError); ok && strings.Contains(e.Error(), "use of closed network connection") { if e, ok := err.(*net.OpError); ok && strings.Contains(e.Error(), "use of closed network connection") {
//send to a closed connection, try again //send to a closed connection, try again
@ -86,36 +106,41 @@ func (c *Client) Close() {
co := e.Value.(*Conn) co := e.Value.(*Conn)
c.conns.Remove(e) c.conns.Remove(e)
co.finalize() co.Close()
} }
} }
func (c *Client) Get() (*Conn, error) { func (c *Client) Get() (*PoolConn, error) {
return c.get() co, err := c.get()
if err != nil {
return nil, err
}
return &PoolConn{co, c}, err
} }
func (c *Client) get() (*Conn, error) { func (c *Client) get() (co *Conn, err error) {
c.Lock() c.Lock()
if c.conns.Len() == 0 { if c.conns.Len() == 0 {
c.Unlock() c.Unlock()
return c.newConn(c.cfg.Addr) co, err = c.newConn(c.addr, c.password)
} else { } else {
e := c.conns.Front() e := c.conns.Front()
co := e.Value.(*Conn) co = e.Value.(*Conn)
c.conns.Remove(e) c.conns.Remove(e)
c.Unlock() c.Unlock()
return co, nil
} }
return
} }
func (c *Client) put(conn *Conn) { func (c *Client) put(conn *Conn) {
c.Lock() c.Lock()
if c.conns.Len() >= c.cfg.MaxIdleConns { if c.conns.Len() >= c.maxIdleConns {
c.Unlock() c.Unlock()
conn.finalize() conn.Close()
} else { } else {
c.conns.PushFront(conn) c.conns.PushFront(conn)
c.Unlock() c.Unlock()

View File

@ -1,4 +1,4 @@
package ledis package goredis
import ( import (
"bufio" "bufio"
@ -16,9 +16,14 @@ type Error string
func (err Error) Error() string { return string(err) } func (err Error) Error() string { return string(err) }
type Conn struct { type sizeWriter int64
client *Client
func (s *sizeWriter) Write(p []byte) (int, error) {
*s += sizeWriter(len(p))
return len(p), nil
}
type Conn struct {
c net.Conn c net.Conn
br *bufio.Reader br *bufio.Reader
bw *bufio.Writer bw *bufio.Writer
@ -29,10 +34,13 @@ type Conn struct {
// Scratch space for formatting integers and floats. // Scratch space for formatting integers and floats.
numScratch [40]byte numScratch [40]byte
totalReadSize sizeWriter
totalWriteSize sizeWriter
} }
func Connect(addr string) (*Conn, error) { func Connect(addr string) (*Conn, error) {
return ConnectWithSize(addr, 4096, 4096) return ConnectWithSize(addr, 1024, 1024)
} }
func ConnectWithSize(addr string, readSize int, writeSize int) (*Conn, error) { func ConnectWithSize(addr string, readSize int, writeSize int) (*Conn, error) {
@ -44,18 +52,22 @@ func ConnectWithSize(addr string, readSize int, writeSize int) (*Conn, error) {
return nil, err return nil, err
} }
c.br = bufio.NewReaderSize(c.c, readSize) c.br = bufio.NewReaderSize(io.TeeReader(c.c, &c.totalReadSize), readSize)
c.bw = bufio.NewWriterSize(c.c, writeSize) c.bw = bufio.NewWriterSize(io.MultiWriter(c.c, &c.totalWriteSize), writeSize)
return c, nil return c, nil
} }
func (c *Conn) Close() { func (c *Conn) Close() {
if c.client != nil { c.c.Close()
c.client.put(c) }
} else {
c.finalize() func (c *Conn) GetTotalReadSize() int64 {
} return int64(c.totalReadSize)
}
func (c *Conn) GetTotalWriteSize() int64 {
return int64(c.totalWriteSize)
} }
func (c *Conn) SetReadDeadline(t time.Time) { func (c *Conn) SetReadDeadline(t time.Time) {
@ -76,12 +88,12 @@ func (c *Conn) Do(cmd string, args ...interface{}) (interface{}, error) {
func (c *Conn) Send(cmd string, args ...interface{}) error { func (c *Conn) Send(cmd string, args ...interface{}) error {
if err := c.writeCommand(cmd, args); err != nil { if err := c.writeCommand(cmd, args); err != nil {
c.finalize() c.Close()
return err return err
} }
if err := c.bw.Flush(); err != nil { if err := c.bw.Flush(); err != nil {
c.finalize() c.Close()
return err return err
} }
return nil return nil
@ -89,7 +101,7 @@ func (c *Conn) Send(cmd string, args ...interface{}) error {
func (c *Conn) Receive() (interface{}, error) { func (c *Conn) Receive() (interface{}, error) {
if reply, err := c.readReply(); err != nil { if reply, err := c.readReply(); err != nil {
c.finalize() c.Close()
return nil, err return nil, err
} else { } else {
if e, ok := reply.(Error); ok { if e, ok := reply.(Error); ok {
@ -104,16 +116,12 @@ func (c *Conn) ReceiveBulkTo(w io.Writer) error {
err := c.readBulkReplyTo(w) err := c.readBulkReplyTo(w)
if err != nil { if err != nil {
if _, ok := err.(Error); !ok { if _, ok := err.(Error); !ok {
c.finalize() c.Close()
} }
} }
return err return err
} }
func (c *Conn) finalize() {
c.c.Close()
}
func (c *Conn) writeLen(prefix byte, n int) error { func (c *Conn) writeLen(prefix byte, n int) error {
c.lenScratch[len(c.lenScratch)-1] = '\n' c.lenScratch[len(c.lenScratch)-1] = '\n'
c.lenScratch[len(c.lenScratch)-2] = '\r' c.lenScratch[len(c.lenScratch)-2] = '\r'
@ -191,14 +199,14 @@ func (c *Conn) writeCommand(cmd string, args []interface{}) (err error) {
func (c *Conn) readLine() ([]byte, error) { func (c *Conn) readLine() ([]byte, error) {
p, err := c.br.ReadSlice('\n') p, err := c.br.ReadSlice('\n')
if err == bufio.ErrBufferFull { if err == bufio.ErrBufferFull {
return nil, errors.New("ledis: long response line") return nil, errors.New("long response line")
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
i := len(p) - 2 i := len(p) - 2
if i < 0 || p[i] != '\r' { if i < 0 || p[i] != '\r' {
return nil, errors.New("ledis: bad response line terminator") return nil, errors.New("bad response line terminator")
} }
return p[:i], nil return p[:i], nil
} }
@ -206,7 +214,7 @@ func (c *Conn) readLine() ([]byte, error) {
// parseLen parses bulk string and array lengths. // parseLen parses bulk string and array lengths.
func parseLen(p []byte) (int, error) { func parseLen(p []byte) (int, error) {
if len(p) == 0 { if len(p) == 0 {
return -1, errors.New("ledis: malformed length") return -1, errors.New("malformed length")
} }
if p[0] == '-' && len(p) == 2 && p[1] == '1' { if p[0] == '-' && len(p) == 2 && p[1] == '1' {
@ -218,7 +226,7 @@ func parseLen(p []byte) (int, error) {
for _, b := range p { for _, b := range p {
n *= 10 n *= 10
if b < '0' || b > '9' { if b < '0' || b > '9' {
return -1, errors.New("ledis: illegal bytes in length") return -1, errors.New("illegal bytes in length")
} }
n += int(b - '0') n += int(b - '0')
} }
@ -229,7 +237,7 @@ func parseLen(p []byte) (int, error) {
// parseInt parses an integer reply. // parseInt parses an integer reply.
func parseInt(p []byte) (interface{}, error) { func parseInt(p []byte) (interface{}, error) {
if len(p) == 0 { if len(p) == 0 {
return 0, errors.New("ledis: malformed integer") return 0, errors.New("malformed integer")
} }
var negate bool var negate bool
@ -237,7 +245,7 @@ func parseInt(p []byte) (interface{}, error) {
negate = true negate = true
p = p[1:] p = p[1:]
if len(p) == 0 { if len(p) == 0 {
return 0, errors.New("ledis: malformed integer") return 0, errors.New("malformed integer")
} }
} }
@ -245,7 +253,7 @@ func parseInt(p []byte) (interface{}, error) {
for _, b := range p { for _, b := range p {
n *= 10 n *= 10
if b < '0' || b > '9' { if b < '0' || b > '9' {
return 0, errors.New("ledis: illegal bytes in length") return 0, errors.New("illegal bytes in length")
} }
n += int64(b - '0') n += int64(b - '0')
} }
@ -288,11 +296,11 @@ func (c *Conn) readBulkReplyTo(w io.Writer) error {
if line, err := c.readLine(); err != nil { if line, err := c.readLine(); err != nil {
return err return err
} else if len(line) != 0 { } else if len(line) != 0 {
return errors.New("ledis: bad bulk string format") return errors.New("bad bulk string format")
} }
return nil return nil
default: default:
return fmt.Errorf("ledis: not invalid bulk string type, but %c", line[0]) return fmt.Errorf("not invalid bulk string type, but %c", line[0])
} }
} }
@ -302,7 +310,7 @@ func (c *Conn) readReply() (interface{}, error) {
return nil, err return nil, err
} }
if len(line) == 0 { if len(line) == 0 {
return nil, errors.New("ledis: short response line") return nil, errors.New("short response line")
} }
switch line[0] { switch line[0] {
case '+': case '+':
@ -333,7 +341,7 @@ func (c *Conn) readReply() (interface{}, error) {
if line, err := c.readLine(); err != nil { if line, err := c.readLine(); err != nil {
return nil, err return nil, err
} else if len(line) != 0 { } else if len(line) != 0 {
return nil, errors.New("ledis: bad bulk string format") return nil, errors.New("bad bulk string format")
} }
return p, nil return p, nil
case '*': case '*':
@ -350,15 +358,22 @@ func (c *Conn) readReply() (interface{}, error) {
} }
return r, nil return r, nil
} }
return nil, errors.New("ledis: unexpected response line") return nil, errors.New("unexpected response line")
} }
func (c *Client) newConn(addr string) (*Conn, error) { func (c *Client) newConn(addr string, pass string) (*Conn, error) {
co, err := ConnectWithSize(addr, c.cfg.ReadBufferSize, c.cfg.WriteBufferSize) co, err := ConnectWithSize(addr, c.readBufferSize, c.writeBufferSize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
co.client = c
if len(pass) > 0 {
_, err = co.Do("AUTH", pass)
if err != nil {
co.Close()
return nil, err
}
}
return co, nil return co, nil
} }

View File

@ -0,0 +1,34 @@
// Package goredis is a client for the redis and ledisdb.
//
// Client
//
// The client is the primary interface for redis. You must first create a client with redis address for working.
//
// c := NewClient("127.0.0.1:6380")
//
// The most important function for client is Do function to send commands to remote server.
//
// reply, err := c.Do("ping")
//
// reply, err := c.Do("set", "key", "value")
//
// reply, err := c.Do("get", "key")
//
// Connection
//
// You can use an independent connection to send commands.
//
// //get a connection
// conn, _ := c.Get()
//
// //connection send command
// conn.Do("ping")
//
// Reply Helper
//
// You can use reply helper to convert a reply to a specific type.
//
// exists, err := Bool(c.Do("exists", "key"))
//
// score, err := Int64(c.Do("zscore", "key", "member"))
package goredis

View File

@ -0,0 +1,39 @@
package goredis
import (
"github.com/alicebob/miniredis"
"testing"
)
func Test(t *testing.T) {
s, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer s.Close()
s.RequireAuth("123456")
addr := s.Addr()
c := NewClient(addr, "123456")
defer c.Close()
conn, err := c.Get()
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if pong, err := String(conn.Do("PING")); err != nil {
t.Fatal(err)
} else if pong != "PONG" {
t.Fatal(pong)
}
if pong, err := String(conn.Do("PING")); err != nil {
t.Fatal(err)
} else if pong != "PONG" {
t.Fatal(pong)
}
}

View File

@ -1,4 +1,4 @@
package ledis package goredis
import ( import (
"errors" "errors"
@ -7,7 +7,7 @@ import (
) )
// ErrNil indicates that a reply value is nil. // ErrNil indicates that a reply value is nil.
var ErrNil = errors.New("ledis: nil returned") var ErrNil = errors.New("nil returned")
// Int is a helper that converts a command reply to an integer. If err is not // Int is a helper that converts a command reply to an integer. If err is not
// equal to nil, then Int returns 0, err. Otherwise, Int converts the // equal to nil, then Int returns 0, err. Otherwise, Int converts the
@ -37,7 +37,7 @@ func Int(reply interface{}, err error) (int, error) {
case Error: case Error:
return 0, reply return 0, reply
} }
return 0, fmt.Errorf("ledis: unexpected type for Int, got type %T", reply) return 0, fmt.Errorf("unexpected type for Int, got type %T", reply)
} }
// Int64 is a helper that converts a command reply to 64 bit integer. If err is // Int64 is a helper that converts a command reply to 64 bit integer. If err is
@ -64,10 +64,10 @@ func Int64(reply interface{}, err error) (int64, error) {
case Error: case Error:
return 0, reply return 0, reply
} }
return 0, fmt.Errorf("ledis: unexpected type for Int64, got type %T", reply) return 0, fmt.Errorf("unexpected type for Int64, got type %T", reply)
} }
var errNegativeInt = errors.New("ledis: unexpected value for Uint64") var errNegativeInt = errors.New("unexpected value for Uint64")
// Uint64 is a helper that converts a command reply to 64 bit integer. If err is // Uint64 is a helper that converts a command reply to 64 bit integer. If err is
// not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the // not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
@ -96,7 +96,7 @@ func Uint64(reply interface{}, err error) (uint64, error) {
case Error: case Error:
return 0, reply return 0, reply
} }
return 0, fmt.Errorf("ledis: unexpected type for Uint64, got type %T", reply) return 0, fmt.Errorf("unexpected type for Uint64, got type %T", reply)
} }
// Float64 is a helper that converts a command reply to 64 bit float. If err is // Float64 is a helper that converts a command reply to 64 bit float. If err is
@ -120,7 +120,7 @@ func Float64(reply interface{}, err error) (float64, error) {
case Error: case Error:
return 0, reply return 0, reply
} }
return 0, fmt.Errorf("ledis: unexpected type for Float64, got type %T", reply) return 0, fmt.Errorf("unexpected type for Float64, got type %T", reply)
} }
// String is a helper that converts a command reply to a string. If err is not // String is a helper that converts a command reply to a string. If err is not
@ -146,7 +146,7 @@ func String(reply interface{}, err error) (string, error) {
case Error: case Error:
return "", reply return "", reply
} }
return "", fmt.Errorf("ledis: unexpected type for String, got type %T", reply) return "", fmt.Errorf("unexpected type for String, got type %T", reply)
} }
// Bytes is a helper that converts a command reply to a slice of bytes. If err // Bytes is a helper that converts a command reply to a slice of bytes. If err
@ -172,7 +172,7 @@ func Bytes(reply interface{}, err error) ([]byte, error) {
case Error: case Error:
return nil, reply return nil, reply
} }
return nil, fmt.Errorf("ledis: unexpected type for Bytes, got type %T", reply) return nil, fmt.Errorf("unexpected type for Bytes, got type %T", reply)
} }
// Bool is a helper that converts a command reply to a boolean. If err is not // Bool is a helper that converts a command reply to a boolean. If err is not
@ -198,7 +198,7 @@ func Bool(reply interface{}, err error) (bool, error) {
case Error: case Error:
return false, reply return false, reply
} }
return false, fmt.Errorf("ledis: unexpected type for Bool, got type %T", reply) return false, fmt.Errorf("unexpected type for Bool, got type %T", reply)
} }
// MultiBulk is deprecated. Use Values. // MultiBulk is deprecated. Use Values.
@ -224,7 +224,7 @@ func Values(reply interface{}, err error) ([]interface{}, error) {
case Error: case Error:
return nil, reply return nil, reply
} }
return nil, fmt.Errorf("ledis: unexpected type for Values, got type %T", reply) return nil, fmt.Errorf("unexpected type for Values, got type %T", reply)
} }
// Strings is a helper that converts an array command reply to a []string. If // Strings is a helper that converts an array command reply to a []string. If
@ -243,7 +243,7 @@ func Strings(reply interface{}, err error) ([]string, error) {
} }
p, ok := reply[i].([]byte) p, ok := reply[i].([]byte)
if !ok { if !ok {
return nil, fmt.Errorf("ledis: unexpected element type for Strings, got type %T", reply[i]) return nil, fmt.Errorf("unexpected element type for Strings, got type %T", reply[i])
} }
result[i] = string(p) result[i] = string(p)
} }
@ -253,5 +253,5 @@ func Strings(reply interface{}, err error) ([]string, error) {
case Error: case Error:
return nil, reply return nil, reply
} }
return nil, fmt.Errorf("ledis: unexpected type for Strings, got type %T", reply) return nil, fmt.Errorf("unexpected type for Strings, got type %T", reply)
} }

View File

@ -19,3 +19,5 @@ go get -u github.com/syndtr/goleveldb/leveldb
go get -u github.com/cupcake/rdb go get -u github.com/cupcake/rdb
go get -u github.com/siddontang/go go get -u github.com/siddontang/go
go get -u github.com/siddontang/goredis
go get -u github.com/siddontang/rdb

View File

@ -1 +0,0 @@
[Clients](https://github.com/siddontang/ledisdb/wiki/Clients)

View File

@ -1,45 +0,0 @@
// Package ledis is a client for the ledisdb.
//
// Config
//
// Config struct contains configuration for ledisdb:
//
// Addr ledisdb server address, like 127.0.0.1:6380
// MaxIdleConns max idle connections for ledisdb
//
// Client
//
// The client is the primary interface for ledisdb. You must first create a client with proper config for working.
//
// cfg := new(Config)
// cfg.Addr = "127.0.0.1:6380"
// cfg.MaxIdleConns = 4
//
// c := NewClient(cfg)
//
// The most important function for client is Do function to send commands to remote server.
//
// reply, err := c.Do("ping")
//
// reply, err := c.Do("set", "key", "value")
//
// reply, err := c.Do("get", "key")
//
// Connection
//
// You can use an independent connection to send commands.
//
// //get a connection
// conn := c.Get()
//
// //connection send command
// conn.Do("ping")
//
// Reply Helper
//
// You can use reply helper to convert a reply to a specific type.
//
// exists, err := ledis.Bool(c.Do("exists", "key"))
//
// score, err := ledis.Int64(c.Do("zscore", "key", "member"))
package ledis

View File

@ -1,15 +0,0 @@
package ledis
import (
"testing"
)
func TestClient(t *testing.T) {
cfg := new(Config)
cfg.Addr = "127.0.0.1:6380"
cfg.MaxIdleConns = 4
c := NewClient(cfg)
c.Close()
}

View File

@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"math/rand" "math/rand"
"runtime" "runtime"
"strings" "strings"
@ -21,10 +21,10 @@ var valueSize = flag.Int("vsize", 100, "kv value size")
var tests = flag.String("t", "set,get,randget,del,lpush,lrange,lpop,hset,hget,hdel,zadd,zincr,zrange,zrevrange,zdel", "only run the comma separated list of tests") var tests = flag.String("t", "set,get,randget,del,lpush,lrange,lpop,hset,hget,hdel,zadd,zincr,zrange,zrevrange,zdel", "only run the comma separated list of tests")
var wg sync.WaitGroup var wg sync.WaitGroup
var client *ledis.Client var client *goredis.Client
var loop int = 0 var loop int = 0
func waitBench(c *ledis.Conn, cmd string, args ...interface{}) { func waitBench(c *goredis.PoolConn, cmd string, args ...interface{}) {
_, err := c.Do(strings.ToUpper(cmd), args...) _, err := c.Do(strings.ToUpper(cmd), args...)
if err != nil { if err != nil {
fmt.Printf("do %s error %s\n", cmd, err.Error()) fmt.Printf("do %s error %s\n", cmd, err.Error())
@ -32,7 +32,7 @@ func waitBench(c *ledis.Conn, cmd string, args ...interface{}) {
} }
func bench(cmd string, f func(c *ledis.Conn)) { func bench(cmd string, f func(c *goredis.PoolConn)) {
wg.Add(*clients) wg.Add(*clients)
t1 := time.Now() t1 := time.Now()
@ -66,7 +66,7 @@ var kvIncrBase int64 = 0
var kvDelBase int64 = 0 var kvDelBase int64 = 0
func benchSet() { func benchSet() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
value := make([]byte, *valueSize) value := make([]byte, *valueSize)
n := atomic.AddInt64(&kvSetBase, 1) n := atomic.AddInt64(&kvSetBase, 1)
waitBench(c, "SET", n, value) waitBench(c, "SET", n, value)
@ -76,7 +76,7 @@ func benchSet() {
} }
func benchGet() { func benchGet() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := atomic.AddInt64(&kvGetBase, 1) n := atomic.AddInt64(&kvGetBase, 1)
waitBench(c, "GET", n) waitBench(c, "GET", n)
} }
@ -85,7 +85,7 @@ func benchGet() {
} }
func benchRandGet() { func benchRandGet() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := rand.Int() % *number n := rand.Int() % *number
waitBench(c, "GET", n) waitBench(c, "GET", n)
} }
@ -94,7 +94,7 @@ func benchRandGet() {
} }
func benchDel() { func benchDel() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := atomic.AddInt64(&kvDelBase, 1) n := atomic.AddInt64(&kvDelBase, 1)
waitBench(c, "DEL", n) waitBench(c, "DEL", n)
} }
@ -103,7 +103,7 @@ func benchDel() {
} }
func benchPushList() { func benchPushList() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
value := make([]byte, 100) value := make([]byte, 100)
waitBench(c, "RPUSH", "mytestlist", value) waitBench(c, "RPUSH", "mytestlist", value)
} }
@ -112,7 +112,7 @@ func benchPushList() {
} }
func benchRangeList10() { func benchRangeList10() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "LRANGE", "mytestlist", 0, 10) waitBench(c, "LRANGE", "mytestlist", 0, 10)
} }
@ -120,7 +120,7 @@ func benchRangeList10() {
} }
func benchRangeList50() { func benchRangeList50() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "LRANGE", "mytestlist", 0, 50) waitBench(c, "LRANGE", "mytestlist", 0, 50)
} }
@ -128,7 +128,7 @@ func benchRangeList50() {
} }
func benchRangeList100() { func benchRangeList100() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "LRANGE", "mytestlist", 0, 100) waitBench(c, "LRANGE", "mytestlist", 0, 100)
} }
@ -136,7 +136,7 @@ func benchRangeList100() {
} }
func benchPopList() { func benchPopList() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "LPOP", "mytestlist") waitBench(c, "LPOP", "mytestlist")
} }
@ -149,7 +149,7 @@ var hashGetBase int64 = 0
var hashDelBase int64 = 0 var hashDelBase int64 = 0
func benchHset() { func benchHset() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
value := make([]byte, 100) value := make([]byte, 100)
n := atomic.AddInt64(&hashSetBase, 1) n := atomic.AddInt64(&hashSetBase, 1)
@ -160,7 +160,7 @@ func benchHset() {
} }
func benchHGet() { func benchHGet() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := atomic.AddInt64(&hashGetBase, 1) n := atomic.AddInt64(&hashGetBase, 1)
waitBench(c, "HGET", "myhashkey", n) waitBench(c, "HGET", "myhashkey", n)
} }
@ -169,7 +169,7 @@ func benchHGet() {
} }
func benchHRandGet() { func benchHRandGet() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := rand.Int() % *number n := rand.Int() % *number
waitBench(c, "HGET", "myhashkey", n) waitBench(c, "HGET", "myhashkey", n)
} }
@ -178,7 +178,7 @@ func benchHRandGet() {
} }
func benchHDel() { func benchHDel() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := atomic.AddInt64(&hashDelBase, 1) n := atomic.AddInt64(&hashDelBase, 1)
waitBench(c, "HDEL", "myhashkey", n) waitBench(c, "HDEL", "myhashkey", n)
} }
@ -191,7 +191,7 @@ var zsetDelBase int64 = 0
var zsetIncrBase int64 = 0 var zsetIncrBase int64 = 0
func benchZAdd() { func benchZAdd() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
member := make([]byte, 16) member := make([]byte, 16)
n := atomic.AddInt64(&zsetAddBase, 1) n := atomic.AddInt64(&zsetAddBase, 1)
waitBench(c, "ZADD", "myzsetkey", n, member) waitBench(c, "ZADD", "myzsetkey", n, member)
@ -201,7 +201,7 @@ func benchZAdd() {
} }
func benchZDel() { func benchZDel() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := atomic.AddInt64(&zsetDelBase, 1) n := atomic.AddInt64(&zsetDelBase, 1)
waitBench(c, "ZREM", "myzsetkey", n) waitBench(c, "ZREM", "myzsetkey", n)
} }
@ -210,7 +210,7 @@ func benchZDel() {
} }
func benchZIncr() { func benchZIncr() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
n := atomic.AddInt64(&zsetIncrBase, 1) n := atomic.AddInt64(&zsetIncrBase, 1)
waitBench(c, "ZINCRBY", "myzsetkey", 1, n) waitBench(c, "ZINCRBY", "myzsetkey", 1, n)
} }
@ -219,7 +219,7 @@ func benchZIncr() {
} }
func benchZRangeByScore() { func benchZRangeByScore() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "ZRANGEBYSCORE", "myzsetkey", 0, rand.Int(), "withscores", "limit", rand.Int()%100, 100) waitBench(c, "ZRANGEBYSCORE", "myzsetkey", 0, rand.Int(), "withscores", "limit", rand.Int()%100, 100)
} }
@ -227,7 +227,7 @@ func benchZRangeByScore() {
} }
func benchZRangeByRank() { func benchZRangeByRank() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "ZRANGE", "myzsetkey", 0, rand.Int()%100) waitBench(c, "ZRANGE", "myzsetkey", 0, rand.Int()%100)
} }
@ -235,7 +235,7 @@ func benchZRangeByRank() {
} }
func benchZRevRangeByScore() { func benchZRevRangeByScore() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "ZREVRANGEBYSCORE", "myzsetkey", 0, rand.Int(), "withscores", "limit", rand.Int()%100, 100) waitBench(c, "ZREVRANGEBYSCORE", "myzsetkey", 0, rand.Int(), "withscores", "limit", rand.Int()%100, 100)
} }
@ -243,7 +243,7 @@ func benchZRevRangeByScore() {
} }
func benchZRevRangeByRank() { func benchZRevRangeByRank() {
f := func(c *ledis.Conn) { f := func(c *goredis.PoolConn) {
waitBench(c, "ZREVRANGE", "myzsetkey", 0, rand.Int()%100) waitBench(c, "ZREVRANGE", "myzsetkey", 0, rand.Int()%100)
} }
@ -269,12 +269,10 @@ func main() {
addr := fmt.Sprintf("%s:%d", *ip, *port) addr := fmt.Sprintf("%s:%d", *ip, *port)
cfg := new(ledis.Config) client = goredis.NewClient(addr, "")
cfg.Addr = addr client.SetReadBufferSize(10240)
cfg.MaxIdleConns = *clients client.SetWriteBufferSize(10240)
cfg.ReadBufferSize = 10240 client.SetMaxIdleConns(16)
cfg.WriteBufferSize = 10240
client = ledis.NewClient(cfg)
for i := 0; i < *clients; i++ { for i := 0; i < *clients; i++ {
c, _ := client.Get() c, _ := client.Get()

View File

@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -17,16 +17,15 @@ var dbn = flag.Int("n", 0, "ledisdb database number(default 0)")
func main() { func main() {
flag.Parse() flag.Parse()
cfg := new(ledis.Config) var addr string
if len(*socket) > 0 { if len(*socket) > 0 {
cfg.Addr = *socket addr = *socket
} else { } else {
cfg.Addr = fmt.Sprintf("%s:%d", *ip, *port) addr = fmt.Sprintf("%s:%d", *ip, *port)
} }
cfg.MaxIdleConns = 1 c := goredis.NewClient(addr, "")
c.SetMaxIdleConns(1)
c := ledis.NewClient(cfg)
sendSelect(c, *dbn) sendSelect(c, *dbn)
SetCompletionHandler(completionHandler) SetCompletionHandler(completionHandler)
@ -38,9 +37,9 @@ func main() {
for { for {
if *dbn > 0 && *dbn < 16 { if *dbn > 0 && *dbn < 16 {
prompt = fmt.Sprintf("%s[%d]>", cfg.Addr, *dbn) prompt = fmt.Sprintf("%s[%d]>", addr, *dbn)
} else { } else {
prompt = fmt.Sprintf("%s>", cfg.Addr) prompt = fmt.Sprintf("%s>", addr)
} }
cmd, err := line(prompt) cmd, err := line(prompt)
@ -102,7 +101,7 @@ func printReply(level int, reply interface{}) {
fmt.Printf("%q", reply) fmt.Printf("%q", reply)
case nil: case nil:
fmt.Printf("(nil)") fmt.Printf("(nil)")
case ledis.Error: case goredis.Error:
fmt.Printf("%s", string(reply)) fmt.Printf("%s", string(reply))
case []interface{}: case []interface{}:
for i, v := range reply { for i, v := range reply {
@ -154,7 +153,7 @@ func printHelp(cmds []string) {
} }
} }
func sendSelect(client *ledis.Client, index int) { func sendSelect(client *goredis.Client, index int) {
if index > 16 || index < 0 { if index > 16 || index < 0 {
index = 0 index = 0
fmt.Println("index out of range, should less than 16") fmt.Println("index out of range, should less than 16")

View File

@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"os" "os"
) )
@ -32,7 +32,7 @@ func main() {
addr = fmt.Sprintf("%s:%d", *host, *port) addr = fmt.Sprintf("%s:%d", *host, *port)
} }
c, err := ledis.ConnectWithSize(addr, 16*1024, 4096) c, err := goredis.ConnectWithSize(addr, 16*1024, 4096)
if err != nil { if err != nil {
println(err.Error()) println(err.Error())
return return

View File

@ -1,7 +1,7 @@
package server package server
import ( import (
goledis "github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"net" "net"
@ -45,7 +45,7 @@ type App struct {
rcs map[*respClient]struct{} rcs map[*respClient]struct{}
migrateM sync.Mutex migrateM sync.Mutex
migrateClients map[string]*goledis.Client migrateClients map[string]*goredis.Client
migrateKeyLockers map[string]*migrateKeyLocker migrateKeyLockers map[string]*migrateKeyLocker
} }
@ -76,7 +76,7 @@ func NewApp(cfg *config.Config) (*App, error) {
app.rcs = make(map[*respClient]struct{}) app.rcs = make(map[*respClient]struct{})
app.migrateClients = make(map[string]*goledis.Client) app.migrateClients = make(map[string]*goredis.Client)
app.newMigrateKeyLockers() app.newMigrateKeyLockers()
var err error var err error

View File

@ -1,7 +1,7 @@
package server package server
import ( import (
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"os" "os"
"sync" "sync"
@ -11,16 +11,14 @@ import (
var testAppOnce sync.Once var testAppOnce sync.Once
var testApp *App var testApp *App
var testLedisClient *ledis.Client var testLedisClient *goredis.Client
func newTestLedisClient() { func newTestLedisClient() {
cfg := new(ledis.Config) testLedisClient = goredis.NewClient("127.0.0.1:16380", "")
cfg.Addr = "127.0.0.1:16380" testLedisClient.SetMaxIdleConns(4)
cfg.MaxIdleConns = 4
testLedisClient = ledis.NewClient(cfg)
} }
func getTestConn() *ledis.Conn { func getTestConn() *goredis.PoolConn {
startTestApp() startTestApp()
conn, _ := testLedisClient.Get() conn, _ := testLedisClient.Get()
return conn return conn

View File

@ -1,7 +1,7 @@
package server package server
// import ( // import (
// "github.com/siddontang/ledisdb/client/goledis" // "github.com/siddontang/goredis"
// "testing" // "testing"
// ) // )
@ -19,19 +19,19 @@ package server
// key := []byte("test_cmd_bin_basic") // key := []byte("test_cmd_bin_basic")
// // get / set // // get / set
// if v, err := ledis.Int(c.Do("bgetbit", key, 1024)); err != nil { // if v, err := goredis.Int(c.Do("bgetbit", key, 1024)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if v != 0 { // } else if v != 0 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if ori, err := ledis.Int(c.Do("bsetbit", key, 1024, 1)); err != nil { // if ori, err := goredis.Int(c.Do("bsetbit", key, 1024, 1)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if ori != 0 { // } else if ori != 0 {
// t.Fatal(ori) // t.Fatal(ori)
// } // }
// if v, err := ledis.Int(c.Do("bgetbit", key, 1024)); err != nil { // if v, err := goredis.Int(c.Do("bgetbit", key, 1024)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if v != 1 { // } else if v != 1 {
// t.Fatal(v) // t.Fatal(v)
@ -40,26 +40,26 @@ package server
// // fetch from revert pos // // fetch from revert pos
// c.Do("bsetbit", key, 1000, 1) // c.Do("bsetbit", key, 1000, 1)
// if v, err := ledis.Int(c.Do("bgetbit", key, -1)); err != nil { // if v, err := goredis.Int(c.Do("bgetbit", key, -1)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if v != 1 { // } else if v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, err := ledis.Int(c.Do("bgetbit", key, -25)); err != nil { // if v, err := goredis.Int(c.Do("bgetbit", key, -25)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if v != 1 { // } else if v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// // delete // // delete
// if drop, err := ledis.Int(c.Do("bdelete", key)); err != nil { // if drop, err := goredis.Int(c.Do("bdelete", key)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if drop != 1 { // } else if drop != 1 {
// t.Fatal(drop) // t.Fatal(drop)
// } // }
// if drop, err := ledis.Int(c.Do("bdelete", key)); err != nil { // if drop, err := goredis.Int(c.Do("bdelete", key)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if drop != 0 { // } else if drop != 0 {
// t.Fatal(drop) // t.Fatal(drop)
@ -72,7 +72,7 @@ package server
// key := []byte("test_cmd_bin_mset") // key := []byte("test_cmd_bin_mset")
// if n, err := ledis.Int( // if n, err := goredis.Int(
// c.Do("bmsetbit", key, // c.Do("bmsetbit", key,
// 500, 0, // 500, 0,
// 100, 1, // 100, 1,
@ -90,14 +90,14 @@ package server
// fillPos := []int{100, 200, 300, 1000, 100000, 500000} // fillPos := []int{100, 200, 300, 1000, 100000, 500000}
// for _, pos := range fillPos { // for _, pos := range fillPos {
// v, err := ledis.Int(c.Do("bgetbit", key, pos)) // v, err := goredis.Int(c.Do("bgetbit", key, pos))
// if err != nil || v != 1 { // if err != nil || v != 1 {
// t.Fatal(pos) // t.Fatal(pos)
// } // }
// } // }
// // err // // err
// if n, err := ledis.Int( // if n, err := goredis.Int(
// c.Do("bmsetbit", key, 3, 0, 2, 1, 3, 0, 1, 1)); err == nil || n != 0 { // c.Do("bmsetbit", key, 3, 0, 2, 1, 3, 0, 1, 1)); err == nil || n != 0 {
// t.Fatal(n) // duplication on pos // t.Fatal(n) // duplication on pos
// } // }
@ -114,7 +114,7 @@ package server
// sum++ // sum++
// } // }
// if n, err := ledis.Int(c.Do("bcount", key)); err != nil { // if n, err := goredis.Int(c.Do("bcount", key)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if n != sum { // } else if n != sum {
// t.Fatal(n) // t.Fatal(n)
@ -137,21 +137,21 @@ package server
// // todo ... // // todo ...
// // case - 'not' on inexisting key // // case - 'not' on inexisting key
// if blen, err := ledis.Int( // if blen, err := goredis.Int(
// c.Do("bopt", "not", dstk, kmiss)); err != nil { // c.Do("bopt", "not", dstk, kmiss)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if blen != 0 { // } else if blen != 0 {
// t.Fatal(blen) // t.Fatal(blen)
// } // }
// if v, _ := ledis.String(c.Do("bget", dstk)); v != "" { // if v, _ := goredis.String(c.Do("bget", dstk)); v != "" {
// t.Fatal(v) // t.Fatal(v)
// } // }
// // case - 'and', 'or', 'xor' with inexisting key // // case - 'and', 'or', 'xor' with inexisting key
// opts := []string{"and", "or", "xor"} // opts := []string{"and", "or", "xor"}
// for _, op := range opts { // for _, op := range opts {
// if blen, err := ledis.Int( // if blen, err := goredis.Int(
// c.Do("bopt", op, dstk, kmiss, k0)); err != nil { // c.Do("bopt", op, dstk, kmiss, k0)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if blen != 0 { // } else if blen != 0 {
@ -160,62 +160,62 @@ package server
// } // }
// // case - 'and' // // case - 'and'
// if blen, err := ledis.Int( // if blen, err := goredis.Int(
// c.Do("bopt", "and", dstk, k0, k1)); err != nil { // c.Do("bopt", "and", dstk, k0, k1)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if blen != 101 { // } else if blen != 101 {
// t.Fatal(blen) // t.Fatal(blen)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 100)); v != 1 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 100)); v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 20)); v != 0 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 20)); v != 0 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 40)); v != 0 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 40)); v != 0 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// // case - 'or' // // case - 'or'
// if blen, err := ledis.Int( // if blen, err := goredis.Int(
// c.Do("bopt", "or", dstk, k0, k1)); err != nil { // c.Do("bopt", "or", dstk, k0, k1)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if blen != 101 { // } else if blen != 101 {
// t.Fatal(blen) // t.Fatal(blen)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 100)); v != 1 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 100)); v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 20)); v != 1 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 20)); v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 40)); v != 1 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 40)); v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// // case - 'xor' // // case - 'xor'
// if blen, err := ledis.Int( // if blen, err := goredis.Int(
// c.Do("bopt", "xor", dstk, k0, k1)); err != nil { // c.Do("bopt", "xor", dstk, k0, k1)); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } else if blen != 101 { // } else if blen != 101 {
// t.Fatal(blen) // t.Fatal(blen)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 100)); v != 0 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 100)); v != 0 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 20)); v != 1 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 20)); v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }
// if v, _ := ledis.Int(c.Do("bgetbit", dstk, 40)); v != 1 { // if v, _ := goredis.Int(c.Do("bgetbit", dstk, 40)); v != 1 {
// t.Fatal(v) // t.Fatal(v)
// } // }

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"strconv" "strconv"
"testing" "testing"
) )
@ -12,54 +12,54 @@ func TestHash(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("a") key := []byte("a")
if n, err := ledis.Int(c.Do("hkeyexists", key)); err != nil { if n, err := goredis.Int(c.Do("hkeyexists", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hset", key, 1, 0)); err != nil { if n, err := goredis.Int(c.Do("hset", key, 1, 0)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hkeyexists", key)); err != nil { if n, err := goredis.Int(c.Do("hkeyexists", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hexists", key, 1)); err != nil { if n, err := goredis.Int(c.Do("hexists", key, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hexists", key, -1)); err != nil { if n, err := goredis.Int(c.Do("hexists", key, -1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hget", key, 1)); err != nil { if n, err := goredis.Int(c.Do("hget", key, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hset", key, 1, 1)); err != nil { if n, err := goredis.Int(c.Do("hset", key, 1, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hget", key, 1)); err != nil { if n, err := goredis.Int(c.Do("hget", key, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
@ -95,19 +95,19 @@ func TestHashM(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("b") key := []byte("b")
if ok, err := ledis.String(c.Do("hmset", key, 1, 1, 2, 2, 3, 3)); err != nil { if ok, err := goredis.String(c.Do("hmset", key, 1, 1, 2, 2, 3, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ok != OK { } else if ok != OK {
t.Fatal(ok) t.Fatal(ok)
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.MultiBulk(c.Do("hmget", key, 1, 2, 3, 4)); err != nil { if v, err := goredis.MultiBulk(c.Do("hmget", key, 1, 2, 3, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testHashArray(v, 1, 2, 3, 0); err != nil { if err := testHashArray(v, 1, 2, 3, 0); err != nil {
@ -115,19 +115,19 @@ func TestHashM(t *testing.T) {
} }
} }
if n, err := ledis.Int(c.Do("hdel", key, 1, 2, 3, 4)); err != nil { if n, err := goredis.Int(c.Do("hdel", key, 1, 2, 3, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.MultiBulk(c.Do("hmget", key, 1, 2, 3, 4)); err != nil { if v, err := goredis.MultiBulk(c.Do("hmget", key, 1, 2, 3, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testHashArray(v, 0, 0, 0, 0); err != nil { if err := testHashArray(v, 0, 0, 0, 0); err != nil {
@ -135,7 +135,7 @@ func TestHashM(t *testing.T) {
} }
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
@ -147,31 +147,31 @@ func TestHashIncr(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("c") key := []byte("c")
if n, err := ledis.Int(c.Do("hincrby", key, 1, 1)); err != nil { if n, err := goredis.Int(c.Do("hincrby", key, 1, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hincrby", key, 1, 10)); err != nil { if n, err := goredis.Int(c.Do("hincrby", key, 1, 10)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 11 { } else if n != 11 {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hincrby", key, 1, -11)); err != nil { if n, err := goredis.Int(c.Do("hincrby", key, 1, -11)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(err) t.Fatal(err)
@ -185,13 +185,13 @@ func TestHashGetAll(t *testing.T) {
key := []byte("d") key := []byte("d")
if ok, err := ledis.String(c.Do("hmset", key, 1, 1, 2, 2, 3, 3)); err != nil { if ok, err := goredis.String(c.Do("hmset", key, 1, 1, 2, 2, 3, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ok != OK { } else if ok != OK {
t.Fatal(ok) t.Fatal(ok)
} }
if v, err := ledis.MultiBulk(c.Do("hgetall", key)); err != nil { if v, err := goredis.MultiBulk(c.Do("hgetall", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testHashArray(v, 1, 1, 2, 2, 3, 3); err != nil { if err := testHashArray(v, 1, 1, 2, 2, 3, 3); err != nil {
@ -199,7 +199,7 @@ func TestHashGetAll(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("hkeys", key)); err != nil { if v, err := goredis.MultiBulk(c.Do("hkeys", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testHashArray(v, 1, 2, 3); err != nil { if err := testHashArray(v, 1, 2, 3); err != nil {
@ -207,7 +207,7 @@ func TestHashGetAll(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("hvals", key)); err != nil { if v, err := goredis.MultiBulk(c.Do("hvals", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testHashArray(v, 1, 2, 3); err != nil { if err := testHashArray(v, 1, 2, 3); err != nil {
@ -215,13 +215,13 @@ func TestHashGetAll(t *testing.T) {
} }
} }
if n, err := ledis.Int(c.Do("hclear", key)); err != nil { if n, err := goredis.Int(c.Do("hclear", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("hlen", key)); err != nil { if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)

View File

@ -1,7 +1,7 @@
package server package server
import ( import (
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"testing" "testing"
) )
@ -9,121 +9,121 @@ func TestKV(t *testing.T) {
c := getTestConn() c := getTestConn()
defer c.Close() defer c.Close()
if ok, err := ledis.String(c.Do("set", "a", "1234")); err != nil { if ok, err := goredis.String(c.Do("set", "a", "1234")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ok != OK { } else if ok != OK {
t.Fatal(ok) t.Fatal(ok)
} }
if n, err := ledis.Int(c.Do("setnx", "a", "123")); err != nil { if n, err := goredis.Int(c.Do("setnx", "a", "123")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("setnx", "b", "123")); err != nil { if n, err := goredis.Int(c.Do("setnx", "b", "123")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if ok, err := ledis.String(c.Do("setex", "xx", 10, "hello world")); err != nil { if ok, err := goredis.String(c.Do("setex", "xx", 10, "hello world")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ok != OK { } else if ok != OK {
t.Fatal(ok) t.Fatal(ok)
} }
if v, err := ledis.String(c.Do("get", "a")); err != nil { if v, err := goredis.String(c.Do("get", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != "1234" { } else if v != "1234" {
t.Fatal(v) t.Fatal(v)
} }
if v, err := ledis.String(c.Do("getset", "a", "123")); err != nil { if v, err := goredis.String(c.Do("getset", "a", "123")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != "1234" { } else if v != "1234" {
t.Fatal(v) t.Fatal(v)
} }
if v, err := ledis.String(c.Do("get", "a")); err != nil { if v, err := goredis.String(c.Do("get", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != "123" { } else if v != "123" {
t.Fatal(v) t.Fatal(v)
} }
if n, err := ledis.Int(c.Do("exists", "a")); err != nil { if n, err := goredis.Int(c.Do("exists", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("exists", "empty_key_test")); err != nil { if n, err := goredis.Int(c.Do("exists", "empty_key_test")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if _, err := ledis.Int(c.Do("del", "a", "b")); err != nil { if _, err := goredis.Int(c.Do("del", "a", "b")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("exists", "a")); err != nil { if n, err := goredis.Int(c.Do("exists", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("exists", "b")); err != nil { if n, err := goredis.Int(c.Do("exists", "b")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
rangeKey := "range_key" rangeKey := "range_key"
if n, err := ledis.Int(c.Do("append", rangeKey, "Hello ")); err != nil { if n, err := goredis.Int(c.Do("append", rangeKey, "Hello ")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 6 { } else if n != 6 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("setrange", rangeKey, 6, "Redis")); err != nil { if n, err := goredis.Int(c.Do("setrange", rangeKey, 6, "Redis")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 11 { } else if n != 11 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("strlen", rangeKey)); err != nil { if n, err := goredis.Int(c.Do("strlen", rangeKey)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 11 { } else if n != 11 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.String(c.Do("getrange", rangeKey, 0, -1)); err != nil { if v, err := goredis.String(c.Do("getrange", rangeKey, 0, -1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != "Hello Redis" { } else if v != "Hello Redis" {
t.Fatal(v) t.Fatal(v)
} }
bitKey := "bit_key" bitKey := "bit_key"
if n, err := ledis.Int(c.Do("setbit", bitKey, 7, 1)); err != nil { if n, err := goredis.Int(c.Do("setbit", bitKey, 7, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("getbit", bitKey, 7)); err != nil { if n, err := goredis.Int(c.Do("getbit", bitKey, 7)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("bitcount", bitKey)); err != nil { if n, err := goredis.Int(c.Do("bitcount", bitKey)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("bitpos", bitKey, 1)); err != nil { if n, err := goredis.Int(c.Do("bitpos", bitKey, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 7 { } else if n != 7 {
t.Fatal(n) t.Fatal(n)
@ -132,13 +132,13 @@ func TestKV(t *testing.T) {
c.Do("set", "key1", "foobar") c.Do("set", "key1", "foobar")
c.Do("set", "key2", "abcdef") c.Do("set", "key2", "abcdef")
if n, err := ledis.Int(c.Do("bitop", "and", "bit_dest_key", "key1", "key2")); err != nil { if n, err := goredis.Int(c.Do("bitop", "and", "bit_dest_key", "key1", "key2")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 6 { } else if n != 6 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.String(c.Do("get", "bit_dest_key")); err != nil { if v, err := goredis.String(c.Do("get", "bit_dest_key")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != "`bc`ab" { } else if v != "`bc`ab" {
t.Fatal(v) t.Fatal(v)
@ -150,13 +150,13 @@ func TestKVM(t *testing.T) {
c := getTestConn() c := getTestConn()
defer c.Close() defer c.Close()
if ok, err := ledis.String(c.Do("mset", "a", "1", "b", "2")); err != nil { if ok, err := goredis.String(c.Do("mset", "a", "1", "b", "2")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ok != OK { } else if ok != OK {
t.Fatal(ok) t.Fatal(ok)
} }
if v, err := ledis.MultiBulk(c.Do("mget", "a", "b", "c")); err != nil { if v, err := goredis.MultiBulk(c.Do("mget", "a", "b", "c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(v) != 3 { } else if len(v) != 3 {
t.Fatal(len(v)) t.Fatal(len(v))
@ -179,31 +179,31 @@ func TestKVIncrDecr(t *testing.T) {
c := getTestConn() c := getTestConn()
defer c.Close() defer c.Close()
if n, err := ledis.Int64(c.Do("incr", "n")); err != nil { if n, err := goredis.Int64(c.Do("incr", "n")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int64(c.Do("incr", "n")); err != nil { if n, err := goredis.Int64(c.Do("incr", "n")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int64(c.Do("decr", "n")); err != nil { if n, err := goredis.Int64(c.Do("decr", "n")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int64(c.Do("incrby", "n", 10)); err != nil { if n, err := goredis.Int64(c.Do("incrby", "n", 10)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 11 { } else if n != 11 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int64(c.Do("decrby", "n", 10)); err != nil { if n, err := goredis.Int64(c.Do("decrby", "n", 10)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"strconv" "strconv"
"testing" "testing"
) )
@ -11,10 +11,10 @@ func testListIndex(key []byte, index int64, v int) error {
c := getTestConn() c := getTestConn()
defer c.Close() defer c.Close()
n, err := ledis.Int(c.Do("lindex", key, index)) n, err := goredis.Int(c.Do("lindex", key, index))
if err == ledis.ErrNil && v != 0 { if err == goredis.ErrNil && v != 0 {
return fmt.Errorf("must nil") return fmt.Errorf("must nil")
} else if err != nil && err != ledis.ErrNil { } else if err != nil && err != goredis.ErrNil {
return err return err
} else if n != v { } else if n != v {
return fmt.Errorf("index err number %d != %d", n, v) return fmt.Errorf("index err number %d != %d", n, v)
@ -27,7 +27,7 @@ func testListRange(key []byte, start int64, stop int64, checkValues ...int) erro
c := getTestConn() c := getTestConn()
defer c.Close() defer c.Close()
vs, err := ledis.MultiBulk(c.Do("lrange", key, start, stop)) vs, err := goredis.MultiBulk(c.Do("lrange", key, start, stop))
if err != nil { if err != nil {
return err return err
} }
@ -58,37 +58,37 @@ func TestList(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("a") key := []byte("a")
if n, err := ledis.Int(c.Do("lkeyexists", key)); err != nil { if n, err := goredis.Int(c.Do("lkeyexists", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("lpush", key, 1)); err != nil { if n, err := goredis.Int(c.Do("lpush", key, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("lkeyexists", key)); err != nil { if n, err := goredis.Int(c.Do("lkeyexists", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(1) t.Fatal(1)
} }
if n, err := ledis.Int(c.Do("rpush", key, 2)); err != nil { if n, err := goredis.Int(c.Do("rpush", key, 2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("rpush", key, 3)); err != nil { if n, err := goredis.Int(c.Do("rpush", key, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("llen", key)); err != nil { if n, err := goredis.Int(c.Do("llen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
@ -212,7 +212,7 @@ func TestListMPush(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("b") key := []byte("b")
if n, err := ledis.Int(c.Do("rpush", key, 1, 2, 3)); err != nil { if n, err := goredis.Int(c.Do("rpush", key, 1, 2, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
@ -222,7 +222,7 @@ func TestListMPush(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("lpush", key, 1, 2, 3)); err != nil { if n, err := goredis.Int(c.Do("lpush", key, 1, 2, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 6 { } else if n != 6 {
t.Fatal(n) t.Fatal(n)
@ -238,25 +238,25 @@ func TestPop(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("c") key := []byte("c")
if n, err := ledis.Int(c.Do("rpush", key, 1, 2, 3, 4, 5, 6)); err != nil { if n, err := goredis.Int(c.Do("rpush", key, 1, 2, 3, 4, 5, 6)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 6 { } else if n != 6 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.Int(c.Do("lpop", key)); err != nil { if v, err := goredis.Int(c.Do("lpop", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != 1 { } else if v != 1 {
t.Fatal(v) t.Fatal(v)
} }
if v, err := ledis.Int(c.Do("rpop", key)); err != nil { if v, err := goredis.Int(c.Do("rpop", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != 6 { } else if v != 6 {
t.Fatal(v) t.Fatal(v)
} }
if n, err := ledis.Int(c.Do("lpush", key, 1)); err != nil { if n, err := goredis.Int(c.Do("lpush", key, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 5 { } else if n != 5 {
t.Fatal(n) t.Fatal(n)
@ -267,14 +267,14 @@ func TestPop(t *testing.T) {
} }
for i := 1; i <= 5; i++ { for i := 1; i <= 5; i++ {
if v, err := ledis.Int(c.Do("lpop", key)); err != nil { if v, err := goredis.Int(c.Do("lpop", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if v != i { } else if v != i {
t.Fatal(v) t.Fatal(v)
} }
} }
if n, err := ledis.Int(c.Do("llen", key)); err != nil { if n, err := goredis.Int(c.Do("llen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
@ -282,13 +282,13 @@ func TestPop(t *testing.T) {
c.Do("rpush", key, 1, 2, 3, 4, 5) c.Do("rpush", key, 1, 2, 3, 4, 5)
if n, err := ledis.Int(c.Do("lclear", key)); err != nil { if n, err := goredis.Int(c.Do("lclear", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 5 { } else if n != 5 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("llen", key)); err != nil { if n, err := goredis.Int(c.Do("llen", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)

View File

@ -9,7 +9,7 @@ import (
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/log" "github.com/siddontang/go/log"
goledis "github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
) )
@ -224,14 +224,13 @@ func xdumpCommand(c *client) error {
return nil return nil
} }
func (app *App) getMigrateClient(addr string) *goledis.Client { func (app *App) getMigrateClient(addr string) *goredis.Client {
app.migrateM.Lock() app.migrateM.Lock()
mc, ok := app.migrateClients[addr] mc, ok := app.migrateClients[addr]
if !ok { if !ok {
mc = goledis.NewClient(&goledis.Config{addr, 4, 0, 0}) mc = goredis.NewClient(addr, "")
app.migrateClients[addr] = mc app.migrateClients[addr] = mc
} }
app.migrateM.Unlock() app.migrateM.Unlock()
@ -448,7 +447,7 @@ func xmigrateCommand(c *client) error {
return nil return nil
} }
func getMigrateDBConn(c *client, addr string, db uint64) (*goledis.Conn, error) { func getMigrateDBConn(c *client, addr string, db uint64) (*goredis.PoolConn, error) {
mc := c.app.getMigrateClient(addr) mc := c.app.getMigrateClient(addr)
conn, err := mc.Get() conn, err := mc.Get()
@ -469,7 +468,7 @@ var (
errKeyInMigrating = errors.New("key is in migrating yet") errKeyInMigrating = errors.New("key is in migrating yet")
) )
func migrateKey(c *client, conn *goledis.Conn, tp string, key []byte, timeout int64) error { func migrateKey(c *client, conn *goredis.PoolConn, tp string, key []byte, timeout int64) error {
if !c.app.migrateKeyLock(tp, key) { if !c.app.migrateKeyLock(tp, key) {
// other may also migrate this key, skip it // other may also migrate this key, skip it
return errKeyInMigrating return errKeyInMigrating
@ -506,7 +505,7 @@ func migrateKey(c *client, conn *goledis.Conn, tp string, key []byte, timeout in
return nil return nil
} }
func migrateAllTypeKeys(c *client, conn *goledis.Conn, key []byte, timeout int64) error { func migrateAllTypeKeys(c *client, conn *goredis.PoolConn, key []byte, timeout int64) error {
for _, tp := range TypeNames { for _, tp := range TypeNames {
err := migrateKey(c, conn, tp, key, timeout) err := migrateKey(c, conn, tp, key, timeout)
if err != nil { if err != nil {

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"os" "os"
"testing" "testing"
@ -42,8 +42,8 @@ func TestDumpRestore(t *testing.T) {
testDumpRestore(c, "zdump", "mtest_za", t) testDumpRestore(c, "zdump", "mtest_za", t)
} }
func testDumpRestore(c *ledis.Conn, dump string, key string, t *testing.T) { func testDumpRestore(c *goredis.PoolConn, dump string, key string, t *testing.T) {
if data, err := ledis.Bytes(c.Do(dump, key)); err != nil { if data, err := goredis.Bytes(c.Do(dump, key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if _, err := c.Do("restore", key, 0, data); err != nil { } else if _, err := c.Do("restore", key, 0, data); err != nil {
t.Fatal(err) t.Fatal(err)
@ -80,10 +80,10 @@ func TestMigrate(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
c1, _ := ledis.Connect(s1Cfg.Addr) c1, _ := goredis.Connect(s1Cfg.Addr)
defer c1.Close() defer c1.Close()
c2, _ := ledis.Connect(s2Cfg.Addr) c2, _ := goredis.Connect(s2Cfg.Addr)
defer c2.Close() defer c2.Close()
if _, err = c1.Do("set", "a", "1"); err != nil { if _, err = c1.Do("set", "a", "1"); err != nil {
@ -95,31 +95,31 @@ func TestMigrate(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if s, err := ledis.String(c2.Do("get", "a")); err != nil { if s, err := goredis.String(c2.Do("get", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if s != "1" { } else if s != "1" {
t.Fatal(s, "must 1") t.Fatal(s, "must 1")
} }
if s, err := ledis.String(c1.Do("get", "a")); err != nil && err != ledis.ErrNil { if s, err := goredis.String(c1.Do("get", "a")); err != nil && err != goredis.ErrNil {
t.Fatal(err) t.Fatal(err)
} else if s != "" { } else if s != "" {
t.Fatal(s, "must empty") t.Fatal(s, "must empty")
} }
if num, err := ledis.Int(c2.Do("xmigratedb", "127.0.0.1", 11185, "KV", 10, 0, timeout)); err != nil { if num, err := goredis.Int(c2.Do("xmigratedb", "127.0.0.1", 11185, "KV", 10, 0, timeout)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if num != 1 { } else if num != 1 {
t.Fatal(num, "must number 1") t.Fatal(num, "must number 1")
} }
if s, err := ledis.String(c1.Do("get", "a")); err != nil { if s, err := goredis.String(c1.Do("get", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if s != "1" { } else if s != "1" {
t.Fatal(s, "must 1") t.Fatal(s, "must 1")
} }
if s, err := ledis.String(c2.Do("get", "a")); err != nil && err != ledis.ErrNil { if s, err := goredis.String(c2.Do("get", "a")); err != nil && err != goredis.ErrNil {
t.Fatal(err) t.Fatal(err)
} else if s != "" { } else if s != "" {
t.Fatal(s, "must empty") t.Fatal(s, "must empty")
@ -129,13 +129,13 @@ func TestMigrate(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if s, err := ledis.String(c2.Do("get", "a")); err != nil { if s, err := goredis.String(c2.Do("get", "a")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if s != "1" { } else if s != "1" {
t.Fatal(s, "must 1") t.Fatal(s, "must 1")
} }
if s, err := ledis.String(c1.Do("get", "a")); err != nil && err != ledis.ErrNil { if s, err := goredis.String(c1.Do("get", "a")); err != nil && err != goredis.ErrNil {
t.Fatal(err) t.Fatal(err)
} else if s != "" { } else if s != "" {
t.Fatal(s, "must empty") t.Fatal(s, "must empty")

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
goledis "github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"os" "os"
"reflect" "reflect"
@ -159,9 +159,9 @@ func TestReplication(t *testing.T) {
} }
func checkTestRole(addr string, checkRoles []interface{}) error { func checkTestRole(addr string, checkRoles []interface{}) error {
conn, _ := goledis.Connect(addr) conn, _ := goredis.Connect(addr)
defer conn.Close() defer conn.Close()
roles, err := goledis.MultiBulk(conn.Do("ROLE")) roles, err := goredis.MultiBulk(conn.Do("ROLE"))
if err != nil { if err != nil {
return err return err
} else if !reflect.DeepEqual(roles, checkRoles) { } else if !reflect.DeepEqual(roles, checkRoles) {

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"os" "os"
"testing" "testing"
@ -22,10 +22,8 @@ func TestScan(t *testing.T) {
go s.Run() go s.Run()
defer s.Close() defer s.Close()
cc := new(ledis.Config) c := goredis.NewClient(cfg.Addr, "")
cc.Addr = cfg.Addr c.SetMaxIdleConns(1)
cc.MaxIdleConns = 1
c := ledis.NewClient(cc)
defer c.Close() defer c.Close()
testKVScan(t, c) testKVScan(t, c)
@ -37,7 +35,7 @@ func TestScan(t *testing.T) {
} }
func checkScanValues(t *testing.T, ay interface{}, values ...interface{}) { func checkScanValues(t *testing.T, ay interface{}, values ...interface{}) {
a, err := ledis.Strings(ay, nil) a, err := goredis.Strings(ay, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -53,8 +51,8 @@ func checkScanValues(t *testing.T, ay interface{}, values ...interface{}) {
} }
} }
func checkScan(t *testing.T, c *ledis.Client, tp string) { func checkScan(t *testing.T, c *goredis.Client, tp string) {
if ay, err := ledis.Values(c.Do("XSCAN", tp, "", "count", 5)); err != nil { if ay, err := goredis.Values(c.Do("XSCAN", tp, "", "count", 5)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(ay) != 2 { } else if len(ay) != 2 {
t.Fatal(len(ay)) t.Fatal(len(ay))
@ -64,7 +62,7 @@ func checkScan(t *testing.T, c *ledis.Client, tp string) {
checkScanValues(t, ay[1], 0, 1, 2, 3, 4) checkScanValues(t, ay[1], 0, 1, 2, 3, 4)
} }
if ay, err := ledis.Values(c.Do("XSCAN", tp, "4", "count", 6)); err != nil { if ay, err := goredis.Values(c.Do("XSCAN", tp, "4", "count", 6)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(ay) != 2 { } else if len(ay) != 2 {
t.Fatal(len(ay)) t.Fatal(len(ay))
@ -76,7 +74,7 @@ func checkScan(t *testing.T, c *ledis.Client, tp string) {
} }
func testKVScan(t *testing.T, c *ledis.Client) { func testKVScan(t *testing.T, c *goredis.Client) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if _, err := c.Do("set", fmt.Sprintf("%d", i), []byte("value")); err != nil { if _, err := c.Do("set", fmt.Sprintf("%d", i), []byte("value")); err != nil {
t.Fatal(err) t.Fatal(err)
@ -86,7 +84,7 @@ func testKVScan(t *testing.T, c *ledis.Client) {
checkScan(t, c, "KV") checkScan(t, c, "KV")
} }
func testHashKeyScan(t *testing.T, c *ledis.Client) { func testHashKeyScan(t *testing.T, c *goredis.Client) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if _, err := c.Do("hset", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i), []byte("value")); err != nil { if _, err := c.Do("hset", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i), []byte("value")); err != nil {
t.Fatal(err) t.Fatal(err)
@ -96,7 +94,7 @@ func testHashKeyScan(t *testing.T, c *ledis.Client) {
checkScan(t, c, "HASH") checkScan(t, c, "HASH")
} }
func testListKeyScan(t *testing.T, c *ledis.Client) { func testListKeyScan(t *testing.T, c *goredis.Client) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if _, err := c.Do("lpush", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil { if _, err := c.Do("lpush", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil {
t.Fatal(err) t.Fatal(err)
@ -106,7 +104,7 @@ func testListKeyScan(t *testing.T, c *ledis.Client) {
checkScan(t, c, "LIST") checkScan(t, c, "LIST")
} }
func testZSetKeyScan(t *testing.T, c *ledis.Client) { func testZSetKeyScan(t *testing.T, c *goredis.Client) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if _, err := c.Do("zadd", fmt.Sprintf("%d", i), i, []byte("value")); err != nil { if _, err := c.Do("zadd", fmt.Sprintf("%d", i), i, []byte("value")); err != nil {
t.Fatal(err) t.Fatal(err)
@ -116,7 +114,7 @@ func testZSetKeyScan(t *testing.T, c *ledis.Client) {
checkScan(t, c, "ZSET") checkScan(t, c, "ZSET")
} }
func testSetKeyScan(t *testing.T, c *ledis.Client) { func testSetKeyScan(t *testing.T, c *goredis.Client) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if _, err := c.Do("sadd", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil { if _, err := c.Do("sadd", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil {
t.Fatal(err) t.Fatal(err)
@ -133,7 +131,7 @@ func TestHashScan(t *testing.T) {
key := "scan_hash" key := "scan_hash"
c.Do("HMSET", key, "a", 1, "b", 2) c.Do("HMSET", key, "a", 1, "b", 2)
if ay, err := ledis.Values(c.Do("XHSCAN", key, "")); err != nil { if ay, err := goredis.Values(c.Do("XHSCAN", key, "")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(ay) != 2 { } else if len(ay) != 2 {
t.Fatal(len(ay)) t.Fatal(len(ay))
@ -149,7 +147,7 @@ func TestSetScan(t *testing.T) {
key := "scan_set" key := "scan_set"
c.Do("SADD", key, "a", "b") c.Do("SADD", key, "a", "b")
if ay, err := ledis.Values(c.Do("XSSCAN", key, "")); err != nil { if ay, err := goredis.Values(c.Do("XSSCAN", key, "")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(ay) != 2 { } else if len(ay) != 2 {
t.Fatal(len(ay)) t.Fatal(len(ay))
@ -166,7 +164,7 @@ func TestZSetScan(t *testing.T) {
key := "scan_zset" key := "scan_zset"
c.Do("ZADD", key, 1, "a", 2, "b") c.Do("ZADD", key, 1, "a", 2, "b")
if ay, err := ledis.Values(c.Do("XZSCAN", key, "")); err != nil { if ay, err := goredis.Values(c.Do("XZSCAN", key, "")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(ay) != 2 { } else if len(ay) != 2 {
t.Fatal(len(ay)) t.Fatal(len(ay))

View File

@ -4,7 +4,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"reflect" "reflect"
"testing" "testing"
) )
@ -13,13 +13,13 @@ func TestCmdEval(t *testing.T) {
c := getTestConn() c := getTestConn()
defer c.Close() defer c.Close()
if v, err := ledis.Strings(c.Do("eval", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second")); err != nil { if v, err := goredis.Strings(c.Do("eval", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) { } else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) {
t.Fatal(fmt.Sprintf("%v", v)) t.Fatal(fmt.Sprintf("%v", v))
} }
if v, err := ledis.Strings(c.Do("eval", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second")); err != nil { if v, err := goredis.Strings(c.Do("eval", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) { } else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) {
t.Fatal(fmt.Sprintf("%v", v)) t.Fatal(fmt.Sprintf("%v", v))
@ -27,31 +27,31 @@ func TestCmdEval(t *testing.T) {
var sha1 string var sha1 string
var err error var err error
if sha1, err = ledis.String(c.Do("script", "load", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}")); err != nil { if sha1, err = goredis.String(c.Do("script", "load", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(sha1) != 40 { } else if len(sha1) != 40 {
t.Fatal(sha1) t.Fatal(sha1)
} }
if v, err := ledis.Strings(c.Do("evalsha", sha1, 2, "key1", "key2", "first", "second")); err != nil { if v, err := goredis.Strings(c.Do("evalsha", sha1, 2, "key1", "key2", "first", "second")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) { } else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) {
t.Fatal(fmt.Sprintf("%v", v)) t.Fatal(fmt.Sprintf("%v", v))
} }
if ay, err := ledis.Values(c.Do("script", "exists", sha1, "01234567890123456789")); err != nil { if ay, err := goredis.Values(c.Do("script", "exists", sha1, "01234567890123456789")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(ay, []interface{}{int64(1), int64(0)}) { } else if !reflect.DeepEqual(ay, []interface{}{int64(1), int64(0)}) {
t.Fatal(fmt.Sprintf("%v", ay)) t.Fatal(fmt.Sprintf("%v", ay))
} }
if ok, err := ledis.String(c.Do("script", "flush")); err != nil { if ok, err := goredis.String(c.Do("script", "flush")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ok != "OK" { } else if ok != "OK" {
t.Fatal(ok) t.Fatal(ok)
} }
if ay, err := ledis.Values(c.Do("script", "exists", sha1)); err != nil { if ay, err := goredis.Values(c.Do("script", "exists", sha1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(ay, []interface{}{int64(0)}) { } else if !reflect.DeepEqual(ay, []interface{}{int64(0)}) {
t.Fatal(fmt.Sprintf("%v", ay)) t.Fatal(fmt.Sprintf("%v", ay))

View File

@ -1,7 +1,7 @@
package server package server
import ( import (
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"testing" "testing"
) )
@ -12,91 +12,91 @@ func TestSet(t *testing.T) {
key1 := "testdb_cmd_set_1" key1 := "testdb_cmd_set_1"
key2 := "testdb_cmd_set_2" key2 := "testdb_cmd_set_2"
if n, err := ledis.Int(c.Do("skeyexists", key1)); err != nil { if n, err := goredis.Int(c.Do("skeyexists", key1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sadd", key1, 0, 1)); err != nil { if n, err := goredis.Int(c.Do("sadd", key1, 0, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("skeyexists", key1)); err != nil { if n, err := goredis.Int(c.Do("skeyexists", key1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sadd", key2, 0, 1, 2, 3)); err != nil { if n, err := goredis.Int(c.Do("sadd", key2, 0, 1, 2, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("scard", key1)); err != nil { if n, err := goredis.Int(c.Do("scard", key1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.MultiBulk(c.Do("sdiff", key2, key1)); err != nil { if n, err := goredis.MultiBulk(c.Do("sdiff", key2, key1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(n) != 2 { } else if len(n) != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sdiffstore", []byte("cmd_set_em1"), key2, key1)); err != nil { if n, err := goredis.Int(c.Do("sdiffstore", []byte("cmd_set_em1"), key2, key1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.MultiBulk(c.Do("sunion", key1, key2)); err != nil { if n, err := goredis.MultiBulk(c.Do("sunion", key1, key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(n) != 4 { } else if len(n) != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sunionstore", []byte("cmd_set_em2"), key1, key2)); err != nil { if n, err := goredis.Int(c.Do("sunionstore", []byte("cmd_set_em2"), key1, key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.MultiBulk(c.Do("sinter", key1, key2)); err != nil { if n, err := goredis.MultiBulk(c.Do("sinter", key1, key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(n) != 2 { } else if len(n) != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sinterstore", []byte("cmd_set_em3"), key1, key2)); err != nil { if n, err := goredis.Int(c.Do("sinterstore", []byte("cmd_set_em3"), key1, key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("srem", key1, 0, 1)); err != nil { if n, err := goredis.Int(c.Do("srem", key1, 0, 1)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sismember", key2, 0)); err != nil { if n, err := goredis.Int(c.Do("sismember", key2, 0)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.MultiBulk(c.Do("smembers", key2)); err != nil { if n, err := goredis.MultiBulk(c.Do("smembers", key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(n) != 4 { } else if len(n) != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("sclear", key2)); err != nil { if n, err := goredis.Int(c.Do("sclear", key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
@ -104,7 +104,7 @@ func TestSet(t *testing.T) {
c.Do("sadd", key1, 0) c.Do("sadd", key1, 0)
c.Do("sadd", key2, 1) c.Do("sadd", key2, 1)
if n, err := ledis.Int(c.Do("smclear", key1, key2)); err != nil { if n, err := goredis.Int(c.Do("smclear", key1, key2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
goledis "github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"testing" "testing"
) )
@ -79,7 +79,7 @@ func TestSort(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if n, err := goledis.Int(c.Do("XLSORT", key, "STORE", storeKey)); err != nil { if n, err := goredis.Int(c.Do("XLSORT", key, "STORE", storeKey)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatalf("invalid return store sort number, %d != 3", n) t.Fatalf("invalid return store sort number, %d != 3", n)

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"testing" "testing"
"time" "time"
) )
@ -63,13 +63,13 @@ func TestExpire(t *testing.T) {
} }
// expire + ttl // expire + ttl
exp := int64(10) exp := int64(10)
if n, err := ledis.Int(c.Do(expire, key, exp)); err != nil { if n, err := goredis.Int(c.Do(expire, key, exp)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if ttl, err := ledis.Int64(c.Do(ttl, key)); err != nil { if ttl, err := goredis.Int64(c.Do(ttl, key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ttl == -1 { } else if ttl == -1 {
t.Fatal("no ttl") t.Fatal("no ttl")
@ -77,13 +77,13 @@ func TestExpire(t *testing.T) {
// expireat + ttl // expireat + ttl
tm := now() + 3 tm := now() + 3
if n, err := ledis.Int(c.Do(expireat, key, tm)); err != nil { if n, err := goredis.Int(c.Do(expireat, key, tm)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if ttl, err := ledis.Int64(c.Do(ttl, key)); err != nil { if ttl, err := goredis.Int64(c.Do(ttl, key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if ttl == -1 { } else if ttl == -1 {
t.Fatal("no ttl") t.Fatal("no ttl")
@ -92,31 +92,31 @@ func TestExpire(t *testing.T) {
kErr := "not_exist_ttl" kErr := "not_exist_ttl"
// err - expire, expireat // err - expire, expireat
if n, err := ledis.Int(c.Do(expire, kErr, tm)); err != nil || n != 0 { if n, err := goredis.Int(c.Do(expire, kErr, tm)); err != nil || n != 0 {
t.Fatal(false) t.Fatal(false)
} }
if n, err := ledis.Int(c.Do(expireat, kErr, tm)); err != nil || n != 0 { if n, err := goredis.Int(c.Do(expireat, kErr, tm)); err != nil || n != 0 {
t.Fatal(false) t.Fatal(false)
} }
if n, err := ledis.Int(c.Do(ttl, kErr)); err != nil || n != -1 { if n, err := goredis.Int(c.Do(ttl, kErr)); err != nil || n != -1 {
t.Fatal(false) t.Fatal(false)
} }
if n, err := ledis.Int(c.Do(persist, key)); err != nil { if n, err := goredis.Int(c.Do(persist, key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do(expire, key, 10)); err != nil { if n, err := goredis.Int(c.Do(expire, key, 10)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do(persist, key)); err != nil { if n, err := goredis.Int(c.Do(persist, key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)

View File

@ -2,7 +2,7 @@ package server
import ( import (
"fmt" "fmt"
"github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
@ -14,103 +14,103 @@ func TestZSet(t *testing.T) {
key := []byte("myzset") key := []byte("myzset")
if n, err := ledis.Int(c.Do("zkeyexists", key)); err != nil { if n, err := goredis.Int(c.Do("zkeyexists", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zadd", key, 3, "a", 4, "b")); err != nil { if n, err := goredis.Int(c.Do("zadd", key, 3, "a", 4, "b")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zkeyexists", key)); err != nil { if n, err := goredis.Int(c.Do("zkeyexists", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(n) t.Fatal(n)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b")); err != nil { if n, err := goredis.Int(c.Do("zadd", key, 1, "a", 2, "b")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(n) t.Fatal(n)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zadd", key, 3, "c", 4, "d")); err != nil { if n, err := goredis.Int(c.Do("zadd", key, 3, "c", 4, "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
} }
if s, err := ledis.Int(c.Do("zscore", key, "c")); err != nil { if s, err := goredis.Int(c.Do("zscore", key, "c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if s != 3 { } else if s != 3 {
t.Fatal(s) t.Fatal(s)
} }
if n, err := ledis.Int(c.Do("zrem", key, "d", "e")); err != nil { if n, err := goredis.Int(c.Do("zrem", key, "d", "e")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zincrby", key, 4, "c")); err != nil { if n, err := goredis.Int(c.Do("zincrby", key, 4, "c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 7 { } else if n != 7 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zincrby", key, -4, "c")); err != nil { if n, err := goredis.Int(c.Do("zincrby", key, -4, "c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zincrby", key, 4, "d")); err != nil { if n, err := goredis.Int(c.Do("zincrby", key, 4, "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zrem", key, "a", "b", "c", "d")); err != nil { if n, err := goredis.Int(c.Do("zrem", key, "a", "b", "c", "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
@ -123,47 +123,47 @@ func TestZSetCount(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("myzset") key := []byte("myzset")
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil { if _, err := goredis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("zcount", key, 2, 4)); err != nil { if n, err := goredis.Int(c.Do("zcount", key, 2, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 3 { } else if n != 3 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcount", key, 4, 4)); err != nil { if n, err := goredis.Int(c.Do("zcount", key, 4, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcount", key, 4, 3)); err != nil { if n, err := goredis.Int(c.Do("zcount", key, 4, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcount", key, "(2", 4)); err != nil { if n, err := goredis.Int(c.Do("zcount", key, "(2", 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcount", key, "2", "(4")); err != nil { if n, err := goredis.Int(c.Do("zcount", key, "2", "(4")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcount", key, "(2", "(4")); err != nil { if n, err := goredis.Int(c.Do("zcount", key, "(2", "(4")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcount", key, "-inf", "+inf")); err != nil { if n, err := goredis.Int(c.Do("zcount", key, "-inf", "+inf")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 4 { } else if n != 4 {
t.Fatal(n) t.Fatal(n)
@ -171,7 +171,7 @@ func TestZSetCount(t *testing.T) {
c.Do("zadd", key, 3, "e") c.Do("zadd", key, 3, "e")
if n, err := ledis.Int(c.Do("zcount", key, "(2", "(4")); err != nil { if n, err := goredis.Int(c.Do("zcount", key, "(2", "(4")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
@ -185,27 +185,27 @@ func TestZSetRank(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("myzset") key := []byte("myzset")
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil { if _, err := goredis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("zrank", key, "c")); err != nil { if n, err := goredis.Int(c.Do("zrank", key, "c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if _, err := ledis.Int(c.Do("zrank", key, "e")); err != ledis.ErrNil { if _, err := goredis.Int(c.Do("zrank", key, "e")); err != goredis.ErrNil {
t.Fatal(err) t.Fatal(err)
} }
if n, err := ledis.Int(c.Do("zrevrank", key, "c")); err != nil { if n, err := goredis.Int(c.Do("zrevrank", key, "c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(n) t.Fatal(n)
} }
if _, err := ledis.Int(c.Do("zrevrank", key, "e")); err != ledis.ErrNil { if _, err := goredis.Int(c.Do("zrevrank", key, "e")); err != goredis.ErrNil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -242,11 +242,11 @@ func TestZSetRangeScore(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("myzset_range") key := []byte("myzset_range")
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil { if _, err := goredis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, 1, 4, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrangebyscore", key, 1, 4, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil { if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
@ -254,7 +254,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, 1, 4, "withscores", "limit", 1, 2)); err != nil { if v, err := goredis.MultiBulk(c.Do("zrangebyscore", key, 1, 4, "withscores", "limit", 1, 2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "b", 2, "c", 3); err != nil { if err := testZSetRange(v, "b", 2, "c", 3); err != nil {
@ -262,7 +262,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, "-inf", "+inf", "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrangebyscore", key, "-inf", "+inf", "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil { if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
@ -270,7 +270,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, "(1", "(4")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrangebyscore", key, "(1", "(4")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "b", "c"); err != nil { if err := testZSetRange(v, "b", "c"); err != nil {
@ -278,7 +278,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil { if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
@ -286,7 +286,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores", "limit", 1, 2)); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores", "limit", 1, 2)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "c", 3, "b", 2); err != nil { if err := testZSetRange(v, "c", 3, "b", 2); err != nil {
@ -294,7 +294,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "+inf", "-inf", "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrangebyscore", key, "+inf", "-inf", "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil { if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
@ -302,7 +302,7 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "(4", "(1")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrangebyscore", key, "(4", "(1")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "c", "b"); err != nil { if err := testZSetRange(v, "c", "b"); err != nil {
@ -310,19 +310,19 @@ func TestZSetRangeScore(t *testing.T) {
} }
} }
if n, err := ledis.Int(c.Do("zremrangebyscore", key, 2, 3)); err != nil { if n, err := goredis.Int(c.Do("zremrangebyscore", key, 2, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, 1, 4)); err != nil { if v, err := goredis.MultiBulk(c.Do("zrangebyscore", key, 1, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "a", "d"); err != nil { if err := testZSetRange(v, "a", "d"); err != nil {
@ -336,11 +336,11 @@ func TestZSetRange(t *testing.T) {
defer c.Close() defer c.Close()
key := []byte("myzset_range_rank") key := []byte("myzset_range_rank")
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil { if _, err := goredis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if v, err := ledis.MultiBulk(c.Do("zrange", key, 0, 3, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrange", key, 0, 3, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil { if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
@ -348,7 +348,7 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrange", key, 1, 4, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrange", key, 1, 4, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "b", 2, "c", 3, "d", 4); err != nil { if err := testZSetRange(v, "b", 2, "c", 3, "d", 4); err != nil {
@ -356,7 +356,7 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrange", key, -2, -1, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrange", key, -2, -1, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "c", 3, "d", 4); err != nil { if err := testZSetRange(v, "c", 3, "d", 4); err != nil {
@ -364,7 +364,7 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrange", key, 0, -1, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrange", key, 0, -1, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil { if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
@ -372,13 +372,13 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrange", key, -1, -2, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrange", key, -1, -2, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(v) != 0 { } else if len(v) != 0 {
t.Fatal(len(v)) t.Fatal(len(v))
} }
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, 0, 4, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrange", key, 0, 4, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil { if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
@ -386,7 +386,7 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, 0, -1, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrange", key, 0, -1, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil { if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
@ -394,7 +394,7 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, 2, 3, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrange", key, 2, 3, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "b", 2, "a", 1); err != nil { if err := testZSetRange(v, "b", 2, "a", 1); err != nil {
@ -402,7 +402,7 @@ func TestZSetRange(t *testing.T) {
} }
} }
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, -2, -1, "withscores")); err != nil { if v, err := goredis.MultiBulk(c.Do("zrevrange", key, -2, -1, "withscores")); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "b", 2, "a", 1); err != nil { if err := testZSetRange(v, "b", 2, "a", 1); err != nil {
@ -410,19 +410,19 @@ func TestZSetRange(t *testing.T) {
} }
} }
if n, err := ledis.Int(c.Do("zremrangebyrank", key, 2, 3)); err != nil { if n, err := goredis.Int(c.Do("zremrangebyrank", key, 2, 3)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if v, err := ledis.MultiBulk(c.Do("zrange", key, 0, 4)); err != nil { if v, err := goredis.MultiBulk(c.Do("zrange", key, 0, 4)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if err := testZSetRange(v, "a", "b"); err != nil { if err := testZSetRange(v, "a", "b"); err != nil {
@ -430,13 +430,13 @@ func TestZSetRange(t *testing.T) {
} }
} }
if n, err := ledis.Int(c.Do("zclear", key)); err != nil { if n, err := goredis.Int(c.Do("zclear", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int(c.Do("zcard", key)); err != nil { if n, err := goredis.Int(c.Do("zcard", key)); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
t.Fatal(n) t.Fatal(n)
@ -634,7 +634,7 @@ func TestZUnionStore(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if n, err := ledis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "weights", "1", "2")); err != nil { if n, err := goredis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "weights", "1", "2")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 3 { if n != 3 {
@ -642,7 +642,7 @@ func TestZUnionStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "weights", "1", "2", "aggregate", "min")); err != nil { if n, err := goredis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "weights", "1", "2", "aggregate", "min")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 3 { if n != 3 {
@ -650,7 +650,7 @@ func TestZUnionStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "aggregate", "max")); err != nil { if n, err := goredis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "aggregate", "max")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 3 { if n != 3 {
@ -658,7 +658,7 @@ func TestZUnionStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zscore", "out", "two")); err != nil { if n, err := goredis.Int64(c.Do("zscore", "out", "two")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 2 { if n != 2 {
@ -687,7 +687,7 @@ func TestZInterStore(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if n, err := ledis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "weights", "1", "2")); err != nil { if n, err := goredis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "weights", "1", "2")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 1 { if n != 1 {
@ -695,7 +695,7 @@ func TestZInterStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "aggregate", "min", "weights", "1", "2")); err != nil { if n, err := goredis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "aggregate", "min", "weights", "1", "2")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 1 { if n != 1 {
@ -703,7 +703,7 @@ func TestZInterStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "aggregate", "sum")); err != nil { if n, err := goredis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "aggregate", "sum")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 1 { if n != 1 {
@ -711,7 +711,7 @@ func TestZInterStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zscore", "out", "two")); err != nil { if n, err := goredis.Int64(c.Do("zscore", "out", "two")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 3 { if n != 3 {
@ -723,7 +723,7 @@ func TestZInterStore(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if n, err := ledis.Int64(c.Do("zinterstore", "out", "3", "k1", "k2", "k3", "aggregate", "sum")); err != nil { if n, err := goredis.Int64(c.Do("zinterstore", "out", "3", "k1", "k2", "k3", "aggregate", "sum")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 0 { if n != 0 {
@ -735,7 +735,7 @@ func TestZInterStore(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if n, err := ledis.Int64(c.Do("zinterstore", "out", "3", "k1", "k2", "k3", "aggregate", "sum", "weights", "3", "2", "2")); err != nil { if n, err := goredis.Int64(c.Do("zinterstore", "out", "3", "k1", "k2", "k3", "aggregate", "sum", "weights", "3", "2", "2")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 1 { if n != 1 {
@ -743,7 +743,7 @@ func TestZInterStore(t *testing.T) {
} }
} }
if n, err := ledis.Int64(c.Do("zscore", "out", "two")); err != nil { if n, err := goredis.Int64(c.Do("zscore", "out", "two")); err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} else { } else {
if n != 14 { if n != 14 {
@ -762,37 +762,37 @@ func TestZSetLex(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if ay, err := ledis.Strings(c.Do("zrangebylex", key, "-", "[c")); err != nil { if ay, err := goredis.Strings(c.Do("zrangebylex", key, "-", "[c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(ay, []string{"a", "b", "c"}) { } else if !reflect.DeepEqual(ay, []string{"a", "b", "c"}) {
t.Fatal("must equal") t.Fatal("must equal")
} }
if ay, err := ledis.Strings(c.Do("zrangebylex", key, "-", "(c")); err != nil { if ay, err := goredis.Strings(c.Do("zrangebylex", key, "-", "(c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(ay, []string{"a", "b"}) { } else if !reflect.DeepEqual(ay, []string{"a", "b"}) {
t.Fatal("must equal") t.Fatal("must equal")
} }
if ay, err := ledis.Strings(c.Do("zrangebylex", key, "[aaa", "(g")); err != nil { if ay, err := goredis.Strings(c.Do("zrangebylex", key, "[aaa", "(g")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !reflect.DeepEqual(ay, []string{"b", "c", "d", "e", "f"}) { } else if !reflect.DeepEqual(ay, []string{"b", "c", "d", "e", "f"}) {
t.Fatal("must equal") t.Fatal("must equal")
} }
if n, err := ledis.Int64(c.Do("zlexcount", key, "-", "(c")); err != nil { if n, err := goredis.Int64(c.Do("zlexcount", key, "-", "(c")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int64(c.Do("zremrangebylex", key, "[aaa", "(g")); err != nil { if n, err := goredis.Int64(c.Do("zremrangebylex", key, "[aaa", "(g")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 5 { } else if n != 5 {
t.Fatal(n) t.Fatal(n)
} }
if n, err := ledis.Int64(c.Do("zlexcount", key, "-", "+")); err != nil { if n, err := goredis.Int64(c.Do("zlexcount", key, "-", "+")); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 2 { } else if n != 2 {
t.Fatal(n) t.Fatal(n)

View File

@ -7,7 +7,7 @@ import (
"github.com/siddontang/go/log" "github.com/siddontang/go/log"
"github.com/siddontang/go/num" "github.com/siddontang/go/num"
"github.com/siddontang/go/sync2" "github.com/siddontang/go/sync2"
goledis "github.com/siddontang/ledisdb/client/goledis" "github.com/siddontang/goredis"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"github.com/siddontang/ledisdb/rpl" "github.com/siddontang/ledisdb/rpl"
"net" "net"
@ -49,7 +49,7 @@ type master struct {
sync.Mutex sync.Mutex
connLock sync.Mutex connLock sync.Mutex
conn *goledis.Conn conn *goredis.Conn
app *App app *App
@ -108,7 +108,7 @@ func (m *master) checkConn() error {
var err error var err error
if m.conn == nil { if m.conn == nil {
m.conn, err = goledis.Connect(m.addr) m.conn, err = goredis.Connect(m.addr)
} else { } else {
if _, err = m.conn.Do("PING"); err != nil { if _, err = m.conn.Do("PING"); err != nil {
m.conn.Close() m.conn.Close()
@ -227,7 +227,7 @@ func (m *master) replConf() error {
return err return err
} }
if s, err := goledis.String(m.conn.Do("replconf", "listening-port", port)); err != nil { if s, err := goredis.String(m.conn.Do("replconf", "listening-port", port)); err != nil {
return err return err
} else if strings.ToUpper(s) != "OK" { } else if strings.ToUpper(s) != "OK" {
return fmt.Errorf("not ok but %s", s) return fmt.Errorf("not ok but %s", s)