parse static method

BPTreeV1 parse(
  1. Uint8List bytes
)

Implementation

static BPTreeV1 parse(Uint8List bytes) {
  if (bytes.length < 7) {
    throw FormatException('Insufficient bytes.');
  }
  for (var i = 0; i < _magicBytes.length; i++) {
    if (bytes[i] != _magicBytes[i]) {
      throw FormatException('Magic bytes mismatch.');
    }
  }
  final versionByte = bytes[3];
  final endian = (versionByte & 0x80) == 0 ? Endian.little : Endian.big;
  final internalVersion = versionByte & 0x7f;
  if (internalVersion > _internalVersion) {
    throw FormatException(
        'Internal version is not supported: $internalVersion');
  }

  final reader = BytesReader(bytes, endian: endian, offset: 4);
  final optionsBytes = reader.readUint8();
  final isCustomDataPresent = (optionsBytes & 0x01) != 0;
  final isSorted = (optionsBytes & 0x02) != 0;
  final isCompressionPresent = (optionsBytes & 0x04) != 0;
  final isOffsetPresent = (optionsBytes & 0x08) != 0;
  if ((optionsBytes >> 4) > 0) {
    throw FormatException('Unknown option bytes settings ($optionsBytes).');
  }
  final entriesCount = reader.readVarint();

  Uint8List? customData;
  if (isCustomDataPresent) {
    final length = reader.readVarint();
    customData = reader.subview(length);
  }

  Uint8List? prefixBytes;
  Uint8List? postfixBytes;
  if (isCompressionPresent) {
    final prefixLength = reader.readVarint();
    if (prefixLength > 0) {
      prefixBytes = reader.subview(prefixLength);
    }
    final postfixLength = reader.readVarint();
    if (postfixLength > 0) {
      postfixBytes = reader.subview(postfixLength);
    }
  }

  OffsetIndexV1? offsetIndex;
  if (isOffsetPresent) {
    offsetIndex = OffsetIndexV1.fromReader(
      reader,
      entriesCount: entriesCount,
    );
  }
  final entries = KeyValueEntriesV1.fromReader(reader);

  return _BPTreeV1._(
    endian,
    customData,
    isSorted,
    prefixBytes,
    postfixBytes,
    offsetIndex,
    entries,
  );
}