-
Notifications
You must be signed in to change notification settings - Fork 28
perf: serve mostro message queries from a single in-memory index #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+372
−86
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
11eb523
perf: serve mostro message queries from a single in-memory index
grunch 6f564bf
Merge branch 'main' into perf/single-store-watcher
grunch 1931064
fix: clear the message index on full wipes and recover from a failed …
grunch 7261d41
fix: surface a failed index warm-up to watchers and keep deletions on…
grunch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mostro_mobile/data/models/enums/action.dart'; | ||
| import 'package:mostro_mobile/data/models/mostro_message.dart'; | ||
| import 'package:mostro_mobile/data/repositories/mostro_storage.dart'; | ||
| import 'package:sembast/sembast_memory.dart'; | ||
|
|
||
| /// Every `orders`-store write used to wake one Sembast query listener per | ||
| /// OrderNotifier and per visible trade row, each re-filtering the whole | ||
| /// unindexed store on the UI isolate: O((notifiers + rows) × messages) per | ||
| /// incoming message. The storage now keeps a single in-memory index by order | ||
| /// id; watchers are served from it and Sembast remains the persistence | ||
| /// layer, warmed once per cold start. | ||
| void main() { | ||
| late MostroStorage storage; | ||
|
|
||
| MostroMessage message(String orderId, Action action, int timestamp) { | ||
| final m = MostroMessage(action: action, id: orderId); | ||
| m.timestamp = timestamp; | ||
| return m; | ||
| } | ||
|
|
||
| setUp(() async { | ||
| final db = await newDatabaseFactoryMemory().openDatabase('index_test.db'); | ||
| storage = MostroStorage(db: db); | ||
| }); | ||
|
|
||
| test('the latest-message watcher tracks adds for its order', () async { | ||
| final emissions = <MostroMessage?>[]; | ||
| final sub = storage.watchLatestMessage('a').listen(emissions.add); | ||
| addTearDown(sub.cancel); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
| await storage.addMessage('k2', message('a', Action.payInvoice, 2000)); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| expect(emissions.last!.action, Action.payInvoice); | ||
| }); | ||
|
|
||
| test('a write for another order does not notify this watcher', () async { | ||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
| var emissions = 0; | ||
| final sub = storage.watchLatestMessage('a').skip(1).listen((_) { | ||
| emissions++; | ||
| }); | ||
| addTearDown(sub.cancel); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| await storage.addMessage('k2', message('b', Action.newOrder, 2000)); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| expect(emissions, 0, | ||
| reason: 'per-order streams must be demultiplexed in memory'); | ||
| }); | ||
|
|
||
| test('history is served newest-first and per order', () async { | ||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
| await storage.addMessage('k2', message('a', Action.payInvoice, 3000)); | ||
| await storage.addMessage('k3', message('b', Action.newOrder, 2000)); | ||
|
|
||
| final history = await storage.getAllMessagesForOrderId('a'); | ||
|
|
||
| expect(history.map((m) => m.action), | ||
| [Action.payInvoice, Action.newOrder]); | ||
| expect(await storage.getLatestMessageById('b'), | ||
| isA<MostroMessage>().having((m) => m.id, 'id', 'b')); | ||
| }); | ||
|
|
||
| test('a cold start warms the index from disk', () async { | ||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
|
|
||
| // New storage over the same database: what a restart looks like. | ||
| final restarted = MostroStorage(db: storage.db); | ||
| final latest = await restarted.getLatestMessageById('a'); | ||
|
|
||
| expect(latest!.action, Action.newOrder); | ||
| expect(restarted.debugIndexSize, greaterThan(0)); | ||
| }); | ||
|
|
||
| test('deleting an order clears it from index and watchers', () async { | ||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
| final emissions = <MostroMessage?>[]; | ||
| final sub = storage.watchLatestMessage('a').listen(emissions.add); | ||
| addTearDown(sub.cancel); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| await storage.deleteAllMessagesByOrderId('a'); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| expect(emissions.last, isNull); | ||
| expect(await storage.getAllMessagesForOrderId('a'), isEmpty); | ||
| }); | ||
|
|
||
| test('duplicate keys are ignored without notifying watchers twice', | ||
| () async { | ||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
| var emissions = 0; | ||
| final sub = storage.watchLatestMessage('a').skip(1).listen((_) { | ||
| emissions++; | ||
| }); | ||
| addTearDown(sub.cancel); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| await storage.addMessage('k1', message('a', Action.newOrder, 1000)); | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
|
|
||
| expect(emissions, 0); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.