put method

  1. @override
Future<void> put(
  1. String table,
  2. String key,
  3. Object? value, {
  4. bool isDeleted = false,
  5. CrdtType crdtType = const LwwRegister(),
})
override

Writes or tombstones the record identified by table/key.

Setting isDeleted to true tombstones the record (soft-delete); the record remains in the table and is included in changesets so peers can observe the deletion.

crdtType is the merge rule recorded for this record. It defaults to LwwRegister (last-write-wins). An engine stores the tag and treats the value as opaque — the type-aware merge itself happens in TypedCrdtTable, above this port. An in-memory engine that cannot store the type may ignore it (typed values are self-describing); a persistent engine must record it.

Implementation

@override
Future<void> put(
  String table,
  String key,
  Object? value, {
  bool isDeleted = false,
  CrdtType crdtType = const LwwRegister(),
}) async {
  if (isDeleted) {
    return _inner.put(table, key, value, isDeleted: true, crdtType: crdtType);
  }
  switch (crdtType) {
    case LwwRegister():
      return _inner.put(table, key, value, crdtType: crdtType);
    case OrSet():
      if (value is! List) {
        throw ArgumentError.value(
          value,
          'value',
          'An OrSet field takes its desired membership as a List',
        );
      }
      final current = await _inner.value(table, key);
      final next = orApplyDesired(current, value, () => '$nodeId-${newId()}');
      if (current != null && crdtValueEquals(next, current)) return;
      return _inner.put(table, key, next, crdtType: crdtType);
    case PnCounter():
      if (value is! num) {
        throw ArgumentError.value(
          value,
          'value',
          'A PnCounter field takes a starting number',
        );
      }
      final current = await _inner.value(table, key);
      if (current == null) {
        return _inner.put(table, key, pnApplyDelta(null, nodeId, value),
            crdtType: crdtType);
      }
      if (value == pnResolve(current)) return; // Unchanged: quiescent.
      throw ArgumentError(
        'A PnCounter field cannot be set to a new value; its value is the '
        'sum of every device\'s deltas. Use increment() instead.',
      );
  }
}