Search in sources :

Example 36 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class InMemoryStoreTransactionTest method testFilteringRequiresInMemorySorting.

@Test
public void testFilteringRequiresInMemorySorting() {
    FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction");
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("title", Sorting.SortOrder.desc);
    Sorting sorting = new SortingImpl(sortOrder, Book.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Book.class).filterExpression(expression).sorting(sorting).build();
    DataStoreIterable filterInMemory = new DataStoreIterableBuilder(books).filterInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(2, loaded.size());
    List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList());
    assertEquals(Lists.newArrayList("Book 3", "Book 1"), bookTitles);
}
Also used : Path(com.yahoo.elide.core.Path) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Publisher(example.Publisher) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ClassType(com.yahoo.elide.core.type.ClassType) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) RequestScope(com.yahoo.elide.core.RequestScope) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) HashMap(java.util.HashMap) Author(example.Author) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Editor(example.Editor) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) ElideSettings(com.yahoo.elide.ElideSettings) Price(example.Price) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Sorting(com.yahoo.elide.core.request.Sorting) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Mockito.times(org.mockito.Mockito.times) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Address(example.Address) Mockito.reset(org.mockito.Mockito.reset) EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Test(org.junit.jupiter.api.Test)

Example 37 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class InMemoryStoreTransactionTest method testDataStoreRequiresInMemorySorting.

@Test
public void testDataStoreRequiresInMemorySorting() {
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("title", Sorting.SortOrder.desc);
    Sorting sorting = new SortingImpl(sortOrder, Book.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Book.class).sorting(sorting).build();
    DataStoreIterable sortInMemory = new DataStoreIterableBuilder(books).sortInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(3, loaded.size());
    List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList());
    assertEquals(bookTitles, Lists.newArrayList("Book 3", "Book 2", "Book 1"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Publisher(example.Publisher) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ClassType(com.yahoo.elide.core.type.ClassType) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) RequestScope(com.yahoo.elide.core.RequestScope) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) HashMap(java.util.HashMap) Author(example.Author) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Editor(example.Editor) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) ElideSettings(com.yahoo.elide.ElideSettings) Price(example.Price) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Sorting(com.yahoo.elide.core.request.Sorting) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Mockito.times(org.mockito.Mockito.times) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Address(example.Address) Mockito.reset(org.mockito.Mockito.reset) 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) Book(example.Book) Test(org.junit.jupiter.api.Test)

Example 38 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class InMemoryStoreTransactionTest method testPaginationPushDown.

@Test
public void testPaginationPushDown() {
    PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 1, 10, 10, false, false);
    EntityProjection projection = EntityProjection.builder().type(Book.class).pagination(pagination).build();
    ArgumentCaptor<EntityProjection> projectionArgument = ArgumentCaptor.forClass(EntityProjection.class);
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(new DataStoreIterableBuilder<>(books).build());
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(projectionArgument.capture(), eq(scope));
    assertEquals(pagination, projectionArgument.getValue().getPagination());
    assertEquals(3, loaded.size());
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) Test(org.junit.jupiter.api.Test)

Example 39 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder 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 40 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder 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

DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)46 Test (org.junit.jupiter.api.Test)41 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)20 Child (example.Child)16 Book (example.Book)15 EntityProjection (com.yahoo.elide.core.request.EntityProjection)13 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)12 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)11 Parent (example.Parent)11 Path (com.yahoo.elide.core.Path)8 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)7 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)7 Sorting (com.yahoo.elide.core.request.Sorting)7 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)7 Author (example.Author)7 ArrayList (java.util.ArrayList)7 RequestScope (com.yahoo.elide.core.RequestScope)6 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)6 Relationship (com.yahoo.elide.core.request.Relationship)6 HashMap (java.util.HashMap)6