decodeSegment function

(int, List<WalFrame>) decodeSegment(
  1. List<int> compressed
)

The page size and frames of a segment written by encodeSegment.

Implementation

(int, List<WalFrame>) decodeSegment(List<int> compressed) {
  final bytes = Uint8List.fromList(gzip.decode(compressed));
  if (bytes.length < 16 ||
      String.fromCharCodes(bytes.sublist(0, 8)) != 'DRVWAL01') {
    throw const ReplicationException('Not a WAL segment.');
  }
  final data = ByteData.sublistView(bytes);
  final pageSize = data.getUint32(8);
  final count = data.getUint32(12);
  final frames = <WalFrame>[];
  var at = 16;
  for (var i = 0; i < count; i++) {
    if (at + 8 + pageSize > bytes.length) {
      throw const ReplicationException('Truncated WAL segment.');
    }
    frames.add(
      WalFrame(
        data.getUint32(at),
        data.getUint32(at + 4),
        Uint8List.sublistView(bytes, at + 8, at + 8 + pageSize),
      ),
    );
    at += 8 + pageSize;
  }
  return (pageSize, frames);
}