changeset method

  1. @override
Future<CrdtChangeset> changeset(
  1. String table, {
  2. String? sinceHlc,
  3. String? untilHlc,
})
override

Returns records in table in CrdtChangeset format.

When sinceHlc is non-null, only records modified after that HLC string are returned; when untilHlc is non-null, only records at or before it. Both bounds are parsed and compared as HLCs, never as raw strings. Pass neither (the default) for the full table, including tombstones — required for persistence and sync; the bounded form serves range reconciliation's leaf fetches.

Implementation

@override
Future<CrdtChangeset> changeset(
  String table, {
  String? sinceHlc,
  String? untilHlc,
}) async {
  if (_db == null) return {table: <CrdtRecord>[]};
  final conditions = ['collection = ?'];
  final params = <Object?>[table];
  if (sinceHlc != null && sinceHlc.isNotEmpty) {
    conditions.add('hlc_key > ?');
    params.add(_hlcKey(Hlc.parse(sinceHlc)));
  }
  if (untilHlc != null && untilHlc.isNotEmpty) {
    conditions.add('hlc_key <= ?');
    params.add(_hlcKey(Hlc.parse(untilHlc)));
  }
  final rows = _open.select(
    'SELECT key, value, is_deleted, hlc, crdt_type FROM crdt_records '
    'WHERE ${conditions.join(' AND ')}',
    params,
  );
  return {
    table: [
      for (final r in rows)
        {
          'key': r['key'] as String,
          'value': jsonDecode(r['value'] as String),
          'is_deleted': (r['is_deleted'] as int) == 1,
          'hlc': Hlc.parse(r['hlc'] as String),
          'crdt_type': r['crdt_type'] as String,
        },
    ],
  };
}