Search in sources :

Example 6 with DataStoreIterable

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Author(example.Author) ClassType(com.yahoo.elide.core.type.ClassType) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) BeforeAll(org.junit.jupiter.api.BeforeAll) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) DEFAULT_LOGGER(com.yahoo.elide.datastores.jpa.JpaDataStore.DEFAULT_LOGGER) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Collection(java.util.Collection) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with DataStoreIterable

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Author(example.Author) ClassType(com.yahoo.elide.core.type.ClassType) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) BeforeAll(org.junit.jupiter.api.BeforeAll) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) DEFAULT_LOGGER(com.yahoo.elide.datastores.jpa.JpaDataStore.DEFAULT_LOGGER) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Arguments(org.junit.jupiter.params.provider.Arguments) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EntityProjection(com.yahoo.elide.core.request.EntityProjection) ArrayList(java.util.ArrayList) PersistentSet(org.hibernate.collection.internal.PersistentSet) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Collection(java.util.Collection) Author(example.Author) AbstractJpaTransaction(com.yahoo.elide.datastores.jpa.transaction.AbstractJpaTransaction) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with DataStoreIterable

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();
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) Query(com.yahoo.elide.datastores.jpql.porting.Query) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Iterator(java.util.Iterator) Collection(java.util.Collection) RootCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.RootCollectionFetchQueryBuilder)

Example 9 with DataStoreIterable

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));
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Author(example.Author) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Test(org.junit.jupiter.api.Test)

Example 10 with DataStoreIterable

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]);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Editor(example.Editor) Test(org.junit.jupiter.api.Test)

Aggregations

DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)19 EntityProjection (com.yahoo.elide.core.request.EntityProjection)15 Test (org.junit.jupiter.api.Test)13 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)12 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 Book (example.Book)9 Sorting (com.yahoo.elide.core.request.Sorting)8 Path (com.yahoo.elide.core.Path)7 RequestScope (com.yahoo.elide.core.RequestScope)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)7 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)7 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)6 Relationship (com.yahoo.elide.core.request.Relationship)6 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)6 Author (example.Author)6 List (java.util.List)6 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)5 ClassType (com.yahoo.elide.core.type.ClassType)5