Search in sources :

Example 31 with AndFilterExpression

use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.

the class PersistentResource method getRelationUnchecked.

/**
 * Retrieve an unchecked set of relations.
 */
private Observable<PersistentResource> getRelationUnchecked(com.yahoo.elide.core.request.Relationship relationship) {
    String relationName = relationship.getName();
    FilterExpression filterExpression = relationship.getProjection().getFilterExpression();
    Pagination pagination = relationship.getProjection().getPagination();
    Sorting sorting = relationship.getProjection().getSorting();
    RelationshipType type = getRelationshipType(relationName);
    final Type<?> relationClass = dictionary.getParameterizedType(obj, relationName);
    if (relationClass == null) {
        throw new InvalidAttributeException(relationName, this.getTypeName());
    }
    // Invoke filterExpressionCheck and then merge with filterExpression.
    Optional<FilterExpression> permissionFilter = getPermissionFilterExpression(relationClass, requestScope, relationship.getProjection().getRequestedFields());
    Optional<FilterExpression> computedFilters = Optional.ofNullable(filterExpression);
    if (permissionFilter.isPresent() && filterExpression != null) {
        FilterExpression mergedExpression = new AndFilterExpression(filterExpression, permissionFilter.get());
        computedFilters = Optional.of(mergedExpression);
    } else if (permissionFilter.isPresent()) {
        computedFilters = permissionFilter;
    }
    com.yahoo.elide.core.request.Relationship modifiedRelationship = relationship.copyOf().projection(relationship.getProjection().copyOf().filterExpression(computedFilters.orElse(null)).sorting(sorting).pagination(pagination).build()).build();
    Observable<PersistentResource> resources;
    if (type.isToMany()) {
        DataStoreIterable val = transaction.getToManyRelation(transaction, obj, modifiedRelationship, requestScope);
        if (val == null) {
            return Observable.empty();
        }
        resources = Observable.fromIterable(new PersistentResourceSet(this, relationName, val, requestScope));
    } else {
        Object val = transaction.getToOneRelation(transaction, obj, modifiedRelationship, requestScope);
        if (val == null) {
            return Observable.empty();
        }
        resources = Observable.fromArray(new PersistentResource(val, this, relationName, requestScope.getUUIDFor(val), requestScope));
    }
    return resources;
}
Also used : InvalidAttributeException(com.yahoo.elide.core.exceptions.InvalidAttributeException) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) Pagination(com.yahoo.elide.core.request.Pagination) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 32 with AndFilterExpression

use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.

the class AsyncAPICleanerRunnable method timeoutAsyncAPI.

/**
 * This method updates the status of long running async query which
 * were interrupted due to host crash/app shutdown to TIMEDOUT.
 * @param type AsyncAPI Type Implementation.
 */
protected <T extends AsyncAPI> void timeoutAsyncAPI(Class<T> type) {
    try {
        Date filterDate = dateUtil.calculateFilterDate(Calendar.MINUTE, maxRunTimeMinutes);
        PathElement createdOnPathElement = new PathElement(type, Long.class, "createdOn");
        PathElement statusPathElement = new PathElement(type, String.class, "status");
        FilterPredicate inPredicate = new InPredicate(statusPathElement, QueryStatus.PROCESSING, QueryStatus.QUEUED);
        FilterPredicate lePredicate = new LEPredicate(createdOnPathElement, filterDate);
        AndFilterExpression fltTimeoutExp = new AndFilterExpression(inPredicate, lePredicate);
        asyncAPIDao.updateStatusAsyncAPIByFilter(fltTimeoutExp, QueryStatus.TIMEDOUT, type);
    } catch (Exception e) {
        log.error("Exception in scheduled cleanup: {}", e.toString());
    }
}
Also used : PathElement(com.yahoo.elide.core.Path.PathElement) LEPredicate(com.yahoo.elide.core.filter.predicates.LEPredicate) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Date(java.util.Date) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 33 with AndFilterExpression

use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.

the class FilterTranslatorTest method testBetweenOperator.

@Test
public void testBetweenOperator() throws Exception {
    List<Path.PathElement> authorId = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, Long.class, "id"));
    List<Path.PathElement> publishDate = Arrays.asList(new Path.PathElement(Book.class, Long.class, "publishDate"));
    FilterPredicate authorPred = new FilterPredicate(new Path(authorId), Operator.BETWEEN, Arrays.asList(1, 15));
    FilterPredicate publishPred = new FilterPredicate(new Path(publishDate), Operator.NOTBETWEEN, Arrays.asList(1, 15));
    AndFilterExpression andFilter = new AndFilterExpression(authorPred, publishPred);
    FilterTranslator filterOp = new FilterTranslator(dictionary);
    String query = filterOp.apply(andFilter, false);
    String authorP1 = authorPred.getParameters().get(0).getPlaceholder();
    String authorP2 = authorPred.getParameters().get(1).getPlaceholder();
    String publishP1 = publishPred.getParameters().get(0).getPlaceholder();
    String publishP2 = publishPred.getParameters().get(1).getPlaceholder();
    String expected = "(authors.id BETWEEN " + authorP1 + " AND " + authorP2 + " AND " + "publishDate NOT BETWEEN " + publishP1 + " AND " + publishP2 + ")";
    assertEquals(expected, query);
    // Assert excepetion if parameter length is not 2
    assertThrows(IllegalArgumentException.class, () -> filterOp.apply(new FilterPredicate(new Path(authorId), Operator.BETWEEN, Arrays.asList(3))));
}
Also used : Path(com.yahoo.elide.core.Path) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Example 34 with AndFilterExpression

use of com.yahoo.elide.core.filter.expression.AndFilterExpression in project elide by yahoo.

the class FilterTranslatorTest method testMemberOfOperator.

@Test
public void testMemberOfOperator() throws Exception {
    List<Path.PathElement> path = Arrays.asList(new Path.PathElement(Book.class, String.class, "awards"));
    FilterPredicate p1 = new FilterPredicate(new Path(path), Operator.HASMEMBER, Arrays.asList("awards1"));
    FilterPredicate p2 = new FilterPredicate(new Path(path), Operator.HASNOMEMBER, Arrays.asList("awards2"));
    AndFilterExpression and = new AndFilterExpression(p1, p2);
    FilterTranslator filterOp = new FilterTranslator(dictionary);
    String query = filterOp.apply(and, false);
    String p1Params = p1.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String p2Params = p2.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String expected = "(" + p1Params + " MEMBER OF awards " + "AND " + p2Params + " NOT MEMBER OF awards)";
    assertEquals(expected, query);
}
Also used : Path(com.yahoo.elide.core.Path) Book(example.Book) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)34 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)21 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)20 Test (org.junit.jupiter.api.Test)17 Path (com.yahoo.elide.core.Path)15 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)10 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)7 Query (com.yahoo.elide.datastores.aggregation.query.Query)7 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)7 Book (example.Book)6 Author (example.Author)5 PathElement (com.yahoo.elide.core.Path.PathElement)4 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)4 Date (java.util.Date)4 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)3 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)3 Type (com.yahoo.elide.core.type.Type)3 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)3