@@ -41,12 +41,23 @@ func (i *badgerIterator) First() {
4141
4242// Valid returns whether the iterator is positioned at a valid key-value pair.
4343func (i * badgerIterator ) Valid () bool {
44- // if it's beyond the upper bound, it's invalid
44+ // Note: we didn't specify the iteration range with the badger IteratorOptions,
45+ // because the IterationOptions only allows us to specify a single prefix, whereas
46+ // we need to specify a range of prefixes. So we have to manually check the bounds here.
47+ // The First() method, which calls Seek(i.lowerBound), ensures the iteration starts from
48+ // the lowerBound, and the upperbound is checked here by first checking if it's
49+ // reaching the end of the iteration, then checking if the key is within the upperbound.
50+
51+ // check if it's reaching the end of the iteration
4552 if ! i .iter .Valid () {
4653 return false
4754 }
55+
56+ // check if the key is within the upperbound (exclusive)
4857 key := i .iter .Item ().Key ()
49- // "< 0" means "key < upperBound"
58+ // note: for the boundary case,
59+ // upperBound is the exclusive upper bound, should not be included in the iteration,
60+ // so if key == upperBound, it's invalid, should return false.
5061 valid := bytes .Compare (key , i .upperBound ) < 0
5162 return valid
5263}
0 commit comments