getChangeset method

  1. @override
CrdtChangeset getChangeset({
  1. Iterable<String>? onlyTables,
  2. String? onlyNodeId,
  3. String? exceptNodeId,
  4. Hlc? modifiedOn,
  5. Hlc? modifiedAfter,
})
inherited

Get a Changeset using the provided changesetQueries.

Set the filtering parameters to to generate subsets: onlyTables only records from the specified tables. Leave empty for all. onlyNodeId only records set by the specified node. exceptNodeId only records not set by the specified node. modifiedOn records whose modified at this exact Hlc. modifiedAfter records modified after the specified Hlc.

Implementation

@override
CrdtChangeset getChangeset({
  Iterable<String>? onlyTables,
  String? onlyNodeId,
  String? exceptNodeId,
  Hlc? modifiedOn,
  Hlc? modifiedAfter,
}) {
  assert(onlyNodeId == null || exceptNodeId == null);
  assert(modifiedOn == null || modifiedAfter == null);

  // Modified times use the local node id
  modifiedOn = modifiedOn?.apply(nodeId: nodeId);
  modifiedAfter = modifiedAfter?.apply(nodeId: nodeId);

  // Ensure all incoming tables exist in local dataset
  onlyTables ??= tables;
  final badTables = onlyTables.toSet().difference(tables);
  if (badTables.isNotEmpty) {
    throw 'Unknown table(s): ${badTables.join(', ')}';
  }

  // Get records for the specified tables
  final changeset = {
    for (final table in onlyTables) table: getRecords(table)
  };

  // Apply remaining filters
  for (final records in changeset.values) {
    records.removeWhere((_, value) =>
        (onlyNodeId != null && value.hlc.nodeId != onlyNodeId) ||
        (exceptNodeId != null && value.hlc.nodeId == exceptNodeId) ||
        (modifiedOn != null && value.modified != modifiedOn) ||
        (modifiedAfter != null && value.modified <= modifiedAfter));
  }

  // Remove empty table changesets
  changeset.removeWhere((_, records) => records.isEmpty);

  return changeset.map((table, records) => MapEntry(
      table,
      records
          .map((key, record) => MapEntry(key, {
                'key': key,
                ...record.toJson(),
              }))
          .values
          .toList()));
}