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 {
Get(key []byte) ([]byte, error)
GetSlice(key []byte) (store.Slice, error)
Put(key []byte, value []byte) error
Delete(key []byte) error

View File

@ -3,6 +3,7 @@ package ledis
import (
"errors"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/store"
"time"
)
@ -164,6 +165,16 @@ func (db *DB) Get(key []byte) ([]byte, error) {
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) {
if err := checkKeySize(key); err != nil {
return nil, err

View File

@ -7,16 +7,35 @@ import (
"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 {
args := c.args
if len(args) != 1 {
return ErrCmdParams
}
if v, err := c.db.Get(args[0]); err != nil {
if v, err := c.db.GetSlice(args[0]); err != nil {
return err
} else {
c.resp.writeBulk(v)
if v == nil {
c.resp.writeBulk(nil)
} else {
c.resp.writeBulk(v.Data())
v.Free()
}
}
return nil
}

View File

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

View File

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

View File

@ -56,6 +56,16 @@ func (tx *Tx) Get(key []byte) ([]byte, error) {
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 {
tx.st.PutNum.Add(1)
return tx.tx.Put(key, value)