Search in sources :

Example 36 with DocumentKey

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

the class ReferenceSet method removeReferencesForId.

/**
 * Clears all references with a given ID. Calls removeReference() for each key removed.
 *
 * @return The keys of the documents that were removed.
 */
public ImmutableSortedSet<DocumentKey> removeReferencesForId(int targetId) {
    DocumentKey emptyKey = DocumentKey.empty();
    DocumentReference startRef = new DocumentReference(emptyKey, targetId);
    Iterator<DocumentReference> it = referencesByTarget.iteratorFrom(startRef);
    ImmutableSortedSet<DocumentKey> keys = DocumentKey.emptyKeySet();
    while (it.hasNext()) {
        DocumentReference ref = it.next();
        if (ref.getId() == targetId) {
            keys = keys.insert(ref.getKey());
            removeReference(ref);
        } else {
            break;
        }
    }
    return keys;
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 37 with DocumentKey

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

the class ReferenceSet method referencesForId.

/**
 * Returns all of the document keys that have had references added for the given ID.
 */
public ImmutableSortedSet<DocumentKey> referencesForId(int target) {
    DocumentKey emptyKey = DocumentKey.empty();
    DocumentReference startRef = new DocumentReference(emptyKey, target);
    Iterator<DocumentReference> iterator = referencesByTarget.iteratorFrom(startRef);
    ImmutableSortedSet<DocumentKey> keys = DocumentKey.emptyKeySet();
    while (iterator.hasNext()) {
        DocumentReference reference = iterator.next();
        if (reference.getId() == target) {
            keys = keys.insert(reference.getKey());
        } else {
            break;
        }
    }
    return keys;
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 38 with DocumentKey

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

the class TestCaseConverter method convertDatabaseContents.

private List<Collection> convertDatabaseContents(DatastoreTestTrace.FirestoreV1Action action) {
    LinkedHashMap<String, List<Document>> collections = new LinkedHashMap<>();
    List<RunQueryResponse> responses = action.getDatabaseContentsBeforeAction().getResponseList();
    for (RunQueryResponse response : responses) {
        if (response.hasDocument()) {
            Document document = response.getDocument();
            DocumentKey documentKey = DocumentKey.fromName(document.getName());
            String collectionId = documentKey.getCollectionGroup();
            List<Document> documents = collections.computeIfAbsent(collectionId, name -> new ArrayList<>());
            documents.add(document);
        }
    }
    return collections.entrySet().stream().map(entry -> Collection.Builder.builder().setName(entry.getKey()).setDocuments(entry.getValue()).build()).collect(Collectors.toList());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) DatastoreTestTrace(com.google.apphosting.datastore.testing.DatastoreTestTrace) Document(com.google.firestore.v1.Document) Where(com.google.firebase.firestore.conformance.model.Where) ResourcePath(com.google.firebase.firestore.model.ResourcePath) QueryFilter(com.google.firebase.firestore.conformance.model.QueryFilter) Collectors(java.util.stream.Collectors) Order(com.google.firebase.firestore.conformance.model.Order) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Value(com.google.firestore.v1.Value) Result(com.google.firebase.firestore.conformance.model.Result) Cursor(com.google.firestore.v1.Cursor) Query(com.google.firebase.firestore.conformance.model.Query) Collection(com.google.firebase.firestore.conformance.model.Collection) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) DocumentKey(com.google.firebase.firestore.model.DocumentKey) TestCase(com.google.firebase.firestore.conformance.model.TestCase) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) List(java.util.List) Document(com.google.firestore.v1.Document) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with DocumentKey

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

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

the class SpecTestCase method doMutation.

private void doMutation(Mutation mutation) throws Exception {
    DocumentKey documentKey = mutation.getKey();
    TaskCompletionSource<Void> callback = new TaskCompletionSource<>();
    Task<Void> writeProcessed = callback.getTask().continueWith(backgroundExecutor, task -> {
        if (task.isSuccessful()) {
            SpecTestCase.this.acknowledgedDocs.add(documentKey);
        } else {
            SpecTestCase.this.rejectedDocs.add(documentKey);
        }
        return null;
    });
    getCurrentOutstandingWrites().add(new Pair<>(mutation, writeProcessed));
    log("      Sending this write: " + mutation);
    queue.runSync(() -> syncEngine.writeMutations(singletonList(mutation), callback));
}
Also used : TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)139 MutableDocument (com.google.firebase.firestore.model.MutableDocument)53 Test (org.junit.Test)36 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)24 Document (com.google.firebase.firestore.model.Document)20 Mutation (com.google.firebase.firestore.model.mutation.Mutation)18 Map (java.util.Map)18 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)10 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 HashSet (java.util.HashSet)10 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)8 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)8 ByteString (com.google.protobuf.ByteString)7 Target (com.google.firebase.firestore.core.Target)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)6