Skip to content

Commit 508c019

Browse files
authored
perf(llc): Reduce the number of DB reads in ChatPersistenceClient.getChannelStates() (#2664)
* Rename legacy 'Moor' references to 'Drift'. * Optimize getChannelStates by enriching only the relevant page. * Update CHANGELOG.md. * refactor(dao): Add tests * refactor(dao): Update pagination * refactor(dao): Remove unused method * refactor(dao): Add MemberDao missing tests
1 parent 4103da3 commit 508c019

22 files changed

Lines changed: 284 additions & 92 deletions

packages/stream_chat_persistence/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Upcoming
2+
3+
🚀 Performance
4+
5+
- Reduce the number of DB reads in the `ChatPersistenceClient.getChannelStates` method.
6+
17
## 9.24.0
28

39
- Updated `stream_chat` dependency to [`9.24.0`](https://pub.dev/packages/stream_chat/changelog).

packages/stream_chat_persistence/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [Flutter Offline Docs](https://getstream.io/chat/docs/flutter-dart/flutter_offline/)
1717

1818
This package provides a persistence client for fetching and saving chat data locally.
19-
Stream Chat Persistence uses [Moor](https://github.com/simolus3/moor) as a disk cache.
19+
Stream Chat Persistence uses [Drift](https://github.com/simolus3/drift) as a disk cache.
2020

2121
### Changelog
2222

@@ -52,7 +52,7 @@ And you are ready to go...
5252

5353
## Flutter Web
5454

55-
Due to Moor web (for offline storage) you need to include the sql.js library:
55+
Due to Drift web (for offline storage) you need to include the sql.js library:
5656

5757
```html
5858
<!doctype html>

packages/stream_chat_persistence/lib/src/dao/member_dao.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ class MemberDao extends DatabaseAccessor<DriftChatDatabase>
2727
return memberEntity.toMember(user: userEntity.toUser());
2828
}).get();
2929

30+
/// Returns the membership row for [userId] in each channel listed in [cids],
31+
/// keyed by `channelCid`.
32+
///
33+
/// Channels in [cids] without a membership row for [userId] are absent from
34+
/// the result. Executes a single `WHERE channel_cid IN (...) AND user_id = ?`
35+
/// query.
36+
Future<Map<String, Member>> getMembershipsForChannels(
37+
List<String> cids,
38+
String userId,
39+
) async {
40+
if (cids.isEmpty) return const {};
41+
42+
final rows = await (select(members).join([
43+
leftOuterJoin(users, members.userId.equalsExp(users.id)),
44+
])
45+
..where(
46+
members.channelCid.isIn(cids) & members.userId.equals(userId),
47+
))
48+
.get();
49+
50+
return {
51+
for (final row in rows)
52+
row.readTable(members).channelCid: row.readTable(members).toMember(
53+
user: row.readTableOrNull(users)?.toUser(),
54+
),
55+
};
56+
}
57+
3058
/// Updates all the members using the new [memberList] data
3159
Future<void> updateMembers(String cid, List<Member> memberList) =>
3260
bulkUpdateMembers({cid: memberList});

packages/stream_chat_persistence/lib/src/db/drift_chat_database.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export 'shared/shared_db.dart';
88

99
part 'drift_chat_database.g.dart';
1010

11-
/// A chat database implemented using moor
11+
/// A chat database implemented using drift
1212
@DriftDatabase(
1313
tables: [
1414
Channels,

packages/stream_chat_persistence/lib/src/db/shared/native_db.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SharedDB {
2626
return DriftChatDatabase(
2727
userId,
2828
DatabaseConnection.delayed(Future(() async {
29-
final isolate = await _createMoorIsolate(
29+
final isolate = await _createDriftIsolate(
3030
dbName,
3131
logStatements: logStatements,
3232
);
@@ -68,13 +68,13 @@ class SharedDB {
6868
File(request.targetPath),
6969
logStatements: request.logStatements,
7070
));
71-
final moorIsolate = DriftIsolate.inCurrent(
71+
final driftIsolate = DriftIsolate.inCurrent(
7272
() => DatabaseConnection(executor),
7373
);
74-
request.sendMoorIsolate.send(moorIsolate);
74+
request.sendDriftIsolate.send(driftIsolate);
7575
}
7676

77-
static Future<DriftIsolate> _createMoorIsolate(
77+
static Future<DriftIsolate> _createDriftIsolate(
7878
String dbName, {
7979
bool logStatements = false,
8080
}) async {
@@ -97,12 +97,12 @@ class SharedDB {
9797

9898
class _IsolateStartRequest {
9999
const _IsolateStartRequest(
100-
this.sendMoorIsolate,
100+
this.sendDriftIsolate,
101101
this.targetPath, {
102102
this.logStatements = false,
103103
});
104104

105-
final SendPort sendMoorIsolate;
105+
final SendPort sendDriftIsolate;
106106
final String targetPath;
107107
final bool logStatements;
108108
}

packages/stream_chat_persistence/lib/src/entity/channel_queries.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// coverage:ignore-file
22
import 'package:drift/drift.dart';
33

4-
/// Represents a [ChannelQueries] table in [MoorChatDatabase].
4+
/// Represents a [ChannelQueries] table in [DriftChatDatabase].
55
@DataClassName('ChannelQueryEntity')
66
class ChannelQueries extends Table {
77
/// The unique hash of this query

packages/stream_chat_persistence/lib/src/entity/channels.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import 'package:drift/drift.dart';
33
import 'package:stream_chat_persistence/src/converter/converter.dart';
44

5-
/// Represents a [Channels] table in [MoorChatDatabase].
5+
/// Represents a [Channels] table in [DriftChatDatabase].
66
@DataClassName('ChannelEntity')
77
class Channels extends Table {
88
/// The id of this channel

packages/stream_chat_persistence/lib/src/entity/connection_events.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import 'package:drift/drift.dart';
33
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
44

5-
/// Represents a [ConnectionEvents] table in [MoorChatDatabase].
5+
/// Represents a [ConnectionEvents] table in [DriftChatDatabase].
66
@DataClassName('ConnectionEventEntity')
77
class ConnectionEvents extends Table {
88
/// event id

packages/stream_chat_persistence/lib/src/entity/draft_messages.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:stream_chat_persistence/src/converter/converter.dart';
44
import 'package:stream_chat_persistence/src/entity/channels.dart';
55
import 'package:stream_chat_persistence/src/entity/messages.dart';
66

7-
/// Represents a [DraftMessages] table in [MoorChatDatabase].
7+
/// Represents a [DraftMessages] table in [DriftChatDatabase].
88
@DataClassName('DraftMessageEntity')
99
class DraftMessages extends Table {
1010
/// The message id

packages/stream_chat_persistence/lib/src/entity/members.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:drift/drift.dart';
33
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
44
import 'package:stream_chat_persistence/src/entity/channels.dart';
55

6-
/// Represents a [Members] table in [MoorChatDatabase].
6+
/// Represents a [Members] table in [DriftChatDatabase].
77
@DataClassName('MemberEntity')
88
class Members extends Table {
99
/// The interested user id

0 commit comments

Comments
 (0)