Skip to content

Simplify MemoryRemoteDocumentCache #3195

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

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.google.firebase.firestore.model.DocumentCollections.emptyMutableDocumentMap;
import static com.google.firebase.firestore.util.Assert.hardAssert;

import android.util.Pair;
import androidx.annotation.NonNull;
import com.google.firebase.database.collection.ImmutableSortedMap;
import com.google.firebase.firestore.core.Query;
Expand All @@ -34,7 +33,7 @@
final class MemoryRemoteDocumentCache implements RemoteDocumentCache {

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

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

@Override
public MutableDocument get(DocumentKey key) {
Pair<MutableDocument, SnapshotVersion> entry = docs.get(key);
return entry != null ? entry.first.clone() : MutableDocument.newInvalidDocument(key);
MutableDocument doc = docs.get(key);
return doc != null ? doc.clone() : MutableDocument.newInvalidDocument(key);
}

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

while (iterator.hasNext()) {
Map.Entry<DocumentKey, Pair<MutableDocument, SnapshotVersion>> entry = iterator.next();
Map.Entry<DocumentKey, MutableDocument> entry = iterator.next();

DocumentKey key = entry.getKey();
if (!queryPath.isPrefixOf(key.getPath())) {
break;
}

MutableDocument doc = entry.getValue().first;
MutableDocument doc = entry.getValue();
if (!doc.isFoundDocument()) {
continue;
}

SnapshotVersion readTime = entry.getValue().second;
if (IndexOffset.create(readTime, doc.getKey()).compareTo(offset) <= 0) {
if (IndexOffset.create(doc.getReadTime(), doc.getKey()).compareTo(offset) <= 0) {
continue;
}

Expand Down Expand Up @@ -149,7 +146,7 @@ private class DocumentIterable implements Iterable<MutableDocument> {
@NonNull
@Override
public Iterator<MutableDocument> iterator() {
Iterator<Map.Entry<DocumentKey, Pair<MutableDocument, SnapshotVersion>>> iterator =
Iterator<Map.Entry<DocumentKey, MutableDocument>> iterator =
MemoryRemoteDocumentCache.this.docs.iterator();
return new Iterator<MutableDocument>() {
@Override
Expand All @@ -159,7 +156,7 @@ public boolean hasNext() {

@Override
public MutableDocument next() {
return iterator.next().getValue().first;
return iterator.next().getValue();
}
};
}
Expand Down