Skip to content

Commit

Permalink
organized cold storage keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ereio committed Jan 30, 2021
1 parent 9de7860 commit 62eb020
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 124 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';
}
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
32 changes: 0 additions & 32 deletions lib/store/events/receipts/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,3 @@ Future<Map<String, ReadReceipt>> loadReceipts(
return Map();
}
}

///
/// Load Receipts
///
/// Iterates through
///
Future<Map<String, Map<String, ReadReceipt>>> loadReceiptsOLD(
List<String> messageIds, {
Database storage,
}) async {
try {
final store = StoreRef<String, String>(StorageKeys.RECEIPTS);

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

final flatReceipts = await store.find(storage);

for (RecordSnapshot<String, String> record in flatReceipts) {
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;
}

return receipts;
} catch (error) {
printError(error);
return null;
}
}
27 changes: 11 additions & 16 deletions lib/store/events/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@ import 'dart:convert';
import 'package:sembast/sembast.dart';
import 'package:syphon/global/algos.dart';
import 'package:syphon/global/print.dart';
import 'package:syphon/storage/constants.dart';
import 'package:syphon/store/events/ephemeral/m.read/model.dart';
import 'package:syphon/store/events/model.dart';
import 'package:syphon/store/events/messages/model.dart';
import 'package:syphon/store/events/reactions/model.dart';
import 'package:syphon/store/events/redaction/model.dart';

const String EVENTS = 'events';
const String MESSAGES = 'messages';
const String RECEIPTS = 'receipts';
const String REACTIONS = 'reactions';
const String REDACTIONS = 'redactions';

Future<void> saveEvents(
List<Event> events, {
Database storage,
}) async {
final store = StoreRef<String, String>(EVENTS);
final store = StoreRef<String, String>(StorageKeys.EVENTS);

return await storage.transaction((txn) async {
for (Event event in events) {
Expand All @@ -34,8 +29,8 @@ Future<void> deleteEvents(
Database storage,
}) async {
final stores = [
StoreRef<String, String>(MESSAGES),
StoreRef<String, String>(REACTIONS),
StoreRef<String, String>(StorageKeys.MESSAGES),
StoreRef<String, String>(StorageKeys.REACTIONS),
];

return await Future.wait(stores.map((store) async {
Expand All @@ -59,7 +54,7 @@ Future<void> saveRedactions(
Database storage,
}) async {
try {
final store = StoreRef<String, String>(REDACTIONS);
final store = StoreRef<String, String>(StorageKeys.REDACTIONS);

return await storage.transaction((txn) async {
for (Redaction redaction in redactions) {
Expand All @@ -83,7 +78,7 @@ Future<void> saveRedactions(
Future<Map<String, Redaction>> loadRedactions({
Database storage,
}) async {
final store = StoreRef<String, String>(REDACTIONS);
final store = StoreRef<String, String>(StorageKeys.REDACTIONS);

final redactions = Map<String, Redaction>();

Expand Down Expand Up @@ -111,7 +106,7 @@ Future<void> saveReactions(
Database storage,
}) async {
try {
final store = StoreRef<String, String>(REACTIONS);
final store = StoreRef<String, String>(StorageKeys.REACTIONS);

return await storage.transaction((txn) async {
for (Reaction reaction in reactions) {
Expand Down Expand Up @@ -158,7 +153,7 @@ Future<Map<String, List<Reaction>>> loadReactions(
Database storage,
}) async {
try {
final store = StoreRef<String, String>(REACTIONS);
final store = StoreRef<String, String>(StorageKeys.REACTIONS);
final reactionsMap = Map<String, List<Reaction>>();
final reactionsRecords =
await store.records(messageIds).getSnapshots(storage);
Expand All @@ -184,7 +179,7 @@ Future<void> saveMessages(
List<Message> messages, {
Database storage,
}) async {
final store = StoreRef<String, String>(MESSAGES);
final store = StoreRef<String, String>(StorageKeys.MESSAGES);

return await storage.transaction((txn) async {
for (Message message in messages) {
Expand All @@ -195,7 +190,7 @@ Future<void> saveMessages(
}

Future<Message> loadMessage(String eventId, {Database storage}) async {
final store = StoreRef<String, String>(MESSAGES);
final store = StoreRef<String, String>(StorageKeys.MESSAGES);

final message = await store.record(eventId).get(storage);

Expand All @@ -217,7 +212,7 @@ Future<List<Message>> loadMessages(
final List<Message> messages = [];

try {
final store = StoreRef<String, String>(MESSAGES);
final store = StoreRef<String, String>(StorageKeys.MESSAGES);

// TODO: properly paginate through cold storage messages instead of loading all
final messageIds = eventIds ?? []; //.skip(offset).take(limit).toList();
Expand Down
11 changes: 5 additions & 6 deletions lib/store/media/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:sembast/sembast.dart';
import 'package:syphon/global/print.dart';

const String MEDIA = 'MEDIA';
import 'package:syphon/storage/constants.dart';

Future<bool> checkMedia(
String mxcUri, {
Database storage,
}) async {
final store = StoreRef<String, String>(MEDIA);
final store = StoreRef<String, String>(StorageKeys.MEDIA);

return await store.record(mxcUri).exists(storage);
}
Expand All @@ -21,7 +20,7 @@ Future<void> saveMedia(
Uint8List data, {
Database storage,
}) async {
final store = StoreRef<String, String>(MEDIA);
final store = StoreRef<String, String>(StorageKeys.MEDIA);

return await storage.transaction((txn) async {
final record = store.record(mxcUri);
Expand All @@ -39,7 +38,7 @@ Future<Uint8List> loadMedia({
Database storage,
}) async {
try {
final store = StoreRef<String, String>(MEDIA);
final store = StoreRef<String, String>(StorageKeys.MEDIA);

final mediaData = await store.record(mxcUri).get(storage);

Expand All @@ -64,7 +63,7 @@ Future<Map<String, Uint8List>> loadMediaAll({
}) async {
try {
final Map<String, Uint8List> media = {};
final store = StoreRef<String, String>(MEDIA);
final store = StoreRef<String, String>(StorageKeys.MEDIA);

final mediaDataAll = await store.find(storage);

Expand Down
59 changes: 3 additions & 56 deletions lib/store/user/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'dart:convert';

import 'package:sembast/sembast.dart';
import 'package:syphon/global/print.dart';
import 'package:syphon/storage/constants.dart';
import 'package:syphon/store/user/model.dart';

Future<void> saveUsers(
Map<String, User> users, {
Database cache,
Database storage,
}) async {
final store = StoreRef<String, String>('users');
final store = StoreRef<String, String>(StorageKeys.USERS);

return await storage.transaction((txn) async {
for (User user in users.values) {
Expand All @@ -33,61 +34,7 @@ Future<Map<String, User>> loadUsers({
final Map<String, User> users = {};

try {
final store = StoreRef<String, String>('users');
final count = await store.count(storage);

final finder = Finder(
limit: page,
offset: offset,
);

final usersPaginated = await store.find(
storage,
finder: finder,
);

if (usersPaginated.isEmpty) {
return users;
}

for (RecordSnapshot<String, String> record in usersPaginated) {
users[record.key] = User.fromJson(json.decode(record.value));
}

if (offset < count) {
users.addAll(await loadUsers(
offset: offset + page,
storage: storage,
));
}

if (users.isEmpty) {
return null;
}

printInfo('[users] loaded ${users.length}');
return users;
} catch (error) {
printError(error.toString(), title: 'loadUsers');
return null;
}
}

/**
* Load Users (Cold Storage)
*
* Example of useful recursion
*/
Future<Map<String, User>> loadUsersOld({
Database cache,
Database storage,
int offset = 0,
int page = 5000,
}) async {
final Map<String, User> users = {};

try {
final store = StoreRef<String, String>('users');
final store = StoreRef<String, String>(StorageKeys.USERS);
final count = await store.count(storage);

final finder = Finder(
Expand Down

0 comments on commit 62eb020

Please sign in to comment.