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