use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder 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);
}
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class LifeCycleTest method testRemoveFromCollectionTrigger.
@Test
public void testRemoveFromCollectionTrigger() {
PropertyTestModel mockModel = mock(PropertyTestModel.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
RequestScope scope = buildRequestScope(dictionary, tx);
when(tx.createNewObject(ClassType.of(PropertyTestModel.class), scope)).thenReturn(mockModel);
PropertyTestModel childModel1 = mock(PropertyTestModel.class);
PropertyTestModel childModel2 = mock(PropertyTestModel.class);
PropertyTestModel childModel3 = mock(PropertyTestModel.class);
when(childModel1.getId()).thenReturn("2");
when(childModel2.getId()).thenReturn("3");
when(childModel3.getId()).thenReturn("4");
// First we test removing from a newly created object.
PersistentResource resource = PersistentResource.createObject(ClassType.of(PropertyTestModel.class), scope, Optional.of("1"));
PersistentResource childResource1 = new PersistentResource(childModel1, "2", scope);
PersistentResource childResource2 = new PersistentResource(childModel2, "3", scope);
PersistentResource childResource3 = new PersistentResource(childModel3, "3", scope);
resource.updateRelation("models", new HashSet<>(Arrays.asList(childResource1, childResource2)));
scope.runQueuedPreSecurityTriggers();
scope.runQueuedPreCommitTriggers();
scope.runQueuedPostCommitTriggers();
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
ArgumentCaptor<ChangeSpec> changes = ArgumentCaptor.forClass(ChangeSpec.class);
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), changes.capture());
changes.getValue().getModified().equals(List.of(childModel1, childModel2));
changes.getValue().getOriginal().equals(List.of());
// Build another resource, scope & reset the mock to do a pure update (no create):
scope = buildRequestScope(dictionary, tx);
resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
reset(mockModel);
Relationship relationship = Relationship.builder().projection(EntityProjection.builder().type(PropertyTestModel.class).build()).name("models").build();
when(tx.getToManyRelation(tx, mockModel, relationship, scope)).thenReturn(new DataStoreIterableBuilder<Object>(Arrays.asList(childModel1, childModel2)).build());
when(mockModel.getModels()).thenReturn(new HashSet<>(Arrays.asList(childModel1, childModel2)));
resource.updateRelation("models", new HashSet<>(Arrays.asList(childResource1, childResource3)));
scope.runQueuedPreSecurityTriggers();
scope.runQueuedPreCommitTriggers();
scope.runQueuedPostCommitTriggers();
verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
changes = ArgumentCaptor.forClass(ChangeSpec.class);
verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), changes.capture());
changes.getValue().getModified().equals(List.of(childModel1, childModel3));
changes.getValue().getOriginal().equals(List.of(childModel1, childModel2));
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class PersistentResourceTest method testGetRelationByIdSuccess.
@Test
public void testGetRelationByIdSuccess() {
FunWithPermissions fun = new FunWithPermissions();
Child child1 = newChild(1);
Child child2 = newChild(2);
Child child3 = newChild(3);
fun.setRelation2(Sets.newHashSet(child1, child2, child3));
when(tx.getToManyRelation(eq(tx), any(), any(), any())).thenReturn(new DataStoreIterableBuilder(Sets.newHashSet(child1)).build());
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, "3", goodScope);
PersistentResource<?> result = funResource.getRelation(getRelationship(ClassType.of(FunWithPermissions.class), "relation2"), "1");
assertEquals(1, ((Child) result.getObject()).getId(), "The correct relationship element should be returned");
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class InMemoryStoreTransactionTest method testSortOnComplexAttribute.
@Test
public void testSortOnComplexAttribute() {
Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
sortOrder.put("homeAddress.street1", Sorting.SortOrder.asc);
Sorting sorting = new SortingImpl(sortOrder, Author.class, dictionary);
EntityProjection projection = EntityProjection.builder().type(Author.class).sorting(sorting).build();
DataStoreIterable sortInMemory = new DataStoreIterableBuilder(Arrays.asList(author1, author2)).sortInMemory(true).build();
when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
assertEquals(2, loaded.size());
Object[] sorted = loaded.toArray();
assertEquals(author2, sorted[0]);
assertEquals(author1, sorted[1]);
}
use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.
the class InMemoryStoreTransactionTest method testSortingRequiresInMemoryPagination.
@Test
public void testSortingRequiresInMemoryPagination() {
PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 3, 10, 10, true, false);
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).pagination(pagination).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(Lists.newArrayList("Book 3", "Book 2", "Book 1"), bookTitles);
assertEquals(3, pagination.getPageTotals());
}
Aggregations