remove method

  1. @override
Future<int?> remove(
  1. String key, {
  2. bool skipCommit = false,
  3. DateTime? deletedAt,
})
override

Returns an integer if the key to be deleted is present in keystore or cache.

Implementation

@override
Future<int?> remove(String key,
    {bool skipCommit = false, DateTime? deletedAt}) async {
  key = key.toLowerCase();

  for (final hook in preRemoveHooks) {
    await hook(key, skipCommit: skipCommit);
  }

  int? retVal;
  try {
    // The prepared (utf7-encoded) form is the one every commit-log call
    // takes — commit() and removeEntryFor() both decode internally.
    // Passing the raw key here would get decoded a second time, which
    // mangles keys containing utf7 escape sequences.
    String hiveKey = HiveKeyStoreHelper.prepareKey(key);
    await getBox().delete(hiveKey);
    // On deleting the key, remove it from the expiryKeyCache.
    _expiryKeysCache.remove(key);
    final commitLog = _commitLog;
    if (skipCommit) {
      // When skipping commits, remove any existing commit entry for this
      // key from commitLog. This is critical for synchronization - during
      // sync, other commit entries for this key might be considered valid
      // if we don't remove them. (No-op on a commit-log-free keystore.)
      await commitLog?.removeEntryFor(hiveKey);
      retVal = -1;
    } else {
      // `commitLog` is null on a commit-log-free keystore — the
      // delete still succeeds, it just produces no sequence number.
      retVal = await commitLog?.commit(hiveKey, CommitOp.DELETE,
          opTime: deletedAt);
    }
    _changesController.add(KeyRemoved(key));
  } on Exception catch (exception) {
    logger.severe('HiveAtKeyValueStore delete exception: $exception');
    throw DataStoreException('exception in remove: ${exception.toString()}');
  } on HiveError catch (error) {
    await _restartHiveBox(error);
    logger.severe('HiveAtKeyValueStore delete error: $error');
    throw DataStoreException(error.message);
  }

  for (final hook in postRemoveHooks) {
    await hook(key, skipCommit: skipCommit);
  }

  return retVal;
}