put method
Future<int?>
put(
- String key,
- AtData value, {
- bool skipCommit = false,
- AtAssertedTimestamps? assertedTimestamps,
override
Associates value with key. Overrides KeyValueStore.put
with the atKey-aware extras:
skipCommit— as well as writing no commit entry, the key's existing commit entry (if any) is purged, so the commit log carries no trace of the key's history. Sync would otherwise keep serving a stale entry for a record whose latest change was deliberately uncommitted.assertedTimestamps— caller-asserted timestamps stored faithfully instead of being rederived; see AtAssertedTimestamps for the precedence rules. An assertedupdatedAtis also recorded as the commit entry'sopTime.
Implementation
@override
Future<int?> put(String key, AtData value,
{bool skipCommit = false,
AtAssertedTimestamps? assertedTimestamps}) async {
final k = _validateAndNormalize(key);
if (!_existsSync(k)) {
return create(key, value,
skipCommit: skipCommit, assertedTimestamps: assertedTimestamps);
}
final existing = _getSync(k);
final toStore = AtData()
..data = value.data
..metaData = AtMetadataBuilder(
atSign: atSign,
newAtMetaData: value.metaData,
existingMetaData: existing?.metaData,
asserted: assertedTimestamps,
).build();
final result = _db.runInTransaction(() {
_upsertAtData(k, toStore);
if (skipCommit) {
// A skipCommit write leaves no trace in the commit log: purge
// the key's previous entry as well as writing none, matching
// remove()'s behaviour.
_sqliteCommitLog?.purgeSync(k);
return -1;
}
return _commitOrNull(k, CommitOp.UPDATE_ALL,
opTime: assertedTimestamps?.updatedAt);
});
_changes.add(KeyUpdated(k));
return result;
}