Skip to content

Commit f629ebe

Browse files
authored
Rename QueryData to TargetData, and QueryCache to TargetCache. (#1087)
* Rename QueryData to TargetData, and QueryCache to TargetCache. * Delete mapping.txt
1 parent 7105d44 commit f629ebe

40 files changed

+817
-6257
lines changed

firebase-firestore/mapping.txt

Lines changed: 0 additions & 5446 deletions
This file was deleted.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import com.google.firebase.firestore.local.LocalStore;
3232
import com.google.firebase.firestore.local.LocalViewChanges;
3333
import com.google.firebase.firestore.local.LocalWriteResult;
34-
import com.google.firebase.firestore.local.QueryData;
3534
import com.google.firebase.firestore.local.QueryPurpose;
3635
import com.google.firebase.firestore.local.QueryResult;
3736
import com.google.firebase.firestore.local.ReferenceSet;
37+
import com.google.firebase.firestore.local.TargetData;
3838
import com.google.firebase.firestore.model.DocumentKey;
3939
import com.google.firebase.firestore.model.MaybeDocument;
4040
import com.google.firebase.firestore.model.NoDocument;
@@ -181,12 +181,12 @@ public int listen(Query query) {
181181
assertCallback("listen");
182182
hardAssert(!queryViewsByQuery.containsKey(query), "We already listen to query: %s", query);
183183

184-
QueryData queryData = localStore.allocateTarget(query.toTarget());
185-
ViewSnapshot viewSnapshot = initializeViewAndComputeSnapshot(query, queryData.getTargetId());
184+
TargetData targetData = localStore.allocateTarget(query.toTarget());
185+
ViewSnapshot viewSnapshot = initializeViewAndComputeSnapshot(query, targetData.getTargetId());
186186
syncEngineListener.onViewSnapshots(Collections.singletonList(viewSnapshot));
187187

188-
remoteStore.listen(queryData);
189-
return queryData.getTargetId();
188+
remoteStore.listen(targetData);
189+
return targetData.getTargetId();
190190
}
191191

192192
private ViewSnapshot initializeViewAndComputeSnapshot(Query query, int targetId) {
@@ -610,14 +610,14 @@ private void trackLimboChange(LimboDocumentChange change) {
610610
Logger.debug(TAG, "New document in limbo: %s", key);
611611
int limboTargetId = targetIdGenerator.nextId();
612612
Query query = Query.atPath(key.getPath());
613-
QueryData queryData =
614-
new QueryData(
613+
TargetData targetData =
614+
new TargetData(
615615
query.toTarget(),
616616
limboTargetId,
617617
ListenSequence.INVALID,
618618
QueryPurpose.LIMBO_RESOLUTION);
619619
limboResolutionsByTarget.put(limboTargetId, new LimboResolution(key));
620-
remoteStore.listen(queryData);
620+
remoteStore.listen(targetData);
621621
limboTargetsByKey.put(key, limboTargetId);
622622
}
623623
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* Generates monotonically increasing target IDs for sending targets to the watch stream.
2121
*
22-
* <p>The client constructs two generators, one for the query cache (via forQueryCache()), and one
22+
* <p>The client constructs two generators, one for the query cache (via forTargetCache()), and one
2323
* for limbo documents (via forSyncEngine()). These two generators produce non-overlapping IDs (by
2424
* using even and odd IDs respectively).
2525
*
@@ -36,7 +36,7 @@ public class TargetIdGenerator {
3636
*
3737
* @return A shared instance of TargetIdGenerator.
3838
*/
39-
public static TargetIdGenerator forQueryCache(int after) {
39+
public static TargetIdGenerator forTargetCache(int after) {
4040
TargetIdGenerator generator = new TargetIdGenerator(QUERY_CACHE_ID, after);
4141
// Make sure that the next call to `nextId()` returns the first value after 'after'.
4242
generator.nextId();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// TOOD(b/140938512): Drop SimpleQueryEngine and rename IndexFreeQueryEngine.
3131

3232
/**
33-
* A query engine that takes advantage of the target document mapping in the QueryCache. The
33+
* A query engine that takes advantage of the target document mapping in the TargetCache. The
3434
* IndexFreeQueryEngine optimizes query execution by only reading the documents that previously
3535
* matched a query plus any documents that were edited after the query was last listened to.
3636
*

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,25 @@ MutationBatch decodeMutationBatch(com.google.firebase.firestore.proto.WriteBatch
192192
return new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
193193
}
194194

195-
com.google.firebase.firestore.proto.Target encodeQueryData(QueryData queryData) {
195+
com.google.firebase.firestore.proto.Target encodeTargetData(TargetData targetData) {
196196
hardAssert(
197-
QueryPurpose.LISTEN.equals(queryData.getPurpose()),
197+
QueryPurpose.LISTEN.equals(targetData.getPurpose()),
198198
"Only queries with purpose %s may be stored, got %s",
199199
QueryPurpose.LISTEN,
200-
queryData.getPurpose());
200+
targetData.getPurpose());
201201

202202
com.google.firebase.firestore.proto.Target.Builder result =
203203
com.google.firebase.firestore.proto.Target.newBuilder();
204204

205205
result
206-
.setTargetId(queryData.getTargetId())
207-
.setLastListenSequenceNumber(queryData.getSequenceNumber())
206+
.setTargetId(targetData.getTargetId())
207+
.setLastListenSequenceNumber(targetData.getSequenceNumber())
208208
.setLastLimboFreeSnapshotVersion(
209-
rpcSerializer.encodeVersion(queryData.getLastLimboFreeSnapshotVersion()))
210-
.setSnapshotVersion(rpcSerializer.encodeVersion(queryData.getSnapshotVersion()))
211-
.setResumeToken(queryData.getResumeToken());
209+
rpcSerializer.encodeVersion(targetData.getLastLimboFreeSnapshotVersion()))
210+
.setSnapshotVersion(rpcSerializer.encodeVersion(targetData.getSnapshotVersion()))
211+
.setResumeToken(targetData.getResumeToken());
212212

213-
Target target = queryData.getTarget();
213+
Target target = targetData.getTarget();
214214
if (target.isDocumentQuery()) {
215215
result.setDocuments(rpcSerializer.encodeDocumentsTarget(target));
216216
} else {
@@ -220,7 +220,7 @@ com.google.firebase.firestore.proto.Target encodeQueryData(QueryData queryData)
220220
return result.build();
221221
}
222222

223-
QueryData decodeQueryData(com.google.firebase.firestore.proto.Target targetProto) {
223+
TargetData decodeTargetData(com.google.firebase.firestore.proto.Target targetProto) {
224224
int targetId = targetProto.getTargetId();
225225
SnapshotVersion version = rpcSerializer.decodeVersion(targetProto.getSnapshotVersion());
226226
SnapshotVersion lastLimboFreeSnapshotVersion =
@@ -242,7 +242,7 @@ QueryData decodeQueryData(com.google.firebase.firestore.proto.Target targetProto
242242
throw fail("Unknown targetType %d", targetProto.getTargetTypeCase());
243243
}
244244

245-
return new QueryData(
245+
return new TargetData(
246246
target,
247247
targetId,
248248
sequenceNumber,

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

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public final class LocalStore {
119119
private final ReferenceSet localViewReferences;
120120

121121
/** Maps a query to the data about that query. */
122-
private final QueryCache queryCache;
122+
private final TargetCache targetCache;
123123

124124
/** Maps a targetId to data about its query. */
125-
private final SparseArray<QueryData> queryDataByTarget;
125+
private final SparseArray<TargetData> queryDataByTarget;
126126

127127
/** Maps a target to its targetID. */
128128
private final Map<Target, Integer> targetIdByTarget;
@@ -134,8 +134,8 @@ public LocalStore(Persistence persistence, QueryEngine queryEngine, User initial
134134
hardAssert(
135135
persistence.isStarted(), "LocalStore was passed an unstarted persistence implementation");
136136
this.persistence = persistence;
137-
queryCache = persistence.getQueryCache();
138-
targetIdGenerator = TargetIdGenerator.forQueryCache(queryCache.getHighestTargetId());
137+
targetCache = persistence.getTargetCache();
138+
targetIdGenerator = TargetIdGenerator.forTargetCache(targetCache.getHighestTargetId());
139139
mutationQueue = persistence.getMutationQueue(initialUser);
140140
remoteDocuments = persistence.getRemoteDocumentCache();
141141
localDocuments =
@@ -320,7 +320,7 @@ public void setLastStreamToken(ByteString streamToken) {
320320
* buffer incoming snapshots from the backend).
321321
*/
322322
public SnapshotVersion getLastRemoteSnapshotVersion() {
323-
return queryCache.getLastRemoteSnapshotVersion();
323+
return targetCache.getLastRemoteSnapshotVersion();
324324
}
325325

326326
/**
@@ -345,29 +345,29 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyRemoteEvent(RemoteEve
345345
int targetId = boxedTargetId;
346346
TargetChange change = entry.getValue();
347347

348-
QueryData oldQueryData = queryDataByTarget.get(targetId);
349-
if (oldQueryData == null) {
348+
TargetData oldTargetData = queryDataByTarget.get(targetId);
349+
if (oldTargetData == null) {
350350
// We don't update the remote keys if the query is not active. This ensures that
351351
// we persist the updated query data along with the updated assignment.
352352
continue;
353353
}
354354

355-
queryCache.removeMatchingKeys(change.getRemovedDocuments(), targetId);
356-
queryCache.addMatchingKeys(change.getAddedDocuments(), targetId);
355+
targetCache.removeMatchingKeys(change.getRemovedDocuments(), targetId);
356+
targetCache.addMatchingKeys(change.getAddedDocuments(), targetId);
357357

358358
ByteString resumeToken = change.getResumeToken();
359359
// Update the resume token if the change includes one.
360360
if (!resumeToken.isEmpty()) {
361-
QueryData newQueryData =
362-
oldQueryData
361+
TargetData newTargetData =
362+
oldTargetData
363363
.withResumeToken(resumeToken, remoteEvent.getSnapshotVersion())
364364
.withSequenceNumber(sequenceNumber);
365-
queryDataByTarget.put(targetId, newQueryData);
365+
queryDataByTarget.put(targetId, newTargetData);
366366

367367
// Update the query data if there are target changes (or if sufficient time has
368368
// passed since the last update).
369-
if (shouldPersistQueryData(oldQueryData, newQueryData, change)) {
370-
queryCache.updateQueryData(newQueryData);
369+
if (shouldPersistTargetData(oldTargetData, newTargetData, change)) {
370+
targetCache.updateTargetData(newTargetData);
371371
}
372372
}
373373
}
@@ -420,45 +420,45 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> applyRemoteEvent(RemoteEve
420420
// HACK: The only reason we allow snapshot version NONE is so that we can synthesize
421421
// remote events when we get permission denied errors while trying to resolve the
422422
// state of a locally cached document that is in limbo.
423-
SnapshotVersion lastRemoteVersion = queryCache.getLastRemoteSnapshotVersion();
423+
SnapshotVersion lastRemoteVersion = targetCache.getLastRemoteSnapshotVersion();
424424
if (!remoteVersion.equals(SnapshotVersion.NONE)) {
425425
hardAssert(
426426
remoteVersion.compareTo(lastRemoteVersion) >= 0,
427427
"Watch stream reverted to previous snapshot?? (%s < %s)",
428428
remoteVersion,
429429
lastRemoteVersion);
430-
queryCache.setLastRemoteSnapshotVersion(remoteVersion);
430+
targetCache.setLastRemoteSnapshotVersion(remoteVersion);
431431
}
432432

433433
return localDocuments.getLocalViewOfDocuments(changedDocs);
434434
});
435435
}
436436

437437
/**
438-
* Returns true if the newQueryData should be persisted during an update of an active target.
439-
* QueryData should always be persisted when a target is being released and should not call this
438+
* Returns true if the newTargetData should be persisted during an update of an active target.
439+
* TargetData should always be persisted when a target is being released and should not call this
440440
* function.
441441
*
442-
* <p>While the target is active, QueryData updates can be omitted when nothing about the target
442+
* <p>While the target is active, TargetData updates can be omitted when nothing about the target
443443
* has changed except metadata like the resume token or snapshot version. Occasionally it's worth
444444
* the extra write to prevent these values from getting too stale after a crash, but this doesn't
445445
* have to be too frequent.
446446
*/
447-
private static boolean shouldPersistQueryData(
448-
QueryData oldQueryData, QueryData newQueryData, TargetChange change) {
447+
private static boolean shouldPersistTargetData(
448+
TargetData oldTargetData, TargetData newTargetData, TargetChange change) {
449449
hardAssert(
450-
!newQueryData.getResumeToken().isEmpty(),
450+
!newTargetData.getResumeToken().isEmpty(),
451451
"Attempted to persist query data with empty resume token");
452452

453453
// Always persist query data if we don't already have a resume token.
454-
if (oldQueryData.getResumeToken().isEmpty()) return true;
454+
if (oldTargetData.getResumeToken().isEmpty()) return true;
455455

456456
// Don't allow resume token changes to be buffered indefinitely. This allows us to be reasonably
457457
// up-to-date after a crash and avoids needing to loop over all active queries on shutdown.
458458
// Especially in the browser we may not get time to do anything interesting while the current
459459
// tab is closing.
460-
long newSeconds = newQueryData.getSnapshotVersion().getTimestamp().getSeconds();
461-
long oldSeconds = oldQueryData.getSnapshotVersion().getTimestamp().getSeconds();
460+
long newSeconds = newTargetData.getSnapshotVersion().getTimestamp().getSeconds();
461+
long oldSeconds = oldTargetData.getSnapshotVersion().getTimestamp().getSeconds();
462462
long timeDelta = newSeconds - oldSeconds;
463463
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) return true;
464464

@@ -489,17 +489,17 @@ public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
489489
localViewReferences.removeReferences(removed, targetId);
490490

491491
if (!viewChange.isFromCache()) {
492-
QueryData queryData = queryDataByTarget.get(targetId);
492+
TargetData targetData = queryDataByTarget.get(targetId);
493493
hardAssert(
494-
queryData != null,
494+
targetData != null,
495495
"Can't set limbo-free snapshot version for unknown target: %s",
496496
targetId);
497497

498498
// Advance the last limbo free snapshot version
499-
SnapshotVersion lastLimboFreeSnapshotVersion = queryData.getSnapshotVersion();
500-
QueryData updatedQueryData =
501-
queryData.withLastLimboFreeSnapshotVersion(lastLimboFreeSnapshotVersion);
502-
queryDataByTarget.put(targetId, updatedQueryData);
499+
SnapshotVersion lastLimboFreeSnapshotVersion = targetData.getSnapshotVersion();
500+
TargetData updatedTargetData =
501+
targetData.withLastLimboFreeSnapshotVersion(lastLimboFreeSnapshotVersion);
502+
queryDataByTarget.put(targetId, updatedTargetData);
503503
}
504504
}
505505
});
@@ -526,12 +526,12 @@ public MaybeDocument readDocument(DocumentKey key) {
526526
* GC'd. A query must be allocated in the local store before the store can be used to manage its
527527
* view.
528528
*
529-
* <p>Allocating an already allocated target will return the existing @{code QueryData} for that
529+
* <p>Allocating an already allocated target will return the existing @{code TargetData} for that
530530
* target.
531531
*/
532-
public QueryData allocateTarget(Target target) {
532+
public TargetData allocateTarget(Target target) {
533533
int targetId;
534-
QueryData cached = queryCache.getQueryData(target);
534+
TargetData cached = targetCache.getTargetData(target);
535535
if (cached != null) {
536536
// This query has been listened to previously, so reuse the previous targetID.
537537
// TODO: freshen last accessed date?
@@ -543,12 +543,12 @@ public QueryData allocateTarget(Target target) {
543543
() -> {
544544
holder.targetId = targetIdGenerator.nextId();
545545
holder.cached =
546-
new QueryData(
546+
new TargetData(
547547
target,
548548
holder.targetId,
549549
persistence.getReferenceDelegate().getCurrentSequenceNumber(),
550550
QueryPurpose.LISTEN);
551-
queryCache.addQueryData(holder.cached);
551+
targetCache.addTargetData(holder.cached);
552552
});
553553
targetId = holder.targetId;
554554
cached = holder.cached;
@@ -562,22 +562,22 @@ public QueryData allocateTarget(Target target) {
562562
}
563563

564564
/**
565-
* Returns the QueryData as seen by the LocalStore, including updates that may have not yet been
566-
* persisted to the QueryCache.
565+
* Returns the TargetData as seen by the LocalStore, including updates that may have not yet been
566+
* persisted to the TargetCache.
567567
*/
568568
@VisibleForTesting
569569
@Nullable
570-
QueryData getQueryData(Target target) {
570+
TargetData getTargetData(Target target) {
571571
Integer targetId = targetIdByTarget.get(target);
572572
if (targetId != null) {
573573
return queryDataByTarget.get(targetId);
574574
}
575-
return queryCache.getQueryData(target);
575+
return targetCache.getTargetData(target);
576576
}
577577

578578
/** Mutable state for the transaction in allocateQuery. */
579579
private static class AllocateQueryHolder {
580-
QueryData cached;
580+
TargetData cached;
581581
int targetId;
582582
}
583583

@@ -590,8 +590,8 @@ public void releaseTarget(int targetId) {
590590
persistence.runTransaction(
591591
"Release target",
592592
() -> {
593-
QueryData queryData = queryDataByTarget.get(targetId);
594-
hardAssert(queryData != null, "Tried to release nonexistent target: %s", targetId);
593+
TargetData targetData = queryDataByTarget.get(targetId);
594+
hardAssert(targetData != null, "Tried to release nonexistent target: %s", targetId);
595595

596596
// References for documents sent via Watch are automatically removed when we delete a
597597
// query's target data from the reference delegate. Since this does not remove references
@@ -604,9 +604,9 @@ public void releaseTarget(int targetId) {
604604
}
605605

606606
// Note: This also updates the query cache
607-
persistence.getReferenceDelegate().removeTarget(queryData);
607+
persistence.getReferenceDelegate().removeTarget(targetData);
608608
queryDataByTarget.remove(targetId);
609-
targetIdByTarget.remove(queryData.getTarget());
609+
targetIdByTarget.remove(targetData.getTarget());
610610
});
611611
}
612612

@@ -618,13 +618,13 @@ public void releaseTarget(int targetId) {
618618
* query execution.
619619
*/
620620
public QueryResult executeQuery(Query query, boolean usePreviousResults) {
621-
QueryData queryData = getQueryData(query.toTarget());
621+
TargetData targetData = getTargetData(query.toTarget());
622622
SnapshotVersion lastLimboFreeSnapshotVersion = SnapshotVersion.NONE;
623623
ImmutableSortedSet<DocumentKey> remoteKeys = DocumentKey.emptyKeySet();
624624

625-
if (queryData != null) {
626-
lastLimboFreeSnapshotVersion = queryData.getLastLimboFreeSnapshotVersion();
627-
remoteKeys = this.queryCache.getMatchingKeysForTargetId(queryData.getTargetId());
625+
if (targetData != null) {
626+
lastLimboFreeSnapshotVersion = targetData.getLastLimboFreeSnapshotVersion();
627+
remoteKeys = this.targetCache.getMatchingKeysForTargetId(targetData.getTargetId());
628628
}
629629

630630
ImmutableSortedMap<DocumentKey, Document> documents =
@@ -640,7 +640,7 @@ public QueryResult executeQuery(Query query, boolean usePreviousResults) {
640640
* table.
641641
*/
642642
public ImmutableSortedSet<DocumentKey> getRemoteDocumentKeys(int targetId) {
643-
return queryCache.getMatchingKeysForTargetId(targetId);
643+
return targetCache.getMatchingKeysForTargetId(targetId);
644644
}
645645

646646
private void applyWriteToRemoteDocuments(MutationBatchResult batchResult) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
public interface LruDelegate {
2525

26-
/** Enumerates all the targets in the QueryCache. */
27-
void forEachTarget(Consumer<QueryData> consumer);
26+
/** Enumerates all the targets in the TargetCache. */
27+
void forEachTarget(Consumer<TargetData> consumer);
2828

2929
long getSequenceNumberCount();
3030

0 commit comments

Comments
 (0)