Search in sources :

Example 1 with RefreshQuery

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;
}
Also used : RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) PrefetchSelectQuery(org.apache.cayenne.query.PrefetchSelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) GenericResponse(org.apache.cayenne.util.GenericResponse) ObjectId(org.apache.cayenne.ObjectId) RefreshQuery(org.apache.cayenne.query.RefreshQuery) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Persistent(org.apache.cayenne.Persistent)

Example 2 with RefreshQuery

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;
}
Also used : Query(org.apache.cayenne.query.Query) RefreshQuery(org.apache.cayenne.query.RefreshQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery)

Example 3 with RefreshQuery

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Test(org.junit.Test)

Example 4 with RefreshQuery

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());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 5 with RefreshQuery

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());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

RefreshQuery (org.apache.cayenne.query.RefreshQuery)24 Test (org.junit.Test)21 SelectQuery (org.apache.cayenne.query.SelectQuery)9 Artist (org.apache.cayenne.testdo.testmap.Artist)9 Painting (org.apache.cayenne.testdo.testmap.Painting)5 Map (java.util.Map)3 Set (java.util.Set)3 Expression (org.apache.cayenne.exp.Expression)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 Query (org.apache.cayenne.query.Query)3 IdMapToMany (org.apache.cayenne.testdo.map_to_many.IdMapToMany)3 MapToMany (org.apache.cayenne.testdo.map_to_many.MapToMany)3 MapToManyTarget (org.apache.cayenne.testdo.map_to_many.MapToManyTarget)3 CollectionToMany (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany)3 CollectionToManyTarget (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget)3 SetToMany (org.apache.cayenne.testdo.relationships_set_to_many.SetToMany)3 SetToManyTarget (org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget)3 Collection (java.util.Collection)2 ClientIdMapToMany (org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany)2 ClientIdMapToManyTarget (org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget)2