Search in sources :

Example 1 with LocalViewChanges

use of com.google.firebase.firestore.local.LocalViewChanges in project firebase-android-sdk by firebase.

the class TestUtil method viewChanges.

public static LocalViewChanges viewChanges(int targetId, boolean fromCache, List<String> addedKeys, List<String> removedKeys) {
    ImmutableSortedSet<DocumentKey> added = DocumentKey.emptyKeySet();
    for (String keyPath : addedKeys) {
        added = added.insert(key(keyPath));
    }
    ImmutableSortedSet<DocumentKey> removed = DocumentKey.emptyKeySet();
    for (String keyPath : removedKeys) {
        removed = removed.insert(key(keyPath));
    }
    return new LocalViewChanges(targetId, fromCache, added, removed);
}
Also used : LocalViewChanges(com.google.firebase.firestore.local.LocalViewChanges) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ByteString(com.google.protobuf.ByteString)

Example 2 with LocalViewChanges

use of com.google.firebase.firestore.local.LocalViewChanges 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);
}
Also used : LocalViewChanges(com.google.firebase.firestore.local.LocalViewChanges) ArrayList(java.util.ArrayList) QueryResult(com.google.firebase.firestore.local.QueryResult) TargetChange(com.google.firebase.firestore.remote.TargetChange) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap)

Aggregations

LocalViewChanges (com.google.firebase.firestore.local.LocalViewChanges)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)1 QueryResult (com.google.firebase.firestore.local.QueryResult)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 TargetChange (com.google.firebase.firestore.remote.TargetChange)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1