From b73d60d3d3b0b720ab60ec071bca066f0551cde0 Mon Sep 17 00:00:00 2001 From: Alessandro Date: Thu, 11 Jul 2024 16:04:00 +0200 Subject: [PATCH] changes in goLevelDBIterator Value() impl: - returns the value itself, not a copy - updated tests to reflect the change - updated docs to reflect the change --- backend_test.go | 5 ++++- common_test.go | 4 +++- goleveldb_iterator.go | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/backend_test.go b/backend_test.go index 894a509..b30600f 100644 --- a/backend_test.go +++ b/backend_test.go @@ -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) diff --git a/common_test.go b/common_test.go index f4e97c2..742d772 100644 --- a/common_test.go +++ b/common_test.go @@ -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) { diff --git a/goleveldb_iterator.go b/goleveldb_iterator.go index 1e2bb5c..6a3c445 100644 --- a/goleveldb_iterator.go +++ b/goleveldb_iterator.go @@ -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.