Skip to content

Commit

Permalink
Duplicate iterator buffers (#823)
Browse files Browse the repository at this point in the history
The slice that pebble.Iterator returns for Key() and Value() requests
are owned by the iterator. So it can be reused or garbage collected anytime.

We should make sure that we own those slices by copying the data first
  • Loading branch information
omerfirmak committed Jun 15, 2023
1 parent e4b6255 commit 257a50b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions db/pebble/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ func (i *iterator) Valid() bool {

// Key : see db.Transaction.Iterator.Key
func (i *iterator) Key() []byte {
return i.iter.Key()
key := i.iter.Key()
if key == nil {
return nil
}
buf := make([]byte, len(key))
copy(buf, key)
return buf
}

// Value : see db.Transaction.Iterator.Value
func (i *iterator) Value() ([]byte, error) {
return i.iter.ValueAndErr()
val, err := i.iter.ValueAndErr()
if err != nil || val == nil {
return nil, err
}
buf := make([]byte, len(val))
copy(buf, val)
return buf, nil
}

// Next : see db.Transaction.Iterator.Next
Expand Down

0 comments on commit 257a50b

Please sign in to comment.