forked from mirror/ledisdb
use tiny go package
This commit is contained in:
parent
7d1f13f9d7
commit
5bccfd13dc
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/siddontang/go/hack"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -114,27 +115,27 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {
|
|||
if key, err := db.decodeKVKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
case HashType:
|
||||
if key, field, err := db.hDecodeHashKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendQuote(buf, String(field))
|
||||
buf = strconv.AppendQuote(buf, hack.String(field))
|
||||
}
|
||||
case HSizeType:
|
||||
if key, err := db.hDecodeSizeKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
case ListType:
|
||||
if key, seq, err := db.lDecodeListKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendInt(buf, int64(seq), 10)
|
||||
}
|
||||
|
@ -142,29 +143,29 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {
|
|||
if key, err := db.lDecodeMetaKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
case ZSetType:
|
||||
if key, m, err := db.zDecodeSetKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendQuote(buf, String(m))
|
||||
buf = strconv.AppendQuote(buf, hack.String(m))
|
||||
}
|
||||
case ZSizeType:
|
||||
if key, err := db.zDecodeSizeKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
case ZScoreType:
|
||||
if key, m, score, err := db.zDecodeScoreKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendQuote(buf, String(m))
|
||||
buf = strconv.AppendQuote(buf, hack.String(m))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendInt(buf, score, 10)
|
||||
}
|
||||
|
@ -172,7 +173,7 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {
|
|||
if key, seq, err := db.bDecodeBinKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendUint(buf, uint64(seq), 10)
|
||||
}
|
||||
|
@ -180,21 +181,21 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {
|
|||
if key, err := db.bDecodeMetaKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
case SetType:
|
||||
if key, member, err := db.sDecodeSetKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendQuote(buf, String(member))
|
||||
buf = strconv.AppendQuote(buf, hack.String(member))
|
||||
}
|
||||
case SSizeType:
|
||||
if key, err := db.sDecodeSizeKey(k); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
case ExpTimeType:
|
||||
if tp, key, t, err := db.expDecodeTimeKey(k); err != nil {
|
||||
|
@ -202,7 +203,7 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {
|
|||
} else {
|
||||
buf = append(buf, TypeName[tp]...)
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendInt(buf, t, 10)
|
||||
}
|
||||
|
@ -212,7 +213,7 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {
|
|||
} else {
|
||||
buf = append(buf, TypeName[tp]...)
|
||||
buf = append(buf, ' ')
|
||||
buf = strconv.AppendQuote(buf, String(key))
|
||||
buf = strconv.AppendQuote(buf, hack.String(key))
|
||||
}
|
||||
default:
|
||||
return nil, errInvalidEvent
|
||||
|
|
|
@ -3,6 +3,7 @@ package ledis
|
|||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/siddontang/go/num"
|
||||
"github.com/siddontang/ledisdb/store"
|
||||
"sort"
|
||||
"time"
|
||||
|
@ -239,8 +240,8 @@ func (db *DB) bUpdateMeta(t *batch, key []byte, seq uint32, off uint32) (tailSeq
|
|||
} else if tseq < 0 {
|
||||
update = true
|
||||
} else {
|
||||
tailSeq = uint32(MaxInt32(tseq, 0))
|
||||
tailOff = uint32(MaxInt32(toff, 0))
|
||||
tailSeq = uint32(num.MaxInt32(tseq, 0))
|
||||
tailOff = uint32(num.MaxInt32(toff, 0))
|
||||
update = (seq > tailSeq || (seq == tailSeq && off > tailOff))
|
||||
}
|
||||
|
||||
|
@ -461,7 +462,7 @@ func (db *DB) BGet(key []byte) (data []byte, err error) {
|
|||
}
|
||||
|
||||
s = seq << segByteWidth
|
||||
e = MinUInt32(s+segByteSize, capByteSize)
|
||||
e = num.MinUint32(s+segByteSize, capByteSize)
|
||||
copy(data[s:e], it.RawValue())
|
||||
}
|
||||
it.Close()
|
||||
|
|
|
@ -3,6 +3,7 @@ package ledis
|
|||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/ledisdb/store"
|
||||
"time"
|
||||
)
|
||||
|
@ -240,7 +241,7 @@ func (db *DB) sDiffGeneric(keys ...[]byte) ([][]byte, error) {
|
|||
}
|
||||
|
||||
for _, m := range members {
|
||||
destMap[String(m)] = true
|
||||
destMap[hack.String(m)] = true
|
||||
}
|
||||
|
||||
for _, k := range keys[1:] {
|
||||
|
@ -250,10 +251,10 @@ func (db *DB) sDiffGeneric(keys ...[]byte) ([][]byte, error) {
|
|||
}
|
||||
|
||||
for _, m := range members {
|
||||
if _, ok := destMap[String(m)]; !ok {
|
||||
if _, ok := destMap[hack.String(m)]; !ok {
|
||||
continue
|
||||
} else if ok {
|
||||
delete(destMap, String(m))
|
||||
delete(destMap, hack.String(m))
|
||||
}
|
||||
}
|
||||
// O - A = O, O is zero set.
|
||||
|
@ -294,7 +295,7 @@ func (db *DB) sInterGeneric(keys ...[]byte) ([][]byte, error) {
|
|||
}
|
||||
|
||||
for _, m := range members {
|
||||
destMap[String(m)] = true
|
||||
destMap[hack.String(m)] = true
|
||||
}
|
||||
|
||||
for _, key := range keys[1:] {
|
||||
|
@ -314,8 +315,8 @@ func (db *DB) sInterGeneric(keys ...[]byte) ([][]byte, error) {
|
|||
if err := checkKeySize(member); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := destMap[String(member)]; ok {
|
||||
tempMap[String(member)] = true //mark this item as selected
|
||||
if _, ok := destMap[hack.String(member)]; ok {
|
||||
tempMap[hack.String(member)] = true //mark this item as selected
|
||||
}
|
||||
}
|
||||
destMap = tempMap //reduce the size of the result set
|
||||
|
@ -439,7 +440,7 @@ func (db *DB) sUnionGeneric(keys ...[]byte) ([][]byte, error) {
|
|||
}
|
||||
|
||||
for _, member := range members {
|
||||
dstMap[String(member)] = true
|
||||
dstMap[hack.String(member)] = true
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package ledis
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/siddontang/go/hack"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -49,7 +50,7 @@ func listAdaptor(db *DB) *adaptor {
|
|||
adp.set = func(k []byte, v []byte) (int64, error) {
|
||||
eles := make([][]byte, 0)
|
||||
for i := 0; i < 3; i++ {
|
||||
e := []byte(String(v) + fmt.Sprintf("_%d", i))
|
||||
e := []byte(hack.String(v) + fmt.Sprintf("_%d", i))
|
||||
eles = append(eles, e)
|
||||
}
|
||||
|
||||
|
@ -87,8 +88,8 @@ func hashAdaptor(db *DB) *adaptor {
|
|||
for i := 0; i < 3; i++ {
|
||||
suffix := fmt.Sprintf("_%d", i)
|
||||
pair := FVPair{
|
||||
Field: []byte(String(k) + suffix),
|
||||
Value: []byte(String(v) + suffix)}
|
||||
Field: []byte(hack.String(k) + suffix),
|
||||
Value: []byte(hack.String(v) + suffix)}
|
||||
|
||||
datas = append(datas, pair)
|
||||
}
|
||||
|
@ -125,7 +126,7 @@ func zsetAdaptor(db *DB) *adaptor {
|
|||
adp.set = func(k []byte, v []byte) (int64, error) {
|
||||
datas := make([]ScorePair, 0)
|
||||
for i := 0; i < 3; i++ {
|
||||
memb := []byte(String(k) + fmt.Sprintf("_%d", i))
|
||||
memb := []byte(hack.String(k) + fmt.Sprintf("_%d", i))
|
||||
pair := ScorePair{
|
||||
Score: int64(i),
|
||||
Member: memb}
|
||||
|
@ -165,7 +166,7 @@ func setAdaptor(db *DB) *adaptor {
|
|||
adp.set = func(k []byte, v []byte) (int64, error) {
|
||||
eles := make([][]byte, 0)
|
||||
for i := 0; i < 3; i++ {
|
||||
e := []byte(String(v) + fmt.Sprintf("_%d", i))
|
||||
e := []byte(hack.String(v) + fmt.Sprintf("_%d", i))
|
||||
eles = append(eles, e)
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/ledisdb/store"
|
||||
"time"
|
||||
)
|
||||
|
@ -833,10 +834,10 @@ func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
|||
return 0, err
|
||||
}
|
||||
for _, pair := range scorePairs {
|
||||
if score, ok := destMap[String(pair.Member)]; !ok {
|
||||
destMap[String(pair.Member)] = pair.Score * weights[i]
|
||||
if score, ok := destMap[hack.String(pair.Member)]; !ok {
|
||||
destMap[hack.String(pair.Member)] = pair.Score * weights[i]
|
||||
} else {
|
||||
destMap[String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i])
|
||||
destMap[hack.String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -893,7 +894,7 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
|||
return 0, err
|
||||
}
|
||||
for _, pair := range scorePairs {
|
||||
destMap[String(pair.Member)] = pair.Score * weights[0]
|
||||
destMap[hack.String(pair.Member)] = pair.Score * weights[0]
|
||||
}
|
||||
|
||||
for i, key := range srcKeys[1:] {
|
||||
|
@ -903,8 +904,8 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
|
|||
}
|
||||
tmpMap := map[string]int64{}
|
||||
for _, pair := range scorePairs {
|
||||
if score, ok := destMap[String(pair.Member)]; ok {
|
||||
tmpMap[String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i+1])
|
||||
if score, ok := destMap[hack.String(pair.Member)]; ok {
|
||||
tmpMap[hack.String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i+1])
|
||||
}
|
||||
}
|
||||
destMap = tmpMap
|
||||
|
|
|
@ -3,6 +3,7 @@ package ledis
|
|||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/siddontang/go/hack"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
|
@ -10,27 +11,6 @@ import (
|
|||
|
||||
var errIntNumber = errors.New("invalid integer")
|
||||
|
||||
// no copy to change slice to string
|
||||
// use your own risk
|
||||
func String(b []byte) (s string) {
|
||||
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
pstring.Data = pbytes.Data
|
||||
pstring.Len = pbytes.Len
|
||||
return
|
||||
}
|
||||
|
||||
// no copy to change string to slice
|
||||
// use your own risk
|
||||
func Slice(s string) (b []byte) {
|
||||
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
pbytes.Data = pstring.Data
|
||||
pbytes.Len = pstring.Len
|
||||
pbytes.Cap = pstring.Len
|
||||
return
|
||||
}
|
||||
|
||||
func Int64(v []byte, err error) (int64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -70,7 +50,7 @@ func StrInt64(v []byte, err error) (int64, error) {
|
|||
} else if v == nil {
|
||||
return 0, nil
|
||||
} else {
|
||||
return strconv.ParseInt(String(v), 10, 64)
|
||||
return strconv.ParseInt(hack.String(v), 10, 64)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,7 +60,7 @@ func StrUint64(v []byte, err error) (uint64, error) {
|
|||
} else if v == nil {
|
||||
return 0, nil
|
||||
} else {
|
||||
return strconv.ParseUint(String(v), 10, 64)
|
||||
return strconv.ParseUint(hack.String(v), 10, 64)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,7 +70,7 @@ func StrInt32(v []byte, err error) (int32, error) {
|
|||
} else if v == nil {
|
||||
return 0, nil
|
||||
} else {
|
||||
res, err := strconv.ParseInt(String(v), 10, 32)
|
||||
res, err := strconv.ParseInt(hack.String(v), 10, 32)
|
||||
return int32(res), err
|
||||
}
|
||||
}
|
||||
|
@ -101,7 +81,7 @@ func StrInt8(v []byte, err error) (int8, error) {
|
|||
} else if v == nil {
|
||||
return 0, nil
|
||||
} else {
|
||||
res, err := strconv.ParseInt(String(v), 10, 8)
|
||||
res, err := strconv.ParseInt(hack.String(v), 10, 8)
|
||||
return int8(res), err
|
||||
}
|
||||
}
|
||||
|
@ -114,30 +94,6 @@ func StrPutUint64(v uint64) []byte {
|
|||
return strconv.AppendUint(nil, v, 10)
|
||||
}
|
||||
|
||||
func MinUInt32(a uint32, b uint32) uint32 {
|
||||
if a > b {
|
||||
return b
|
||||
} else {
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
func MaxUInt32(a uint32, b uint32) uint32 {
|
||||
if a > b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func MaxInt32(a int32, b int32) int32 {
|
||||
if a > b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func AsyncNotify(ch chan struct{}) {
|
||||
select {
|
||||
case ch <- struct{}{}:
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/siddontang/go/bson"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/go/log"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/ugorji/go/codec"
|
||||
|
@ -151,7 +152,7 @@ func (w *httpWriter) writeBulk(b []byte) {
|
|||
if b == nil {
|
||||
w.genericWrite(nil)
|
||||
} else {
|
||||
w.genericWrite(ledis.String(b))
|
||||
w.genericWrite(hack.String(b))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,7 +166,7 @@ func (w *httpWriter) writeSliceArray(lst [][]byte) {
|
|||
if elem == nil {
|
||||
arr[i] = nil
|
||||
} else {
|
||||
arr[i] = ledis.String(elem)
|
||||
arr[i] = hack.String(elem)
|
||||
}
|
||||
}
|
||||
w.genericWrite(arr)
|
||||
|
@ -174,7 +175,7 @@ func (w *httpWriter) writeSliceArray(lst [][]byte) {
|
|||
func (w *httpWriter) writeFVPairArray(lst []ledis.FVPair) {
|
||||
m := make(map[string]string)
|
||||
for _, elem := range lst {
|
||||
m[ledis.String(elem.Field)] = ledis.String(elem.Value)
|
||||
m[hack.String(elem.Field)] = hack.String(elem.Value)
|
||||
}
|
||||
w.genericWrite(m)
|
||||
}
|
||||
|
@ -184,13 +185,13 @@ func (w *httpWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool)
|
|||
if withScores {
|
||||
arr = make([]string, 2*len(lst))
|
||||
for i, data := range lst {
|
||||
arr[2*i] = ledis.String(data.Member)
|
||||
arr[2*i] = hack.String(data.Member)
|
||||
arr[2*i+1] = strconv.FormatInt(data.Score, 10)
|
||||
}
|
||||
} else {
|
||||
arr = make([]string, len(lst))
|
||||
for i, data := range lst {
|
||||
arr[i] = ledis.String(data.Member)
|
||||
arr[i] = hack.String(data.Member)
|
||||
}
|
||||
}
|
||||
w.genericWrite(arr)
|
||||
|
|
|
@ -3,6 +3,7 @@ package server
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/go/log"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"io"
|
||||
|
@ -80,7 +81,7 @@ func (c *respClient) readRequest() ([][]byte, error) {
|
|||
}
|
||||
|
||||
var nparams int
|
||||
if nparams, err = strconv.Atoi(ledis.String(l[1:])); err != nil {
|
||||
if nparams, err = strconv.Atoi(hack.String(l[1:])); err != nil {
|
||||
return nil, err
|
||||
} else if nparams <= 0 {
|
||||
return nil, errReadRequest
|
||||
|
@ -97,7 +98,7 @@ func (c *respClient) readRequest() ([][]byte, error) {
|
|||
return nil, errReadRequest
|
||||
} else if l[0] == '$' {
|
||||
//handle resp string
|
||||
if n, err = strconv.Atoi(ledis.String(l[1:])); err != nil {
|
||||
if n, err = strconv.Atoi(hack.String(l[1:])); err != nil {
|
||||
return nil, err
|
||||
} else if n == -1 {
|
||||
req = append(req, nil)
|
||||
|
@ -130,7 +131,7 @@ func (c *respClient) handleRequest(reqData [][]byte) {
|
|||
c.cmd = ""
|
||||
c.args = reqData[0:0]
|
||||
} else {
|
||||
c.cmd = strings.ToLower(ledis.String(reqData[0]))
|
||||
c.cmd = strings.ToLower(hack.String(reqData[0]))
|
||||
c.args = reqData[1:]
|
||||
}
|
||||
if c.cmd == "quit" {
|
||||
|
@ -154,17 +155,17 @@ func newWriterRESP(conn net.Conn) *respWriter {
|
|||
}
|
||||
|
||||
func (w *respWriter) writeError(err error) {
|
||||
w.buff.Write(ledis.Slice("-ERR"))
|
||||
w.buff.Write(hack.Slice("-ERR"))
|
||||
if err != nil {
|
||||
w.buff.WriteByte(' ')
|
||||
w.buff.Write(ledis.Slice(err.Error()))
|
||||
w.buff.Write(hack.Slice(err.Error()))
|
||||
}
|
||||
w.buff.Write(Delims)
|
||||
}
|
||||
|
||||
func (w *respWriter) writeStatus(status string) {
|
||||
w.buff.WriteByte('+')
|
||||
w.buff.Write(ledis.Slice(status))
|
||||
w.buff.Write(hack.Slice(status))
|
||||
w.buff.Write(Delims)
|
||||
}
|
||||
|
||||
|
@ -179,7 +180,7 @@ func (w *respWriter) writeBulk(b []byte) {
|
|||
if b == nil {
|
||||
w.buff.Write(NullBulk)
|
||||
} else {
|
||||
w.buff.Write(ledis.Slice(strconv.Itoa(len(b))))
|
||||
w.buff.Write(hack.Slice(strconv.Itoa(len(b))))
|
||||
w.buff.Write(Delims)
|
||||
w.buff.Write(b)
|
||||
}
|
||||
|
@ -193,7 +194,7 @@ func (w *respWriter) writeArray(lst []interface{}) {
|
|||
w.buff.Write(NullArray)
|
||||
w.buff.Write(Delims)
|
||||
} else {
|
||||
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
|
||||
w.buff.Write(hack.Slice(strconv.Itoa(len(lst))))
|
||||
w.buff.Write(Delims)
|
||||
|
||||
for i := 0; i < len(lst); i++ {
|
||||
|
@ -221,7 +222,7 @@ func (w *respWriter) writeSliceArray(lst [][]byte) {
|
|||
w.buff.Write(NullArray)
|
||||
w.buff.Write(Delims)
|
||||
} else {
|
||||
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
|
||||
w.buff.Write(hack.Slice(strconv.Itoa(len(lst))))
|
||||
w.buff.Write(Delims)
|
||||
|
||||
for i := 0; i < len(lst); i++ {
|
||||
|
@ -236,7 +237,7 @@ func (w *respWriter) writeFVPairArray(lst []ledis.FVPair) {
|
|||
w.buff.Write(NullArray)
|
||||
w.buff.Write(Delims)
|
||||
} else {
|
||||
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst) * 2)))
|
||||
w.buff.Write(hack.Slice(strconv.Itoa(len(lst) * 2)))
|
||||
w.buff.Write(Delims)
|
||||
|
||||
for i := 0; i < len(lst); i++ {
|
||||
|
@ -253,10 +254,10 @@ func (w *respWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool)
|
|||
w.buff.Write(Delims)
|
||||
} else {
|
||||
if withScores {
|
||||
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst) * 2)))
|
||||
w.buff.Write(hack.Slice(strconv.Itoa(len(lst) * 2)))
|
||||
w.buff.Write(Delims)
|
||||
} else {
|
||||
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
|
||||
w.buff.Write(hack.Slice(strconv.Itoa(len(lst))))
|
||||
w.buff.Write(Delims)
|
||||
|
||||
}
|
||||
|
@ -273,7 +274,7 @@ func (w *respWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool)
|
|||
|
||||
func (w *respWriter) writeBulkFrom(n int64, rb io.Reader) {
|
||||
w.buff.WriteByte('$')
|
||||
w.buff.Write(ledis.Slice(strconv.FormatInt(n, 10)))
|
||||
w.buff.Write(hack.Slice(strconv.FormatInt(n, 10)))
|
||||
w.buff.Write(Delims)
|
||||
|
||||
io.Copy(w.buff, rb)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/siddontang/go/hack"
|
||||
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"strings"
|
||||
)
|
||||
|
@ -173,7 +175,7 @@ func boptCommand(c *client) error {
|
|||
return ErrCmdParams
|
||||
}
|
||||
|
||||
opDesc := strings.ToLower(ledis.String(args[0]))
|
||||
opDesc := strings.ToLower(hack.String(args[0]))
|
||||
dstKey := args[1]
|
||||
srcKeys := args[2:]
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -292,26 +293,26 @@ func parseScanArgs(c *client) (key []byte, match string, count int, err error) {
|
|||
}
|
||||
|
||||
if len(args) == 3 {
|
||||
switch strings.ToLower(ledis.String(args[1])) {
|
||||
switch strings.ToLower(hack.String(args[1])) {
|
||||
case "match":
|
||||
match = ledis.String(args[2])
|
||||
match = hack.String(args[2])
|
||||
case "count":
|
||||
count, err = strconv.Atoi(ledis.String(args[2]))
|
||||
count, err = strconv.Atoi(hack.String(args[2]))
|
||||
default:
|
||||
err = ErrCmdParams
|
||||
return
|
||||
}
|
||||
} else if len(args) == 5 {
|
||||
if strings.ToLower(ledis.String(args[1])) != "match" {
|
||||
if strings.ToLower(hack.String(args[1])) != "match" {
|
||||
err = ErrCmdParams
|
||||
return
|
||||
} else if strings.ToLower(ledis.String(args[3])) != "count" {
|
||||
} else if strings.ToLower(hack.String(args[3])) != "count" {
|
||||
err = ErrCmdParams
|
||||
return
|
||||
}
|
||||
|
||||
match = ledis.String(args[2])
|
||||
count, err = strconv.Atoi(ledis.String(args[4]))
|
||||
match = hack.String(args[2])
|
||||
count, err = strconv.Atoi(hack.String(args[4]))
|
||||
}
|
||||
|
||||
if count <= 0 {
|
||||
|
|
|
@ -2,7 +2,9 @@ package server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/go/snappy"
|
||||
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -19,11 +21,11 @@ func slaveofCommand(c *client) error {
|
|||
|
||||
masterAddr := ""
|
||||
|
||||
if strings.ToLower(ledis.String(args[0])) == "no" &&
|
||||
strings.ToLower(ledis.String(args[1])) == "one" {
|
||||
if strings.ToLower(hack.String(args[0])) == "no" &&
|
||||
strings.ToLower(hack.String(args[1])) == "one" {
|
||||
//stop replication, use master = ""
|
||||
} else {
|
||||
if _, err := strconv.ParseInt(ledis.String(args[1]), 10, 16); err != nil {
|
||||
if _, err := strconv.ParseInt(hack.String(args[1]), 10, 16); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,8 @@ import (
|
|||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/go/hack"
|
||||
|
||||
"github.com/siddontang/ledisdb/lua"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -20,7 +21,7 @@ func parseEvalArgs(l *lua.State, c *client) error {
|
|||
|
||||
args = args[1:]
|
||||
|
||||
n, err := strconv.Atoi(ledis.String(args[0]))
|
||||
n, err := strconv.Atoi(hack.String(args[0]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -72,7 +73,7 @@ func evalGenericCommand(c *client, evalSha1 bool) error {
|
|||
h := sha1.Sum(c.args[0])
|
||||
key = hex.EncodeToString(h[0:20])
|
||||
} else {
|
||||
key = strings.ToLower(ledis.String(c.args[0]))
|
||||
key = strings.ToLower(hack.String(c.args[0]))
|
||||
}
|
||||
|
||||
l.GetGlobal(key)
|
||||
|
@ -84,7 +85,7 @@ func evalGenericCommand(c *client, evalSha1 bool) error {
|
|||
return fmt.Errorf("missing %s script", key)
|
||||
}
|
||||
|
||||
if r := l.LoadString(ledis.String(c.args[0])); r != 0 {
|
||||
if r := l.LoadString(hack.String(c.args[0])); r != 0 {
|
||||
err := fmt.Errorf("%s", l.ToString(-1))
|
||||
l.Pop(1)
|
||||
return err
|
||||
|
@ -139,7 +140,7 @@ func scriptCommand(c *client) error {
|
|||
return ErrCmdParams
|
||||
}
|
||||
|
||||
switch strings.ToLower(ledis.String(args[0])) {
|
||||
switch strings.ToLower(hack.String(args[0])) {
|
||||
case "load":
|
||||
return scriptLoadCommand(c)
|
||||
case "exists":
|
||||
|
@ -164,7 +165,7 @@ func scriptLoadCommand(c *client) error {
|
|||
h := sha1.Sum(c.args[1])
|
||||
key := hex.EncodeToString(h[0:20])
|
||||
|
||||
if r := l.LoadString(ledis.String(c.args[1])); r != 0 {
|
||||
if r := l.LoadString(hack.String(c.args[1])); r != 0 {
|
||||
err := fmt.Errorf("%s", l.ToString(-1))
|
||||
l.Pop(1)
|
||||
return err
|
||||
|
@ -175,7 +176,7 @@ func scriptLoadCommand(c *client) error {
|
|||
s.chunks[key] = struct{}{}
|
||||
}
|
||||
|
||||
c.resp.writeBulk(ledis.Slice(key))
|
||||
c.resp.writeBulk(hack.Slice(key))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -188,7 +189,7 @@ func scriptExistsCommand(c *client) error {
|
|||
|
||||
ay := make([]interface{}, len(c.args[1:]))
|
||||
for i, n := range c.args[1:] {
|
||||
if _, ok := s.chunks[ledis.String(n)]; ok {
|
||||
if _, ok := s.chunks[hack.String(n)]; ok {
|
||||
ay[i] = int64(1)
|
||||
} else {
|
||||
ay[i] = int64(0)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/go/hack"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
@ -25,7 +25,7 @@ func selectCommand(c *client) error {
|
|||
return ErrCmdParams
|
||||
}
|
||||
|
||||
if index, err := strconv.Atoi(ledis.String(c.args[0])); err != nil {
|
||||
if index, err := strconv.Atoi(hack.String(c.args[0])); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if c.db.IsInMulti() {
|
||||
|
@ -53,7 +53,7 @@ func infoCommand(c *client) error {
|
|||
}
|
||||
var section string
|
||||
if len(c.args) == 1 {
|
||||
section = strings.ToLower(ledis.String(c.args[0]))
|
||||
section = strings.ToLower(hack.String(c.args[0]))
|
||||
}
|
||||
|
||||
buf := c.app.info.Dump(section)
|
||||
|
@ -87,7 +87,7 @@ func readonlyCommand(c *client) error {
|
|||
return ErrCmdParams
|
||||
}
|
||||
|
||||
if flag, err := strconv.Atoi(ledis.String(c.args[0])); err != nil {
|
||||
if flag, err := strconv.Atoi(hack.String(c.args[0])); err != nil {
|
||||
return err
|
||||
} else {
|
||||
c.app.ldb.SetReadOnly(flag != 0)
|
||||
|
|
|
@ -2,6 +2,8 @@ package server
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/siddontang/go/hack"
|
||||
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"math"
|
||||
"strconv"
|
||||
|
@ -117,7 +119,7 @@ func zincrbyCommand(c *client) error {
|
|||
}
|
||||
|
||||
func zparseScoreRange(minBuf []byte, maxBuf []byte) (min int64, max int64, err error) {
|
||||
if strings.ToLower(ledis.String(minBuf)) == "-inf" {
|
||||
if strings.ToLower(hack.String(minBuf)) == "-inf" {
|
||||
min = math.MinInt64
|
||||
} else {
|
||||
|
||||
|
@ -148,7 +150,7 @@ func zparseScoreRange(minBuf []byte, maxBuf []byte) (min int64, max int64, err e
|
|||
}
|
||||
}
|
||||
|
||||
if strings.ToLower(ledis.String(maxBuf)) == "+inf" {
|
||||
if strings.ToLower(hack.String(maxBuf)) == "+inf" {
|
||||
max = math.MaxInt64
|
||||
} else {
|
||||
var ropen = false
|
||||
|
@ -289,11 +291,11 @@ func zremrangebyscoreCommand(c *client) error {
|
|||
}
|
||||
|
||||
func zparseRange(c *client, a1 []byte, a2 []byte) (start int, stop int, err error) {
|
||||
if start, err = strconv.Atoi(ledis.String(a1)); err != nil {
|
||||
if start, err = strconv.Atoi(hack.String(a1)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if stop, err = strconv.Atoi(ledis.String(a2)); err != nil {
|
||||
if stop, err = strconv.Atoi(hack.String(a2)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -320,7 +322,7 @@ func zrangeGeneric(c *client, reverse bool) error {
|
|||
if len(args) != 1 {
|
||||
return ErrCmdParams
|
||||
}
|
||||
if strings.ToLower(ledis.String(args[0])) == "withscores" {
|
||||
if strings.ToLower(hack.String(args[0])) == "withscores" {
|
||||
withScores = true
|
||||
} else {
|
||||
return ErrSyntax
|
||||
|
@ -370,7 +372,7 @@ func zrangebyscoreGeneric(c *client, reverse bool) error {
|
|||
var withScores bool = false
|
||||
|
||||
if len(args) > 0 {
|
||||
if strings.ToLower(ledis.String(args[0])) == "withscores" {
|
||||
if strings.ToLower(hack.String(args[0])) == "withscores" {
|
||||
withScores = true
|
||||
args = args[1:]
|
||||
}
|
||||
|
@ -384,15 +386,15 @@ func zrangebyscoreGeneric(c *client, reverse bool) error {
|
|||
return ErrCmdParams
|
||||
}
|
||||
|
||||
if strings.ToLower(ledis.String(args[0])) != "limit" {
|
||||
if strings.ToLower(hack.String(args[0])) != "limit" {
|
||||
return ErrSyntax
|
||||
}
|
||||
|
||||
if offset, err = strconv.Atoi(ledis.String(args[1])); err != nil {
|
||||
if offset, err = strconv.Atoi(hack.String(args[1])); err != nil {
|
||||
return ErrValue
|
||||
}
|
||||
|
||||
if count, err = strconv.Atoi(ledis.String(args[2])); err != nil {
|
||||
if count, err = strconv.Atoi(hack.String(args[2])); err != nil {
|
||||
return ErrValue
|
||||
}
|
||||
}
|
||||
|
@ -523,7 +525,7 @@ func zpersistCommand(c *client) error {
|
|||
|
||||
func zparseZsetoptStore(args [][]byte) (destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte, err error) {
|
||||
destKey = args[0]
|
||||
nKeys, err := strconv.Atoi(ledis.String(args[1]))
|
||||
nKeys, err := strconv.Atoi(hack.String(args[1]))
|
||||
if err != nil {
|
||||
err = ErrValue
|
||||
return
|
||||
|
@ -542,7 +544,7 @@ func zparseZsetoptStore(args [][]byte) (destKey []byte, srcKeys [][]byte, weight
|
|||
var aggregateFlag = false
|
||||
|
||||
for len(args) > 0 {
|
||||
if strings.ToLower(ledis.String(args[0])) == "weights" {
|
||||
if strings.ToLower(hack.String(args[0])) == "weights" {
|
||||
if weightsFlag {
|
||||
err = ErrSyntax
|
||||
return
|
||||
|
@ -565,7 +567,7 @@ func zparseZsetoptStore(args [][]byte) (destKey []byte, srcKeys [][]byte, weight
|
|||
|
||||
weightsFlag = true
|
||||
|
||||
} else if strings.ToLower(ledis.String(args[0])) == "aggregate" {
|
||||
} else if strings.ToLower(hack.String(args[0])) == "aggregate" {
|
||||
if aggregateFlag {
|
||||
err = ErrSyntax
|
||||
return
|
||||
|
@ -575,11 +577,11 @@ func zparseZsetoptStore(args [][]byte) (destKey []byte, srcKeys [][]byte, weight
|
|||
return
|
||||
}
|
||||
|
||||
if strings.ToLower(ledis.String(args[1])) == "sum" {
|
||||
if strings.ToLower(hack.String(args[1])) == "sum" {
|
||||
aggregate = ledis.AggregateSum
|
||||
} else if strings.ToLower(ledis.String(args[1])) == "min" {
|
||||
} else if strings.ToLower(hack.String(args[1])) == "min" {
|
||||
aggregate = ledis.AggregateMin
|
||||
} else if strings.ToLower(ledis.String(args[1])) == "max" {
|
||||
} else if strings.ToLower(hack.String(args[1])) == "max" {
|
||||
aggregate = ledis.AggregateMax
|
||||
} else {
|
||||
err = ErrSyntax
|
||||
|
|
|
@ -5,7 +5,9 @@ import (
|
|||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/go/log"
|
||||
|
||||
"github.com/siddontang/go/snappy"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/ledisdb/rpl"
|
||||
|
@ -188,7 +190,7 @@ func (m *master) sync() error {
|
|||
|
||||
logIDStr := strconv.FormatUint(syncID, 10)
|
||||
|
||||
cmd := ledis.Slice(fmt.Sprintf(syncCmdFormat, len(logIDStr),
|
||||
cmd := hack.Slice(fmt.Sprintf(syncCmdFormat, len(logIDStr),
|
||||
logIDStr))
|
||||
|
||||
if _, err := m.conn.Write(cmd); err != nil {
|
||||
|
|
|
@ -5,6 +5,7 @@ package server
|
|||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/siddontang/go/hack"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/ledisdb/lua"
|
||||
"io"
|
||||
|
@ -38,7 +39,7 @@ func (w *luaWriter) writeBulk(b []byte) {
|
|||
if b == nil {
|
||||
w.l.PushBoolean(false)
|
||||
} else {
|
||||
w.l.PushString(ledis.String(b))
|
||||
w.l.PushString(hack.String(b))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,7 +82,7 @@ func (w *luaWriter) writeSliceArray(lst [][]byte) {
|
|||
|
||||
w.l.CreateTable(len(lst), 0)
|
||||
for i, v := range lst {
|
||||
w.l.PushString(ledis.String(v))
|
||||
w.l.PushString(hack.String(v))
|
||||
w.l.RawSeti(-2, i+1)
|
||||
}
|
||||
}
|
||||
|
@ -94,10 +95,10 @@ func (w *luaWriter) writeFVPairArray(lst []ledis.FVPair) {
|
|||
|
||||
w.l.CreateTable(len(lst)*2, 0)
|
||||
for i, v := range lst {
|
||||
w.l.PushString(ledis.String(v.Field))
|
||||
w.l.PushString(hack.String(v.Field))
|
||||
w.l.RawSeti(-2, 2*i+1)
|
||||
|
||||
w.l.PushString(ledis.String(v.Value))
|
||||
w.l.PushString(hack.String(v.Value))
|
||||
w.l.RawSeti(-2, 2*i+2)
|
||||
}
|
||||
}
|
||||
|
@ -111,16 +112,16 @@ func (w *luaWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool)
|
|||
if withScores {
|
||||
w.l.CreateTable(len(lst)*2, 0)
|
||||
for i, v := range lst {
|
||||
w.l.PushString(ledis.String(v.Member))
|
||||
w.l.PushString(hack.String(v.Member))
|
||||
w.l.RawSeti(-2, 2*i+1)
|
||||
|
||||
w.l.PushString(ledis.String(ledis.StrPutInt64(v.Score)))
|
||||
w.l.PushString(hack.String(ledis.StrPutInt64(v.Score)))
|
||||
w.l.RawSeti(-2, 2*i+2)
|
||||
}
|
||||
} else {
|
||||
w.l.CreateTable(len(lst), 0)
|
||||
for i, v := range lst {
|
||||
w.l.PushString(ledis.String(v.Member))
|
||||
w.l.PushString(hack.String(v.Member))
|
||||
w.l.RawSeti(-2, i+1)
|
||||
}
|
||||
}
|
||||
|
@ -280,7 +281,7 @@ func luaSha1Hex(l *lua.State) int {
|
|||
}
|
||||
|
||||
s := l.ToString(1)
|
||||
s = hex.EncodeToString(ledis.Slice(s))
|
||||
s = hex.EncodeToString(hack.Slice(s))
|
||||
|
||||
l.PushString(s)
|
||||
return 1
|
||||
|
@ -333,7 +334,7 @@ func luaSetGlobalArray(l *lua.State, name string, ay [][]byte) {
|
|||
l.NewTable()
|
||||
|
||||
for i := 0; i < len(ay); i++ {
|
||||
l.PushString(ledis.String(ay[i]))
|
||||
l.PushString(hack.String(ay[i]))
|
||||
l.RawSeti(-2, i+1)
|
||||
}
|
||||
|
||||
|
@ -348,7 +349,7 @@ func luaReplyToLedisReply(l *lua.State) interface{} {
|
|||
|
||||
switch l.Type(-1) {
|
||||
case lua.LUA_TSTRING:
|
||||
return ledis.Slice(l.ToString(-1))
|
||||
return hack.Slice(l.ToString(-1))
|
||||
case lua.LUA_TBOOLEAN:
|
||||
if l.ToBoolean(-1) {
|
||||
return int64(1)
|
||||
|
|
|
@ -3,7 +3,7 @@ package server
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/go/hack"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -36,7 +36,7 @@ func ReadBulkTo(rb *bufio.Reader, w io.Writer) error {
|
|||
} else if l[0] == '$' {
|
||||
var n int
|
||||
//handle resp string
|
||||
if n, err = strconv.Atoi(ledis.String(l[1:])); err != nil {
|
||||
if n, err = strconv.Atoi(hack.String(l[1:])); err != nil {
|
||||
return err
|
||||
} else if n == -1 {
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue