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);
}
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);
}
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);
}
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);
}
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());
}
Aggregations