use tiny go package

This commit is contained in:
siddontang 2014-09-24 13:29:27 +08:00
parent 5bccfd13dc
commit b7de9e6354
8 changed files with 28 additions and 32 deletions

View File

@ -3,6 +3,7 @@ package ledis
import ( import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/store" "github.com/siddontang/ledisdb/store"
"time" "time"
) )
@ -332,7 +333,7 @@ func (db *DB) HIncrBy(key []byte, field []byte, delta int64) (int64, error) {
n += delta n += delta
_, err = db.hSetItem(key, field, StrPutInt64(n)) _, err = db.hSetItem(key, field, num.FormatInt64ToSlice(n))
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -2,6 +2,7 @@ package ledis
import ( import (
"errors" "errors"
"github.com/siddontang/go/num"
"time" "time"
) )
@ -75,7 +76,7 @@ func (db *DB) incr(key []byte, delta int64) (int64, error) {
n += delta n += delta
t.Put(key, StrPutInt64(n)) t.Put(key, num.FormatInt64ToSlice(n))
err = t.Commit() err = t.Commit()
return n, err return n, err

View File

@ -509,14 +509,14 @@ func (db *DB) sStoreGeneric(dstKey []byte, optType byte, keys ...[]byte) (int64,
t.Put(ek, nil) t.Put(ek, nil)
} }
var num = int64(len(v)) var n = int64(len(v))
sk := db.sEncodeSizeKey(dstKey) sk := db.sEncodeSizeKey(dstKey)
t.Put(sk, PutInt64(num)) t.Put(sk, PutInt64(n))
if err = t.Commit(); err != nil { if err = t.Commit(); err != nil {
return 0, err return 0, err
} }
return num, nil return n, nil
} }
func (db *DB) SClear(key []byte) (int64, error) { func (db *DB) SClear(key []byte) (int64, error) {

View File

@ -858,14 +858,14 @@ func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
} }
} }
var num = int64(len(destMap)) var n = int64(len(destMap))
sk := db.zEncodeSizeKey(destKey) sk := db.zEncodeSizeKey(destKey)
t.Put(sk, PutInt64(num)) t.Put(sk, PutInt64(n))
if err := t.Commit(); err != nil { if err := t.Commit(); err != nil {
return 0, err return 0, err
} }
return num, nil return n, nil
} }
func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error) { func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error) {
@ -926,14 +926,14 @@ func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, agg
} }
} }
var num int64 = int64(len(destMap)) var n int64 = int64(len(destMap))
sk := db.zEncodeSizeKey(destKey) sk := db.zEncodeSizeKey(destKey)
t.Put(sk, PutInt64(num)) t.Put(sk, PutInt64(n))
if err := t.Commit(); err != nil { if err := t.Commit(); err != nil {
return 0, err return 0, err
} }
return num, nil return n, nil
} }
func (db *DB) ZScan(key []byte, count int, inclusive bool, match string) ([][]byte, error) { func (db *DB) ZScan(key []byte, count int, inclusive bool, match string) ([][]byte, error) {

View File

@ -4,13 +4,16 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"reflect"
"strconv" "strconv"
"unsafe"
) )
var errIntNumber = errors.New("invalid integer") var errIntNumber = errors.New("invalid integer")
/*
Below I forget why I use little endian to store int.
Maybe I was foolish at that time.
*/
func Int64(v []byte, err error) (int64, error) { func Int64(v []byte, err error) (int64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
@ -36,11 +39,8 @@ func Uint64(v []byte, err error) (uint64, error) {
} }
func PutInt64(v int64) []byte { func PutInt64(v int64) []byte {
var b []byte b := make([]byte, 8)
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b)) binary.LittleEndian.PutUint64(b, uint64(v))
pbytes.Data = uintptr(unsafe.Pointer(&v))
pbytes.Len = 8
pbytes.Cap = 8
return b return b
} }
@ -86,14 +86,6 @@ func StrInt8(v []byte, err error) (int8, error) {
} }
} }
func StrPutInt64(v int64) []byte {
return strconv.AppendInt(nil, v, 10)
}
func StrPutUint64(v uint64) []byte {
return strconv.AppendUint(nil, v, 10)
}
func AsyncNotify(ch chan struct{}) { func AsyncNotify(ch chan struct{}) {
select { select {
case ch <- struct{}{}: case ch <- struct{}{}:

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/log" "github.com/siddontang/go/log"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"io" "io"
"net" "net"
@ -171,7 +172,7 @@ func (w *respWriter) writeStatus(status string) {
func (w *respWriter) writeInteger(n int64) { func (w *respWriter) writeInteger(n int64) {
w.buff.WriteByte(':') w.buff.WriteByte(':')
w.buff.Write(ledis.StrPutInt64(n)) w.buff.Write(num.FormatInt64ToSlice(n))
w.buff.Write(Delims) w.buff.Write(Delims)
} }
@ -266,7 +267,7 @@ func (w *respWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool)
w.writeBulk(lst[i].Member) w.writeBulk(lst[i].Member)
if withScores { if withScores {
w.writeBulk(ledis.StrPutInt64(lst[i].Score)) w.writeBulk(num.FormatInt64ToSlice(lst[i].Score))
} }
} }
} }

View File

@ -3,7 +3,7 @@ package server
import ( import (
"errors" "errors"
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"math" "math"
"strconv" "strconv"
@ -75,7 +75,7 @@ func zscoreCommand(c *client) error {
return err return err
} }
} else { } else {
c.resp.writeBulk(ledis.StrPutInt64(s)) c.resp.writeBulk(num.FormatInt64ToSlice(s))
} }
return nil return nil
@ -112,7 +112,7 @@ func zincrbyCommand(c *client) error {
v, err := c.db.ZIncrBy(key, delta, args[2]) v, err := c.db.ZIncrBy(key, delta, args[2])
if err == nil { if err == nil {
c.resp.writeBulk(ledis.StrPutInt64(v)) c.resp.writeBulk(num.FormatInt64ToSlice(v))
} }
return err return err

View File

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
"github.com/siddontang/ledisdb/lua" "github.com/siddontang/ledisdb/lua"
"io" "io"
@ -115,7 +116,7 @@ func (w *luaWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool)
w.l.PushString(hack.String(v.Member)) w.l.PushString(hack.String(v.Member))
w.l.RawSeti(-2, 2*i+1) w.l.RawSeti(-2, 2*i+1)
w.l.PushString(hack.String(ledis.StrPutInt64(v.Score))) w.l.PushString(hack.String(num.FormatInt64ToSlice(v.Score)))
w.l.RawSeti(-2, 2*i+2) w.l.RawSeti(-2, 2*i+2)
} }
} else { } else {