putMeta method

  1. @override
Future<int?> putMeta(
  1. String key,
  2. AtMetaData? metadata, {
  3. bool skipCommit = false,
  4. AtAssertedTimestamps? assertedTimestamps,
})
override

Updates the metadata for key without touching its value. Returns the commit-log sequence number assigned to this write, or null if no sequence number was produced.

skipCommit and assertedTimestamps behave as on put.

Implementation

@override
Future<int?> putMeta(String key, AtMetaData? metadata,
    {bool skipCommit = false,
    AtAssertedTimestamps? assertedTimestamps}) async {
  key = key.toLowerCase();
  try {
    String hive_key = HiveKeyStoreHelper.prepareKey(key);
    AtData? existingData;
    if (await exists(key)) {
      existingData = await get(key);
    }
    // putMeta is intended to updates only the metadata of a key.
    // So, fetch the value from the existing key and set the same value.
    AtData newData = existingData ?? AtData();
    newData.metaData = AtMetadataBuilder(
            newAtMetaData: metadata!,
            existingMetaData: existingData?.metaData,
            atSign: atSign,
            asserted: assertedTimestamps)
        .build();

    await getBox().put(hive_key, newData);
    _updateMetadataCache(key, newData.metaData);
    int? result;
    if (skipCommit) {
      // Purge any previous entry for this key, matching put/remove's
      // skipCommit behaviour. (No-op on a commit-log-free keystore.)
      await _commitLog?.removeEntryFor(hive_key);
      result = -1;
    } else {
      // `_commitLog` is null on a commit-log-free keystore — the write
      // still succeeds, it just produces no sequence number.
      result = await _commitLog?.commit(hive_key, CommitOp.UPDATE_META,
          opTime: assertedTimestamps?.updatedAt);
    }
    _changesController
        .add(existingData == null ? KeyAdded(key) : KeyUpdated(key));
    return result;
  } on HiveError catch (error) {
    logger.severe('HiveAtKeyValueStore get error: $error');
    await _restartHiveBox(error);
    throw DataStoreException(error.message);
  }
}