15
15
package com .google .firebase .firestore .bundle ;
16
16
17
17
import static com .google .firebase .firestore .model .DocumentCollections .emptyMaybeDocumentMap ;
18
- import static com .google .firebase .firestore .util .Assert .hardAssert ;
19
18
20
19
import androidx .annotation .Nullable ;
21
20
import com .google .firebase .database .collection .ImmutableSortedMap ;
24
23
import com .google .firebase .firestore .model .DocumentKey ;
25
24
import com .google .firebase .firestore .model .MaybeDocument ;
26
25
import com .google .firebase .firestore .model .NoDocument ;
26
+ import com .google .firebase .firestore .util .Preconditions ;
27
27
import java .util .ArrayList ;
28
28
import java .util .HashMap ;
29
29
import java .util .List ;
@@ -43,7 +43,6 @@ public class BundleLoader {
43
43
44
44
private ImmutableSortedMap <DocumentKey , MaybeDocument > documents ;
45
45
private long bytesLoaded ;
46
- private LoadBundleTaskProgress .TaskState taskState ;
47
46
@ Nullable private DocumentKey currentDocument ;
48
47
49
48
public BundleLoader (
@@ -58,7 +57,6 @@ public BundleLoader(
58
57
this .queries = new ArrayList <>();
59
58
this .documents = emptyMaybeDocumentMap ();
60
59
this .documentsMetadata = new HashMap <>();
61
- this .taskState = LoadBundleTaskProgress .TaskState .RUNNING ;
62
60
}
63
61
64
62
/**
@@ -68,9 +66,8 @@ public BundleLoader(
68
66
* null.
69
67
*/
70
68
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." );
74
71
75
72
boolean updateProgress = false ;
76
73
@@ -94,7 +91,8 @@ public BundleLoader(
94
91
} else if (bundleElement instanceof BundleDocument ) {
95
92
BundleDocument bundleDocument = (BundleDocument ) bundleElement ;
96
93
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." );
98
96
}
99
97
documents = documents .insert (bundleDocument .getKey (), bundleDocument .getDocument ());
100
98
updateProgress = true ;
@@ -103,46 +101,29 @@ public BundleLoader(
103
101
104
102
bytesLoaded += byteSize ;
105
103
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
-
124
104
return updateProgress
125
105
? 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 )
127
112
: null ;
128
113
}
129
114
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
-
141
115
/** Applies the loaded documents and queries to local store. Returns the document view changes. */
142
116
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
+
146
127
ImmutableSortedMap <DocumentKey , MaybeDocument > changes =
147
128
bundleListener .applyBundledDocuments (documents , bundleMetadata .getBundleId ());
148
129
@@ -158,12 +139,12 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyChanges() {
158
139
159
140
private Map <String , ImmutableSortedSet <DocumentKey >> getQueryDocumentMapping () {
160
141
Map <String , ImmutableSortedSet <DocumentKey >> queryDocumentMap = new HashMap <>();
142
+ for (NamedQuery namedQuery : queries ) {
143
+ queryDocumentMap .put (namedQuery .getName (), DocumentKey .emptyKeySet ());
144
+ }
161
145
for (BundledDocumentMetadata metadata : documentsMetadata .values ()) {
162
146
for (String query : metadata .getQueries ()) {
163
147
ImmutableSortedSet <DocumentKey > matchingKeys = queryDocumentMap .get (query );
164
- if (matchingKeys == null ) {
165
- matchingKeys = DocumentKey .emptyKeySet ();
166
- }
167
148
queryDocumentMap .put (query , matchingKeys .insert (metadata .getKey ()));
168
149
}
169
150
}
0 commit comments