remove method

  1. @override
Future<int?> remove(
  1. String key, {
  2. bool skipCommit = false,
})
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}) async {
  key = key.toLowerCase();

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

  int? retVal;
  try {
    await getBox().delete(HiveKeyStoreHelper.prepareKey(key));
    // On deleting the key, remove it from the expiryKeyCache.
    _expiryKeysCache.remove(key);
    final commitLog = _commitLog;
    if (skipCommit) {
      // When skipping commits, remove any existing commit entries 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.)
      if (commitLog != null) {
        CommitEntry? commitEntry = commitLog.getLatestCommitEntry(key);
        if (commitEntry != null) {
          await commitLog.commitLogKeyStore.remove(commitEntry.commitId!);
        }
      }
      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(key, CommitOp.DELETE);
    }
    _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;
}