use of com.google.firebase.firestore.local.QueryResult in project firebase-android-sdk by firebase.
the class FirestoreClient method getDocumentsFromLocalCache.
public Task<ViewSnapshot> getDocumentsFromLocalCache(Query query) {
this.verifyNotTerminated();
return asyncQueue.enqueue(() -> {
QueryResult queryResult = localStore.executeQuery(query, /* usePreviousResults= */
true);
View view = new View(query, queryResult.getRemoteKeys());
View.DocumentChanges viewDocChanges = view.computeDocChanges(queryResult.getDocuments());
return view.applyChanges(viewDocChanges).getSnapshot();
});
}
use of com.google.firebase.firestore.local.QueryResult in project firebase-android-sdk by firebase.
the class SyncEngine method emitNewSnapsAndNotifyLocalStore.
/**
* Computes a new snapshot from the changes and calls the registered callback with the new
* snapshot.
*/
private void emitNewSnapsAndNotifyLocalStore(ImmutableSortedMap<DocumentKey, Document> changes, @Nullable RemoteEvent remoteEvent) {
List<ViewSnapshot> newSnapshots = new ArrayList<>();
List<LocalViewChanges> documentChangesInAllViews = new ArrayList<>();
for (Map.Entry<Query, QueryView> entry : queryViewsByQuery.entrySet()) {
QueryView queryView = entry.getValue();
View view = queryView.getView();
View.DocumentChanges viewDocChanges = view.computeDocChanges(changes);
if (viewDocChanges.needsRefill()) {
// The query has a limit and some docs were removed/updated, so we need to re-run the query
// against the local store to make sure we didn't lose any good docs that had been past the
// limit.
QueryResult queryResult = localStore.executeQuery(queryView.getQuery(), /* usePreviousResults= */
false);
viewDocChanges = view.computeDocChanges(queryResult.getDocuments(), viewDocChanges);
}
TargetChange targetChange = remoteEvent == null ? null : remoteEvent.getTargetChanges().get(queryView.getTargetId());
ViewChange viewChange = queryView.getView().applyChanges(viewDocChanges, targetChange);
updateTrackedLimboDocuments(viewChange.getLimboChanges(), queryView.getTargetId());
if (viewChange.getSnapshot() != null) {
newSnapshots.add(viewChange.getSnapshot());
LocalViewChanges docChanges = LocalViewChanges.fromViewSnapshot(queryView.getTargetId(), viewChange.getSnapshot());
documentChangesInAllViews.add(docChanges);
}
}
syncEngineListener.onViewSnapshots(newSnapshots);
localStore.notifyLocalViewChanges(documentChangesInAllViews);
}
use of com.google.firebase.firestore.local.QueryResult in project firebase-android-sdk by firebase.
the class SyncEngine method initializeViewAndComputeSnapshot.
private ViewSnapshot initializeViewAndComputeSnapshot(Query query, int targetId) {
QueryResult queryResult = localStore.executeQuery(query, /* usePreviousResults= */
true);
SyncState currentTargetSyncState = SyncState.NONE;
TargetChange synthesizedCurrentChange = null;
// apply the sync state from those queries to the new query.
if (this.queriesByTarget.get(targetId) != null) {
Query mirrorQuery = this.queriesByTarget.get(targetId).get(0);
currentTargetSyncState = this.queryViewsByQuery.get(mirrorQuery).getView().getSyncState();
synthesizedCurrentChange = TargetChange.createSynthesizedTargetChangeForCurrentChange(currentTargetSyncState == SyncState.SYNCED);
}
// TODO(wuandy): Investigate if we can extract the logic of view change computation and
// update tracked limbo in one place, and have both emitNewSnapsAndNotifyLocalStore
// and here to call that.
View view = new View(query, queryResult.getRemoteKeys());
View.DocumentChanges viewDocChanges = view.computeDocChanges(queryResult.getDocuments());
ViewChange viewChange = view.applyChanges(viewDocChanges, synthesizedCurrentChange);
updateTrackedLimboDocuments(viewChange.getLimboChanges(), targetId);
QueryView queryView = new QueryView(query, targetId, view);
queryViewsByQuery.put(query, queryView);
if (!queriesByTarget.containsKey(targetId)) {
// Most likely there will only be one query mapping to a target, so construct the
// query list with capacity 1.
queriesByTarget.put(targetId, new ArrayList<>(1));
}
queriesByTarget.get(targetId).add(query);
return viewChange.getSnapshot();
}
Aggregations