endDoc method

int endDoc({
  1. bool checksum = false,
})

Patches the header of the open document and returns its start offset.

When checksum is true a CRC-32C of the body is appended and the header flag is set. This costs roughly one pass over the body, so it is meant for standalone documents (WAL entries) rather than every record in a block.

Implementation

int endDoc({bool checksum = false}) {
  if (!_open) {
    throw StateError('no document is open');
  }
  var bodyStart = _docStart + vxsHeaderSize;
  final bodyLength = _pos - bodyStart;
  var headerSize = vxsHeaderSize;
  var flags = checksum ? vxsFlagCrc32c : 0;

  if (bodyLength > vxsMaxSmallBody) {
    bodyStart = _shiftBodyForWideHeader(bodyStart);
    headerSize = vxsHeaderSizeLarge;
    flags |= vxsFlagLength32;
  }
  if (checksum) {
    _ensure(4);
    final crc = VxsCrc32c.compute(_data, bodyStart, bodyStart + bodyLength);
    VxsLe.w32(_data, bodyStart + bodyLength, crc);
    _pos = bodyStart + bodyLength + 4;
  }

  _data[_docStart] = vxsMagic;
  _data[_docStart + 1] = flags;
  _data[_docStart + 2] = _schemaId;
  _data[_docStart + 3] = _schemaVersion;
  if (headerSize == vxsHeaderSizeLarge) {
    VxsLe.w32(_data, _docStart + 4, bodyLength);
  } else {
    VxsLe.w16(_data, _docStart + 4, bodyLength);
  }

  final start = _docStart;
  _open = false;
  _docStart = -1;
  if (collectOffsets) offsets.add(start);
  return start;
}