getKeys method

  1. @override
Future<Stream<String>> getKeys({
  1. String? regex,
})
override

Returns list of keys from the secondary storage. @param - regex : Optional parameter to filter keys on regular expression. @return - List<String> : List of keys from secondary storage.

Implementation

@override
Future<Stream<String>> getKeys({String? regex}) async {
  if (!getBox().isOpen) {
    throw DataStoreException(
        'Failed to fetch keys. Hive Keystore is not initialized or opened');
  }
  List<String> keys = <String>[];
  regex ??= '.*';
  final RegExp regExp;
  try {
    regExp = _compiledRegex(regex);
  } on FormatException catch (exception) {
    logger.severe('Invalid regular expression : $regex');
    throw InvalidSyntaxException('Invalid syntax ${exception.toString()}');
  }

  // Capture `now` once outside the loop and pass it into the availability
  // check so each key doesn't allocate two DateTime objects.
  final now = DateTime.timestamp();
  String key;
  try {
    for (int index = 0; index < getBox().length; index++) {
      key = Utf7.decode(getBox().keyAt(index));
      if (_isKeyAvailable(key, now: now) && regExp.hasMatch(key)) {
        keys.add(key);
      }
    }
  } on Exception catch (exception) {
    logger.severe(
        'HiveAtKeyValueStore getKeys exception: ${exception.toString()}');
    throw DataStoreException('exception in getKeys: ${exception.toString()}');
  } on HiveError catch (error) {
    logger.severe('HiveAtKeyValueStore get error: $error');
    await _restartHiveBox(error);
    throw DataStoreException(error.message);
  }
  return Stream.fromIterable(keys);
}