entryCursor method

SegmentEntryCursor entryCursor({
  1. Uint8List? lower,
  2. bool includeLower = true,
  3. Uint8List? upper,
  4. bool includeUpper = false,
})

Creates a forward cursor over a key range.

lower and upper are full keys (collection prefix included). The cursor seeks with a binary search instead of walking the segment from the start.

Implementation

SegmentEntryCursor entryCursor({
  Uint8List? lower,
  bool includeLower = true,
  Uint8List? upper,
  bool includeUpper = false,
}) {
  var index = 0;
  if (lower != null) {
    var low = 0;
    var high = _indexCount - 1;
    while (low <= high) {
      final middle = (low + high) >> 1;
      final comparison = _compareEntry(middle, lower);
      if (comparison < 0) {
        low = middle + 1;
      } else {
        high = middle - 1;
      }
    }
    index = low;
    if (!includeLower && index < _indexCount) {
      final entryStart = _entryStart(index);
      final keyLength = VxsLe.u16(buffer, entryStart);
      if (keyLength == lower.length &&
          _rangeStartsWith(entryStart + 2, keyLength, lower)) {
        index++;
      }
    }
  }
  return SegmentEntryCursor._(this, index, upper, includeUpper);
}