From b7de9e6354d564b8b280e39b5310160f2eb022c7 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 24 Sep 2014 13:29:27 +0800 Subject: [PATCH] use tiny go package --- ledis/t_hash.go | 3 ++- ledis/t_kv.go | 3 ++- ledis/t_set.go | 6 +++--- ledis/t_zset.go | 12 ++++++------ ledis/util.go | 22 +++++++--------------- server/client_resp.go | 5 +++-- server/cmd_zset.go | 6 +++--- server/script.go | 3 ++- 8 files changed, 28 insertions(+), 32 deletions(-) diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 952ddae..a2e0bd3 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -3,6 +3,7 @@ package ledis import ( "encoding/binary" "errors" + "github.com/siddontang/go/num" "github.com/siddontang/ledisdb/store" "time" ) @@ -332,7 +333,7 @@ func (db *DB) HIncrBy(key []byte, field []byte, delta int64) (int64, error) { n += delta - _, err = db.hSetItem(key, field, StrPutInt64(n)) + _, err = db.hSetItem(key, field, num.FormatInt64ToSlice(n)) if err != nil { return 0, err } diff --git a/ledis/t_kv.go b/ledis/t_kv.go index ce0874a..14d477b 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -2,6 +2,7 @@ package ledis import ( "errors" + "github.com/siddontang/go/num" "time" ) @@ -75,7 +76,7 @@ func (db *DB) incr(key []byte, delta int64) (int64, error) { n += delta - t.Put(key, StrPutInt64(n)) + t.Put(key, num.FormatInt64ToSlice(n)) err = t.Commit() return n, err diff --git a/ledis/t_set.go b/ledis/t_set.go index 7f41f53..2eb6c4c 100644 --- a/ledis/t_set.go +++ b/ledis/t_set.go @@ -509,14 +509,14 @@ func (db *DB) sStoreGeneric(dstKey []byte, optType byte, keys ...[]byte) (int64, t.Put(ek, nil) } - var num = int64(len(v)) + var n = int64(len(v)) sk := db.sEncodeSizeKey(dstKey) - t.Put(sk, PutInt64(num)) + t.Put(sk, PutInt64(n)) if err = t.Commit(); err != nil { return 0, err } - return num, nil + return n, nil } func (db *DB) SClear(key []byte) (int64, error) { diff --git a/ledis/t_zset.go b/ledis/t_zset.go index d7cb2b1..3a2336f 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -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) - t.Put(sk, PutInt64(num)) + t.Put(sk, PutInt64(n)) if err := t.Commit(); err != nil { return 0, err } - return num, nil + return n, nil } 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) - t.Put(sk, PutInt64(num)) + t.Put(sk, PutInt64(n)) if err := t.Commit(); err != nil { return 0, err } - return num, nil + return n, nil } func (db *DB) ZScan(key []byte, count int, inclusive bool, match string) ([][]byte, error) { diff --git a/ledis/util.go b/ledis/util.go index 9454532..a0abdd0 100644 --- a/ledis/util.go +++ b/ledis/util.go @@ -4,13 +4,16 @@ import ( "encoding/binary" "errors" "github.com/siddontang/go/hack" - "reflect" "strconv" - "unsafe" ) 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) { if err != nil { return 0, err @@ -36,11 +39,8 @@ func Uint64(v []byte, err error) (uint64, error) { } func PutInt64(v int64) []byte { - var b []byte - pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - pbytes.Data = uintptr(unsafe.Pointer(&v)) - pbytes.Len = 8 - pbytes.Cap = 8 + b := make([]byte, 8) + binary.LittleEndian.PutUint64(b, uint64(v)) 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{}) { select { case ch <- struct{}{}: diff --git a/server/client_resp.go b/server/client_resp.go index ed72108..1b50bc3 100644 --- a/server/client_resp.go +++ b/server/client_resp.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/siddontang/go/hack" "github.com/siddontang/go/log" + "github.com/siddontang/go/num" "github.com/siddontang/ledisdb/ledis" "io" "net" @@ -171,7 +172,7 @@ func (w *respWriter) writeStatus(status string) { func (w *respWriter) writeInteger(n int64) { w.buff.WriteByte(':') - w.buff.Write(ledis.StrPutInt64(n)) + w.buff.Write(num.FormatInt64ToSlice(n)) w.buff.Write(Delims) } @@ -266,7 +267,7 @@ func (w *respWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool) w.writeBulk(lst[i].Member) if withScores { - w.writeBulk(ledis.StrPutInt64(lst[i].Score)) + w.writeBulk(num.FormatInt64ToSlice(lst[i].Score)) } } } diff --git a/server/cmd_zset.go b/server/cmd_zset.go index 9c58f5b..4763683 100644 --- a/server/cmd_zset.go +++ b/server/cmd_zset.go @@ -3,7 +3,7 @@ package server import ( "errors" "github.com/siddontang/go/hack" - + "github.com/siddontang/go/num" "github.com/siddontang/ledisdb/ledis" "math" "strconv" @@ -75,7 +75,7 @@ func zscoreCommand(c *client) error { return err } } else { - c.resp.writeBulk(ledis.StrPutInt64(s)) + c.resp.writeBulk(num.FormatInt64ToSlice(s)) } return nil @@ -112,7 +112,7 @@ func zincrbyCommand(c *client) error { v, err := c.db.ZIncrBy(key, delta, args[2]) if err == nil { - c.resp.writeBulk(ledis.StrPutInt64(v)) + c.resp.writeBulk(num.FormatInt64ToSlice(v)) } return err diff --git a/server/script.go b/server/script.go index 24566c3..711b412 100644 --- a/server/script.go +++ b/server/script.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "fmt" "github.com/siddontang/go/hack" + "github.com/siddontang/go/num" "github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/lua" "io" @@ -115,7 +116,7 @@ func (w *luaWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool) w.l.PushString(hack.String(v.Member)) 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) } } else {