Skip to content

Commit d02c60f

Browse files
wcjordWilsonLe
andauthored
implement get space hierarchy cache (#1)
* implement get space hierarchy cache * move inmem cache to db cache * fix storing non json serialized get space hierarchy response --------- Co-authored-by: WilsonLe <leanhminh2907@gmail.com>
1 parent 0ac0ef6 commit d02c60f

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

lib/src/client.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,22 @@ class Client extends MatrixApi {
21942194
EventUpdateType.accountData,
21952195
);
21962196
}
2197+
2198+
// invalidate _getSpaceHierarchyResponseCache
2199+
if (room.spaceParents.isNotEmpty) {
2200+
for (final space in room.spaceParents) {
2201+
final String? spaceId = space.roomId;
2202+
if (spaceId == null) continue;
2203+
if (spaceId == room.id) continue;
2204+
final Room? spaceAsRoom = getRoomById(spaceId);
2205+
if (spaceAsRoom == null) continue;
2206+
if (spaceAsRoom.spaceChildren
2207+
.map((child) => child.roomId)
2208+
.contains(room.id)) {
2209+
await database?.removeSpaceHierarchy(spaceId);
2210+
}
2211+
}
2212+
}
21972213
}
21982214

21992215
if (syncRoomUpdate is LeftRoomUpdate) {
@@ -3323,6 +3339,24 @@ class Client extends MatrixApi {
33233339
waitUntilLoadCompletedLoaded: false,
33243340
);
33253341
}
3342+
3343+
@override
3344+
Future<GetSpaceHierarchyResponse> getSpaceHierarchy(String roomId,
3345+
{bool? suggestedOnly, int? limit, int? maxDepth, String? from}) async {
3346+
final cachedResponse = await database?.getSpaceHierarchy(roomId);
3347+
if (cachedResponse == null) {
3348+
final response = await super.getSpaceHierarchy(
3349+
roomId,
3350+
suggestedOnly: suggestedOnly,
3351+
limit: limit,
3352+
maxDepth: maxDepth,
3353+
from: from,
3354+
);
3355+
await database?.storeSpaceHierarchy(roomId, response);
3356+
return response;
3357+
}
3358+
return cachedResponse;
3359+
}
33263360
}
33273361

33283362
class SdkError {

lib/src/database/database_api.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,13 @@ abstract class DatabaseApi {
327327

328328
Future<CachedPresence?> getPresence(String userId);
329329

330+
Future<GetSpaceHierarchyResponse?> getSpaceHierarchy(String spaceId);
331+
332+
Future<void> storeSpaceHierarchy(
333+
String spaceId, GetSpaceHierarchyResponse hierarchy);
334+
335+
Future<void> removeSpaceHierarchy(String spaceId);
336+
330337
/// Deletes the whole database. The database needs to be created again after
331338
/// this. Used for migration only.
332339
Future<void> delete();

lib/src/database/hive_collections_database.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class HiveCollectionsDatabase extends DatabaseApi {
8989

9090
late CollectionBox<String> _seenDeviceKeysBox;
9191

92+
late CollectionBox<GetSpaceHierarchyResponse> _spacesHierarchyBox;
93+
9294
String get _clientBoxName => 'box_client';
9395

9496
String get _accountDataBoxName => 'box_account_data';
@@ -127,6 +129,8 @@ class HiveCollectionsDatabase extends DatabaseApi {
127129

128130
String get _seenDeviceKeysBoxName => 'box_seen_device_keys';
129131

132+
static const String _spacesHierarchyBoxName = 'box_spaces_hierarchy';
133+
130134
HiveCollectionsDatabase(
131135
this.name,
132136
this.path, {
@@ -160,6 +164,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
160164
_eventsBoxName,
161165
_seenDeviceIdsBoxName,
162166
_seenDeviceKeysBoxName,
167+
_spacesHierarchyBoxName,
163168
},
164169
key: key,
165170
path: path,
@@ -226,6 +231,9 @@ class HiveCollectionsDatabase extends DatabaseApi {
226231
_seenDeviceKeysBox = await _collection.openBox(
227232
_seenDeviceKeysBoxName,
228233
);
234+
_spacesHierarchyBox = await _collection.openBox(
235+
_spacesHierarchyBoxName,
236+
);
229237

230238
// Check version and check if we need a migration
231239
final currentVersion = int.tryParse(await _clientBox.get('version') ?? '');
@@ -280,6 +288,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
280288
await _outboundGroupSessionsBox.clear();
281289
await _presencesBox.clear();
282290
await _clientBox.delete('prev_batch');
291+
await _spacesHierarchyBox.clear();
283292
});
284293

285294
@override
@@ -326,6 +335,11 @@ class HiveCollectionsDatabase extends DatabaseApi {
326335
if (multiKey.parts.first != roomId) continue;
327336
await _roomAccountDataBox.delete(key);
328337
}
338+
final spaceHierarchyKeys = await _spacesHierarchyBox.getAllKeys();
339+
for (final key in spaceHierarchyKeys) {
340+
if (key != roomId) continue;
341+
await _spacesHierarchyBox.delete(key);
342+
}
329343
await _roomsBox.delete(roomId);
330344
});
331345

@@ -1517,6 +1531,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
15171531
_eventsBoxName: await _eventsBox.getAllValues(),
15181532
_seenDeviceIdsBoxName: await _seenDeviceIdsBox.getAllValues(),
15191533
_seenDeviceKeysBoxName: await _seenDeviceKeysBox.getAllValues(),
1534+
_spacesHierarchyBoxName: await _spacesHierarchyBox.getAllValues(),
15201535
};
15211536
final json = jsonEncode(dataMap);
15221537
await clear();
@@ -1588,13 +1603,30 @@ class HiveCollectionsDatabase extends DatabaseApi {
15881603
for (final key in json[_seenDeviceKeysBoxName]!.keys) {
15891604
await _seenDeviceKeysBox.put(key, json[_seenDeviceKeysBoxName]![key]);
15901605
}
1606+
for (final key in json[_spacesHierarchyBoxName]!.keys) {
1607+
await _spacesHierarchyBox.put(key, json[_spacesHierarchyBoxName]![key]);
1608+
}
15911609
return true;
15921610
} catch (e, s) {
15931611
Logs().e('Database import error: ', e, s);
15941612
return false;
15951613
}
15961614
}
15971615

1616+
@override
1617+
Future<GetSpaceHierarchyResponse?> getSpaceHierarchy(String spaceId) async {
1618+
return await _spacesHierarchyBox.get(spaceId);
1619+
}
1620+
1621+
@override
1622+
Future<void> storeSpaceHierarchy(
1623+
String spaceId, GetSpaceHierarchyResponse hierarchy) =>
1624+
_spacesHierarchyBox.put(spaceId, hierarchy);
1625+
1626+
@override
1627+
Future<void> removeSpaceHierarchy(String spaceId) =>
1628+
_spacesHierarchyBox.delete(spaceId);
1629+
15981630
@override
15991631
Future<void> delete() => _collection.deleteFromDisk();
16001632
}

lib/src/database/hive_database.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
8484

8585
late LazyBox _seenDeviceKeysBox;
8686

87+
late LazyBox<GetSpaceHierarchyResponse> _spacesHierarchyBox;
88+
8789
String get _clientBoxName => '$name.box.client';
8890

8991
String get _accountDataBoxName => '$name.box.account_data';
@@ -124,6 +126,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
124126

125127
String get _seenDeviceKeysBoxName => '$name.box.seen_device_keys';
126128

129+
static const String _spacesHierarchyBoxName = 'box_spaces_hierarchy';
130+
127131
final HiveCipher? encryptionCipher;
128132

129133
FamedlySdkHiveDatabase(this.name, {this.encryptionCipher});
@@ -152,6 +156,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
152156
action(_eventsBox),
153157
action(_seenDeviceIdsBox),
154158
action(_seenDeviceKeysBox),
159+
action(_spacesHierarchyBox)
155160
]);
156161

157162
Future<void> open() async {
@@ -232,6 +237,11 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
232237
encryptionCipher: encryptionCipher,
233238
);
234239

240+
_spacesHierarchyBox = await Hive.openLazyBox(
241+
_spacesHierarchyBoxName,
242+
encryptionCipher: encryptionCipher,
243+
);
244+
235245
// Check version and check if we need a migration
236246
final currentVersion = (await _clientBox.get('version') as int?);
237247
if (currentVersion == null) {
@@ -295,6 +305,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
295305
await _outboundGroupSessionsBox.deleteAll(_outboundGroupSessionsBox.keys);
296306
await _presencesBox.deleteAll(_presencesBox.keys);
297307
await _clientBox.delete('prev_batch');
308+
await _spacesHierarchyBox.clear();
298309
}
299310

300311
@override
@@ -339,6 +350,10 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
339350
if (multiKey.parts.first != roomId) continue;
340351
await _roomAccountDataBox.delete(key);
341352
}
353+
for (final key in _spacesHierarchyBox.keys) {
354+
if (key != roomId) continue;
355+
await _spacesHierarchyBox.delete(key);
356+
}
342357
await _roomsBox.delete(roomId.toHiveKey);
343358
}
344359

@@ -1401,6 +1416,20 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
14011416
throw UnimplementedError();
14021417
}
14031418

1419+
@override
1420+
Future<GetSpaceHierarchyResponse?> getSpaceHierarchy(String spaceId) async {
1421+
return await _spacesHierarchyBox.get(spaceId);
1422+
}
1423+
1424+
@override
1425+
Future<void> storeSpaceHierarchy(
1426+
String spaceId, GetSpaceHierarchyResponse hierarchy) =>
1427+
_spacesHierarchyBox.put(spaceId, hierarchy);
1428+
1429+
@override
1430+
Future<void> removeSpaceHierarchy(String spaceId) =>
1431+
_spacesHierarchyBox.delete(spaceId);
1432+
14041433
@override
14051434
Future<void> delete() => Hive.deleteFromDisk();
14061435
}

lib/src/database/matrix_sdk_database.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
103103

104104
late Box<String> _seenDeviceKeysBox;
105105

106+
late Box<Map> _spacesHierarchyBox;
107+
106108
@override
107109
final int maxFileSize;
108110

@@ -159,6 +161,8 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
159161

160162
static const String _seenDeviceKeysBoxName = 'box_seen_device_keys';
161163

164+
static const String _spacesHierarchyBoxName = 'box_spaces_hierarchy';
165+
162166
Database? database;
163167

164168
/// Custom IdbFactory used to create the indexedDB. On IO platforms it would
@@ -214,6 +218,7 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
214218
_eventsBoxName,
215219
_seenDeviceIdsBoxName,
216220
_seenDeviceKeysBoxName,
221+
_spacesHierarchyBoxName,
217222
},
218223
sqfliteDatabase: database,
219224
sqfliteFactory: sqfliteFactory,
@@ -283,6 +288,9 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
283288
_seenDeviceKeysBox = _collection.openBox(
284289
_seenDeviceKeysBoxName,
285290
);
291+
_spacesHierarchyBox = _collection.openBox(
292+
_spacesHierarchyBoxName,
293+
);
286294

287295
// Check version and check if we need a migration
288296
final currentVersion = int.tryParse(await _clientBox.get('version') ?? '');
@@ -341,6 +349,7 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
341349
await _outboundGroupSessionsBox.clear();
342350
await _presencesBox.clear();
343351
await _clientBox.delete('prev_batch');
352+
await _spacesHierarchyBox.clear();
344353
});
345354

346355
@override
@@ -389,6 +398,11 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
389398
if (multiKey.parts.first != roomId) continue;
390399
await _roomAccountDataBox.delete(key);
391400
}
401+
final spaceHierarchyKeys = await _spacesHierarchyBox.getAllKeys();
402+
for (final key in spaceHierarchyKeys) {
403+
if (key != roomId) continue;
404+
await _spacesHierarchyBox.delete(key);
405+
}
392406
await _roomsBox.delete(roomId);
393407
}
394408

@@ -1477,6 +1491,7 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
14771491
_eventsBoxName: await _eventsBox.getAllValues(),
14781492
_seenDeviceIdsBoxName: await _seenDeviceIdsBox.getAllValues(),
14791493
_seenDeviceKeysBoxName: await _seenDeviceKeysBox.getAllValues(),
1494+
_spacesHierarchyBoxName: await _spacesHierarchyBox.getAllValues(),
14801495
};
14811496
final json = jsonEncode(dataMap);
14821497
await clear();
@@ -1557,6 +1572,9 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
15571572
for (final key in json[_seenDeviceKeysBoxName]!.keys) {
15581573
await _seenDeviceKeysBox.put(key, json[_seenDeviceKeysBoxName]![key]);
15591574
}
1575+
for (final key in json[_spacesHierarchyBoxName]!.keys) {
1576+
await _spacesHierarchyBox.put(key, json[_spacesHierarchyBoxName]![key]);
1577+
}
15601578
return true;
15611579
} catch (e, s) {
15621580
Logs().e('Database import error: ', e, s);
@@ -1613,6 +1631,22 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
16131631
return CachedPresence.fromJson(copyMap(rawPresence));
16141632
}
16151633

1634+
@override
1635+
Future<GetSpaceHierarchyResponse?> getSpaceHierarchy(String spaceId) async {
1636+
final raw_space_hierarchy = await _spacesHierarchyBox.get(spaceId);
1637+
if (raw_space_hierarchy == null) return null;
1638+
return GetSpaceHierarchyResponse.fromJson(copyMap(raw_space_hierarchy));
1639+
}
1640+
1641+
@override
1642+
Future<void> storeSpaceHierarchy(
1643+
String spaceId, GetSpaceHierarchyResponse hierarchy) =>
1644+
_spacesHierarchyBox.put(spaceId, hierarchy.toJson());
1645+
1646+
@override
1647+
Future<void> removeSpaceHierarchy(String spaceId) =>
1648+
_spacesHierarchyBox.delete(spaceId);
1649+
16161650
@override
16171651
Future<void> delete() => BoxCollection.delete(
16181652
name,

0 commit comments

Comments
 (0)