Badger for range scan


(Zhu Yuejun) #1

I’m looking for an example of how to set and iterate over data using range scan, such as BadgerIterator(first byte, last byte). Could someone please point me in the right direction?


(Ibrahim Jarif) #2

@ToWorld How about doing something like this?

startKey := []byte("fooKey")
endKey := []byte("barKey")

iopt := DefaultIteratorOptions
itr := txn.NewIterator(iopt)
defer itr.Close()

for itr.Seek(startKey); itr.Valid(); itr.Next() {
	item := itr.Item()
	if bytes.Equal(item.Key(), endKey) {
		break
	}
    // Process the item
}

(Zhu Yuejun) #3

Thank you for your response. And there is an obvious mistake to be corrected that you should use the function of Compare instead of Equal when the endKey is absent.