Merge pull request #1960 from famedly/krille/store-room-states-as-tripple-keys
refactor: Store room states as triple keys
This commit is contained in:
commit
f3e249dab3
|
|
@ -52,7 +52,7 @@ import 'package:matrix/src/database/database_file_storage_stub.dart'
|
||||||
/// Learn more at:
|
/// Learn more at:
|
||||||
/// https://github.com/famedly/matrix-dart-sdk/issues/1642#issuecomment-1865827227
|
/// https://github.com/famedly/matrix-dart-sdk/issues/1642#issuecomment-1865827227
|
||||||
class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
static const int version = 9;
|
static const int version = 10;
|
||||||
final String name;
|
final String name;
|
||||||
|
|
||||||
late BoxCollection _collection;
|
late BoxCollection _collection;
|
||||||
|
|
@ -61,12 +61,12 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
late Box<Map> _roomsBox;
|
late Box<Map> _roomsBox;
|
||||||
late Box<Map> _toDeviceQueueBox;
|
late Box<Map> _toDeviceQueueBox;
|
||||||
|
|
||||||
/// Key is a tuple as TupleKey(roomId, type) where stateKey can be
|
/// Key is a tuple as TupleKey(roomId, type, stateKey) where stateKey can be
|
||||||
/// an empty string. Must contain only states of type
|
/// an empty string. Must contain only states of type
|
||||||
/// client.importantRoomStates.
|
/// client.importantRoomStates.
|
||||||
late Box<Map> _preloadRoomStateBox;
|
late Box<Map> _preloadRoomStateBox;
|
||||||
|
|
||||||
/// Key is a tuple as TupleKey(roomId, type) where stateKey can be
|
/// Key is a tuple as TupleKey(roomId, type, stateKey) where stateKey can be
|
||||||
/// an empty string. Must NOT contain states of a type from
|
/// an empty string. Must NOT contain states of a type from
|
||||||
/// client.importantRoomStates.
|
/// client.importantRoomStates.
|
||||||
late Box<Map> _nonPreloadRoomStateBox;
|
late Box<Map> _nonPreloadRoomStateBox;
|
||||||
|
|
@ -593,13 +593,15 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
|
|
||||||
// Get important states:
|
// Get important states:
|
||||||
if (loadImportantStates) {
|
if (loadImportantStates) {
|
||||||
final dbKeys = client.importantStateEvents
|
final preloadRoomStateKeys = await _preloadRoomStateBox.getAllKeys();
|
||||||
.map((state) => TupleKey(roomId, state).toString())
|
final keysForRoom = preloadRoomStateKeys
|
||||||
|
.where((key) => TupleKey.fromString(key).parts.first == roomId)
|
||||||
.toList();
|
.toList();
|
||||||
final rawStates = await _preloadRoomStateBox.getAll(dbKeys);
|
final rawStates = await _preloadRoomStateBox.getAll(keysForRoom);
|
||||||
for (final rawState in rawStates) {
|
|
||||||
if (rawState == null || rawState[''] == null) continue;
|
for (final raw in rawStates) {
|
||||||
room.setState(Event.fromJson(copyMap(rawState['']), room));
|
if (raw == null) continue;
|
||||||
|
room.setState(Event.fromJson(copyMap(raw), room));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -630,17 +632,12 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
Logs().w('Found event in store for unknown room', entry.value);
|
Logs().w('Found event in store for unknown room', entry.value);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final states = entry.value;
|
final raw = entry.value;
|
||||||
final stateEvents = states.values
|
room.setState(
|
||||||
.map(
|
room.membership == Membership.invite
|
||||||
(raw) => room.membership == Membership.invite
|
? StrippedStateEvent.fromJson(copyMap(raw))
|
||||||
? StrippedStateEvent.fromJson(copyMap(raw))
|
: Event.fromJson(copyMap(raw), room),
|
||||||
: Event.fromJson(copyMap(raw), room),
|
);
|
||||||
)
|
|
||||||
.toList();
|
|
||||||
for (final state in stateEvents) {
|
|
||||||
room.setState(state);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the room account data
|
// Get the room account data
|
||||||
|
|
@ -697,11 +694,9 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
|
|
||||||
final unimportantEvents = <Event>[];
|
final unimportantEvents = <Event>[];
|
||||||
for (final key in keys) {
|
for (final key in keys) {
|
||||||
final states = await _nonPreloadRoomStateBox.get(key);
|
final raw = await _nonPreloadRoomStateBox.get(key);
|
||||||
if (states == null) continue;
|
if (raw == null) continue;
|
||||||
unimportantEvents.addAll(
|
unimportantEvents.add(Event.fromJson(copyMap(raw), room));
|
||||||
states.values.map((raw) => Event.fromJson(copyMap(raw), room)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return unimportantEvents.where((event) => event.stateKey != null).toList();
|
return unimportantEvents.where((event) => event.stateKey != null).toList();
|
||||||
|
|
@ -1068,13 +1063,13 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
if (tmpRoom.lastEvent?.eventId == event.eventId) {
|
if (tmpRoom.lastEvent?.eventId == event.eventId) {
|
||||||
if (client.importantStateEvents.contains(event.type)) {
|
if (client.importantStateEvents.contains(event.type)) {
|
||||||
await _preloadRoomStateBox.put(
|
await _preloadRoomStateBox.put(
|
||||||
TupleKey(eventUpdate.roomID, event.type).toString(),
|
TupleKey(eventUpdate.roomID, event.type, '').toString(),
|
||||||
{'': event.toJson()},
|
event.toJson(),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
await _nonPreloadRoomStateBox.put(
|
await _nonPreloadRoomStateBox.put(
|
||||||
TupleKey(eventUpdate.roomID, event.type).toString(),
|
TupleKey(eventUpdate.roomID, event.type, '').toString(),
|
||||||
{'': event.toJson()},
|
event.toJson(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1198,11 +1193,10 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
||||||
final key = TupleKey(
|
final key = TupleKey(
|
||||||
eventUpdate.roomID,
|
eventUpdate.roomID,
|
||||||
type,
|
type,
|
||||||
|
stateKey,
|
||||||
).toString();
|
).toString();
|
||||||
final stateMap = copyMap(await roomStateBox.get(key) ?? {});
|
|
||||||
|
|
||||||
stateMap[stateKey] = eventUpdate.content;
|
await roomStateBox.put(key, eventUpdate.content);
|
||||||
await roomStateBox.put(key, stateMap);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue