Merge branch 'soru/less-sql-statements' into 'main'

chore: Reduce needed database accessess

See merge request famedly/famedlysdk!666
This commit is contained in:
Krille Fear 2021-03-09 18:12:40 +00:00
commit 57fde98fec
3 changed files with 17 additions and 8 deletions

View File

@ -159,6 +159,8 @@ class KeyManager {
if (uploaded) { if (uploaded) {
client.database client.database
.markInboundGroupSessionAsUploaded(client.id, roomId, sessionId); .markInboundGroupSessionAsUploaded(client.id, roomId, sessionId);
} else {
_haveKeysToUpload = true;
} }
}); });
final room = client.getRoomById(roomId); final room = client.getRoomById(roomId);
@ -680,18 +682,20 @@ class KeyManager {
} }
bool _isUploadingKeys = false; bool _isUploadingKeys = false;
bool _haveKeysToUpload = true;
Future<void> backgroundTasks() async { Future<void> backgroundTasks() async {
if (_isUploadingKeys || client.database == null) { if (_isUploadingKeys || client.database == null) {
return; return;
} }
_isUploadingKeys = true; _isUploadingKeys = true;
try { try {
if (!(await isCached())) { if (!_haveKeysToUpload || !(await isCached())) {
return; // we can't backup anyways return; // we can't backup anyways
} }
final dbSessions = final dbSessions =
await client.database.getInboundGroupSessionsToUpload().get(); await client.database.getInboundGroupSessionsToUpload().get();
if (dbSessions.isEmpty) { if (dbSessions.isEmpty) {
_haveKeysToUpload = false;
return; // nothing to do return; // nothing to do
} }
final privateKey = final privateKey =

View File

@ -1698,14 +1698,20 @@ sort order of ${prevState.sortOrder}. This should never happen...''');
} }
} }
bool _toDeviceQueueNeedsProcessing = true;
/// Processes the to_device queue and tries to send every entry. /// Processes the to_device queue and tries to send every entry.
/// This function MAY throw an error, which just means the to_device queue wasn't /// This function MAY throw an error, which just means the to_device queue wasn't
/// proccessed all the way. /// proccessed all the way.
Future<void> processToDeviceQueue() async { Future<void> processToDeviceQueue() async {
if (database == null) { if (database == null || !_toDeviceQueueNeedsProcessing) {
return; return;
} }
final entries = await database.getToDeviceQueue(id).get(); final entries = await database.getToDeviceQueue(id).get();
if (entries.isEmpty) {
_toDeviceQueueNeedsProcessing = false;
return;
}
for (final entry in entries) { for (final entry in entries) {
// ohgod what is this... // ohgod what is this...
final data = (json.decode(entry.content) as Map).map((k, v) => final data = (json.decode(entry.content) as Map).map((k, v) =>
@ -1736,6 +1742,7 @@ sort order of ${prevState.sortOrder}. This should never happen...''');
e, e,
s); s);
if (database != null) { if (database != null) {
_toDeviceQueueNeedsProcessing = true;
await database.insertIntoToDeviceQueue( await database.insertIntoToDeviceQueue(
id, eventType, txnId, json.encode(messages)); id, eventType, txnId, json.encode(messages));
} }

View File

@ -282,7 +282,8 @@ class Database extends _$Database {
); );
roomList.add(room); roomList.add(room);
// let's see if we need any m.room.member events // let's see if we need any m.room.member events
final membersToPostload = <String>{}; // We always need the member event for ourself
final membersToPostload = <String>{client.userID};
// the lastEvent message preview might have an author we need to fetch, if it is a group chat // the lastEvent message preview might have an author we need to fetch, if it is a group chat
if (room.getState(api.EventTypes.Message) != null && !room.isDirectChat) { if (room.getState(api.EventTypes.Message) != null && !room.isDirectChat) {
membersToPostload.add(room.getState(api.EventTypes.Message).senderId); membersToPostload.add(room.getState(api.EventTypes.Message).senderId);
@ -296,12 +297,9 @@ class Database extends _$Database {
// post-load the heroes // post-load the heroes
membersToPostload.addAll(room.mHeroes.where((h) => h.isNotEmpty)); membersToPostload.addAll(room.mHeroes.where((h) => h.isNotEmpty));
} }
// okay, only load from the database if we actually have stuff to load
if (membersToPostload.isNotEmpty) {
// save it for loading later // save it for loading later
allMembersToPostload[room.id] = membersToPostload; allMembersToPostload[room.id] = membersToPostload;
} }
}
// now we postload all members, if thre are any // now we postload all members, if thre are any
if (allMembersToPostload.isNotEmpty) { if (allMembersToPostload.isNotEmpty) {
// we will generate a query to fetch as many events as possible at once, as that // we will generate a query to fetch as many events as possible at once, as that