From b05e28bb640ec4cc0cb78e63fad7d9921b2a369d Mon Sep 17 00:00:00 2001 From: Mohd Imran Date: Fri, 19 Jul 2024 18:52:48 +0530 Subject: [PATCH] Updated: Hive implementation Updated: deleteMany in Hive --- .../services/storage/local/services/hive.dart | 352 +++++++++++++----- 1 file changed, 262 insertions(+), 90 deletions(-) diff --git a/lib/vaahextendflutter/services/storage/local/services/hive.dart b/lib/vaahextendflutter/services/storage/local/services/hive.dart index 032f100..33645d9 100644 --- a/lib/vaahextendflutter/services/storage/local/services/hive.dart +++ b/lib/vaahextendflutter/services/storage/local/services/hive.dart @@ -1,9 +1,8 @@ import 'package:hive/hive.dart'; -import '../../failures.dart'; -import 'base_service.dart'; import '../../storage_error.dart'; -import '../../storage_result.dart'; +import '../../storage_response.dart'; +import 'base_service.dart'; /// A class implementing LocalStorageService interface using Hive as storage backend. class LocalStorageWithHive implements LocalStorageService { @@ -12,45 +11,77 @@ class LocalStorageWithHive implements LocalStorageService { }; @override - void add(String collectionName) { + StorageResponse add(String collectionName) { if (!_collections.containsKey(collectionName)) { _collections[collectionName] = Hive.openBox(collectionName); + return StorageResponse( + data: _collections[collectionName], + message: 'Collection created with :$collectionName.', + isSuccess: true, + ); } + return StorageResponse(errors: [ + StorageError( + message: 'Collection $collectionName already exists.', + failedKey: '', + stackTrace: StackTrace.current, + ), + ]); } @override - Future create({ + Future create({ String collectionName = 'vaah-flutter-box', required String key, required String value, }) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { Box box = await _collections[collectionName]!; if (box.containsKey(key)) { - return handleKeyAlreadyExistsFailure(key); + return StorageResponse( + errors: [ + StorageError( + message: 'Key "$key" already exists in collection "$collectionName".', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { try { await box.put(key, value); // main operation - return Success( + return StorageResponse( data: key, message: 'Entry created with key: $key in collection $collectionName.', + isSuccess: true, ); } catch (e) { - StorageError error = StorageError( - message: 'Error while creating entry to the box "$collectionName" at key: $key: $e', - failedKey: key, - stackTrace: StackTrace.current, + return StorageResponse( + errors: [ + StorageError( + message: 'Error while creating entry to the box "$collectionName" at key: $key: $e', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], ); - return Failure(errors: [error]); } } } } @override - Future createMany({ + Future createMany({ String collectionName = 'vaah-flutter-box', required Map values, }) async { @@ -60,58 +91,80 @@ class LocalStorageWithHive implements LocalStorageService { for (String k in values.keys) { final result = await create(collectionName: collectionName, key: k, value: values[k]!); - if (result is Success) { + if (result.isSuccess) { totalEntries--; success.add(result.data); - } else if (result is Failure) { - errors.add(result.errors.first); + } else if (result.hasError) { + errors.add(result.errors!.first); } } if (errors.isEmpty) { - return Success(data: success, message: 'All entries created successfully.'); + return StorageResponse( + data: success, + message: 'All entries created successfully.', + isSuccess: true, + ); } else if (totalEntries == values.length) { - return Failure(errors: errors); + return StorageResponse(errors: errors); } else { - return PartialSuccess( - successData: [success], - successMessage: '${values.length - totalEntries}/${values.length} entries created.', + return StorageResponse( + data: success, + message: '${values.length - totalEntries}/${values.length} entries created.', errors: errors, + isPartialSuccess: true, ); } } @override - Future read({ + Future read({ String collectionName = 'vaah-flutter-box', required String key, }) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { Box box = await _collections[collectionName]!; if (!box.containsKey(key)) { - return handleKeyNotFoundFailure(key); + return StorageResponse( + errors: [ + StorageError( + message: 'Key "$key" does not exist in collection "$collectionName".', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { try { String? result = box.get(key); - return Success( + return StorageResponse( data: result, - message: 'Read successful: Entry with key: $key in collection: $collectionName.', + message: 'Read successful: Entry with key: "$key" in collection: "$collectionName".', + isSuccess: true, ); } catch (e) { StorageError error = StorageError( - message: 'Error while reading data from the box "$collectionName" at key: $key: $e', + message: 'Error while reading data from the box "$collectionName" at key: "$key": $e', failedKey: key, stackTrace: StackTrace.current, ); - return Failure(errors: [error]); + return StorageResponse(errors: [error]); } } } } @override - Future readMany({ + Future readMany({ String collectionName = 'vaah-flutter-box', required List keys, }) async { @@ -121,26 +174,27 @@ class LocalStorageWithHive implements LocalStorageService { Map data = {}; for (String k in keys) { final result = await read(collectionName: collectionName, key: k); - if (result is Success) { + if (result.isSuccess) { data[k] = result.data; remainingKeys--; - } else if (result is Failure) { - errors.add(result.errors.first); + } else if (result.hasError) { + errors.add(result.errors!.first); } } if (errors.isEmpty) { - return Success(data: data, message: 'Read successful: associated keys: $keys'); + return StorageResponse(data: data, message: 'Read successful.', isSuccess: true); } else if (remainingKeys == keys.length) { - return Failure(errors: errors); + return StorageResponse(errors: errors); } else { - return PartialSuccess( - successData: [data], - successMessage: 'Read $remainingKeys/${keys.length}.', + return StorageResponse( + data: data, + message: 'Read $remainingKeys/${keys.length}.', errors: errors, + isPartialSuccess: true, ); } } else { - return Failure( + return StorageResponse( errors: [ StorageError( message: 'No keys provided', @@ -153,9 +207,17 @@ class LocalStorageWithHive implements LocalStorageService { } @override - Future readAll({String collectionName = 'vaah-flutter-box'}) async { + Future readAll({String collectionName = 'vaah-flutter-box'}) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: '', + stackTrace: StackTrace.current, + ), + ], + ); } else { try { Box box = await _collections[collectionName]!; @@ -165,46 +227,68 @@ class LocalStorageWithHive implements LocalStorageService { value.toString(), ), ); - return Success( + return StorageResponse( data: result, message: result.isEmpty ? 'No data found' : 'Read all successfull.', + isSuccess: true, ); - } catch (_) { - rethrow; + } catch (e) { + StorageError error = StorageError( + message: 'Error reading all data: $e', + failedKey: '', + stackTrace: StackTrace.current, + ); + return StorageResponse(errors: [error]); } } } @override - Future update({ + Future update({ String collectionName = 'vaah-flutter-box', required String key, required String value, }) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { Box box = await _collections[collectionName]!; if (!box.containsKey(key)) { - return handleKeyNotFoundFailure(key); + return StorageResponse( + errors: [ + StorageError( + message: 'Key "$key" does not exist in collection "$collectionName"', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { try { - box.put(key, value); - return Success(data: key, message: 'Entry updated at key: $key'); + await box.put(key, value); + return StorageResponse(data: key, message: 'Entry updated at key: $key', isSuccess: true); } catch (e) { final error = StorageError( failedKey: key, message: 'Error while updating the entry: $e', stackTrace: StackTrace.current, ); - return Failure(errors: [error]); + return StorageResponse(errors: [error]); } } } } @override - Future updateMany({ + Future updateMany({ String collectionName = 'vaah-flutter-box', required Map values, }) async { @@ -213,38 +297,47 @@ class LocalStorageWithHive implements LocalStorageService { List success = []; for (String k in values.keys) { final result = await update(collectionName: collectionName, key: k, value: values[k]!); - if (result is Success) { + if (result.isSuccess) { remainigEntries--; success.add(result.data); - } else if (result is Failure) { - errors.add(result.errors.first); + } else if (result.hasError) { + errors.add(result.errors!.first); } } if (errors.isEmpty) { - return Success(data: success, message: 'Updated all given entries.'); + return StorageResponse(data: success, message: 'Updated all given entries.', isSuccess: true); } else if (remainigEntries == values.length) { - return Failure(errors: errors); + return StorageResponse(errors: errors); } else { - return PartialSuccess( - successData: success, - successMessage: 'Updated $remainigEntries/${values.length} entries.', + return StorageResponse( + data: success, + message: 'Updated $remainigEntries/${values.length} entries.', errors: errors, + isPartialSuccess: true, ); } } @override - Future createOrUpdate({ + Future createOrUpdate({ String collectionName = 'vaah-flutter-box', required String key, required String value, }) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { Box box = await _collections[collectionName]!; if (!box.containsKey(key)) { - return create(key: key, value: value); + return create(collectionName: collectionName, key: key, value: value); } else { return update(collectionName: collectionName, key: key, value: value); } @@ -252,7 +345,7 @@ class LocalStorageWithHive implements LocalStorageService { } @override - Future createOrUpdateMany({ + Future createOrUpdateMany({ String collectionName = 'vaah-flutter-box', required Map values, }) async { @@ -260,96 +353,175 @@ class LocalStorageWithHive implements LocalStorageService { List errors = []; List success = []; for (String k in values.keys) { - final result = - await createOrUpdate(collectionName: collectionName, key: k, value: values[k]!); - if (result is Success) { + final result = await createOrUpdate( + collectionName: collectionName, + key: k, + value: values[k]!, + ); + if (result.isSuccess) { remainigEntries--; success.add(result.data); - } else if (result is Failure) { - errors.add(result.errors.first); + } else if (result.hasError) { + errors.add(result.errors!.first); } } if (errors.isEmpty) { - return Success(data: success, message: 'Set all given entries.'); + return StorageResponse(data: success, message: 'Set all given entries.', isSuccess: true); } else if (remainigEntries == values.length) { - return Failure(errors: errors); + return StorageResponse(errors: errors); } else { - return PartialSuccess( - successData: success, - successMessage: 'Set $remainigEntries/${values.length} entries.', + return StorageResponse( + data: success, + message: 'Set $remainigEntries/${values.length} entries.', errors: errors, + isPartialSuccess: true, ); } } @override - Future delete({String collectionName = 'vaah-flutter-box', dynamic key}) async { + Future delete({String collectionName = 'vaah-flutter-box', dynamic key}) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { Box box = await _collections[collectionName]!; if (!box.containsKey(key)) { - return handleKeyNotFoundFailure(key); + return StorageResponse( + errors: [ + StorageError( + message: 'Key "$key" does not exist in collection "$collectionName"', + failedKey: key, + stackTrace: StackTrace.current, + ), + ], + ); } else { try { await box.delete(key); - return Success(data: key, message: 'Deleted value at key: $key'); + return StorageResponse(data: key, message: 'Deleted value at key: $key', isSuccess: true); } catch (e) { final error = StorageError( failedKey: key, message: 'Error while deleting the entry at key: $key: $e', stackTrace: StackTrace.current, ); - return Failure(errors: [error]); + return StorageResponse(errors: [error]); } } } } @override - Future deleteMany({ + Future deleteMany({ String collectionName = 'vaah-flutter-box', List keys = const [], }) async { + List errors = []; if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: '', + stackTrace: StackTrace.current, + ), + ], + ); } else { try { + final List existingKeys = []; Box box = await _collections[collectionName]!; + for (int i = 0; i < keys.length; i++) { + if (!box.containsKey(keys[i])) { + errors.add( + StorageError( + message: 'Key "${keys[i]}" does not exist in collection "$collectionName"', + failedKey: keys[i], + stackTrace: StackTrace.current, + ), + ); + } else { + existingKeys.add(keys[i]); + } + } if (keys.isNotEmpty) { - await box.deleteAll(keys); + if (existingKeys.isEmpty) { + return StorageResponse(errors: errors); + } else if (existingKeys.length == keys.length) { + await box.deleteAll(existingKeys); + return StorageResponse( + data: keys, + message: + 'Deleted multiple entries in collection: $collectionName, associated keys: $keys', + isSuccess: true, + ); + } else { + await box.deleteAll(existingKeys); + return StorageResponse( + data: existingKeys, + message: 'Deleted ${existingKeys.length}/${keys.length} entries.', + errors: errors, + isPartialSuccess: true, + ); + } + } else { + return StorageResponse( + errors: [ + StorageError( + message: 'No keys provided', + failedKey: '', + stackTrace: StackTrace.current, + ), + ], + ); } - return Success( - data: keys, - message: - 'Deleted multiple entries in collection: $collectionName, associated keys: $keys'); } catch (e) { final error = StorageError( failedKey: '', message: 'Error while deleting the data: $e', stackTrace: StackTrace.current, ); - return Failure(errors: [error]); + return StorageResponse(errors: [error]); } } } @override - Future deleteAll({String collectionName = 'vaah-flutter-box'}) async { + Future deleteAll({String collectionName = 'vaah-flutter-box'}) async { if (!_collections.containsKey(collectionName)) { - return handleCollectionNotFoundFailure(collectionName); + return StorageResponse( + errors: [ + StorageError( + message: 'Collection "$collectionName" not found.', + failedKey: '', + stackTrace: StackTrace.current, + ), + ], + ); } else { try { Box box = await _collections[collectionName]!; await box.clear(); - return Success(data: null, message: 'Deleted all entries from collection: $collectionName'); + return StorageResponse( + data: null, + message: 'Deleted all entries from collection: $collectionName', + isSuccess: true, + ); } catch (e) { final error = StorageError( failedKey: '', message: 'Error while deleting the entries: $e', stackTrace: StackTrace.current, ); - return Failure(errors: [error]); + return StorageResponse(errors: [error]); } } }