Skip to content

Commit 686c4b3

Browse files
Backport readTime changes
1 parent c35626a commit 686c4b3

File tree

9 files changed

+29
-32
lines changed

9 files changed

+29
-32
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class BundleLoader {
4141

4242
private ImmutableSortedMap<DocumentKey, MutableDocument> documents;
4343
private long bytesLoaded;
44-
@Nullable private DocumentKey currentDocument;
44+
@Nullable private BundledDocumentMetadata currentMetadata;
4545

4646
public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata) {
4747
this.bundleCallback = bundleCallback;
@@ -68,23 +68,27 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
6868
} else if (bundleElement instanceof BundledDocumentMetadata) {
6969
BundledDocumentMetadata bundledDocumentMetadata = (BundledDocumentMetadata) bundleElement;
7070
documentsMetadata.put(bundledDocumentMetadata.getKey(), bundledDocumentMetadata);
71-
currentDocument = bundledDocumentMetadata.getKey();
71+
currentMetadata = bundledDocumentMetadata;
7272
if (!((BundledDocumentMetadata) bundleElement).exists()) {
7373
documents =
7474
documents.insert(
7575
bundledDocumentMetadata.getKey(),
7676
MutableDocument.newNoDocument(
77-
bundledDocumentMetadata.getKey(), bundledDocumentMetadata.getReadTime()));
78-
currentDocument = null;
77+
bundledDocumentMetadata.getKey(), bundledDocumentMetadata.getReadTime())
78+
.setReadTime(bundledDocumentMetadata.getReadTime()));
79+
currentMetadata = null;
7980
}
8081
} else if (bundleElement instanceof BundleDocument) {
8182
BundleDocument bundleDocument = (BundleDocument) bundleElement;
82-
if (!bundleDocument.getKey().equals(currentDocument)) {
83+
if (currentMetadata == null || !bundleDocument.getKey().equals(currentMetadata.getKey())) {
8384
throw new IllegalArgumentException(
8485
"The document being added does not match the stored metadata.");
8586
}
86-
documents = documents.insert(bundleDocument.getKey(), bundleDocument.getDocument());
87-
currentDocument = null;
87+
documents =
88+
documents.insert(
89+
bundleDocument.getKey(),
90+
bundleDocument.getDocument().setReadTime(currentMetadata.getReadTime()));
91+
currentMetadata = null;
8892
}
8993

9094
bytesLoaded += byteSize;
@@ -103,7 +107,7 @@ public BundleLoader(BundleCallback bundleCallback, BundleMetadata bundleMetadata
103107
/** Applies the loaded documents and queries to local store. Returns the document view changes. */
104108
public ImmutableSortedMap<DocumentKey, Document> applyChanges() {
105109
Preconditions.checkArgument(
106-
currentDocument == null,
110+
currentMetadata == null,
107111
"Bundled documents end with a document metadata element instead of a document.");
108112
Preconditions.checkArgument(bundleMetadata.getBundleId() != null, "Bundle ID must be set");
109113
Preconditions.checkArgument(

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ public ImmutableSortedMap<DocumentKey, Document> applyRemoteEvent(RemoteEvent re
442442
}
443443
}
444444

445-
DocumentChangeResult result =
446-
populateDocumentChanges(documentUpdates, null, remoteEvent.getSnapshotVersion());
445+
DocumentChangeResult result = populateDocumentChanges(documentUpdates);
447446
Map<DocumentKey, MutableDocument> changedDocs = result.changedDocuments;
448447

449448
// HACK: The only reason we allow snapshot version NONE is so that we can synthesize
@@ -483,15 +482,9 @@ private DocumentChangeResult(
483482
* resorts to `globalVersion`.
484483
*
485484
* @param documents Documents to be applied.
486-
* @param documentVersions A DocumentKey-to-SnapshotVersion map if documents have their own read
487-
* time.
488-
* @param globalVersion A SnapshotVersion representing the read time if all documents have the
489-
* same read time.
490485
*/
491486
private DocumentChangeResult populateDocumentChanges(
492-
Map<DocumentKey, MutableDocument> documents,
493-
@Nullable Map<DocumentKey, SnapshotVersion> documentVersions,
494-
SnapshotVersion globalVersion) {
487+
Map<DocumentKey, MutableDocument> documents) {
495488
Map<DocumentKey, MutableDocument> changedDocs = new HashMap<>();
496489
List<DocumentKey> removedDocs = new ArrayList<>();
497490
Set<DocumentKey> conditionChanged = new HashSet<>();
@@ -504,8 +497,6 @@ private DocumentChangeResult populateDocumentChanges(
504497
DocumentKey key = entry.getKey();
505498
MutableDocument doc = entry.getValue();
506499
MutableDocument existingDoc = existingDocs.get(key);
507-
SnapshotVersion readTime =
508-
documentVersions != null ? documentVersions.get(key) : globalVersion;
509500
// Check if see if there is a existence state change for this document.
510501
if (doc.isFoundDocument() != existingDoc.isFoundDocument()) {
511502
conditionChanged.add(key);
@@ -524,9 +515,9 @@ private DocumentChangeResult populateDocumentChanges(
524515
|| (doc.getVersion().compareTo(existingDoc.getVersion()) == 0
525516
&& existingDoc.hasPendingWrites())) {
526517
hardAssert(
527-
!SnapshotVersion.NONE.equals(readTime),
518+
!SnapshotVersion.NONE.equals(doc.getReadTime()),
528519
"Cannot add a document when the remote version is zero");
529-
remoteDocuments.add(doc, readTime);
520+
remoteDocuments.add(doc, doc.getReadTime());
530521
changedDocs.put(key, doc);
531522
} else {
532523
Logger.debug(
@@ -713,7 +704,6 @@ public ImmutableSortedMap<DocumentKey, Document> applyBundledDocuments(
713704
() -> {
714705
ImmutableSortedSet<DocumentKey> documentKeys = DocumentKey.emptyKeySet();
715706
Map<DocumentKey, MutableDocument> documentMap = new HashMap<>();
716-
Map<DocumentKey, SnapshotVersion> versionMap = new HashMap<>();
717707

718708
for (Entry<DocumentKey, MutableDocument> entry : documents) {
719709
DocumentKey documentKey = entry.getKey();
@@ -723,14 +713,12 @@ public ImmutableSortedMap<DocumentKey, Document> applyBundledDocuments(
723713
documentKeys = documentKeys.insert(documentKey);
724714
}
725715
documentMap.put(documentKey, document);
726-
versionMap.put(documentKey, document.getVersion());
727716
}
728717

729718
targetCache.removeMatchingKeysForTargetId(umbrellaTargetData.getTargetId());
730719
targetCache.addMatchingKeys(documentKeys, umbrellaTargetData.getTargetId());
731720

732-
DocumentChangeResult result =
733-
populateDocumentChanges(documentMap, versionMap, SnapshotVersion.NONE);
721+
DocumentChangeResult result = populateDocumentChanges(documentMap);
734722
Map<DocumentKey, MutableDocument> changedDocs = result.changedDocuments;
735723
return localDocuments.getLocalViewOfDocuments(changedDocs, result.existenceChangedKeys);
736724
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void add(MutableDocument document, SnapshotVersion readTime) {
5353
hardAssert(
5454
!readTime.equals(SnapshotVersion.NONE),
5555
"Cannot add document to the RemoteDocumentCache with a read time of zero");
56-
docs = docs.insert(document.getKey(), document.mutableCopy().withReadTime(readTime));
56+
docs = docs.insert(document.getKey(), document.mutableCopy().setReadTime(readTime));
5757

5858
indexManager.addToCollectionParentIndex(document.getKey().getCollectionPath());
5959
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private MutableDocument decodeMaybeDocument(
260260
try {
261261
return serializer
262262
.decodeMaybeDocument(com.google.firebase.firestore.proto.MaybeDocument.parseFrom(bytes))
263-
.withReadTime(new SnapshotVersion(new Timestamp(readTimeSeconds, readTimeNanos)));
263+
.setReadTime(new SnapshotVersion(new Timestamp(readTimeSeconds, readTimeNanos)));
264264
} catch (InvalidProtocolBufferException e) {
265265
throw fail("MaybeDocument failed to parse: %s", e);
266266
}

firebase-firestore/src/main/java/com/google/firebase/firestore/model/MutableDocument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public MutableDocument setHasLocalMutations() {
163163
return this;
164164
}
165165

166-
public MutableDocument withReadTime(SnapshotVersion readTime) {
166+
public MutableDocument setReadTime(SnapshotVersion readTime) {
167167
this.readTime = readTime;
168168
return this;
169169
}

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/WatchChangeAggregator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ public RemoteEvent createRemoteEvent(SnapshotVersion snapshotVersion) {
264264
}
265265
}
266266

267+
for (MutableDocument document : pendingDocumentUpdates.values()) {
268+
document.setReadTime(snapshotVersion);
269+
}
270+
267271
RemoteEvent remoteEvent =
268272
new RemoteEvent(
269273
snapshotVersion,

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ public void testHoldsBackTransforms() {
13581358
acknowledgeMutationWithTransformResults(3, 1338, asList("bar", "foo"));
13591359
assertChanged(
13601360
doc("foo/bar", 3, map("sum", 1338, "array_union", asList("bar", "foo")))
1361-
.withReadTime(new SnapshotVersion(new Timestamp(0, 3000)))
1361+
.setReadTime(new SnapshotVersion(new Timestamp(0, 3000)))
13621362
.setHasCommittedMutations());
13631363
}
13641364

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteOverlayMigrationManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void tearDown() {
6565

6666
private void writeRemoteDocument(MutableDocument document) {
6767
// Set read time to update time.
68-
document = document.withReadTime(document.getVersion());
68+
document.setReadTime(document.getVersion());
6969
persistence.getRemoteDocumentCache().add(document, document.getReadTime());
7070
}
7171

firebase-firestore/src/testUtil/java/com/google/firebase/firestore/testutil/TestUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ public static MutableDocument doc(String key, long version, ObjectValue data) {
215215
}
216216

217217
public static MutableDocument doc(DocumentKey key, long version, ObjectValue data) {
218-
return MutableDocument.newFoundDocument(key, version(version), data);
218+
return MutableDocument.newFoundDocument(key, version(version), data)
219+
.setReadTime(version(version));
219220
}
220221

221222
public static MutableDocument deletedDoc(String key, long version) {
222-
return MutableDocument.newNoDocument(key(key), version(version));
223+
return MutableDocument.newNoDocument(key(key), version(version)).setReadTime(version(version));
223224
}
224225

225226
public static MutableDocument unknownDoc(String key, long version) {

0 commit comments

Comments
 (0)