Repository navigation
fix(cloud_firestore): chunk large Android query snapshots #19005
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
Open
SelaseKay
wants to merge
6
commits into
main
Choose a base branch
from
fix/firestore-chunk-android-query-snapshots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
326d20a
fix(firestore): chunk Android query snapshot transport
ben-milanko 5645721
Merge remote-tracking branch 'upstream/main' into codex/firestore-sna…
ben-milanko e6bf25d
Merge current FlutterFire main and preserve chunked query listener li…
ben-milanko 26fa09d
Merge current FlutterFire main while preserving query transport regre…
ben-milanko 2433ae6
Merge remote-tracking branch 'origin/main' into fix/firestore-chunk-a…
SelaseKay 941160f
fix(cloud_firestore): refine chunked snapshot transport and preserve …
SelaseKay 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
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
56 changes: 56 additions & 0 deletions
56
...restore/android/src/main/java/io/flutter/plugins/firebase/firestore/PigeonCodecSizer.java
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,56 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package io.flutter.plugins.firebase.firestore; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
|
|
||
| public final class PigeonCodecSizer { | ||
| private PigeonCodecSizer() {} | ||
|
|
||
| public static int encodedMessageSize(Object message) { | ||
| if (message == null) { | ||
| return 0; | ||
| } | ||
| CountingByteArrayOutputStream stream = new CountingByteArrayOutputStream(); | ||
| GeneratedAndroidFirebaseFirestore.PigeonCodec.INSTANCE.writeValue(stream, message); | ||
| return stream.size(); | ||
| } | ||
|
|
||
| public static int encodedSuccessEnvelopeSize(Object message) { | ||
| CountingByteArrayOutputStream stream = new CountingByteArrayOutputStream(); | ||
| // StandardMethodCodec prefixes success envelopes with a single 0 byte. | ||
| stream.write(0); | ||
| GeneratedAndroidFirebaseFirestore.PigeonCodec.INSTANCE.writeValue(stream, message); | ||
| return stream.size(); | ||
| } | ||
|
|
||
| private static final class CountingByteArrayOutputStream extends ByteArrayOutputStream { | ||
| private int totalBytes; | ||
|
|
||
| CountingByteArrayOutputStream() { | ||
| super(0); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(int b) { | ||
| totalBytes++; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) { | ||
| if (len < 0 || off < 0 || off > b.length || off + len > b.length) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| totalBytes += len; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return totalBytes; | ||
| } | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
.../java/io/flutter/plugins/firebase/firestore/streamhandler/QuerySnapshotChunkProtocol.java
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,78 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package io.flutter.plugins.firebase.firestore.streamhandler; | ||
|
|
||
| import io.flutter.plugins.firebase.firestore.GeneratedAndroidFirebaseFirestore; | ||
| import io.flutter.plugins.firebase.firestore.PigeonCodecSizer; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| final class QuerySnapshotChunkProtocol { | ||
| // Kept in sync with query_snapshot_chunk_assembler.dart. | ||
| static final String LISTEN_CHUNKED_ARG_KEY = "firestoreQuerySnapshotChunked"; | ||
| private static final String CHUNK_MARKER_KEY = "firestoreQuerySnapshotChunk"; | ||
| private static final String SNAPSHOT_ID_KEY = "snapshotId"; | ||
| private static final String KIND_KEY = "kind"; | ||
| private static final String PAYLOAD_KEY = "payload"; | ||
| private static final String DOCUMENT_COUNT_KEY = "documentCount"; | ||
| private static final String DOCUMENT_CHANGE_COUNT_KEY = "documentChangeCount"; | ||
|
|
||
| static final int START_KIND = 0; | ||
| static final int DOCUMENTS_KIND = 1; | ||
| static final int DOCUMENT_CHANGES_KIND = 2; | ||
| static final int END_KIND = 3; | ||
|
|
||
| static final int MAX_ENCODED_ENVELOPE_BYTES = 512 * 1024; | ||
|
|
||
| private QuerySnapshotChunkProtocol() {} | ||
|
|
||
| static boolean supportsChunking(Object arguments) { | ||
| if (!(arguments instanceof Map<?, ?>)) { | ||
| return false; | ||
| } | ||
| return Boolean.TRUE.equals(((Map<?, ?>) arguments).get(LISTEN_CHUNKED_ARG_KEY)); | ||
| } | ||
|
|
||
| static Map<String, Object> start( | ||
| long snapshotId, | ||
| int documentCount, | ||
| int documentChangeCount, | ||
| GeneratedAndroidFirebaseFirestore.InternalSnapshotMetadata metadata) { | ||
| Map<String, Object> message = base(snapshotId, START_KIND); | ||
| message.put(PAYLOAD_KEY, metadata); | ||
| message.put(DOCUMENT_COUNT_KEY, documentCount); | ||
| message.put(DOCUMENT_CHANGE_COUNT_KEY, documentChangeCount); | ||
| return message; | ||
| } | ||
|
|
||
| static Map<String, Object> itemChunk(long snapshotId, int kind, List<?> items) { | ||
| Map<String, Object> message = base(snapshotId, kind); | ||
| message.put(PAYLOAD_KEY, items); | ||
| return message; | ||
| } | ||
|
|
||
| static Map<String, Object> end(long snapshotId) { | ||
| return base(snapshotId, END_KIND); | ||
| } | ||
|
|
||
| static int encodedMessageSize(Object message) { | ||
| return PigeonCodecSizer.encodedMessageSize(message); | ||
| } | ||
|
|
||
| static int encodedEnvelopeSize(Object message) { | ||
| return PigeonCodecSizer.encodedSuccessEnvelopeSize(message); | ||
| } | ||
|
|
||
| private static Map<String, Object> base(long snapshotId, int kind) { | ||
| Map<String, Object> message = new LinkedHashMap<>(); | ||
| message.put(CHUNK_MARKER_KEY, true); | ||
| message.put(SNAPSHOT_ID_KEY, snapshotId); | ||
| message.put(KIND_KEY, kind); | ||
| return message; | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
...c/main/java/io/flutter/plugins/firebase/firestore/streamhandler/QuerySnapshotChunker.java
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,69 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package io.flutter.plugins.firebase.firestore.streamhandler; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.function.Function; | ||
| import java.util.function.ToIntFunction; | ||
|
|
||
| final class QuerySnapshotChunker<S, T> { | ||
| private final Iterator<S> source; | ||
| private final Function<S, T> converter; | ||
| private final ToIntFunction<T> itemSize; | ||
| private final int targetBytes; | ||
| private final ArrayDeque<T> pending = new ArrayDeque<>(); | ||
|
|
||
| QuerySnapshotChunker( | ||
| Iterator<S> source, Function<S, T> converter, ToIntFunction<T> itemSize, int targetBytes) { | ||
| if (targetBytes <= 0) { | ||
| throw new IllegalArgumentException("targetBytes must be positive"); | ||
| } | ||
| this.source = source; | ||
| this.converter = converter; | ||
| this.itemSize = itemSize; | ||
| this.targetBytes = targetBytes; | ||
| } | ||
|
|
||
| boolean hasNext() { | ||
| return !pending.isEmpty() || source.hasNext(); | ||
| } | ||
|
|
||
| List<T> nextChunk(ToIntFunction<List<T>> exactEnvelopeSize) { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException("No snapshot items remain"); | ||
| } | ||
|
|
||
| ArrayList<T> chunk = new ArrayList<>(); | ||
| int estimatedBytes = 0; | ||
|
|
||
| while (hasNext()) { | ||
| T item = pending.isEmpty() ? converter.apply(source.next()) : pending.removeFirst(); | ||
| int encodedItemBytes = Math.max(0, itemSize.applyAsInt(item)); | ||
|
|
||
| if (!chunk.isEmpty() && estimatedBytes + encodedItemBytes > targetBytes) { | ||
| pending.addFirst(item); | ||
| break; | ||
| } | ||
|
|
||
| chunk.add(item); | ||
| estimatedBytes += encodedItemBytes; | ||
| if (estimatedBytes >= targetBytes) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| while (chunk.size() > 1 && exactEnvelopeSize.applyAsInt(chunk) > targetBytes) { | ||
| pending.addFirst(chunk.remove(chunk.size() - 1)); | ||
| } | ||
|
|
||
| return chunk; | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential integer overflow when checking array bounds in
write(byte[] b, int off, int len), it is safer to use subtraction instead of addition. For example,off + len > b.lengthcan overflow ifoffandlenare large, whereaslen > b.length - offis safe from overflow.