Search in sources :

Example 1 with DocumentViewChange

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

the class QuerySnapshotTest method testIncludeMetadataChanges.

@Test
public void testIncludeMetadataChanges() {
    MutableDocument doc1Old = doc("foo/bar", 1, wrapObject("a", "b")).setHasLocalMutations();
    MutableDocument doc1New = doc("foo/bar", 1, wrapObject("a", "b"));
    MutableDocument doc2Old = doc("foo/baz", 1, wrapObject("a", "b"));
    MutableDocument doc2New = doc("foo/baz", 1, wrapObject("a", "c"));
    DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR, doc1Old, doc2Old);
    DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR, doc1New, doc2New);
    List<DocumentViewChange> documentChanges = Arrays.asList(DocumentViewChange.create(DocumentViewChange.Type.METADATA, doc1New), DocumentViewChange.create(DocumentViewChange.Type.MODIFIED, doc2New));
    FirebaseFirestore firestore = TestUtil.firestore();
    com.google.firebase.firestore.core.Query fooQuery = query("foo");
    ViewSnapshot viewSnapshot = new ViewSnapshot(fooQuery, newDocuments, oldDocuments, documentChanges, /*isFromCache=*/
    false, /*mutatedKeys=*/
    keySet(), /*didSyncStateChange=*/
    true, /* excludesMetadataChanges= */
    false);
    QuerySnapshot snapshot = new QuerySnapshot(new Query(fooQuery, firestore), viewSnapshot, firestore);
    QueryDocumentSnapshot doc1Snap = QueryDocumentSnapshot.fromDocument(firestore, doc1New, /*fromCache=*/
    false, /*hasPendingWrites=*/
    false);
    QueryDocumentSnapshot doc2Snap = QueryDocumentSnapshot.fromDocument(firestore, doc2New, /*fromCache=*/
    false, /*hasPendingWrites=*/
    false);
    assertEquals(1, snapshot.getDocumentChanges().size());
    List<DocumentChange> changesWithoutMetadata = Arrays.asList(new DocumentChange(doc2Snap, DocumentChange.Type.MODIFIED, /*oldIndex=*/
    1, /*newIndex=*/
    1));
    assertEquals(changesWithoutMetadata, snapshot.getDocumentChanges());
    List<DocumentChange> changesWithMetadata = Arrays.asList(new DocumentChange(doc1Snap, DocumentChange.Type.MODIFIED, /*oldIndex=*/
    0, /*newIndex=*/
    0), new DocumentChange(doc2Snap, DocumentChange.Type.MODIFIED, /*oldIndex=*/
    1, /*newIndex=*/
    1));
    assertEquals(changesWithMetadata, snapshot.getDocumentChanges(MetadataChanges.INCLUDE));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Test(org.junit.Test)

Example 2 with DocumentViewChange

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

the class TestUtil method querySnapshot.

/**
 * A convenience method for creating a particular query snapshot for tests.
 *
 * @param path To be used in constructing the query.
 * @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each entry maps to a
 *     document, with the key being the document id, and the value being the document contents.
 * @param docsToAdd Specifies data to be added into the query snapshot as of now. Each entry maps
 *     to a document, with the key being the document id, and the value being the document
 *     contents.
 * @param isFromCache Whether the query snapshot is cache result.
 * @return A query snapshot that consists of both sets of documents.
 */
public static QuerySnapshot querySnapshot(String path, Map<String, ObjectValue> oldDocs, Map<String, ObjectValue> docsToAdd, boolean hasPendingWrites, boolean isFromCache) {
    DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR);
    ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
    for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
        String docKey = path + "/" + pair.getKey();
        MutableDocument doc = doc(docKey, 1L, pair.getValue());
        if (hasPendingWrites) {
            doc.setHasCommittedMutations();
            mutatedKeys = mutatedKeys.insert(key(docKey));
        }
        oldDocuments = oldDocuments.add(doc);
    }
    DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR);
    List<DocumentViewChange> documentChanges = new ArrayList<>();
    for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
        String docKey = path + "/" + pair.getKey();
        MutableDocument docToAdd = doc(docKey, 1L, pair.getValue());
        if (hasPendingWrites) {
            docToAdd.setHasCommittedMutations();
            mutatedKeys = mutatedKeys.insert(key(docKey));
        }
        newDocuments = newDocuments.add(docToAdd);
        documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));
    }
    ViewSnapshot viewSnapshot = new ViewSnapshot(com.google.firebase.firestore.testutil.TestUtil.query(path), newDocuments, oldDocuments, documentChanges, isFromCache, mutatedKeys, /* didSyncStateChange= */
    true, /* excludesMetadataChanges= */
    false);
    return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) ObjectValue(com.google.firebase.firestore.model.ObjectValue) DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Map(java.util.Map)

Example 3 with DocumentViewChange

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

the class LocalViewChanges method fromViewSnapshot.

public static LocalViewChanges fromViewSnapshot(int targetId, ViewSnapshot snapshot) {
    ImmutableSortedSet<DocumentKey> addedKeys = new ImmutableSortedSet<DocumentKey>(new ArrayList<>(), DocumentKey.comparator());
    ImmutableSortedSet<DocumentKey> removedKeys = new ImmutableSortedSet<DocumentKey>(new ArrayList<>(), DocumentKey.comparator());
    for (DocumentViewChange docChange : snapshot.getChanges()) {
        switch(docChange.getType()) {
            case ADDED:
                addedKeys = addedKeys.insert(docChange.getDocument().getKey());
                break;
            case REMOVED:
                removedKeys = removedKeys.insert(docChange.getDocument().getKey());
                break;
            default:
                // Do nothing.
                break;
        }
    }
    return new LocalViewChanges(targetId, snapshot.isFromCache(), addedKeys, removedKeys);
}
Also used : DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 4 with DocumentViewChange

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

the class DocumentChange method changesFromSnapshot.

/**
 * Creates the list of document changes from a {@code ViewSnapshot}.
 */
static List<DocumentChange> changesFromSnapshot(FirebaseFirestore firestore, MetadataChanges metadataChanges, ViewSnapshot snapshot) {
    List<DocumentChange> documentChanges = new ArrayList<>();
    if (snapshot.getOldDocuments().isEmpty()) {
        // Special case the first snapshot because index calculation is easy and fast. Also all
        // changes on the first snapshot are adds so there are also no metadata-only changes to filter
        // out.
        int index = 0;
        Document lastDoc = null;
        for (DocumentViewChange change : snapshot.getChanges()) {
            Document document = change.getDocument();
            QueryDocumentSnapshot documentSnapshot = QueryDocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), snapshot.getMutatedKeys().contains(document.getKey()));
            hardAssert(change.getType() == DocumentViewChange.Type.ADDED, "Invalid added event for first snapshot");
            hardAssert(lastDoc == null || snapshot.getQuery().comparator().compare(lastDoc, document) < 0, "Got added events in wrong order");
            documentChanges.add(new DocumentChange(documentSnapshot, Type.ADDED, -1, index++));
            lastDoc = document;
        }
    } else {
        // A DocumentSet that is updated incrementally as changes are applied to use to lookup the
        // index of a document.
        DocumentSet indexTracker = snapshot.getOldDocuments();
        for (DocumentViewChange change : snapshot.getChanges()) {
            if (metadataChanges == MetadataChanges.EXCLUDE && change.getType() == DocumentViewChange.Type.METADATA) {
                continue;
            }
            Document document = change.getDocument();
            QueryDocumentSnapshot documentSnapshot = QueryDocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), snapshot.getMutatedKeys().contains(document.getKey()));
            int oldIndex, newIndex;
            Type type = getType(change);
            if (type != Type.ADDED) {
                oldIndex = indexTracker.indexOf(document.getKey());
                hardAssert(oldIndex >= 0, "Index for document not found");
                indexTracker = indexTracker.remove(document.getKey());
            } else {
                oldIndex = -1;
            }
            if (type != Type.REMOVED) {
                indexTracker = indexTracker.add(document);
                newIndex = indexTracker.indexOf(document.getKey());
                hardAssert(newIndex >= 0, "Index for document not found");
            } else {
                newIndex = -1;
            }
            documentChanges.add(new DocumentChange(documentSnapshot, type, oldIndex, newIndex));
        }
    }
    return documentChanges;
}
Also used : DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) ArrayList(java.util.ArrayList) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Document(com.google.firebase.firestore.model.Document)

Example 5 with DocumentViewChange

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

the class SpecTestCase method assertEventMatches.

// 
// Methods for validating expectations.
// 
private void assertEventMatches(JSONObject expected, QueryEvent actual) throws JSONException {
    Query expectedQuery = parseQuery(expected.get("query"));
    assertEquals(expectedQuery, actual.query);
    if (expected.has("errorCode") && !Status.fromCodeValue(expected.getInt("errorCode")).isOk()) {
        assertNotNull(actual.error);
        assertEquals(expected.getInt("errorCode"), actual.error.getCode().value());
    } else {
        List<DocumentViewChange> expectedChanges = new ArrayList<>();
        JSONArray removed = expected.optJSONArray("removed");
        for (int i = 0; removed != null && i < removed.length(); ++i) {
            expectedChanges.add(parseChange(removed.getJSONObject(i), Type.REMOVED));
        }
        JSONArray added = expected.optJSONArray("added");
        for (int i = 0; added != null && i < added.length(); ++i) {
            expectedChanges.add(parseChange(added.getJSONObject(i), Type.ADDED));
        }
        JSONArray modified = expected.optJSONArray("modified");
        for (int i = 0; modified != null && i < modified.length(); ++i) {
            expectedChanges.add(parseChange(modified.getJSONObject(i), Type.MODIFIED));
        }
        JSONArray metadata = expected.optJSONArray("metadata");
        for (int i = 0; metadata != null && i < metadata.length(); ++i) {
            expectedChanges.add(parseChange(metadata.getJSONObject(i), Type.METADATA));
        }
        assertEquals(expectedChanges, actual.view.getChanges());
        boolean expectedHasPendingWrites = expected.optBoolean("hasPendingWrites", false);
        boolean expectedFromCache = expected.optBoolean("fromCache", false);
        assertEquals("hasPendingWrites", expectedHasPendingWrites, actual.view.hasPendingWrites());
        assertEquals("fromCache", expectedFromCache, actual.view.isFromCache());
    }
}
Also used : DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) Query(com.google.firebase.firestore.core.Query) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Aggregations

DocumentViewChange (com.google.firebase.firestore.core.DocumentViewChange)5 DocumentSet (com.google.firebase.firestore.model.DocumentSet)3 ArrayList (java.util.ArrayList)3 ViewSnapshot (com.google.firebase.firestore.core.ViewSnapshot)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)2 MutableDocument (com.google.firebase.firestore.model.MutableDocument)2 ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)1 Query (com.google.firebase.firestore.core.Query)1 Document (com.google.firebase.firestore.model.Document)1 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 Test (org.junit.Test)1