Search in sources :

Example 16 with DataStoreIterable

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

the class InMemoryStoreTransactionTest method testSortingPushDown.

@Test
public void testSortingPushDown() {
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("title", Sorting.SortOrder.asc);
    Sorting sorting = new SortingImpl(sortOrder, Book.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Book.class).sorting(sorting).build();
    DataStoreIterable expected = new DataStoreIterableBuilder<>(books).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(expected);
    DataStoreIterable actual = inMemoryStoreTransaction.loadObjects(projection, scope);
    verify(wrappedTransaction, times(1)).loadObjects(eq(projection), eq(scope));
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) HashMap(java.util.HashMap) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) Test(org.junit.jupiter.api.Test)

Example 17 with DataStoreIterable

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

the class JPQLTransaction method getToManyRelation.

@Override
public <T, R> DataStoreIterable<R> getToManyRelation(DataStoreTransaction relationTx, T entity, Relationship relation, RequestScope scope) {
    FilterExpression filterExpression = relation.getProjection().getFilterExpression();
    Sorting sorting = relation.getProjection().getSorting();
    Pagination pagination = relation.getProjection().getPagination();
    EntityDictionary dictionary = scope.getDictionary();
    Iterable val = (Iterable) com.yahoo.elide.core.PersistentResource.getValue(entity, relation.getName(), scope);
    // If the query is safe for N+1 and the value is an ORM managed, persistent collection, run a JPQL query...
    if (doInDatabase(entity) && val instanceof Collection && isPersistentCollection().test((Collection<?>) val)) {
        /*
             * If there is no filtering or sorting required in the data store, and the pagination is default,
             * return the proxy and let Hibernate manage the SQL generation.
             */
        if (filterExpression == null && sorting == null && (pagination == null || (pagination.isDefaultInstance()))) {
            return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
        }
        RelationshipImpl relationship = new RelationshipImpl(dictionary.lookupEntityClass(EntityDictionary.getType(entity)), entity, relation);
        if (pagination != null && pagination.returnPageTotals()) {
            pagination.setPageTotals(getTotalRecords(relationship, scope.getDictionary()));
        }
        final Query query = new SubCollectionFetchQueryBuilder(relationship, dictionary, sessionWrapper).build();
        if (query != null) {
            return new DataStoreIterableBuilder(addSingleElement(query.list())).build();
        }
    }
    return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Query(com.yahoo.elide.datastores.jpql.porting.Query) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) Collection(java.util.Collection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Sorting(com.yahoo.elide.core.request.Sorting)

Example 18 with DataStoreIterable

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

the class LifeCycleTest method testElidePatchRelationshipAddMultiple.

@Test
public void testElidePatchRelationshipAddMultiple() {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel parent = mock(FieldTestModel.class);
    FieldTestModel child1 = mock(FieldTestModel.class);
    FieldTestModel child2 = mock(FieldTestModel.class);
    FieldTestModel child3 = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"relationships\": { \"models\": { \"data\": [ { \"type\": \"testModel\", \"id\": \"2\" }, {\"type\": \"testModel\", \"id\": \"3\" } ] } } } }";
    dictionary.setValue(parent, "id", "1");
    dictionary.setValue(child1, "id", "2");
    dictionary.setValue(child2, "id", "3");
    dictionary.setValue(child3, "id", "4");
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), eq("1"), isA(RequestScope.class))).thenReturn(parent);
    when(tx.loadObject(isA(EntityProjection.class), eq("2"), isA(RequestScope.class))).thenReturn(child1);
    when(tx.loadObject(isA(EntityProjection.class), eq("3"), isA(RequestScope.class))).thenReturn(child2);
    when(tx.loadObject(isA(EntityProjection.class), eq("4"), isA(RequestScope.class))).thenReturn(child3);
    DataStoreIterable iterable = new DataStoreIterableBuilder(List.of(child3)).build();
    when(tx.getToManyRelation(any(), any(), isA(Relationship.class), isA(RequestScope.class))).thenReturn(iterable);
    String contentType = JSONAPI_CONTENT_TYPE;
    ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/testModel/1", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
    verify(parent, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
    verify(parent, times(4)).classCallback(eq(UPDATE), any());
    verify(parent, never()).classAllFieldsCallback(any(), any());
    verify(parent, never()).attributeCallback(any(), any(), any());
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) Relationship(com.yahoo.elide.core.request.Relationship) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Test(org.junit.jupiter.api.Test)

Example 19 with DataStoreIterable

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

the class PersistentResource method getRelationUnchecked.

/**
 * Retrieve an unchecked set of relations.
 */
private Observable<PersistentResource> getRelationUnchecked(com.yahoo.elide.core.request.Relationship relationship) {
    String relationName = relationship.getName();
    FilterExpression filterExpression = relationship.getProjection().getFilterExpression();
    Pagination pagination = relationship.getProjection().getPagination();
    Sorting sorting = relationship.getProjection().getSorting();
    RelationshipType type = getRelationshipType(relationName);
    final Type<?> relationClass = dictionary.getParameterizedType(obj, relationName);
    if (relationClass == null) {
        throw new InvalidAttributeException(relationName, this.getTypeName());
    }
    // Invoke filterExpressionCheck and then merge with filterExpression.
    Optional<FilterExpression> permissionFilter = getPermissionFilterExpression(relationClass, requestScope, relationship.getProjection().getRequestedFields());
    Optional<FilterExpression> computedFilters = Optional.ofNullable(filterExpression);
    if (permissionFilter.isPresent() && filterExpression != null) {
        FilterExpression mergedExpression = new AndFilterExpression(filterExpression, permissionFilter.get());
        computedFilters = Optional.of(mergedExpression);
    } else if (permissionFilter.isPresent()) {
        computedFilters = permissionFilter;
    }
    com.yahoo.elide.core.request.Relationship modifiedRelationship = relationship.copyOf().projection(relationship.getProjection().copyOf().filterExpression(computedFilters.orElse(null)).sorting(sorting).pagination(pagination).build()).build();
    Observable<PersistentResource> resources;
    if (type.isToMany()) {
        DataStoreIterable val = transaction.getToManyRelation(transaction, obj, modifiedRelationship, requestScope);
        if (val == null) {
            return Observable.empty();
        }
        resources = Observable.fromIterable(new PersistentResourceSet(this, relationName, val, requestScope));
    } else {
        Object val = transaction.getToOneRelation(transaction, obj, modifiedRelationship, requestScope);
        if (val == null) {
            return Observable.empty();
        }
        resources = Observable.fromArray(new PersistentResource(val, this, relationName, requestScope.getUUIDFor(val), requestScope));
    }
    return resources;
}
Also used : InvalidAttributeException(com.yahoo.elide.core.exceptions.InvalidAttributeException) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) Pagination(com.yahoo.elide.core.request.Pagination) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

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