Skip to content

Commit c55cd93

Browse files
Add a Document interface
1 parent a45f274 commit c55cd93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+375
-289
lines changed

firebase-firestore/ktx/src/test/java/com/google/firebase/firestore/TestUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.firebase.firestore.core.DocumentViewChange;
2424
import com.google.firebase.firestore.core.DocumentViewChange.Type;
2525
import com.google.firebase.firestore.core.ViewSnapshot;
26+
import com.google.firebase.firestore.model.Document;
2627
import com.google.firebase.firestore.model.DocumentKey;
2728
import com.google.firebase.firestore.model.DocumentSet;
2829
import com.google.firebase.firestore.model.MutableDocument;
@@ -42,8 +43,7 @@ public static FirebaseFirestore firestore() {
4243
public static DocumentSnapshot documentSnapshot(
4344
String path, Map<String, Object> data, boolean isFromCache) {
4445
if (data == null) {
45-
return DocumentSnapshot.fromNoDocument(
46-
FIRESTORE, key(path), isFromCache, /*hasPendingWrites=*/ false);
46+
return DocumentSnapshot.fromNoDocument(FIRESTORE, key(path), isFromCache);
4747
} else {
4848
return DocumentSnapshot.fromDocument(
4949
FIRESTORE, doc(path, 1L, data), isFromCache, /*hasPendingWrites=*/ false);
@@ -72,7 +72,7 @@ public static QuerySnapshot querySnapshot(
7272
Map<String, ObjectValue> docsToAdd,
7373
boolean hasPendingWrites,
7474
boolean isFromCache) {
75-
DocumentSet oldDocuments = docSet(MutableDocument.keyComparator());
75+
DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR);
7676
ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
7777
for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
7878
String docKey = path + "/" + pair.getKey();
@@ -83,7 +83,7 @@ public static QuerySnapshot querySnapshot(
8383
mutatedKeys = mutatedKeys.insert(key(docKey));
8484
}
8585
}
86-
DocumentSet newDocuments = docSet(MutableDocument.keyComparator());
86+
DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR);
8787
List<DocumentViewChange> documentChanges = new ArrayList<>();
8888
for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
8989
String docKey = path + "/" + pair.getKey();

firebase-firestore/ktx/src/test/java/com/google/firebase/firestore/testutil/TestUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.firebase.firestore.UserDataReader;
1919
import com.google.firebase.firestore.core.Query;
2020
import com.google.firebase.firestore.model.DatabaseId;
21+
import com.google.firebase.firestore.model.Document;
2122
import com.google.firebase.firestore.model.DocumentKey;
2223
import com.google.firebase.firestore.model.DocumentSet;
2324
import com.google.firebase.firestore.model.MutableDocument;
@@ -74,8 +75,7 @@ public static MutableDocument doc(String key, long version, ObjectValue data) {
7475
return new MutableDocument(key(key)).setFoundDocument(version(version), data);
7576
}
7677

77-
public static DocumentSet docSet(
78-
Comparator<MutableDocument> comparator, MutableDocument... documents) {
78+
public static DocumentSet docSet(Comparator<Document> comparator, MutableDocument... documents) {
7979
DocumentSet set = DocumentSet.emptySet(comparator);
8080
for (MutableDocument document : documents) {
8181
set = set.add(document);

firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentChange.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import androidx.annotation.VisibleForTesting;
2222
import com.google.firebase.firestore.core.DocumentViewChange;
2323
import com.google.firebase.firestore.core.ViewSnapshot;
24+
import com.google.firebase.firestore.model.Document;
2425
import com.google.firebase.firestore.model.DocumentSet;
25-
import com.google.firebase.firestore.model.MutableDocument;
2626
import java.util.ArrayList;
2727
import java.util.List;
2828

@@ -98,7 +98,7 @@ public Type getType() {
9898
*
9999
* @return A snapshot of the new data (for {@link DocumentChange.Type#ADDED} or {@link
100100
* DocumentChange.Type#MODIFIED}) or the removed data (for {@link
101-
* DocumentChange.Type.REMOVED}).
101+
* DocumentChange.Type#REMOVED}).
102102
*/
103103
@NonNull
104104
public QueryDocumentSnapshot getDocument() {
@@ -132,9 +132,9 @@ static List<DocumentChange> changesFromSnapshot(
132132
// changes on the first snapshot are adds so there are also no metadata-only changes to filter
133133
// out.
134134
int index = 0;
135-
MutableDocument lastDoc = null;
135+
Document lastDoc = null;
136136
for (DocumentViewChange change : snapshot.getChanges()) {
137-
MutableDocument document = change.getDocument();
137+
Document document = change.getDocument();
138138
QueryDocumentSnapshot documentSnapshot =
139139
QueryDocumentSnapshot.fromDocument(
140140
firestore,
@@ -159,7 +159,7 @@ static List<DocumentChange> changesFromSnapshot(
159159
&& change.getType() == DocumentViewChange.Type.METADATA) {
160160
continue;
161161
}
162-
MutableDocument document = change.getDocument();
162+
Document document = change.getDocument();
163163
QueryDocumentSnapshot documentSnapshot =
164164
QueryDocumentSnapshot.fromDocument(
165165
firestore,

firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentReference.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.firebase.firestore.core.UserData.ParsedSetData;
3535
import com.google.firebase.firestore.core.UserData.ParsedUpdateData;
3636
import com.google.firebase.firestore.core.ViewSnapshot;
37+
import com.google.firebase.firestore.model.Document;
3738
import com.google.firebase.firestore.model.DocumentKey;
3839
import com.google.firebase.firestore.model.MutableDocument;
3940
import com.google.firebase.firestore.model.ResourcePath;
@@ -278,8 +279,8 @@ public Task<DocumentSnapshot> get(@NonNull Source source) {
278279
.getDocumentFromLocalCache(key)
279280
.continueWith(
280281
Executors.DIRECT_EXECUTOR,
281-
(Task<MutableDocument> task) -> {
282-
MutableDocument doc = task.getResult();
282+
(Task<Document> task) -> {
283+
Document doc = task.getResult();
283284
boolean hasPendingWrites = doc != null && doc.hasLocalMutations();
284285
return new DocumentSnapshot(
285286
firestore, key, doc, /*isFromCache=*/ true, hasPendingWrites);
@@ -489,7 +490,7 @@ private ListenerRegistration addSnapshotListenerInternal(
489490
snapshot.getDocuments().size() <= 1,
490491
"Too many documents returned on a document query");
491492

492-
MutableDocument document = snapshot.getDocuments().getDocument(key);
493+
Document document = snapshot.getDocuments().getDocument(key);
493494
DocumentSnapshot documentSnapshot;
494495
if (document != null) {
495496
boolean hasPendingWrites = snapshot.getMutatedKeys().contains(document.getKey());
@@ -499,8 +500,7 @@ private ListenerRegistration addSnapshotListenerInternal(
499500
} else {
500501
// We don't raise `hasPendingWrites` for deleted documents.
501502
documentSnapshot =
502-
DocumentSnapshot.fromNoDocument(
503-
firestore, key, snapshot.isFromCache(), /* hasPendingWrites= */ false);
503+
DocumentSnapshot.fromNoDocument(firestore, key, snapshot.isFromCache());
504504
}
505505
userListener.onEvent(documentSnapshot, null);
506506
};

firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentSnapshot.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.Nullable;
2121
import com.google.firebase.Timestamp;
22+
import com.google.firebase.firestore.model.Document;
2223
import com.google.firebase.firestore.model.DocumentKey;
23-
import com.google.firebase.firestore.model.MutableDocument;
2424
import com.google.firebase.firestore.util.CustomClassMapper;
2525
import com.google.firestore.v1.Value;
2626
import java.util.Date;
@@ -73,14 +73,14 @@ public enum ServerTimestampBehavior {
7373
private final DocumentKey key;
7474

7575
/** Is {@code null} if the document doesn't exist */
76-
private final @Nullable MutableDocument doc;
76+
private final @Nullable Document doc;
7777

7878
private final SnapshotMetadata metadata;
7979

8080
DocumentSnapshot(
8181
FirebaseFirestore firestore,
8282
DocumentKey key,
83-
@Nullable MutableDocument doc,
83+
@Nullable Document doc,
8484
boolean isFromCache,
8585
boolean hasPendingWrites) {
8686
this.firestore = checkNotNull(firestore);
@@ -90,16 +90,13 @@ public enum ServerTimestampBehavior {
9090
}
9191

9292
static DocumentSnapshot fromDocument(
93-
FirebaseFirestore firestore,
94-
MutableDocument doc,
95-
boolean fromCache,
96-
boolean hasPendingWrites) {
93+
FirebaseFirestore firestore, Document doc, boolean fromCache, boolean hasPendingWrites) {
9794
return new DocumentSnapshot(firestore, doc.getKey(), doc, fromCache, hasPendingWrites);
9895
}
9996

10097
static DocumentSnapshot fromNoDocument(
101-
FirebaseFirestore firestore, DocumentKey key, boolean fromCache, boolean hasPendingWrites) {
102-
return new DocumentSnapshot(firestore, key, null, fromCache, hasPendingWrites);
98+
FirebaseFirestore firestore, DocumentKey key, boolean fromCache) {
99+
return new DocumentSnapshot(firestore, key, null, fromCache, /* hasPendingWrites= */ false);
103100
}
104101

105102
/** @return The id of the document. */
@@ -120,7 +117,7 @@ public boolean exists() {
120117
}
121118

122119
@Nullable
123-
MutableDocument getDocument() {
120+
Document getDocument() {
124121
return doc;
125122
}
126123

@@ -537,7 +534,8 @@ public boolean equals(@Nullable Object obj) {
537534
public int hashCode() {
538535
int hash = firestore.hashCode();
539536
hash = hash * 31 + key.hashCode();
540-
hash = hash * 31 + (doc != null ? doc.hashCode() : 0);
537+
hash = hash * 31 + (doc != null ? doc.getKey().hashCode() : 0);
538+
hash = hash * 31 + (doc != null ? doc.getData().hashCode() : 0);
541539
hash = hash * 31 + metadata.hashCode();
542540
return hash;
543541
}

firebase-firestore/src/main/java/com/google/firebase/firestore/Query.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import com.google.firebase.firestore.core.OrderBy;
3737
import com.google.firebase.firestore.core.QueryListener;
3838
import com.google.firebase.firestore.core.ViewSnapshot;
39+
import com.google.firebase.firestore.model.Document;
3940
import com.google.firebase.firestore.model.DocumentKey;
40-
import com.google.firebase.firestore.model.MutableDocument;
4141
import com.google.firebase.firestore.model.ResourcePath;
4242
import com.google.firebase.firestore.model.ServerTimestamps;
4343
import com.google.firebase.firestore.model.Values;
@@ -826,7 +826,7 @@ private Bound boundFromDocumentSnapshot(
826826
+ methodName
827827
+ "().");
828828
}
829-
MutableDocument document = snapshot.getDocument();
829+
Document document = snapshot.getDocument();
830830
List<Value> components = new ArrayList<>();
831831

832832
// Because people expect to continue/end a query at the exact document provided, we need to

firebase-firestore/src/main/java/com/google/firebase/firestore/QueryDocumentSnapshot.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.Nullable;
21+
import com.google.firebase.firestore.model.Document;
2122
import com.google.firebase.firestore.model.DocumentKey;
22-
import com.google.firebase.firestore.model.MutableDocument;
2323
import com.google.firebase.firestore.util.Assert;
2424
import java.util.Map;
2525

@@ -42,17 +42,14 @@ public class QueryDocumentSnapshot extends DocumentSnapshot {
4242
private QueryDocumentSnapshot(
4343
FirebaseFirestore firestore,
4444
DocumentKey key,
45-
@Nullable MutableDocument doc,
45+
@Nullable Document doc,
4646
boolean isFromCache,
4747
boolean hasPendingWrites) {
4848
super(firestore, key, doc, isFromCache, hasPendingWrites);
4949
}
5050

5151
static QueryDocumentSnapshot fromDocument(
52-
FirebaseFirestore firestore,
53-
MutableDocument doc,
54-
boolean fromCache,
55-
boolean hasPendingWrites) {
52+
FirebaseFirestore firestore, Document doc, boolean fromCache, boolean hasPendingWrites) {
5653
return new QueryDocumentSnapshot(firestore, doc.getKey(), doc, fromCache, hasPendingWrites);
5754
}
5855

firebase-firestore/src/main/java/com/google/firebase/firestore/QuerySnapshot.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.Nullable;
2121
import com.google.firebase.firestore.core.ViewSnapshot;
22-
import com.google.firebase.firestore.model.MutableDocument;
22+
import com.google.firebase.firestore.model.Document;
2323
import java.util.ArrayList;
2424
import java.util.Collections;
2525
import java.util.Iterator;
@@ -55,9 +55,9 @@ public class QuerySnapshot implements Iterable<QueryDocumentSnapshot> {
5555
}
5656

5757
private class QuerySnapshotIterator implements Iterator<QueryDocumentSnapshot> {
58-
private final Iterator<MutableDocument> it;
58+
private final Iterator<Document> it;
5959

60-
QuerySnapshotIterator(Iterator<MutableDocument> it) {
60+
QuerySnapshotIterator(Iterator<Document> it) {
6161
this.it = it;
6262
}
6363

@@ -133,7 +133,7 @@ public List<DocumentChange> getDocumentChanges(@NonNull MetadataChanges metadata
133133
@NonNull
134134
public List<DocumentSnapshot> getDocuments() {
135135
List<DocumentSnapshot> res = new ArrayList<>(snapshot.getDocuments().size());
136-
for (MutableDocument doc : snapshot.getDocuments()) {
136+
for (Document doc : snapshot.getDocuments()) {
137137
res.add(convertDocument(doc));
138138
}
139139
return res;
@@ -186,7 +186,7 @@ public <T> List<T> toObjects(
186186
return res;
187187
}
188188

189-
private QueryDocumentSnapshot convertDocument(MutableDocument document) {
189+
private QueryDocumentSnapshot convertDocument(Document document) {
190190
return QueryDocumentSnapshot.fromDocument(
191191
firestore,
192192
document,

firebase-firestore/src/main/java/com/google/firebase/firestore/Transaction.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,10 @@ private Task<DocumentSnapshot> getAsync(DocumentReference documentRef) {
200200
MutableDocument doc = docs.get(0);
201201
if (doc.isFoundDocument()) {
202202
return DocumentSnapshot.fromDocument(
203-
firestore,
204-
(MutableDocument) doc,
205-
/*fromCache=*/ false,
206-
/*hasPendingWrites=*/ false);
203+
firestore, doc, /*fromCache=*/ false, /*hasPendingWrites=*/ false);
207204
} else if (doc.isNoDocument()) {
208205
return DocumentSnapshot.fromNoDocument(
209-
firestore, doc.getKey(), /*fromCache=*/ false, /*hasPendingWrites=*/ false);
206+
firestore, doc.getKey(), /*fromCache=*/ false);
210207
} else {
211208
throw fail(
212209
"BatchGetDocumentsRequest returned unexpected document type: "

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.firebase.database.collection.ImmutableSortedMap;
1818
import com.google.firebase.database.collection.ImmutableSortedSet;
19+
import com.google.firebase.firestore.model.Document;
1920
import com.google.firebase.firestore.model.DocumentKey;
2021
import com.google.firebase.firestore.model.MutableDocument;
2122

@@ -26,7 +27,7 @@ public interface BundleCallback {
2627
*
2728
* <p>LocalDocuments are re-calculated if there are remaining mutations in the queue.
2829
*/
29-
ImmutableSortedMap<DocumentKey, MutableDocument> applyBundledDocuments(
30+
ImmutableSortedMap<DocumentKey, Document> applyBundledDocuments(
3031
ImmutableSortedMap<DocumentKey, MutableDocument> documents, String bundleId);
3132

3233
/** Saves the given NamedQuery to local persistence. */

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
package com.google.firebase.firestore.bundle;
1616

17-
import static com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap;
17+
import static com.google.firebase.firestore.model.DocumentCollections.emptyMutableDocumentMap;
1818

1919
import androidx.annotation.Nullable;
2020
import com.google.firebase.database.collection.ImmutableSortedMap;
2121
import com.google.firebase.database.collection.ImmutableSortedSet;
2222
import com.google.firebase.firestore.LoadBundleTaskProgress;
23+
import com.google.firebase.firestore.model.Document;
2324
import com.google.firebase.firestore.model.DocumentKey;
2425
import com.google.firebase.firestore.model.MutableDocument;
2526
import com.google.firebase.firestore.util.Preconditions;
@@ -46,7 +47,7 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
4647
this.bundleCallback = bundleCallback;
4748
this.bundleMetadata = bundleMetadata;
4849
this.queries = new ArrayList<>();
49-
this.documents = emptyDocumentMap();
50+
this.documents = emptyMutableDocumentMap();
5051
this.documentsMetadata = new HashMap<>();
5152
}
5253

@@ -100,7 +101,7 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
100101
}
101102

102103
/** Applies the loaded documents and queries to local store. Returns the document view changes. */
103-
public ImmutableSortedMap<DocumentKey, MutableDocument> applyChanges() {
104+
public ImmutableSortedMap<DocumentKey, Document> applyChanges() {
104105
Preconditions.checkArgument(
105106
currentDocument == null,
106107
"Bundled documents end with a document metadata element instead of a document.");
@@ -111,7 +112,7 @@ public ImmutableSortedMap<DocumentKey, MutableDocument> applyChanges() {
111112
bundleMetadata.getTotalDocuments(),
112113
documents.size());
113114

114-
ImmutableSortedMap<DocumentKey, MutableDocument> changes =
115+
ImmutableSortedMap<DocumentKey, Document> changes =
115116
bundleCallback.applyBundledDocuments(documents, bundleMetadata.getBundleId());
116117

117118
Map<String, ImmutableSortedSet<DocumentKey>> queryDocumentMap = getQueryDocumentMapping();

firebase-firestore/src/main/java/com/google/firebase/firestore/core/ArrayContainsAnyFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import static com.google.firebase.firestore.util.Assert.hardAssert;
1818

19+
import com.google.firebase.firestore.model.Document;
1920
import com.google.firebase.firestore.model.FieldPath;
20-
import com.google.firebase.firestore.model.MutableDocument;
2121
import com.google.firebase.firestore.model.Values;
2222
import com.google.firestore.v1.Value;
2323

@@ -29,7 +29,7 @@ public class ArrayContainsAnyFilter extends FieldFilter {
2929
}
3030

3131
@Override
32-
public boolean matches(MutableDocument doc) {
32+
public boolean matches(Document doc) {
3333
Value other = doc.getField(getField());
3434
if (!Values.isArray(other)) {
3535
return false;

firebase-firestore/src/main/java/com/google/firebase/firestore/core/ArrayContainsFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
package com.google.firebase.firestore.core;
1616

17+
import com.google.firebase.firestore.model.Document;
1718
import com.google.firebase.firestore.model.FieldPath;
18-
import com.google.firebase.firestore.model.MutableDocument;
1919
import com.google.firebase.firestore.model.Values;
2020
import com.google.firestore.v1.Value;
2121

@@ -26,7 +26,7 @@ public class ArrayContainsFilter extends FieldFilter {
2626
}
2727

2828
@Override
29-
public boolean matches(MutableDocument doc) {
29+
public boolean matches(Document doc) {
3030
Value other = doc.getField(getField());
3131
return Values.isArray(other) && Values.contains(other.getArrayValue(), getValue());
3232
}

0 commit comments

Comments
 (0)