Search in sources :

Example 1 with ImmutableSortedSet

use of com.google.firebase.database.collection.ImmutableSortedSet in project firebase-android-sdk by firebase.

the class TestUtil method addedRemoteEvent.

public static RemoteEvent addedRemoteEvent(List<MutableDocument> docs, List<Integer> updatedInTargets, List<Integer> removedFromTargets) {
    Preconditions.checkArgument(!docs.isEmpty(), "Cannot pass empty docs array");
    WatchChangeAggregator aggregator = new WatchChangeAggregator(new WatchChangeAggregator.TargetMetadataProvider() {

        @Override
        public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
            return DocumentKey.emptyKeySet();
        }

        @Override
        public TargetData getTargetDataForTarget(int targetId) {
            ResourcePath collectionPath = docs.get(0).getKey().getCollectionPath();
            return targetData(targetId, QueryPurpose.LISTEN, collectionPath.toString());
        }
    });
    SnapshotVersion version = SnapshotVersion.NONE;
    for (MutableDocument doc : docs) {
        DocumentChange change = new DocumentChange(updatedInTargets, removedFromTargets, doc.getKey(), doc);
        aggregator.handleDocumentChange(change);
        version = doc.getVersion().compareTo(version) > 0 ? doc.getVersion() : version;
    }
    return aggregator.createRemoteEvent(version);
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchChangeAggregator(com.google.firebase.firestore.remote.WatchChangeAggregator) ResourcePath(com.google.firebase.firestore.model.ResourcePath) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange)

Example 2 with ImmutableSortedSet

use of com.google.firebase.database.collection.ImmutableSortedSet 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 3 with ImmutableSortedSet

use of com.google.firebase.database.collection.ImmutableSortedSet in project firebase-android-sdk by firebase.

the class MemoryMutationQueue method getAllMutationBatchesAffectingDocumentKeys.

@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(Iterable<DocumentKey> documentKeys) {
    ImmutableSortedSet<Integer> uniqueBatchIDs = new ImmutableSortedSet<Integer>(emptyList(), Util.comparator());
    for (DocumentKey key : documentKeys) {
        DocumentReference start = new DocumentReference(key, 0);
        Iterator<DocumentReference> batchesIter = batchesByDocumentKey.iteratorFrom(start);
        while (batchesIter.hasNext()) {
            DocumentReference reference = batchesIter.next();
            if (!key.equals(reference.getKey())) {
                break;
            }
            uniqueBatchIDs = uniqueBatchIDs.insert(reference.getId());
        }
    }
    return lookupMutationBatches(uniqueBatchIDs);
}
Also used : ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 4 with ImmutableSortedSet

use of com.google.firebase.database.collection.ImmutableSortedSet in project firebase-android-sdk by firebase.

the class MemoryMutationQueue method getAllMutationBatchesAffectingQuery.

@Override
public List<MutationBatch> getAllMutationBatchesAffectingQuery(Query query) {
    hardAssert(!query.isCollectionGroupQuery(), "CollectionGroup queries should be handled in LocalDocumentsView");
    // Use the query path as a prefix for testing if a document matches the query.
    ResourcePath prefix = query.getPath();
    int immediateChildrenPathLength = prefix.length() + 1;
    // Construct a document reference for actually scanning the index. Unlike the prefix, the
    // document key in this reference must have an even number of segments. The empty segment can be
    // used as a suffix of the query path because it precedes all other segments in an ordered
    // traversal.
    ResourcePath startPath = prefix;
    if (!DocumentKey.isDocumentKey(startPath)) {
        startPath = startPath.append("");
    }
    DocumentReference start = new DocumentReference(DocumentKey.fromPath(startPath), 0);
    // Find unique batchIDs referenced by all documents potentially matching the query.
    ImmutableSortedSet<Integer> uniqueBatchIDs = new ImmutableSortedSet<Integer>(emptyList(), Util.comparator());
    Iterator<DocumentReference> iterator = batchesByDocumentKey.iteratorFrom(start);
    while (iterator.hasNext()) {
        DocumentReference reference = iterator.next();
        ResourcePath rowKeyPath = reference.getKey().getPath();
        if (!prefix.isPrefixOf(rowKeyPath)) {
            break;
        }
        // TODO: we'll need a different scanner when we implement ancestor queries.
        if (rowKeyPath.length() == immediateChildrenPathLength) {
            uniqueBatchIDs = uniqueBatchIDs.insert(reference.getId());
        }
    }
    return lookupMutationBatches(uniqueBatchIDs);
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet)

Example 5 with ImmutableSortedSet

use of com.google.firebase.database.collection.ImmutableSortedSet in project firebase-android-sdk by firebase.

the class TestUtil method updateRemoteEvent.

public static RemoteEvent updateRemoteEvent(MutableDocument doc, List<Integer> updatedInTargets, List<Integer> removedFromTargets, List<Integer> activeTargets) {
    DocumentChange change = new DocumentChange(updatedInTargets, removedFromTargets, doc.getKey(), doc);
    WatchChangeAggregator aggregator = new WatchChangeAggregator(new WatchChangeAggregator.TargetMetadataProvider() {

        @Override
        public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
            return DocumentKey.emptyKeySet().insert(doc.getKey());
        }

        @Override
        public TargetData getTargetDataForTarget(int targetId) {
            return activeTargets.contains(targetId) ? targetData(targetId, QueryPurpose.LISTEN, doc.getKey().toString()) : null;
        }
    });
    aggregator.handleDocumentChange(change);
    return aggregator.createRemoteEvent(doc.getVersion());
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchChangeAggregator(com.google.firebase.firestore.remote.WatchChangeAggregator) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange)

Aggregations

ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)5 TargetData (com.google.firebase.firestore.local.TargetData)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)2 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)2 WatchChangeAggregator (com.google.firebase.firestore.remote.WatchChangeAggregator)2 DocumentViewChange (com.google.firebase.firestore.core.DocumentViewChange)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1