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