use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataDomainQueryAction method interceptRefreshQuery.
/**
* @since 3.0
*/
private boolean interceptRefreshQuery() {
if (query instanceof RefreshQuery) {
RefreshQuery refreshQuery = (RefreshQuery) query;
if (refreshQuery.isRefreshAll()) {
// not sending any events - peer contexts will not get refreshed
if (domain.getSharedSnapshotCache() != null) {
domain.getSharedSnapshotCache().clear();
} else {
// remove snapshots from local ObjectStore only
context.getObjectStore().getDataRowCache().clear();
}
context.getQueryCache().clear();
GenericResponse response = new GenericResponse();
response.addUpdateCount(1);
this.response = response;
return DONE;
}
Collection<Persistent> objects = (Collection<Persistent>) refreshQuery.getObjects();
if (objects != null && !objects.isEmpty()) {
Collection<ObjectId> ids = new ArrayList<>(objects.size());
for (final Persistent object : objects) {
ids.add(object.getObjectId());
}
if (domain.getSharedSnapshotCache() != null) {
// send an event for removed snapshots
domain.getSharedSnapshotCache().processSnapshotChanges(context.getObjectStore(), Collections.EMPTY_MAP, Collections.EMPTY_LIST, ids, Collections.EMPTY_LIST);
} else {
// remove snapshots from local ObjectStore only
context.getObjectStore().getDataRowCache().processSnapshotChanges(context.getObjectStore(), Collections.EMPTY_MAP, Collections.EMPTY_LIST, ids, Collections.EMPTY_LIST);
}
GenericResponse response = new GenericResponse();
response.addUpdateCount(1);
this.response = response;
return DONE;
}
// usually does a cascading refresh
if (refreshQuery.getQuery() != null) {
Query cachedQuery = refreshQuery.getQuery();
String cacheKey = cachedQuery.getMetaData(context.getEntityResolver()).getCacheKey();
context.getQueryCache().remove(cacheKey);
this.response = domain.onQuery(context, cachedQuery);
return DONE;
}
// 4. refresh groups...
if (refreshQuery.getGroupKeys() != null && refreshQuery.getGroupKeys().length > 0) {
String[] groups = refreshQuery.getGroupKeys();
for (String group : groups) {
domain.getQueryCache().removeGroup(group);
}
GenericResponse response = new GenericResponse();
response.addUpdateCount(1);
this.response = response;
return DONE;
}
}
return !DONE;
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class CayenneContextQueryAction method interceptRefreshQuery.
@Override
protected boolean interceptRefreshQuery() {
if (query instanceof RefreshQuery) {
RefreshQuery refreshQuery = (RefreshQuery) query;
CayenneContext context = (CayenneContext) actingContext;
// 1. refresh all
if (refreshQuery.isRefreshAll()) {
invalidateLocally(context.internalGraphManager(), context.internalGraphManager().registeredNodes().iterator());
context.getQueryCache().clear();
// cascade
return !DONE;
}
// 2. invalidate object collection
Collection<?> objects = refreshQuery.getObjects();
if (objects != null && !objects.isEmpty()) {
invalidateLocally(context.internalGraphManager(), objects.iterator());
// cascade
return !DONE;
}
// 3. refresh query - have to do it eagerly to refresh the objects involved
if (refreshQuery.getQuery() != null) {
Query cachedQuery = refreshQuery.getQuery();
String cacheKey = cachedQuery.getMetaData(context.getEntityResolver()).getCacheKey();
context.getQueryCache().remove(cacheKey);
this.response = context.performGenericQuery(cachedQuery);
// do not cascade to avoid running query twice
return DONE;
}
// 4. refresh groups...
if (refreshQuery.getGroupKeys() != null && refreshQuery.getGroupKeys().length > 0) {
String[] groups = refreshQuery.getGroupKeys();
for (String group : groups) {
context.getQueryCache().removeGroup(group);
}
// cascade group invalidation
return !DONE;
}
}
return !DONE;
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshCollection.
@Test
public void testRefreshCollection() throws Exception {
createRefreshCollectionDataSet();
SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
q.addOrdering("db:ARTIST_ID", SortOrder.ASCENDING);
List<?> artists = context.performQuery(q);
Artist a1 = (Artist) artists.get(0);
Artist a2 = (Artist) artists.get(1);
assertEquals(2, a1.getPaintingArray().size());
assertEquals(0, a2.getPaintingArray().size());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a2.getObjectId()));
RefreshQuery refresh = new RefreshQuery(artists);
context.performQuery(refresh);
assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a1.getObjectId()));
assertNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(a2.getObjectId()));
assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, a2.getPersistenceState());
assertTrue(((ValueHolder) a1.readProperty(Artist.PAINTING_ARRAY.getName())).isFault());
assertTrue(((ValueHolder) a2.readProperty(Artist.PAINTING_ARRAY.getName())).isFault());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultsLocalCache.
@Test
public void testRefreshQueryResultsLocalCache() throws Exception {
createRefreshCollectionDataSet();
Expression qual = Painting.PAINTING_TITLE.eq("P2");
SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
q.setCacheGroup("X");
List<?> paints = context.performQuery(q);
// fetch P1 separately from cached query
Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
Painting p2 = (Painting) paints.get(0);
Artist a1 = p2.getToArtist();
assertSame(a1, p1.getToArtist());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
createRefreshCollectionToOneUpdateDataSet();
RefreshQuery refresh = new RefreshQuery(q);
context.performQuery(refresh);
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
// probably refreshed eagerly
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
assertSame(a1, p1.getToArtist());
assertNotSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("b", p2.getToArtist().getArtistName());
}
use of org.apache.cayenne.query.RefreshQuery in project cayenne by apache.
the class DataContextRefreshQueryIT method testRefreshQueryResultGroupLocal.
@Test
public void testRefreshQueryResultGroupLocal() throws Exception {
createRefreshCollectionDataSet();
Expression qual = Painting.PAINTING_TITLE.eq("P2");
SelectQuery<Painting> q = new SelectQuery<>(Painting.class, qual);
q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
q.setCacheGroup("X");
List<?> paints = context.performQuery(q);
// fetch P1 separately from cached query
Painting p1 = Cayenne.objectForPK(context, Painting.class, 33001);
Painting p2 = (Painting) paints.get(0);
Artist a1 = p2.getToArtist();
assertSame(a1, p1.getToArtist());
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p1.getObjectId()));
assertNotNull(context.getParentDataDomain().getSharedSnapshotCache().getCachedSnapshot(p2.getObjectId()));
createRefreshCollectionToOneUpdateDataSet();
// results are served from cache and therefore are not refreshed
context.performQuery(q);
assertSame(a1, p1.getToArtist());
assertSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("c", p2.getToArtist().getArtistName());
RefreshQuery refresh = new RefreshQuery("X");
// this should invalidate results for the next query run
context.performQuery(refresh);
// this should force a refresh
context.performQuery(q);
assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, p2.getPersistenceState());
assertSame(a1, p1.getToArtist());
assertNotSame(a1, p2.getToArtist());
assertEquals("c", p1.getToArtist().getArtistName());
assertEquals("b", p2.getToArtist().getArtistName());
}
Aggregations