Use TupleKey(ObjectKey-AccountId-UserName) to store data to hive

(cherry picked from commit e0ace535d5f3af71eb13598d92524caf2c6d9bbf)
This commit is contained in:
dab246
2023-04-27 11:03:39 +07:00
committed by Dat Vu
parent 7d0a5729b2
commit 876cc368b0
107 changed files with 562 additions and 340 deletions
@@ -9,6 +9,7 @@ import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_change_response.dart';
@@ -22,11 +23,11 @@ import 'package:tmail_ui_user/features/mailbox/domain/model/subscribe_multiple_m
abstract class MailboxDataSource {
Future<MailboxResponse> getAllMailbox(Session session, AccountId accountId, {Properties? properties});
Future<List<Mailbox>> getAllMailboxCache(AccountId accountId);
Future<List<Mailbox>> getAllMailboxCache(AccountId accountId, UserName userName);
Future<MailboxChangeResponse> getChanges(Session session, AccountId accountId, State sinceState);
Future<void> update(AccountId accountId, {List<Mailbox>? updated, List<Mailbox>? created, List<MailboxId>? destroyed});
Future<void> update(AccountId accountId, UserName userName, {List<Mailbox>? updated, List<Mailbox>? created, List<MailboxId>? destroyed});
Future<Mailbox?> createNewMailbox(Session session, AccountId accountId, CreateNewMailboxRequest newMailboxRequest);
@@ -1,10 +1,11 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
abstract class StateDataSource {
Future<State?> getState(AccountId accountId, StateType stateType);
Future<State?> getState(AccountId accountId, UserName userName, StateType stateType);
Future<void> saveState(AccountId accountId, StateCache stateCache);
Future<void> saveState(AccountId accountId, UserName userName, StateCache stateCache);
}
@@ -9,6 +9,7 @@ import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/mailbox_datasource.dart';
@@ -40,16 +41,16 @@ class MailboxCacheDataSourceImpl extends MailboxDataSource {
}
@override
Future<void> update(AccountId accountId, {List<Mailbox>? updated, List<Mailbox>? created, List<MailboxId>? destroyed}) {
Future<void> update(AccountId accountId, UserName userName, {List<Mailbox>? updated, List<Mailbox>? created, List<MailboxId>? destroyed}) {
return Future.sync(() async {
return await _mailboxCacheManager.update(accountId, updated: updated, created: created, destroyed: destroyed);
return await _mailboxCacheManager.update(accountId, userName, updated: updated, created: created, destroyed: destroyed);
}).catchError(_exceptionThrower.throwException);
}
@override
Future<List<Mailbox>> getAllMailboxCache(AccountId accountId) {
Future<List<Mailbox>> getAllMailboxCache(AccountId accountId, UserName userName) {
return Future.sync(() async {
final listMailboxes = await _mailboxCacheManager.getAllMailbox(accountId);
final listMailboxes = await _mailboxCacheManager.getAllMailbox(accountId, userName);
return listMailboxes;
}).catchError(_exceptionThrower.throwException);
}
@@ -9,6 +9,7 @@ import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/mailbox_datasource.dart';
@@ -46,12 +47,12 @@ class MailboxDataSourceImpl extends MailboxDataSource {
}
@override
Future<void> update(AccountId accountId, {List<Mailbox>? updated, List<Mailbox>? created, List<MailboxId>? destroyed}) {
Future<void> update(AccountId accountId, UserName userName, {List<Mailbox>? updated, List<Mailbox>? created, List<MailboxId>? destroyed}) {
throw UnimplementedError();
}
@override
Future<List<Mailbox>> getAllMailboxCache(AccountId accountId) {
Future<List<Mailbox>> getAllMailboxCache(AccountId accountId, UserName userName) {
throw UnimplementedError();
}
@@ -1,6 +1,7 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:tmail_ui_user/features/caching/state_cache_client.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
@@ -18,19 +19,19 @@ class StateDataSourceImpl extends StateDataSource {
StateDataSourceImpl(this._stateCacheClient, this._exceptionThrower);
@override
Future<State?> getState(AccountId accountId, StateType stateType) {
Future<State?> getState(AccountId accountId, UserName userName, StateType stateType) {
return Future.sync(() async {
final stateKey = TupleKey(stateType.value, accountId.asString).toString();
final stateKey = TupleKey(stateType.value, accountId.asString, userName.value).encodeKey;
final stateCache = await _stateCacheClient.getItem(stateKey);
return stateCache?.toState();
}).catchError(_exceptionThrower.throwException);
}
@override
Future<void> saveState(AccountId accountId, StateCache stateCache) {
Future<void> saveState(AccountId accountId, UserName userName, StateCache stateCache) {
return Future.sync(() async {
final stateCacheExist = await _stateCacheClient.isExistTable();
final stateKey = TupleKey(stateCache.type.value, accountId.asString).toString();
final stateKey = TupleKey(stateCache.type.value, accountId.asString, userName.value).encodeKey;
if (stateCacheExist) {
return await _stateCacheClient.updateItem(stateKey, stateCache);
} else {
@@ -1,5 +1,6 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:model/extensions/mailbox_id_extension.dart';
@@ -8,10 +9,10 @@ import 'package:tmail_ui_user/features/mailbox/data/extensions/mailbox_extension
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart';
extension ListMailboxExtension on List<Mailbox> {
Map<String, MailboxCache> toMapCache(AccountId accountId) {
Map<String, MailboxCache> toMapCache(AccountId accountId, UserName userName) {
return {
for (var mailbox in this)
TupleKey(mailbox.id!.asString, accountId.asString).toString() : mailbox.toMailboxCache()
TupleKey(mailbox.id!.asString, accountId.asString, userName.value).encodeKey : mailbox.toMailboxCache()
};
}
}
@@ -1,10 +1,12 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:model/extensions/mailbox_id_extension.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
extension ListMailboxIdExtension on List<MailboxId> {
List<String> toCacheKeyList(AccountId accountId) => map((id) => TupleKey(id.asString, accountId.asString).toString()).toList();
List<String> toCacheKeyList(AccountId accountId, UserName userName) =>
map((id) => TupleKey(id.asString, accountId.asString, userName.value).encodeKey).toList();
}
@@ -1,5 +1,6 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
@@ -15,22 +16,23 @@ class MailboxCacheManager {
MailboxCacheManager(this._mailboxCacheClient);
Future<List<Mailbox>> getAllMailbox(AccountId accountId) async {
final mailboxCacheList = await _mailboxCacheClient.getListByCollectionId(accountId.asString);
Future<List<Mailbox>> getAllMailbox(AccountId accountId, UserName userName) async {
final mailboxCacheList = await _mailboxCacheClient.getListByTupleKey(accountId.asString, userName.value);
return mailboxCacheList.toMailboxList();
}
Future<void> update(
AccountId accountId, {
AccountId accountId,
UserName userName, {
List<Mailbox>? updated,
List<Mailbox>? created,
List<MailboxId>? destroyed
}) async {
final mailboxCacheExist = await _mailboxCacheClient.isExistTable();
if (mailboxCacheExist) {
final updatedCacheMailboxes = updated?.toMapCache(accountId) ?? {};
final createdCacheMailboxes = created?.toMapCache(accountId) ?? {};
final destroyedCacheMailboxes = destroyed?.toCacheKeyList(accountId) ?? [];
final updatedCacheMailboxes = updated?.toMapCache(accountId, userName) ?? {};
final createdCacheMailboxes = created?.toMapCache(accountId, userName) ?? {};
final destroyedCacheMailboxes = destroyed?.toCacheKeyList(accountId, userName) ?? [];
await Future.wait([
_mailboxCacheClient.updateMultipleItem(updatedCacheMailboxes),
@@ -38,14 +40,14 @@ class MailboxCacheManager {
_mailboxCacheClient.deleteMultipleItem(destroyedCacheMailboxes)
]);
} else {
final createdCacheMailboxes = created?.toMapCache(accountId) ?? {};
final createdCacheMailboxes = created?.toMapCache(accountId, userName) ?? {};
await _mailboxCacheClient.insertMultipleItem(createdCacheMailboxes);
}
return Future.value();
}
Future<Mailbox> getSpamMailbox(AccountId accountId) async {
final mailboxCachedList = await _mailboxCacheClient.getListByCollectionId(accountId.asString);
Future<Mailbox> getSpamMailbox(AccountId accountId, UserName userName) async {
final mailboxCachedList = await _mailboxCacheClient.getListByTupleKey(accountId.asString, userName.value);
final listSpamMailboxCached = mailboxCachedList
.toMailboxList()
.where((mailbox) => mailbox.role == PresentationMailbox.roleSpam)
@@ -39,8 +39,8 @@ class MailboxRepositoryImpl extends MailboxRepository {
@override
Stream<MailboxResponse> getAllMailbox(Session session, AccountId accountId, {Properties? properties}) async* {
final localMailboxResponse = await Future.wait([
mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId),
stateDataSource.getState(accountId, StateType.mailbox)
mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId, session.username),
stateDataSource.getState(accountId, session.username, StateType.mailbox)
]).then((List response) {
return MailboxResponse(mailboxes: response.first, state: response.last);
});
@@ -65,26 +65,27 @@ class MailboxRepositoryImpl extends MailboxRepository {
await Future.wait([
mapDataSource[DataSourceType.local]!.update(
accountId,
session.username,
updated: newMailboxUpdated,
created: changesResponse.created,
destroyed: changesResponse.destroyed),
if (changesResponse.newStateMailbox != null)
stateDataSource.saveState(accountId, changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)),
stateDataSource.saveState(accountId, session.username, changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)),
]);
}
} else {
final mailboxResponse = await mapDataSource[DataSourceType.network]!.getAllMailbox(session, accountId);
await Future.wait([
mapDataSource[DataSourceType.local]!.update(accountId, created: mailboxResponse.mailboxes),
mapDataSource[DataSourceType.local]!.update(accountId, session.username, created: mailboxResponse.mailboxes),
if (mailboxResponse.state != null)
stateDataSource.saveState(accountId, mailboxResponse.state!.toStateCache(StateType.mailbox)),
stateDataSource.saveState(accountId, session.username, mailboxResponse.state!.toStateCache(StateType.mailbox)),
]);
}
final newMailboxResponse = await Future.wait([
mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId),
stateDataSource.getState(accountId, StateType.mailbox)
mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId, session.username),
stateDataSource.getState(accountId, session.username, StateType.mailbox)
]).then((List response) {
return MailboxResponse(mailboxes: response.first, state: response.last);
});
@@ -118,7 +119,7 @@ class MailboxRepositoryImpl extends MailboxRepository {
@override
Stream<MailboxResponse> refresh(Session session, AccountId accountId, State currentState) async* {
final localMailboxList = await mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId);
final localMailboxList = await mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId, session.username);
bool hasMoreChanges = true;
State? sinceState = currentState;
@@ -137,17 +138,18 @@ class MailboxRepositoryImpl extends MailboxRepository {
await Future.wait([
mapDataSource[DataSourceType.local]!.update(
accountId,
session.username,
updated: newMailboxUpdated,
created: changesResponse.created,
destroyed: changesResponse.destroyed),
if (changesResponse.newStateMailbox != null)
stateDataSource.saveState(accountId, changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)),
stateDataSource.saveState(accountId, session.username, changesResponse.newStateMailbox!.toStateCache(StateType.mailbox)),
]);
}
final newMailboxResponse = await Future.wait([
mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId),
stateDataSource.getState(accountId, StateType.mailbox)
mapDataSource[DataSourceType.local]!.getAllMailboxCache(accountId, session.username),
stateDataSource.getState(accountId, session.username, StateType.mailbox)
]).then((List response) {
return MailboxResponse(mailboxes: response.first, state: response.last);
});
@@ -191,8 +193,8 @@ class MailboxRepositoryImpl extends MailboxRepository {
}
@override
Future<State?> getMailboxState(AccountId accountId) {
return stateDataSource.getState(accountId, StateType.mailbox);
Future<State?> getMailboxState(Session session, AccountId accountId) {
return stateDataSource.getState(accountId, session.username, StateType.mailbox);
}
@override