Skip to content

[BUG]: 4.3.0 breaks reverse iterators with prefix #2108

@sveeig

Description

@sveeig

What version of Badger are you using?

4.3.0

What version of Go are you using?

1.23.1

Have you tried reproducing the issue with the latest release?

None

What is the hardware spec (RAM, CPU, OS)?

GOARCH='amd64'
GOHOSTARCH='amd64'
GOHOSTOS='linux'

What steps will reproduce the bug?

	db.Update(func(txn *badger.Txn) error {
		key := make([]byte, 6)
		binary.BigEndian.PutUint16(key, 5)
		binary.BigEndian.PutUint32(key[2:], 1)
		txn.Set(key, []byte("value1"))

		binary.BigEndian.PutUint32(key[2:], 2)
		txn.Set(key, []byte("value2"))
		return nil
	})

	db.View(func(txn *badger.Txn) error {
		searchBuffer := make([]byte, 3)
		binary.BigEndian.PutUint16(searchBuffer, 5)
		searchBuffer[2] = 0xFF

		iteratorOptions := badger.DefaultIteratorOptions
		iteratorOptions.Reverse = true
		iteratorOptions.PrefetchValues = false
		iteratorOptions.Prefix = searchBuffer
		it := txn.NewIterator(iteratorOptions)
		defer it.Close()

		it.Rewind()
		fmt.Println(it.Item().Key()) // causes panic with 4.3.0, prints [0 5 0 0 0 2] with 4.2.0
		return nil
	})

Expected behavior and actual result.

prefixes for reverse iterators need to be appended with an 0xFF byte, to make badger seek to the correct starting point.
The change in #2077 compares all key candidates with the prefix, including the extra byte, and therefore nothing is found. Expected is that items are found like before.

Additional information

I am not sure if the "trick" with appending the 0xFF is officially part of the iterator APIs. But for projects that use it, the last release is a regression. Unless there is a different, officially supported way to seek to the end of a prefix range for reverse traversal, this should be fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething is broken.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions