use of com.yahoo.elide.core.datastore.DataStoreIterable in project elide by yahoo.
the class JpaDataStoreTransactionTest method testNoDelegationOnLoadRecords.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testNoDelegationOnLoadRecords(boolean delegateToInMemory) {
AbstractJpaTransaction tx = new AbstractJpaTransaction(entityManager, (unused) -> {
}, DEFAULT_LOGGER, delegateToInMemory) {
@Override
public boolean isOpen() {
return false;
}
@Override
public void begin() {
}
@Override
protected Predicate<Collection<?>> isPersistentCollection() {
return (unused) -> true;
}
};
EntityProjection projection = EntityProjection.builder().type(Book.class).build();
DataStoreIterable<Book> result = tx.loadObjects(projection, scope);
assertFalse(result.needsInMemoryFilter());
assertFalse(result.needsInMemorySort());
assertFalse(result.needsInMemoryPagination());
}
use of com.yahoo.elide.core.datastore.DataStoreIterable in project elide by yahoo.
the class JpaDataStoreTransactionTest method testGetRelationDelegation.
@ParameterizedTest
@MethodSource("getTestArguments")
public void testGetRelationDelegation(boolean delegateToInMemory, int numberOfAuthors, FilterExpression filter, boolean usesInMemory) throws Exception {
AbstractJpaTransaction tx = new AbstractJpaTransaction(entityManager, (unused) -> {
}, DEFAULT_LOGGER, delegateToInMemory, false) {
@Override
public boolean isOpen() {
return false;
}
@Override
public void begin() {
}
@Override
protected Predicate<Collection<?>> isPersistentCollection() {
return (unused) -> true;
}
};
EntityProjection projection = EntityProjection.builder().type(Author.class).build();
List<Author> authors = new ArrayList<>();
Author author1 = mock(Author.class);
authors.add(author1);
for (int idx = 1; idx < numberOfAuthors; idx++) {
authors.add(mock(Author.class));
}
when(query.getResultList()).thenReturn(authors);
DataStoreIterable<Author> loadedAuthors = tx.loadObjects(projection, scope);
assertFalse(loadedAuthors.needsInMemoryPagination());
assertFalse(loadedAuthors.needsInMemorySort());
assertFalse(loadedAuthors.needsInMemoryFilter());
Relationship relationship = Relationship.builder().name("books").projection(EntityProjection.builder().type(Book.class).filterExpression(filter).build()).build();
PersistentSet returnCollection = mock(PersistentSet.class);
when(author1.getBooks()).thenReturn(returnCollection);
DataStoreIterable<Book> loadedBooks = tx.getToManyRelation(tx, author1, relationship, scope);
assertEquals(usesInMemory, loadedBooks.needsInMemoryFilter());
assertEquals(usesInMemory, loadedBooks.needsInMemorySort());
assertEquals(usesInMemory, loadedBooks.needsInMemoryPagination());
}
use of com.yahoo.elide.core.datastore.DataStoreIterable in project elide by yahoo.
the class JPQLTransaction method loadObjects.
@Override
public <T> DataStoreIterable<T> loadObjects(EntityProjection projection, RequestScope scope) {
Pagination pagination = projection.getPagination();
final Query query = new RootCollectionFetchQueryBuilder(projection, scope.getDictionary(), sessionWrapper).build();
Iterable<T> results = new TimedFunction<Iterable<T>>(() -> {
return isScrollEnabled ? query.scroll() : query.list();
}, "Query Hash: " + query.hashCode()).get();
final boolean hasResults;
if (results instanceof Collection) {
hasResults = !((Collection) results).isEmpty();
} else if (results instanceof Iterator) {
hasResults = ((Iterator) results).hasNext();
} else {
hasResults = results.iterator().hasNext();
}
if (pagination != null) {
// Issue #1429
if (pagination.returnPageTotals() && (hasResults || pagination.getLimit() == 0)) {
pagination.setPageTotals(getTotalRecords(projection, scope.getDictionary()));
}
}
return new DataStoreIterableBuilder<T>(addSingleElement(results)).build();
}
use of com.yahoo.elide.core.datastore.DataStoreIterable in project elide by yahoo.
the class InMemoryStoreTransactionTest method testFilterPredicateInMemoryOnComplexAttribute.
@Test
public void testFilterPredicateInMemoryOnComplexAttribute() {
FilterExpression expression = new InPredicate(new Path(Author.class, dictionary, "homeAddress.street1"), "Foo");
EntityProjection projection = EntityProjection.builder().type(Author.class).filterExpression(expression).build();
DataStoreIterable filterInMemory = new DataStoreIterableBuilder(Arrays.asList(author1, author2)).filterInMemory(true).build();
when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
Collection<Object> loaded = ImmutableList.copyOf(inMemoryStoreTransaction.loadObjects(projection, scope));
assertEquals(1, loaded.size());
assertTrue(loaded.contains(author1));
}
use of com.yahoo.elide.core.datastore.DataStoreIterable in project elide by yahoo.
the class InMemoryStoreTransactionTest method testSortOnComputedAttribute.
@Test
public void testSortOnComputedAttribute() {
Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
sortOrder.put("fullName", Sorting.SortOrder.desc);
Editor editor1 = new Editor();
editor1.setFirstName("A");
editor1.setLastName("X");
Editor editor2 = new Editor();
editor2.setFirstName("B");
editor2.setLastName("Y");
Sorting sorting = new SortingImpl(sortOrder, Editor.class, dictionary);
EntityProjection projection = EntityProjection.builder().type(Editor.class).sorting(sorting).build();
DataStoreIterable iterable = new DataStoreIterableBuilder(Arrays.asList(editor1, editor2)).sortInMemory(false).build();
ArgumentCaptor<EntityProjection> projectionArgument = ArgumentCaptor.forClass(EntityProjection.class);
when(wrappedTransaction.loadObjects(projectionArgument.capture(), eq(scope))).thenReturn(iterable);
Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
assertNull(projectionArgument.getValue().getSorting());
assertEquals(2, loaded.size());
Object[] sorted = loaded.toArray();
assertEquals(editor2, sorted[0]);
assertEquals(editor1, sorted[1]);
}
Aggregations