Search in sources :

Example 1 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class AggregationDataStoreTransaction method loadObjects.

@Override
public <T> DataStoreIterable<T> loadObjects(EntityProjection entityProjection, RequestScope scope) {
    QueryResult result = null;
    QueryResponse response = null;
    String cacheKey = null;
    try {
        // Convert multivalued map to map.
        Map<String, String> headers = scope.getRequestHeaders().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> entry.getValue().stream().collect(Collectors.joining(" "))));
        queryLogger.acceptQuery(scope.getRequestId(), scope.getUser(), headers, scope.getApiVersion(), scope.getQueryParams(), scope.getPath());
        Query query = buildQuery(entityProjection, scope);
        Table table = (Table) query.getSource();
        if (cache != null && !query.isBypassingCache()) {
            String tableVersion = queryEngine.getTableVersion(table, queryEngineTransaction);
            tableVersion = tableVersion == null ? "" : tableVersion;
            cacheKey = tableVersion + ';' + QueryKeyExtractor.extractKey(query);
            result = cache.get(cacheKey);
        }
        boolean isCached = result != null;
        List<String> queryText = queryEngine.explain(query);
        queryLogger.processQuery(scope.getRequestId(), query, queryText, isCached);
        if (result == null) {
            result = queryEngine.executeQuery(query, queryEngineTransaction);
            if (cacheKey != null) {
                // The query result needs to be streamed into an in memory list before caching.
                // TODO - add a cap to how many records can be streamed back.  If this is exceeded, abort caching
                // and return the results.
                QueryResult cacheableResult = QueryResult.builder().data(Lists.newArrayList(result.getData().iterator())).pageTotals(result.getPageTotals()).build();
                cache.put(cacheKey, cacheableResult);
                result = cacheableResult;
            }
        }
        if (entityProjection.getPagination() != null && entityProjection.getPagination().returnPageTotals()) {
            entityProjection.getPagination().setPageTotals(result.getPageTotals());
        }
        response = new QueryResponse(HttpStatus.SC_OK, result.getData(), null);
        return new DataStoreIterableBuilder(result.getData()).build();
    } catch (HttpStatusException e) {
        response = new QueryResponse(e.getStatus(), null, e.getMessage());
        throw e;
    } catch (Exception e) {
        response = new QueryResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, null, e.getMessage());
        throw e;
    } finally {
        queryLogger.completeQuery(scope.getRequestId(), response);
    }
}
Also used : HttpStatus(com.yahoo.elide.core.exceptions.HttpStatus) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Argument(com.yahoo.elide.core.request.Argument) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) Map(java.util.Map) MatchesTemplateVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.MatchesTemplateVisitor) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) RequiresFilter(com.yahoo.elide.datastores.aggregation.metadata.models.RequiresFilter) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) Cache(com.yahoo.elide.datastores.aggregation.cache.Cache) Lists(org.apache.commons.compress.utils.Lists) EntityProjection(com.yahoo.elide.core.request.EntityProjection) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryKeyExtractor(com.yahoo.elide.datastores.aggregation.cache.QueryKeyExtractor) Type(com.yahoo.elide.core.type.Type) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) QueryLogger(com.yahoo.elide.datastores.aggregation.core.QueryLogger) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Query(com.yahoo.elide.datastores.aggregation.query.Query) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) ToString(lombok.ToString) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) QueryResponse(com.yahoo.elide.datastores.aggregation.core.QueryResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class EntityProjectionMakerTest method testRootEntityWithSingleInclude.

@Test
public void testRootEntityWithSingleInclude() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.add("include", "authors");
    String path = "/book/1";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).attribute(Attribute.builder().name("name").type(String.class).build()).attribute(Attribute.builder().name("type").type(Author.AuthorType.class).build()).attribute(Attribute.builder().name("homeAddress").type(Address.class).build()).attribute(Attribute.builder().name("vacationHomes").type(Set.class).build()).attribute(Attribute.builder().name("stuff").type(Map.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).relationship("books", EntityProjection.builder().type(Book.class).build()).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Address(example.Address) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Book(example.Book) Collection(java.util.Collection) Author(example.Author) Editor(example.Editor) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.jupiter.api.Test)

Example 3 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class EntityProjectionMakerTest method testNestedCollectionNoQueryParams.

@Test
public void testNestedCollectionNoQueryParams() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    String path = "/author/1/books/3/publisher";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Author.class).relationship("books", EntityProjection.builder().type(Book.class).relationship("publisher", EntityProjection.builder().type(Publisher.class).attribute(Attribute.builder().name("name").type(String.class).build()).attribute(Attribute.builder().name("updateHookInvoked").type(boolean.class).build()).relationship("books", EntityProjection.builder().type(Book.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Publisher.class))).build()).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Publisher(example.Publisher) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 4 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class EntityProjectionMakerTest method testRootEntityNoQueryParams.

@Test
public void testRootEntityNoQueryParams() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    String path = "/book/1";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Collection(java.util.Collection) Author(example.Author) Editor(example.Editor) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Example 5 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class EntityProjectionMakerTest method testRootCollectionNoQueryParams.

@Test
public void testRootCollectionNoQueryParams() {
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    String path = "/book";
    RequestScope scope = new TestRequestScope(dictionary, path, queryParams);
    EntityProjectionMaker maker = new EntityProjectionMaker(dictionary, scope);
    EntityProjection expected = EntityProjection.builder().type(Book.class).attribute(Attribute.builder().name("title").type(String.class).build()).attribute(Attribute.builder().name("awards").type(Collection.class).build()).attribute(Attribute.builder().name("genre").type(String.class).build()).attribute(Attribute.builder().name("language").type(String.class).build()).attribute(Attribute.builder().name("publishDate").type(long.class).build()).attribute(Attribute.builder().name("authorTypes").type(Collection.class).build()).attribute(Attribute.builder().name("price").type(Price.class).build()).relationship("authors", EntityProjection.builder().type(Author.class).build()).relationship("publisher", EntityProjection.builder().type(Publisher.class).build()).relationship("editor", EntityProjection.builder().type(Editor.class).build()).pagination(PaginationImpl.getDefaultPagination(ClassType.of(Book.class))).build();
    EntityProjection actual = maker.parsePath(path);
    projectionEquals(expected, actual);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TestRequestScope(com.yahoo.elide.core.TestRequestScope) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Price(example.Price) Book(example.Book) Publisher(example.Publisher) RequestScope(com.yahoo.elide.core.RequestScope) TestRequestScope(com.yahoo.elide.core.TestRequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

EntityProjection (com.yahoo.elide.core.request.EntityProjection)108 Test (org.junit.jupiter.api.Test)90 Book (example.Book)41 RequestScope (com.yahoo.elide.core.RequestScope)27 Author (example.Author)27 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 Publisher (example.Publisher)22 Relationship (com.yahoo.elide.core.request.Relationship)19 Path (com.yahoo.elide.core.Path)18 TestRequestScope (com.yahoo.elide.core.TestRequestScope)18 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)18 HashMap (java.util.HashMap)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)16 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)15 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)15 Collection (java.util.Collection)15 LinkedHashSet (java.util.LinkedHashSet)14 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)13 Editor (example.Editor)13