Search in sources :

Example 1 with Direction

use of com.google.firebase.firestore.core.OrderBy.Direction 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 2 with Direction

use of com.google.firebase.firestore.core.OrderBy.Direction in project firebase-android-sdk by firebase.

the class RemoteSerializer method decodeOrderBy.

private OrderBy decodeOrderBy(Order proto) {
    FieldPath fieldPath = FieldPath.fromServerFormat(proto.getField().getFieldPath());
    OrderBy.Direction direction;
    switch(proto.getDirection()) {
        case ASCENDING:
            direction = Direction.ASCENDING;
            break;
        case DESCENDING:
            direction = Direction.DESCENDING;
            break;
        default:
            throw fail("Unrecognized direction %d", proto.getDirection());
    }
    return OrderBy.getInstance(direction, fieldPath);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) FieldPath(com.google.firebase.firestore.model.FieldPath) Direction(com.google.firebase.firestore.core.OrderBy.Direction)

Example 3 with Direction

use of com.google.firebase.firestore.core.OrderBy.Direction in project firebase-android-sdk by firebase.

the class Query method toTarget.

/**
 * @return A {@code Target} instance this query will be mapped to in backend and local store.
 */
public Target toTarget() {
    if (this.memoizedTarget == null) {
        if (this.limitType == LimitType.LIMIT_TO_FIRST) {
            this.memoizedTarget = new Target(this.getPath(), this.getCollectionGroup(), this.getFilters(), this.getOrderBy(), this.limit, this.getStartAt(), this.getEndAt());
        } else {
            // Flip the orderBy directions since we want the last results
            ArrayList<OrderBy> newOrderBy = new ArrayList<>();
            for (OrderBy orderBy : this.getOrderBy()) {
                Direction dir = orderBy.getDirection() == Direction.DESCENDING ? Direction.ASCENDING : Direction.DESCENDING;
                newOrderBy.add(OrderBy.getInstance(dir, orderBy.getField()));
            }
            // We need to swap the cursors to match the now-flipped query ordering.
            Bound newStartAt = this.endAt != null ? new Bound(this.endAt.getPosition(), !this.endAt.isInclusive()) : null;
            Bound newEndAt = this.startAt != null ? new Bound(this.startAt.getPosition(), !this.startAt.isInclusive()) : null;
            this.memoizedTarget = new Target(this.getPath(), this.getCollectionGroup(), this.getFilters(), newOrderBy, this.limit, newStartAt, newEndAt);
        }
    }
    return this.memoizedTarget;
}
Also used : ArrayList(java.util.ArrayList) Direction(com.google.firebase.firestore.core.OrderBy.Direction)

Aggregations

Direction (com.google.firebase.firestore.core.OrderBy.Direction)3 FieldPath (com.google.firebase.firestore.model.FieldPath)2 ArrayList (java.util.ArrayList)2 OrderBy (com.google.firebase.firestore.core.OrderBy)1