diff --git a/leveldb/db.go b/leveldb/db.go index d9c2939..d79ee65 100644 --- a/leveldb/db.go +++ b/leveldb/db.go @@ -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 { diff --git a/leveldb/iterator.go b/leveldb/iterator.go index c972fe0..a1e69bb 100644 --- a/leveldb/iterator.go +++ b/leveldb/iterator.go @@ -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() {