add GetSlice, hope can improve get performance

This commit is contained in:
siddontang 2014-10-29 20:17:12 +08:00
parent a2c98cf359
commit 4c19e40159
6 changed files with 45 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
type ibucket interface { type ibucket interface {
Get(key []byte) ([]byte, error) Get(key []byte) ([]byte, error)
GetSlice(key []byte) (store.Slice, error)
Put(key []byte, value []byte) error Put(key []byte, value []byte) error
Delete(key []byte) error Delete(key []byte) error

View File

@ -3,6 +3,7 @@ package ledis
import ( import (
"errors" "errors"
"github.com/siddontang/go/num" "github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/store"
"time" "time"
) )
@ -164,6 +165,16 @@ func (db *DB) Get(key []byte) ([]byte, error) {
return db.bucket.Get(key) return db.bucket.Get(key)
} }
func (db *DB) GetSlice(key []byte) (store.Slice, error) {
if err := checkKeySize(key); err != nil {
return nil, err
}
key = db.encodeKVKey(key)
return db.bucket.GetSlice(key)
}
func (db *DB) GetSet(key []byte, value []byte) ([]byte, error) { func (db *DB) GetSet(key []byte, value []byte) ([]byte, error) {
if err := checkKeySize(key); err != nil { if err := checkKeySize(key); err != nil {
return nil, err return nil, err

View File

@ -7,16 +7,35 @@ import (
"strings" "strings"
) )
// func getCommand(c *client) error {
// args := c.args
// if len(args) != 1 {
// return ErrCmdParams
// }
// if v, err := c.db.Get(args[0]); err != nil {
// return err
// } else {
// c.resp.writeBulk(v)
// }
// return nil
// }
func getCommand(c *client) error { func getCommand(c *client) error {
args := c.args args := c.args
if len(args) != 1 { if len(args) != 1 {
return ErrCmdParams return ErrCmdParams
} }
if v, err := c.db.Get(args[0]); err != nil { if v, err := c.db.GetSlice(args[0]); err != nil {
return err return err
} else { } else {
c.resp.writeBulk(v) if v == nil {
c.resp.writeBulk(nil)
} else {
c.resp.writeBulk(v.Data())
v.Free()
}
} }
return nil return nil
} }

View File

@ -36,8 +36,5 @@ func (s *CSlice) Size() int {
} }
func (s *CSlice) Free() { func (s *CSlice) Free() {
if s.data != nil {
C.leveldb_free(s.data) C.leveldb_free(s.data)
s.data = nil
}
} }

View File

@ -37,8 +37,5 @@ func (s *CSlice) Size() int {
} }
func (s *CSlice) Free() { func (s *CSlice) Free() {
if s.data != nil {
C.free(s.data) C.free(s.data)
s.data = nil
}
} }

View File

@ -56,6 +56,16 @@ func (tx *Tx) Get(key []byte) ([]byte, error) {
return v, err return v, err
} }
func (tx *Tx) GetSlice(key []byte) (Slice, error) {
if v, err := tx.Get(key); err != nil {
return nil, err
} else if v == nil {
return nil, nil
} else {
return driver.GoSlice(v), nil
}
}
func (tx *Tx) Put(key []byte, value []byte) error { func (tx *Tx) Put(key []byte, value []byte) error {
tx.st.PutNum.Add(1) tx.st.PutNum.Add(1)
return tx.tx.Put(key, value) return tx.tx.Put(key, value)