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