Skip to content

Commit 86d8db5

Browse files
Update with changes from latest PR
1 parent fd696d2 commit 86d8db5

File tree

2 files changed

+26
-43
lines changed

2 files changed

+26
-43
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleLoader.java

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.firebase.firestore.bundle;
1616

1717
import static com.google.firebase.firestore.model.DocumentCollections.emptyMaybeDocumentMap;
18-
import static com.google.firebase.firestore.util.Assert.hardAssert;
1918

2019
import androidx.annotation.Nullable;
2120
import com.google.firebase.database.collection.ImmutableSortedMap;
@@ -24,6 +23,7 @@
2423
import com.google.firebase.firestore.model.DocumentKey;
2524
import com.google.firebase.firestore.model.MaybeDocument;
2625
import com.google.firebase.firestore.model.NoDocument;
26+
import com.google.firebase.firestore.util.Preconditions;
2727
import java.util.ArrayList;
2828
import java.util.HashMap;
2929
import java.util.List;
@@ -43,7 +43,6 @@ public class BundleLoader {
4343

4444
private ImmutableSortedMap<DocumentKey, MaybeDocument> documents;
4545
private long bytesLoaded;
46-
private LoadBundleTaskProgress.TaskState taskState;
4746
@Nullable private DocumentKey currentDocument;
4847

4948
public BundleLoader(
@@ -58,7 +57,6 @@ public BundleLoader(
5857
this.queries = new ArrayList<>();
5958
this.documents = emptyMaybeDocumentMap();
6059
this.documentsMetadata = new HashMap<>();
61-
this.taskState = LoadBundleTaskProgress.TaskState.RUNNING;
6260
}
6361

6462
/**
@@ -68,9 +66,8 @@ public BundleLoader(
6866
* null.
6967
*/
7068
public @Nullable LoadBundleTaskProgress addElement(BundleElement bundleElement, long byteSize) {
71-
if (bundleElement instanceof BundleMetadata) {
72-
return fail("Unexpected bundle metadata element.");
73-
}
69+
Preconditions.checkArgument(
70+
!(bundleElement instanceof BundleMetadata), "Unexpected bundle metadata element.");
7471

7572
boolean updateProgress = false;
7673

@@ -94,7 +91,8 @@ public BundleLoader(
9491
} else if (bundleElement instanceof BundleDocument) {
9592
BundleDocument bundleDocument = (BundleDocument) bundleElement;
9693
if (!bundleDocument.getKey().equals(currentDocument)) {
97-
return fail("The document being added does not match the stored metadata.");
94+
throw new IllegalArgumentException(
95+
"The document being added does not match the stored metadata.");
9896
}
9997
documents = documents.insert(bundleDocument.getKey(), bundleDocument.getDocument());
10098
updateProgress = true;
@@ -103,46 +101,29 @@ public BundleLoader(
103101

104102
bytesLoaded += byteSize;
105103

106-
if (bytesLoaded == totalBytes) {
107-
if (documents.size() != totalDocuments) {
108-
return fail(
109-
String.format(
110-
"Expected %s documents, but loaded %s.", totalDocuments, documents.size()));
111-
}
112-
if (currentDocument != null) {
113-
return fail(
114-
"Bundled documents end with a document metadata element instead of a document.");
115-
}
116-
if (bundleMetadata.getBundleId() == null) {
117-
return fail("Bundle ID must be set");
118-
}
119-
120-
taskState = LoadBundleTaskProgress.TaskState.SUCCESS;
121-
updateProgress = true;
122-
}
123-
124104
return updateProgress
125105
? new LoadBundleTaskProgress(
126-
documents.size(), totalDocuments, bytesLoaded, totalBytes, null, taskState)
106+
documents.size(),
107+
totalDocuments,
108+
bytesLoaded,
109+
totalBytes,
110+
null,
111+
LoadBundleTaskProgress.TaskState.RUNNING)
127112
: null;
128113
}
129114

130-
private LoadBundleTaskProgress fail(String error) {
131-
taskState = LoadBundleTaskProgress.TaskState.ERROR;
132-
return new LoadBundleTaskProgress(
133-
documents.size(),
134-
totalDocuments,
135-
bytesLoaded,
136-
totalBytes,
137-
new IllegalArgumentException(error),
138-
LoadBundleTaskProgress.TaskState.ERROR);
139-
}
140-
141115
/** Applies the loaded documents and queries to local store. Returns the document view changes. */
142116
public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
143-
hardAssert(
144-
taskState.equals(LoadBundleTaskProgress.TaskState.SUCCESS),
145-
"Expected successful task, but was " + taskState);
117+
Preconditions.checkArgument(
118+
currentDocument == null,
119+
"Bundled documents end with a document metadata element instead of a document.");
120+
Preconditions.checkArgument(bundleMetadata.getBundleId() != null, "Bundle ID must be set");
121+
Preconditions.checkArgument(
122+
documents.size() == totalDocuments,
123+
"Expected %s documents, but loaded %s.",
124+
totalDocuments,
125+
documents.size());
126+
146127
ImmutableSortedMap<DocumentKey, MaybeDocument> changes =
147128
bundleListener.applyBundledDocuments(documents, bundleMetadata.getBundleId());
148129

@@ -158,12 +139,12 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
158139

159140
private Map<String, ImmutableSortedSet<DocumentKey>> getQueryDocumentMapping() {
160141
Map<String, ImmutableSortedSet<DocumentKey>> queryDocumentMap = new HashMap<>();
142+
for (NamedQuery namedQuery : queries) {
143+
queryDocumentMap.put(namedQuery.getName(), DocumentKey.emptyKeySet());
144+
}
161145
for (BundledDocumentMetadata metadata : documentsMetadata.values()) {
162146
for (String query : metadata.getQueries()) {
163147
ImmutableSortedSet<DocumentKey> matchingKeys = queryDocumentMap.get(query);
164-
if (matchingKeys == null) {
165-
matchingKeys = DocumentKey.emptyKeySet();
166-
}
167148
queryDocumentMap.put(query, matchingKeys.insert(metadata.getKey()));
168149
}
169150
}

firebase-firestore/src/main/java/com/google/firebase/firestore/util/ByteBufferInputStream.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public ByteBufferInputStream(ByteBuffer buf) {
2727
this.buffer = buf;
2828
}
2929

30+
@Override
3031
public int read() {
3132
if (!buffer.hasRemaining()) {
3233
return -1;
@@ -37,6 +38,7 @@ public int read() {
3738
return buffer.get() & 0xFF;
3839
}
3940

41+
@Override
4042
public int read(@NonNull byte[] b, int off, int len) throws IOException {
4143
if (!buffer.hasRemaining()) {
4244
return -1;

0 commit comments

Comments
 (0)