Skip to content

Commit

Permalink
Merge pull request #225 from syphon-org/hot-fixes-0.1.6
Browse files Browse the repository at this point in the history
[hotfix] Performance Syncing & Receipt Storage
  • Loading branch information
ereio authored Jan 30, 2021
2 parents e44d63f + 62eb020 commit 7b455cc
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 148 deletions.
11 changes: 9 additions & 2 deletions lib/storage/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class StorageKeys {
static const String RECEIPTS = 'RECEIPTS';
static const String REDACTIONS = 'REDACTIONS';
static const String AUTH = 'auth';
static const String ROOMS = 'rooms';
static const String USERS = 'users';
static const String MEDIA = 'MEDIA';
static const String CRYPTO = 'crypto';
static const String EVENTS = 'events';
static const String MESSAGES = 'messages';
static const String RECEIPTS = 'receipts';
static const String REACTIONS = 'reactions';
static const String REDACTIONS = 'redactions';
}
96 changes: 52 additions & 44 deletions lib/storage/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:sqflite/sqflite.dart' as sqflite;
import 'package:sqflite_common_ffi/sqflite_ffi.dart' as sqflite_ffi;
import 'package:syphon/store/auth/storage.dart';
import 'package:syphon/store/crypto/storage.dart';
import 'package:syphon/store/events/ephemeral/m.read/model.dart';
import 'package:syphon/store/events/messages/model.dart';
import 'package:syphon/store/events/reactions/model.dart';
import 'package:syphon/store/events/receipts/storage.dart';
Expand Down Expand Up @@ -114,58 +115,65 @@ Future<void> deleteStorage() async {
* TODO: need pagination for pretty much all of these
*/
Future<Map<String, dynamic>> loadStorage(Database storage) async {
final auth = await loadAuth(
storage: storage,
);
try {
final auth = await loadAuth(
storage: storage,
);

final rooms = await loadRooms(
storage: storage,
);
final rooms = await loadRooms(
storage: storage,
);

final users = await loadUsers(
storage: storage,
);
final users = await loadUsers(
storage: storage,
);

final media = await loadMediaAll(
storage: storage,
);
final media = await loadMediaAll(
storage: storage,
);

final crypto = await loadCrypto(
storage: storage,
);

final crypto = await loadCrypto(
storage: storage,
);
final redactions = await loadRedactions(
storage: storage,
);

final redactions = await loadRedactions(
storage: storage,
);
Map<String, List<Message>> messages = Map();
Map<String, List<Reaction>> reactions = Map();
Map<String, Map<String, ReadReceipt>> receipts = Map();

final receipts = await loadReceipts(
storage: storage,
);
for (Room room in rooms.values) {
messages[room.id] = await loadMessages(
room.messageIds,
storage: storage,
);

Map<String, List<Message>> messages = Map();
Map<String, List<Reaction>> reactions = Map();
reactions.addAll(await loadReactions(
room.messageIds,
storage: storage,
));

for (Room room in rooms.values) {
messages[room.id] = await loadMessages(
room.messageIds,
storage: storage,
);
receipts[room.id] = await loadReceipts(
room.messageIds,
storage: storage,
);
}

reactions.addAll(await loadReactions(
room.messageIds,
storage: storage,
));
return {
'auth': auth,
'users': users,
'rooms': rooms,
'media': media,
'crypto': crypto,
'messages': messages.isNotEmpty ? messages : null,
'reactions': reactions,
'redactions': redactions,
'receipts': receipts,
};
} catch (error) {
printError('[loadStorage] ${error.toString()}');
return {};
}

return {
'auth': auth,
'users': users,
'rooms': rooms,
'media': media,
'crypto': crypto,
'messages': messages.isNotEmpty ? messages : null,
'reactions': reactions,
'redactions': redactions,
'receipts': receipts,
};
}
11 changes: 5 additions & 6 deletions lib/store/auth/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import 'dart:convert';

import 'package:sembast/sembast.dart';
import 'package:syphon/global/print.dart';
import 'package:syphon/storage/constants.dart';
import 'package:syphon/store/auth/state.dart';

const String AUTH = 'auth';

Future<void> saveAuth(
AuthStore authStore, {
Database storage,
}) async {
final store = StoreRef<String, String>(AUTH);
final store = StoreRef<String, String>(StorageKeys.AUTH);

return await storage.transaction((txn) async {
final record = store.record(AUTH);
final record = store.record(StorageKeys.AUTH);
await record.put(txn, json.encode(authStore));
});
}
Expand All @@ -26,9 +25,9 @@ Future<void> saveAuth(
*/
Future<AuthStore> loadAuth({Database storage}) async {
try {
final store = StoreRef<String, String>(AUTH);
final store = StoreRef<String, String>(StorageKeys.AUTH);

final auth = await store.record(AUTH).get(storage);
final auth = await store.record(StorageKeys.AUTH).get(storage);

if (auth == null) {
return null;
Expand Down
11 changes: 5 additions & 6 deletions lib/store/crypto/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import 'dart:convert';
import 'package:redux/redux.dart';
import 'package:sembast/sembast.dart';
import 'package:syphon/global/print.dart';
import 'package:syphon/storage/constants.dart';
import 'package:syphon/store/crypto/state.dart';

const String CRYPTO = 'crypto';

/**
* Save Crypto Store
*
Expand All @@ -17,10 +16,10 @@ Future<void> saveCrypto(
CryptoStore cryptoStore, {
Database storage,
}) async {
final store = StoreRef<String, String>(CRYPTO);
final store = StoreRef<String, String>(StorageKeys.CRYPTO);

return await storage.transaction((txn) async {
final record = store.record(CRYPTO);
final record = store.record(StorageKeys.CRYPTO);
await record.put(txn, json.encode(cryptoStore));
});
}
Expand All @@ -33,9 +32,9 @@ Future<void> saveCrypto(
*/
Future<CryptoStore> loadCrypto({Database storage}) async {
try {
final store = StoreRef<String, String>(CRYPTO);
final store = StoreRef<String, String>(StorageKeys.CRYPTO);

final crypto = await store.record(CRYPTO).get(storage);
final crypto = await store.record(StorageKeys.CRYPTO).get(storage);

if (crypto == null) {
return null;
Expand Down
15 changes: 15 additions & 0 deletions lib/store/events/parsers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import 'package:syphon/global/libs/matrix/constants.dart';
import 'package:syphon/store/events/messages/model.dart';
import 'package:syphon/store/events/reactions/model.dart';
import 'package:syphon/store/rooms/room/model.dart';
import 'package:syphon/store/user/model.dart';

Room parseRoom(Map params) {
Map json = params['json'];
Room room = params['room'];
User currentUser = params['currentUser'];
String lastSince = params['lastSince'];

// TODO: eventually remove the need for this with modular parsers
return room.fromSync(
json: json,
currentUser: currentUser,
lastSince: lastSince,
);
}

Map<String, dynamic> parseMessages({
Room room,
Expand Down
45 changes: 26 additions & 19 deletions lib/store/events/receipts/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ import 'package:syphon/store/events/redaction/model.dart';
///
///
Future<void> saveReceipts(
String roomId,
Map<String, ReadReceipt> receipts, {
Database storage,
bool ready,
}) async {
final store = StoreRef<String, String>(StorageKeys.RECEIPTS);

// TODO: the initial sync loads way too many read receipts
if (!ready) return;

return await storage.transaction((txn) async {
final record = store.record(roomId);
await record.put(txn, json.encode(receipts));
for (String key in receipts.keys) {
final record = store.record(key);
await record.put(txn, json.encode(receipts[key]));
}
});
}

Expand All @@ -29,23 +34,25 @@ Future<void> saveReceipts(
///
/// Iterates through
///
Future<Map<String, Map<String, ReadReceipt>>> loadReceipts({
Future<Map<String, ReadReceipt>> loadReceipts(
List<String> messageIds, {
Database storage,
}) async {
final store = StoreRef<String, String>(StorageKeys.RECEIPTS);

final receipts = Map<String, Map<String, ReadReceipt>>();

final roomReceipts = await store.find(storage);

for (RecordSnapshot<String, String> record in roomReceipts) {
final testing = await json.decode(record.value);
final mapped = Map<String, dynamic>.from(testing);
final Map<String, ReadReceipt> converted = mapped.map(
(key, value) => MapEntry(key, ReadReceipt.fromJson(value)),
);
receipts[record.key] = converted;
try {
final store = StoreRef<String, String>(StorageKeys.RECEIPTS);

final receiptsMap = Map<String, ReadReceipt>();
final records = await store.records(messageIds).getSnapshots(storage);

for (RecordSnapshot<String, String> record in records ?? []) {
if (record != null) {
final receipt = ReadReceipt.fromJson(await json.decode(record.value));
receiptsMap.putIfAbsent(record.key, () => receipt);
}
}
return receiptsMap;
} catch (error) {
printError(error.toString());
return Map();
}

return receipts;
}
Loading

0 comments on commit 7b455cc

Please sign in to comment.