Search in sources :

Example 1 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method addToCollectionParentIndex.

@Override
public void addToCollectionParentIndex(ResourcePath collectionPath) {
    hardAssert(started, "IndexManager not started");
    hardAssert(collectionPath.length() % 2 == 1, "Expected a collection path.");
    if (collectionParentsCache.add(collectionPath)) {
        String collectionId = collectionPath.getLastSegment();
        ResourcePath parentPath = collectionPath.popLast();
        db.execute("INSERT OR REPLACE INTO collection_parents " + "(collection_id, parent) " + "VALUES (?, ?)", collectionId, EncodedPath.encode(parentPath));
    }
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath)

Example 2 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class LocalDocumentsView method getDocumentsMatchingCollectionGroupQuery.

private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionGroupQuery(Query query, IndexOffset offset) {
    hardAssert(query.getPath().isEmpty(), "Currently we only support collection group queries at the root.");
    String collectionId = query.getCollectionGroup();
    ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
    List<ResourcePath> parents = indexManager.getCollectionParents(collectionId);
    // aggregate the results.
    for (ResourcePath parent : parents) {
        Query collectionQuery = query.asCollectionQueryAtPath(parent.append(collectionId));
        ImmutableSortedMap<DocumentKey, Document> collectionResults = getDocumentsMatchingCollectionQuery(collectionQuery, offset);
        for (Map.Entry<DocumentKey, Document> docEntry : collectionResults) {
            results = results.insert(docEntry.getKey(), docEntry.getValue());
        }
    }
    return results;
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) Query(com.google.firebase.firestore.core.Query) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Example 3 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class Query method boundFromFields.

/**
 * Converts a list of field values to Bound.
 */
private Bound boundFromFields(String methodName, Object[] values, boolean inclusive) {
    // Use explicit order by's because it has to match the query the user made
    List<OrderBy> explicitOrderBy = query.getExplicitOrderBy();
    if (values.length > explicitOrderBy.size()) {
        throw new IllegalArgumentException("Too many arguments provided to " + methodName + "(). The number of arguments must be less " + "than or equal to the number of orderBy() clauses.");
    }
    List<Value> components = new ArrayList<>();
    for (int i = 0; i < values.length; i++) {
        Object rawValue = values[i];
        OrderBy orderBy = explicitOrderBy.get(i);
        if (orderBy.getField().equals(com.google.firebase.firestore.model.FieldPath.KEY_PATH)) {
            if (!(rawValue instanceof String)) {
                throw new IllegalArgumentException("Invalid query. Expected a string for document ID in " + methodName + "(), but got " + rawValue + ".");
            }
            String documentId = (String) rawValue;
            if (!query.isCollectionGroupQuery() && documentId.contains("/")) {
                throw new IllegalArgumentException("Invalid query. When querying a collection and ordering by FieldPath.documentId(), " + "the value passed to " + methodName + "() must be a plain document ID, but '" + documentId + "' contains a slash.");
            }
            ResourcePath path = query.getPath().append(ResourcePath.fromString(documentId));
            if (!DocumentKey.isDocumentKey(path)) {
                throw new IllegalArgumentException("Invalid query. When querying a collection group and ordering by " + "FieldPath.documentId(), the value passed to " + methodName + "() must result in a valid document path, but '" + path + "' is not because it contains an odd number of segments.");
            }
            DocumentKey key = DocumentKey.fromPath(path);
            components.add(Values.refValue(firestore.getDatabaseId(), key));
        } else {
            Value wrapped = firestore.getUserDataReader().parseQueryValue(rawValue);
            components.add(wrapped);
        }
    }
    return new Bound(components, inclusive);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) ResourcePath(com.google.firebase.firestore.model.ResourcePath) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Bound(com.google.firebase.firestore.core.Bound)

Example 4 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath 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 5 with ResourcePath

use of com.google.firebase.firestore.model.ResourcePath in project firebase-android-sdk by firebase.

the class RemoteSerializer method decodeResourceName.

/**
 * Decodes a fully qualified resource name into a resource path and validates that there is a
 * project and database encoded in the path. There are no guarantees that a local path is also
 * encoded in this resource name.
 */
private ResourcePath decodeResourceName(String encoded) {
    ResourcePath resource = ResourcePath.fromString(encoded);
    Assert.hardAssert(isValidResourceName(resource), "Tried to deserialize invalid key %s", resource);
    return resource;
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath)

Aggregations

ResourcePath (com.google.firebase.firestore.model.ResourcePath)34 MutableDocument (com.google.firebase.firestore.model.MutableDocument)13 DocumentKey (com.google.firebase.firestore.model.DocumentKey)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 OrderBy (com.google.firebase.firestore.core.OrderBy)4 HashMap (java.util.HashMap)4 Bound (com.google.firebase.firestore.core.Bound)3 Cursor (android.database.Cursor)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)2 Filter (com.google.firebase.firestore.core.Filter)2 Query (com.google.firebase.firestore.core.Query)2 EncodedPath.decodeResourcePath (com.google.firebase.firestore.local.EncodedPath.decodeResourcePath)2 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)2 CollectionSelector (com.google.firestore.v1.StructuredQuery.CollectionSelector)2