Search in sources :

Example 1 with SyncState

use of com.google.firebase.firestore.core.ViewSnapshot.SyncState in project firebase-android-sdk by firebase.

the class View method applyChanges.

/**
 * Updates the view with the given ViewDocumentChanges and updates limbo docs and sync state from
 * the given (optional) target change.
 *
 * @param docChanges The set of changes to make to the view's docs.
 * @param targetChange A target change to apply for computing limbo docs and sync state.
 * @return A new ViewChange with the given docs, changes, and sync state.
 */
public ViewChange applyChanges(DocumentChanges docChanges, TargetChange targetChange) {
    hardAssert(!docChanges.needsRefill, "Cannot apply changes that need a refill");
    DocumentSet oldDocumentSet = documentSet;
    documentSet = docChanges.documentSet;
    mutatedKeys = docChanges.mutatedKeys;
    // Sort changes based on type and query comparator.
    List<DocumentViewChange> viewChanges = docChanges.changeSet.getChanges();
    Collections.sort(viewChanges, (DocumentViewChange o1, DocumentViewChange o2) -> {
        int typeComp = compareIntegers(View.changeTypeOrder(o1), View.changeTypeOrder(o2));
        o1.getType().compareTo(o2.getType());
        if (typeComp != 0) {
            return typeComp;
        }
        return query.comparator().compare(o1.getDocument(), o2.getDocument());
    });
    applyTargetChange(targetChange);
    List<LimboDocumentChange> limboDocumentChanges = updateLimboDocuments();
    boolean synced = limboDocuments.size() == 0 && current;
    SyncState newSyncState = synced ? SyncState.SYNCED : SyncState.LOCAL;
    boolean syncStatedChanged = newSyncState != syncState;
    syncState = newSyncState;
    ViewSnapshot snapshot = null;
    if (viewChanges.size() != 0 || syncStatedChanged) {
        boolean fromCache = newSyncState == SyncState.LOCAL;
        snapshot = new ViewSnapshot(query, docChanges.documentSet, oldDocumentSet, viewChanges, fromCache, docChanges.mutatedKeys, syncStatedChanged, /* excludesMetadataChanges= */
        false);
    }
    return new ViewChange(snapshot, limboDocumentChanges);
}
Also used : SyncState(com.google.firebase.firestore.core.ViewSnapshot.SyncState) DocumentSet(com.google.firebase.firestore.model.DocumentSet)

Example 2 with SyncState

use of com.google.firebase.firestore.core.ViewSnapshot.SyncState 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();
}
Also used : QueryResult(com.google.firebase.firestore.local.QueryResult) SyncState(com.google.firebase.firestore.core.ViewSnapshot.SyncState) TargetChange(com.google.firebase.firestore.remote.TargetChange)

Aggregations

SyncState (com.google.firebase.firestore.core.ViewSnapshot.SyncState)2 QueryResult (com.google.firebase.firestore.local.QueryResult)1 DocumentSet (com.google.firebase.firestore.model.DocumentSet)1 TargetChange (com.google.firebase.firestore.remote.TargetChange)1