Skip to content

Commit 2b7812e

Browse files
Simplify MemoryRemoteDocumentCache (#3195)
1 parent 97bac87 commit 2b7812e

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryRemoteDocumentCache.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.firebase.firestore.model.DocumentCollections.emptyMutableDocumentMap;
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
1919

20-
import android.util.Pair;
2120
import androidx.annotation.NonNull;
2221
import com.google.firebase.database.collection.ImmutableSortedMap;
2322
import com.google.firebase.firestore.core.Query;
@@ -34,7 +33,7 @@
3433
final class MemoryRemoteDocumentCache implements RemoteDocumentCache {
3534

3635
/** Underlying cache of documents and their read times. */
37-
private ImmutableSortedMap<DocumentKey, Pair<MutableDocument, SnapshotVersion>> docs;
36+
private ImmutableSortedMap<DocumentKey, MutableDocument> docs;
3837
/** Manages the collection group index. */
3938
private IndexManager indexManager;
4039
/** The latest read time of any document in the cache. */
@@ -56,7 +55,7 @@ public void add(MutableDocument document, SnapshotVersion readTime) {
5655
hardAssert(
5756
!readTime.equals(SnapshotVersion.NONE),
5857
"Cannot add document to the RemoteDocumentCache with a read time of zero");
59-
docs = docs.insert(document.getKey(), new Pair<>(document.clone(), readTime));
58+
docs = docs.insert(document.getKey(), document.clone().withReadTime(readTime));
6059
latestReadTime = readTime.compareTo(latestReadTime) > 0 ? readTime : latestReadTime;
6160

6261
indexManager.addToCollectionParentIndex(document.getKey().getPath().popLast());
@@ -69,8 +68,8 @@ public void remove(DocumentKey key) {
6968

7069
@Override
7170
public MutableDocument get(DocumentKey key) {
72-
Pair<MutableDocument, SnapshotVersion> entry = docs.get(key);
73-
return entry != null ? entry.first.clone() : MutableDocument.newInvalidDocument(key);
71+
MutableDocument doc = docs.get(key);
72+
return doc != null ? doc.clone() : MutableDocument.newInvalidDocument(key);
7473
}
7574

7675
@Override
@@ -94,24 +93,22 @@ public ImmutableSortedMap<DocumentKey, MutableDocument> getAllDocumentsMatchingQ
9493
// we need to match the query against.
9594
ResourcePath queryPath = query.getPath();
9695
DocumentKey prefix = DocumentKey.fromPath(queryPath.append(""));
97-
Iterator<Map.Entry<DocumentKey, Pair<MutableDocument, SnapshotVersion>>> iterator =
98-
docs.iteratorFrom(prefix);
96+
Iterator<Map.Entry<DocumentKey, MutableDocument>> iterator = docs.iteratorFrom(prefix);
9997

10098
while (iterator.hasNext()) {
101-
Map.Entry<DocumentKey, Pair<MutableDocument, SnapshotVersion>> entry = iterator.next();
99+
Map.Entry<DocumentKey, MutableDocument> entry = iterator.next();
102100

103101
DocumentKey key = entry.getKey();
104102
if (!queryPath.isPrefixOf(key.getPath())) {
105103
break;
106104
}
107105

108-
MutableDocument doc = entry.getValue().first;
106+
MutableDocument doc = entry.getValue();
109107
if (!doc.isFoundDocument()) {
110108
continue;
111109
}
112110

113-
SnapshotVersion readTime = entry.getValue().second;
114-
if (IndexOffset.create(readTime, doc.getKey()).compareTo(offset) <= 0) {
111+
if (IndexOffset.create(doc.getReadTime(), doc.getKey()).compareTo(offset) <= 0) {
115112
continue;
116113
}
117114

@@ -149,7 +146,7 @@ private class DocumentIterable implements Iterable<MutableDocument> {
149146
@NonNull
150147
@Override
151148
public Iterator<MutableDocument> iterator() {
152-
Iterator<Map.Entry<DocumentKey, Pair<MutableDocument, SnapshotVersion>>> iterator =
149+
Iterator<Map.Entry<DocumentKey, MutableDocument>> iterator =
153150
MemoryRemoteDocumentCache.this.docs.iterator();
154151
return new Iterator<MutableDocument>() {
155152
@Override
@@ -159,7 +156,7 @@ public boolean hasNext() {
159156

160157
@Override
161158
public MutableDocument next() {
162-
return iterator.next().getValue().first;
159+
return iterator.next().getValue();
163160
}
164161
};
165162
}

0 commit comments

Comments
 (0)