Skip to content

Commit

Permalink
changes in goLevelDBIterator Value() impl:
Browse files Browse the repository at this point in the history
- returns the value itself, not a copy
- updated tests to reflect the change
- updated docs to reflect the change
  • Loading branch information
alesforz committed Jul 11, 2024
1 parent 7ed49f2 commit b73d60d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,10 @@ func assertKeyValues(t *testing.T, db DB, expect map[string][]byte) {
actual := make(map[string][]byte)
for ; iter.Valid(); iter.Next() {
require.NoError(t, iter.Error())
actual[string(iter.Key())] = iter.Value()

value := make([]byte, len(iter.Value()))
copy(value, iter.Value())
actual[string(iter.Key())] = value
}

assert.Equal(t, expect, actual)
Expand Down
4 changes: 3 additions & 1 deletion common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func checkKeyPanics(t *testing.T, itr Iterator) {

func checkValuePanics(t *testing.T, itr Iterator) {
t.Helper()
assert.Panics(t, func() { itr.Value() })

msg := "checkValuePanics expected panic but didn't"
assert.Panics(t, func() { itr.Value() }, msg)
}

func newTempDB(t *testing.T, backend BackendType) (db DB, dbDir string) {
Expand Down
8 changes: 4 additions & 4 deletions goleveldb_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ func (itr *goLevelDBIterator) Valid() bool {

// Key implements Iterator.
// The caller should not modify the contents of the returned slice.
// To work with the slice, make a copy and modify the copy instead.
// Instead, the caller should make a copy and work on the copy.
func (itr *goLevelDBIterator) Key() []byte {
itr.assertIsValid()
return itr.source.Key()
}

// Value implements Iterator.
// The caller should not modify the contents of the returned slice.
// Instead, the caller should make a copy and work on the copy.
func (itr *goLevelDBIterator) Value() []byte {
// Value returns a copy of the current value.
// See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88
itr.assertIsValid()
return cp(itr.source.Value())
return itr.source.Value()
}

// Next implements Iterator.
Expand Down

0 comments on commit b73d60d

Please sign in to comment.