optimize BufGet

This commit is contained in:
siddontang 2014-07-07 15:36:16 +08:00
parent 51a5dc9212
commit 1b1d2b74e1
2 changed files with 15 additions and 14 deletions

View File

@ -345,13 +345,13 @@ func (db *DB) get(r []byte, ro *ReadOptions, key []byte) ([]byte, error) {
defer C.leveldb_get_free_ext(unsafe.Pointer(c))
if r == nil || int(C.int(vallen)) > len(r) {
return C.GoBytes(unsafe.Pointer(value), C.int(vallen)), nil
} else {
b := slice(unsafe.Pointer(value), int(C.int(vallen)))
n := copy(r, b)
return r[0:n], nil
if r == nil {
r = []byte{}
}
r = r[0:0]
b := slice(unsafe.Pointer(value), int(C.int(vallen)))
return append(r, b...), nil
}
func (db *DB) delete(wo *WriteOptions, key []byte) error {

View File

@ -98,12 +98,12 @@ func (it *Iterator) BufKey(b []byte) []byte {
if k == nil {
return nil
}
if b == nil || len(k) > len(b) {
b = make([]byte, len(k))
if b == nil {
b = []byte{}
}
n := copy(b, k)
return b[0:n]
b = b[0:0]
return append(b, k...)
}
//Copy value to b, if b len is small or nil, returns a new one
@ -112,12 +112,13 @@ func (it *Iterator) BufValue(b []byte) []byte {
if v == nil {
return nil
}
if b == nil || len(v) > len(b) {
b = make([]byte, len(v))
if b == nil {
b = []byte{}
}
n := copy(b, v)
return b[0:n]
b = b[0:0]
return append(b, v...)
}
func (it *Iterator) Close() {