Search in sources :

Example 1 with FieldPath

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

the class LocalSerializer method decodeFieldIndexSegments.

public List<FieldIndex.Segment> decodeFieldIndexSegments(Index index) {
    List<FieldIndex.Segment> result = new ArrayList<>();
    for (Index.IndexField field : index.getFieldsList()) {
        FieldPath fieldPath = FieldPath.fromServerFormat(field.getFieldPath());
        FieldIndex.Segment.Kind kind = field.getValueModeCase().equals(Index.IndexField.ValueModeCase.ARRAY_CONFIG) ? FieldIndex.Segment.Kind.CONTAINS : (field.getOrder().equals(Index.IndexField.Order.ASCENDING) ? FieldIndex.Segment.Kind.ASCENDING : FieldIndex.Segment.Kind.DESCENDING);
        result.add(FieldIndex.Segment.create(fieldPath, kind));
    }
    return result;
}
Also used : FieldPath(com.google.firebase.firestore.model.FieldPath) ArrayList(java.util.ArrayList) Index(com.google.firestore.admin.v1.Index) FieldIndex(com.google.firebase.firestore.model.FieldIndex)

Example 2 with FieldPath

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

the class UserDataReader method parseUpdateData.

/**
 * Parse update data from an {@code update()} call.
 */
public ParsedUpdateData parseUpdateData(Map<String, Object> data) {
    checkNotNull(data, "Provided update data must not be null.");
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
    ParseContext context = accumulator.rootContext();
    ObjectValue updateData = new ObjectValue();
    for (Entry<String, Object> entry : data.entrySet()) {
        FieldPath fieldPath = com.google.firebase.firestore.FieldPath.fromDotSeparatedPath(entry.getKey()).getInternalPath();
        Object fieldValue = entry.getValue();
        if (fieldValue instanceof DeleteFieldValue) {
            // Add it to the field mask, but don't add anything to updateData.
            context.addToFieldMask(fieldPath);
        } else {
            @Nullable Value parsedValue = convertAndParseFieldData(fieldValue, context.childContext(fieldPath));
            if (parsedValue != null) {
                context.addToFieldMask(fieldPath);
                updateData.set(fieldPath, parsedValue);
            }
        }
    }
    return accumulator.toUpdateData(updateData);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) FieldPath(com.google.firebase.firestore.model.FieldPath) ParseContext(com.google.firebase.firestore.core.UserData.ParseContext) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) NullValue(com.google.protobuf.NullValue) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) MapValue(com.google.firestore.v1.MapValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue) ArrayValue(com.google.firestore.v1.ArrayValue) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) Nullable(androidx.annotation.Nullable)

Example 3 with FieldPath

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

the class UserDataReader method parseMergeData.

/**
 * Parse document data from a {@code set()} call with {@link SetOptions#merge()} set.
 *
 * @param input A map or POJO object representing document data.
 * @param fieldMask A {@code FieldMask} object representing the fields to be merged.
 */
public ParsedSetData parseMergeData(Object input, @Nullable FieldMask fieldMask) {
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.MergeSet);
    ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());
    if (fieldMask != null) {
        // Verify that all elements specified in the field mask are part of the parsed context.
        for (FieldPath field : fieldMask.getMask()) {
            if (!accumulator.contains(field)) {
                throw new IllegalArgumentException("Field '" + field.toString() + "' is specified in your field mask but not in your input data.");
            }
        }
        return accumulator.toMergeData(updateData, fieldMask);
    } else {
        return accumulator.toMergeData(updateData);
    }
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) FieldPath(com.google.firebase.firestore.model.FieldPath)

Example 4 with FieldPath

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

the class Query method getOrderBy.

/**
 * Returns the full list of ordering constraints on the query.
 *
 * <p>This might include additional sort orders added implicitly to match the backend behavior.
 */
public List<OrderBy> getOrderBy() {
    if (memoizedOrderBy == null) {
        FieldPath inequalityField = inequalityField();
        FieldPath firstOrderByField = getFirstOrderByField();
        if (inequalityField != null && firstOrderByField == null) {
            // ascending.
            if (inequalityField.isKeyField()) {
                this.memoizedOrderBy = Collections.singletonList(KEY_ORDERING_ASC);
            } else {
                memoizedOrderBy = Arrays.asList(OrderBy.getInstance(Direction.ASCENDING, inequalityField), KEY_ORDERING_ASC);
            }
        } else {
            List<OrderBy> res = new ArrayList<>();
            boolean foundKeyOrdering = false;
            for (OrderBy explicit : explicitSortOrder) {
                res.add(explicit);
                if (explicit.getField().equals(FieldPath.KEY_PATH)) {
                    foundKeyOrdering = true;
                }
            }
            if (!foundKeyOrdering) {
                // The direction of the implicit key ordering always matches the direction of the last
                // explicit sort order
                Direction lastDirection = explicitSortOrder.size() > 0 ? explicitSortOrder.get(explicitSortOrder.size() - 1).getDirection() : Direction.ASCENDING;
                res.add(lastDirection.equals(Direction.ASCENDING) ? KEY_ORDERING_ASC : KEY_ORDERING_DESC);
            }
            memoizedOrderBy = res;
        }
    }
    return memoizedOrderBy;
}
Also used : FieldPath(com.google.firebase.firestore.model.FieldPath) ArrayList(java.util.ArrayList) Direction(com.google.firebase.firestore.core.OrderBy.Direction)

Example 5 with FieldPath

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

the class FirebaseFirestore method setIndexConfiguration.

/**
 * Configures indexing for local query execution. Any previous index configuration is overridden.
 * The Task resolves once the index configuration has been persisted.
 *
 * <p>The index entries themselves are created asynchronously. You can continue to use queries
 * that require indexing even if the indices are not yet available. Query execution will
 * automatically start using the index once the index entries have been written.
 *
 * <p>The method accepts the JSON format exported by the Firebase CLI (`firebase
 * firestore:indexes`). If the JSON format is invalid, this method throws an exception.
 *
 * @param json The JSON format exported by the Firebase CLI.
 * @return A task that resolves once all indices are successfully configured.
 * @throws IllegalArgumentException if the JSON format is invalid
 */
@VisibleForTesting
Task<Void> setIndexConfiguration(String json) {
    ensureClientConfigured();
    Preconditions.checkState(settings.isPersistenceEnabled(), "Cannot enable indexes when persistence is disabled");
    List<FieldIndex> parsedIndexes = new ArrayList<>();
    try {
        JSONObject jsonObject = new JSONObject(json);
        if (jsonObject.has("indexes")) {
            JSONArray indexes = jsonObject.getJSONArray("indexes");
            for (int i = 0; i < indexes.length(); ++i) {
                JSONObject definition = indexes.getJSONObject(i);
                String collectionGroup = definition.getString("collectionGroup");
                List<FieldIndex.Segment> segments = new ArrayList<>();
                JSONArray fields = definition.optJSONArray("fields");
                for (int f = 0; fields != null && f < fields.length(); ++f) {
                    JSONObject field = fields.getJSONObject(f);
                    FieldPath fieldPath = FieldPath.fromServerFormat(field.getString("fieldPath"));
                    if ("CONTAINS".equals(field.optString("arrayConfig"))) {
                        segments.add(FieldIndex.Segment.create(fieldPath, FieldIndex.Segment.Kind.CONTAINS));
                    } else if ("ASCENDING".equals(field.optString("order"))) {
                        segments.add(FieldIndex.Segment.create(fieldPath, FieldIndex.Segment.Kind.ASCENDING));
                    } else {
                        segments.add(FieldIndex.Segment.create(fieldPath, FieldIndex.Segment.Kind.DESCENDING));
                    }
                }
                parsedIndexes.add(FieldIndex.create(FieldIndex.UNKNOWN_ID, collectionGroup, segments, FieldIndex.INITIAL_STATE));
            }
        }
    } catch (JSONException e) {
        throw new IllegalArgumentException("Failed to parse index configuration", e);
    }
    return client.configureFieldIndexes(parsedIndexes);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) JSONObject(org.json.JSONObject) FieldPath(com.google.firebase.firestore.model.FieldPath) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) VisibleForTesting(androidx.annotation.VisibleForTesting)

Aggregations

FieldPath (com.google.firebase.firestore.model.FieldPath)22 ObjectValue (com.google.firebase.firestore.model.ObjectValue)10 Value (com.google.firestore.v1.Value)9 ArrayList (java.util.ArrayList)7 ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)3 ByteString (com.google.protobuf.ByteString)3 Nullable (androidx.annotation.Nullable)2 VisibleForTesting (androidx.annotation.VisibleForTesting)2 ArrayRemoveFieldValue (com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)2 ArrayUnionFieldValue (com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue)2 DeleteFieldValue (com.google.firebase.firestore.FieldValue.DeleteFieldValue)2 ServerTimestampFieldValue (com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)2 OrderBy (com.google.firebase.firestore.core.OrderBy)2 Direction (com.google.firebase.firestore.core.OrderBy.Direction)2 ParseContext (com.google.firebase.firestore.core.UserData.ParseContext)2 FieldIndex (com.google.firebase.firestore.model.FieldIndex)2 ArrayValue (com.google.firestore.v1.ArrayValue)2 MapValue (com.google.firestore.v1.MapValue)2 NullValue (com.google.protobuf.NullValue)2