use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.
the class ClientChannel method onQuery.
@SuppressWarnings("unchecked")
public QueryResponse onQuery(ObjectContext context, Query query) {
QueryResponse response = send(new QueryMessage(query), QueryResponse.class);
if (context != null) {
EntityResolver resolver = context.getEntityResolver();
QueryMetadata info = query.getMetaData(resolver);
if (!info.isFetchingDataRows()) {
response.reset();
while (response.next()) {
if (response.isList()) {
List objects = response.currentList();
if (!objects.isEmpty()) {
DeepMergeOperation merger = new DeepMergeOperation(context);
List<Object> rsMapping = info.getResultSetMapping();
if (rsMapping == null) {
convertSingleObjects(objects, merger);
} else {
if (rsMapping.size() == 1) {
if (rsMapping.get(0) instanceof EntityResultSegment) {
convertSingleObjects(objects, merger);
}
} else {
processMixedResult(objects, merger, rsMapping);
}
}
}
}
}
}
}
return response;
}
use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.
the class PrefetchProcessorTreeBuilderIT method testBuildTreeWithPrefetches.
@Test
public void testBuildTreeWithPrefetches() {
final ClassDescriptor descriptor = resolver.getClassDescriptor("Artist");
ObjEntity e2 = resolver.getObjEntity("Painting");
ObjEntity e3 = resolver.getObjEntity("Gallery");
ObjEntity e4 = resolver.getObjEntity("Exhibit");
ObjEntity e5 = resolver.getObjEntity("ArtistExhibit");
List<Object> mainRows = new ArrayList<Object>();
Map<Object, Object> extraRows = new HashMap<Object, Object>();
PrefetchTreeNode tree = new PrefetchTreeNode();
tree.addPath(Artist.PAINTING_ARRAY.getName()).setPhantom(false);
tree.addPath(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).dot(Gallery.EXHIBIT_ARRAY).getName()).setPhantom(false);
tree.addPath(Artist.ARTIST_EXHIBIT_ARRAY.getName()).setPhantom(false);
QueryMetadata metadata = new MockQueryMetadata() {
@Override
public ClassDescriptor getClassDescriptor() {
return descriptor;
}
@Override
public ObjEntity getObjEntity() {
return descriptor.getEntity();
}
@Override
public DbEntity getDbEntity() {
return getObjEntity().getDbEntity();
}
@Override
public DataMap getDataMap() {
return getObjEntity().getDataMap();
}
@Override
public boolean isRefreshingObjects() {
return true;
}
@Override
public boolean isResolvingInherited() {
return true;
}
};
HierarchicalObjectResolver resolver = new HierarchicalObjectResolver(context, metadata);
PrefetchProcessorTreeBuilder builder = new PrefetchProcessorTreeBuilder(resolver, mainRows, extraRows);
PrefetchProcessorNode n1 = builder.buildTree(tree);
assertSame(mainRows, n1.getDataRows());
assertSame(descriptor.getEntity(), n1.getResolver().getEntity());
PrefetchProcessorNode n2 = (PrefetchProcessorNode) n1.getNode("paintingArray");
assertNotNull(n2);
assertSame(e2, n2.getResolver().getEntity());
assertFalse(n2.isPhantom());
assertTrue(n2.isPartitionedByParent());
PrefetchProcessorNode n3 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery");
assertNotNull(n3);
assertSame(e3, n3.getResolver().getEntity());
assertTrue(n3.isPhantom());
assertTrue(n3.isPartitionedByParent());
PrefetchProcessorNode n4 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery.exhibitArray");
assertNotNull(n4);
assertSame(e4, n4.getResolver().getEntity());
assertFalse(n4.isPhantom());
assertTrue(n4.isPartitionedByParent());
PrefetchProcessorNode n5 = (PrefetchProcessorNode) n1.getNode("artistExhibitArray");
assertNotNull(n5);
assertSame(e5, n5.getResolver().getEntity());
assertFalse(n5.isPhantom());
assertTrue(n5.isPartitionedByParent());
}
use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.
the class DataContextQueryCachingIT method testSharedCacheDataRowsRefresh.
@Test
public void testSharedCacheDataRowsRefresh() throws Exception {
SelectQuery<DataRow> select = SelectQuery.dataRowQuery(Artist.class);
select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
try {
// first run, no cache yet
List<?> rows1 = mockupDataRows(2);
engine.reset();
engine.addExpectedResult(select, rows1);
List<?> resultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(rows1, resultRows);
QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
assertEquals(rows1, context.getParentDataDomain().getQueryCache().get(cacheKey));
assertNull(context.getQueryCache().get(cacheKey));
// second run, must refresh the cache
List<?> rows2 = mockupDataRows(5);
engine.reset();
engine.addExpectedResult(select, rows2);
select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE_REFRESH);
List<?> freshResultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(rows2, freshResultRows);
assertEquals(rows2, context.getParentDataDomain().getQueryCache().get(cacheKey));
assertNull(context.getQueryCache().get(cacheKey));
} finally {
engine.stopInterceptNode();
}
}
use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.
the class DataContextQueryCachingIT method testLocalCacheDataObjectsRefresh.
@Test
public void testLocalCacheDataObjectsRefresh() throws Exception {
SelectQuery<Artist> select = new SelectQuery<>(Artist.class);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
try {
// first run, no cache yet
List<?> rows1 = mockupDataRows(2);
engine.reset();
engine.addExpectedResult(select, rows1);
List<?> resultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(2, resultRows.size());
assertTrue(resultRows.get(0) instanceof DataObject);
QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(resultRows, context.getQueryCache().get(cacheKey));
// second run, must refresh the cache
List<?> rows2 = mockupDataRows(4);
engine.reset();
engine.addExpectedResult(select, rows2);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
List<?> freshResultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(4, freshResultRows.size());
assertTrue(resultRows.get(0) instanceof DataObject);
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(freshResultRows, context.getQueryCache().get(cacheKey));
} finally {
engine.stopInterceptNode();
}
}
use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.
the class DataContextQueryCachingIT method testLocalCacheDataRowsRefresh.
@Test
public void testLocalCacheDataRowsRefresh() throws Exception {
SelectQuery<DataRow> select = SelectQuery.dataRowQuery(Artist.class);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
try {
// first run, no cache yet
List<?> rows1 = mockupDataRows(2);
engine.reset();
engine.addExpectedResult(select, rows1);
List<?> resultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(rows1, resultRows);
QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(rows1, context.getQueryCache().get(cacheKey));
// second run, must refresh the cache
List<?> rows2 = mockupDataRows(4);
engine.reset();
engine.addExpectedResult(select, rows2);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
List<?> freshResultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(rows2, freshResultRows);
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(rows2, context.getQueryCache().get(cacheKey));
} finally {
engine.stopInterceptNode();
}
}
Aggregations