Search in sources :

Example 46 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class InMemoryStoreTransactionTest method testTransactionRequiresInMemoryFilterDuringGetRelation.

@Test
public void testTransactionRequiresInMemoryFilterDuringGetRelation() {
    FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction");
    Relationship relationship = Relationship.builder().projection(EntityProjection.builder().type(Book.class).filterExpression(expression).build()).name("books").alias("books").build();
    ArgumentCaptor<Relationship> relationshipArgument = ArgumentCaptor.forClass(Relationship.class);
    when(scope.getNewPersistentResources()).thenReturn(Sets.newHashSet(mock(PersistentResource.class)));
    when(wrappedTransaction.getToManyRelation(eq(inMemoryStoreTransaction), eq(author1), any(), eq(scope))).thenReturn(new DataStoreIterableBuilder<>(books).build());
    Collection<Object> loaded = ImmutableList.copyOf((Iterable) inMemoryStoreTransaction.getToManyRelation(inMemoryStoreTransaction, author1, relationship, scope));
    verify(wrappedTransaction, times(1)).getToManyRelation(eq(inMemoryStoreTransaction), eq(author1), relationshipArgument.capture(), eq(scope));
    assertNull(relationshipArgument.getValue().getProjection().getFilterExpression());
    assertNull(relationshipArgument.getValue().getProjection().getSorting());
    assertNull(relationshipArgument.getValue().getProjection().getPagination());
    assertEquals(2, loaded.size());
    assertTrue(loaded.contains(book1));
    assertTrue(loaded.contains(book3));
}
Also used : Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Test(org.junit.jupiter.api.Test)

Example 47 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class FilterPredicateTest method testComplexAttributeFieldType.

@Test
void testComplexAttributeFieldType() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("filter[author.homeAddress.street1]", "foo");
    Map<String, Set<FilterPredicate>> predicates = parse(queryParams);
    assertTrue(predicates.containsKey("author"));
    FilterPredicate predicate = predicates.get("author").iterator().next();
    assertEquals("street1", predicate.getField());
    assertEquals(Operator.IN, predicate.getOperator());
    Path path = predicate.getPath();
    assertEquals(path.getPathElements().get(0).getFieldName(), "homeAddress");
    assertEquals(path.getPathElements().get(1).getFieldName(), "street1");
    assertEquals(Collections.singletonList("foo"), predicate.getValues());
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Path(com.yahoo.elide.core.Path) HashSet(java.util.HashSet) Set(java.util.Set) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 48 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class RSQLFilterDialectTest method testGraphQLFilterDialectWithComplexAttribute.

@Test
public void testGraphQLFilterDialectWithComplexAttribute() throws Exception {
    Type<Author> authorType = ClassType.of(Author.class);
    FilterPredicate predicate = (FilterPredicate) dialect.parse(authorType, Collections.emptySet(), "homeAddress.street1=in=(foo)", NO_VERSION);
    assertEquals(Operator.IN, predicate.getOperator());
    Path path = predicate.getPath();
    assertEquals(2, path.getPathElements().size());
    assertEquals("homeAddress", path.getPathElements().get(0).getFieldName());
    assertEquals("street1", path.getPathElements().get(1).getFieldName());
}
Also used : Path(com.yahoo.elide.core.Path) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 49 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class InMemoryStoreTransaction method getDataStoreSorting.

/**
 * Returns the sorting (if any) that should be pushed to the datastore.
 * @param scope The request context
 * @param projection The projection being loaded.
 * @param filterInMemory Whether or not the transaction requires in memory filtering.
 * @return An optional sorting.
 */
private Optional<Sorting> getDataStoreSorting(RequestScope scope, EntityProjection projection, boolean filterInMemory) {
    Sorting sorting = projection.getSorting();
    if (filterInMemory) {
        return Optional.empty();
    }
    Map<Path, Sorting.SortOrder> sortRules = sorting == null ? new HashMap<>() : sorting.getSortingPaths();
    boolean sortingOnComputedAttribute = false;
    for (Path path : sortRules.keySet()) {
        if (path.isComputed(scope.getDictionary())) {
            Type<?> pathType = path.getPathElements().get(0).getType();
            if (projection.getType().equals(pathType)) {
                sortingOnComputedAttribute = true;
                break;
            }
        }
    }
    if (sortingOnComputedAttribute) {
        return Optional.empty();
    } else {
        return Optional.ofNullable(sorting);
    }
}
Also used : Path(com.yahoo.elide.core.Path) Sorting(com.yahoo.elide.core.request.Sorting)

Example 50 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class InMemoryStoreTransaction method sortAndPaginateLoadedData.

private DataStoreIterable<Object> sortAndPaginateLoadedData(DataStoreIterable<Object> loadedRecords, boolean sortingInMemory, Sorting sorting, Pagination pagination, RequestScope scope) {
    Map<Path, Sorting.SortOrder> sortRules = sorting == null ? new HashMap<>() : sorting.getSortingPaths();
    boolean mustSortInMemory = !sortRules.isEmpty() && (sortingInMemory || loadedRecords.needsInMemorySort());
    boolean mustPaginateInMemory = pagination != null && (mustSortInMemory || loadedRecords.needsInMemoryPagination());
    // Try to skip the data copy if possible
    if (!mustSortInMemory && !mustPaginateInMemory) {
        return loadedRecords;
    }
    // We need an in memory copy to sort or paginate.
    List<Object> results = StreamSupport.stream(loadedRecords.spliterator(), false).collect(Collectors.toList());
    if (!sortRules.isEmpty()) {
        results = sortInMemory(results, sortRules, scope);
    }
    if (pagination != null) {
        results = paginateInMemory(results, pagination);
    }
    return new DataStoreIterableBuilder(results).build();
}
Also used : Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder)

Aggregations

Path (com.yahoo.elide.core.Path)88 Test (org.junit.jupiter.api.Test)67 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)41 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)35 Query (com.yahoo.elide.datastores.aggregation.query.Query)35 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)34 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)33 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)31 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)29 Book (example.Book)29 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)22 Date (java.util.Date)19 Argument (com.yahoo.elide.core.request.Argument)14 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 GameRevenue (example.GameRevenue)14 HashSet (java.util.HashSet)14 Author (example.Author)12 Publisher (example.Publisher)9 Sorting (com.yahoo.elide.core.request.Sorting)8