-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix(sql_connect): Refactor to improve caching performance for large result sets #18430
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright 2026, the Chromium project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:firebase_data_connect/firebase_data_connect.dart'; | ||
| import 'package:firebase_data_connect_example/generated/movies.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| import 'query_e2e.dart'; // For deleteAllMovies | ||
|
|
||
| void runCacheTests() { | ||
| group( | ||
| '$FirebaseDataConnect cache', | ||
| () { | ||
| setUp(() async { | ||
| final dataConnect = MoviesConnector.instance.dataConnect; | ||
| // Temporarily disable cache during database cleanup to prevent populating it | ||
| dataConnect.cacheSettings = null; | ||
| dataConnect.useDataConnectEmulator('127.0.0.1', 9399); | ||
|
|
||
| await deleteAllMovies(); | ||
|
|
||
| // Enable cache with memory storage and a large TTL for testing. | ||
| dataConnect.cacheSettings = CacheSettings( | ||
| storage: CacheStorage.memory, | ||
| maxAge: const Duration(minutes: 5), | ||
| ); | ||
| // Re-apply emulator to force cache manager recreation with new settings | ||
| dataConnect.useDataConnectEmulator('127.0.0.1', 9399); | ||
| }); | ||
|
|
||
| testWidgets('test cache flow: serverOnly, cacheOnly, preferCache', | ||
| (WidgetTester tester) async { | ||
| final moviesConnector = MoviesConnector.instance; | ||
|
|
||
| // 1. Initial query with preferCache should result in server hit because cache is empty. | ||
| final res1 = await moviesConnector.listMovies().ref().execute( | ||
| fetchPolicy: QueryFetchPolicy.preferCache, | ||
| ); | ||
| expect(res1.source, DataSource.server); | ||
| expect(res1.data.movies, isEmpty); | ||
|
|
||
| // 2. Second query with preferCache should result in cache hit. | ||
| final res2 = await moviesConnector.listMovies().ref().execute( | ||
| fetchPolicy: QueryFetchPolicy.preferCache, | ||
| ); | ||
| expect(res2.source, DataSource.cache); | ||
| expect(res2.data.movies, isEmpty); | ||
|
|
||
| // 3. Mutation to add a movie. This goes to server. | ||
| await moviesConnector | ||
| .createMovie( | ||
| genre: 'Sci-Fi', | ||
| title: 'Inception', | ||
| releaseYear: 2010, | ||
| ) | ||
| .ref() | ||
| .execute(); | ||
|
|
||
| // 4. Query with cacheOnly should still return empty list (cache hit, stale data). | ||
| final res3 = await moviesConnector.listMovies().ref().execute( | ||
| fetchPolicy: QueryFetchPolicy.cacheOnly, | ||
| ); | ||
| expect(res3.source, DataSource.cache); | ||
| expect(res3.data.movies, isEmpty); | ||
|
|
||
| // 5. Query with serverOnly should return the new movie (server hit) and update cache. | ||
| final res4 = await moviesConnector.listMovies().ref().execute( | ||
| fetchPolicy: QueryFetchPolicy.serverOnly, | ||
| ); | ||
| expect(res4.source, DataSource.server); | ||
| expect(res4.data.movies.length, 1); | ||
| expect(res4.data.movies[0].title, 'Inception'); | ||
|
|
||
| // 6. Query with cacheOnly should now return the new movie (cache hit). | ||
| final res5 = await moviesConnector.listMovies().ref().execute( | ||
| fetchPolicy: QueryFetchPolicy.cacheOnly, | ||
| ); | ||
| expect(res5.source, DataSource.cache); | ||
| expect(res5.data.movies.length, 1); | ||
| expect(res5.data.movies[0].title, 'Inception'); | ||
| }); | ||
| }, | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,19 +20,14 @@ class DeleteAllMovieDataVariablesBuilder { | |
|
|
||
| @immutable | ||
| class DeleteAllMovieDataData { | ||
| final int directedByDeleteMany; | ||
| final int movieDeleteMany; | ||
| final int personDeleteMany; | ||
| final int directedBy_deleteMany; | ||
| final int movie_deleteMany; | ||
| final int person_deleteMany; | ||
|
Comment on lines
+23
to
+25
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what change results in the renaming of the generated file?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were some changes done recently to support batch mutations. I suspect this might have caused the code generator to change.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took a look at the gql operation definitions. The example project got regenerated and this correctly matches the gql definitions. Its possible the original names had been manually tweaked in the generated code. |
||
| DeleteAllMovieDataData.fromJson(dynamic json) | ||
| : directedByDeleteMany = nativeFromJson<int>( | ||
| json['directedBy_deleteMany'], | ||
| ), | ||
| movieDeleteMany = nativeFromJson<int>( | ||
| json['movie_deleteMany'], | ||
| ), | ||
| personDeleteMany = nativeFromJson<int>( | ||
| json['person_deleteMany'], | ||
| ); | ||
| : directedBy_deleteMany = | ||
| nativeFromJson<int>(json['directedBy_deleteMany']), | ||
| movie_deleteMany = nativeFromJson<int>(json['movie_deleteMany']), | ||
| person_deleteMany = nativeFromJson<int>(json['person_deleteMany']); | ||
| @override | ||
| bool operator ==(Object other) { | ||
| if (identical(this, other)) { | ||
|
|
@@ -43,29 +38,29 @@ class DeleteAllMovieDataData { | |
| } | ||
|
|
||
| final DeleteAllMovieDataData otherTyped = other as DeleteAllMovieDataData; | ||
| return directedByDeleteMany == otherTyped.directedByDeleteMany && | ||
| movieDeleteMany == otherTyped.movieDeleteMany && | ||
| personDeleteMany == otherTyped.personDeleteMany; | ||
| return directedBy_deleteMany == otherTyped.directedBy_deleteMany && | ||
| movie_deleteMany == otherTyped.movie_deleteMany && | ||
| person_deleteMany == otherTyped.person_deleteMany; | ||
| } | ||
|
|
||
| @override | ||
| int get hashCode => Object.hashAll([ | ||
| directedByDeleteMany.hashCode, | ||
| movieDeleteMany.hashCode, | ||
| personDeleteMany.hashCode, | ||
| directedBy_deleteMany.hashCode, | ||
| movie_deleteMany.hashCode, | ||
| person_deleteMany.hashCode | ||
| ]); | ||
|
|
||
| Map<String, dynamic> toJson() { | ||
| Map<String, dynamic> json = {}; | ||
| json['directedBy_deleteMany'] = nativeToJson<int>(directedByDeleteMany); | ||
| json['movie_deleteMany'] = nativeToJson<int>(movieDeleteMany); | ||
| json['person_deleteMany'] = nativeToJson<int>(personDeleteMany); | ||
| json['directedBy_deleteMany'] = nativeToJson<int>(directedBy_deleteMany); | ||
| json['movie_deleteMany'] = nativeToJson<int>(movie_deleteMany); | ||
| json['person_deleteMany'] = nativeToJson<int>(person_deleteMany); | ||
| return json; | ||
| } | ||
|
|
||
| const DeleteAllMovieDataData({ | ||
| required this.directedByDeleteMany, | ||
| required this.movieDeleteMany, | ||
| required this.personDeleteMany, | ||
| DeleteAllMovieDataData({ | ||
| required this.directedBy_deleteMany, | ||
| required this.movie_deleteMany, | ||
| required this.person_deleteMany, | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.