Search in sources :

Example 1 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class CacheInvalidationFilter method onSync.

public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType, DataChannelFilterChain filterChain) {
    try {
        GraphDiff result = filterChain.onSync(originatingContext, changes, syncType);
        // no exceptions, flush...
        Collection<CacheGroupDescriptor> groupSet = groups.get();
        if (groupSet != null && !groupSet.isEmpty()) {
            QueryCache cache = cacheProvider.get();
            for (CacheGroupDescriptor group : groupSet) {
                if (group.getKeyType() != Void.class) {
                    cache.removeGroup(group.getCacheGroupName(), group.getKeyType(), group.getValueType());
                } else {
                    cache.removeGroup(group.getCacheGroupName());
                }
            }
        }
        return result;
    } finally {
        groups.set(null);
    }
}
Also used : QueryCache(org.apache.cayenne.cache.QueryCache) GraphDiff(org.apache.cayenne.graph.GraphDiff)

Example 2 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class CommitLogFilter method onSync.

@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff beforeDiff, int syncType, DataChannelFilterChain filterChain) {
    // process commits only; skip rollback
    if (syncType != DataChannel.FLUSH_CASCADE_SYNC && syncType != DataChannel.FLUSH_NOCASCADE_SYNC) {
        return filterChain.onSync(originatingContext, beforeDiff, syncType);
    }
    // don't collect changes if there are no listeners
    if (listeners.isEmpty()) {
        return filterChain.onSync(originatingContext, beforeDiff, syncType);
    }
    MutableChangeMap changes = new MutableChangeMap();
    // passing DataDomain, not ObjectContext to speed things up
    // and avoid capturing changed state when fetching snapshots
    DataChannel channel = originatingContext.getChannel();
    beforeCommit(changes, channel, beforeDiff);
    GraphDiff afterDiff = filterChain.onSync(originatingContext, beforeDiff, syncType);
    afterCommit(changes, channel, beforeDiff, afterDiff);
    notifyListeners(originatingContext, changes);
    return afterDiff;
}
Also used : DataChannel(org.apache.cayenne.DataChannel) GraphDiff(org.apache.cayenne.graph.GraphDiff) MutableChangeMap(org.apache.cayenne.commitlog.model.MutableChangeMap)

Example 3 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class TransactionFilter method onSync.

@Override
public GraphDiff onSync(final ObjectContext originatingContext, final GraphDiff changes, final int syncType, final DataChannelFilterChain filterChain) {
    DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(originatingContext.getChannel().getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
    callbackAction.applyPreCommit();
    GraphDiff result;
    switch(syncType) {
        case DataChannel.ROLLBACK_CASCADE_SYNC:
            result = filterChain.onSync(originatingContext, changes, syncType);
            ;
            break;
        // including transaction handling logic
        case DataChannel.FLUSH_NOCASCADE_SYNC:
        case DataChannel.FLUSH_CASCADE_SYNC:
            result = transactionManager.performInTransaction(new TransactionalOperation<GraphDiff>() {

                @Override
                public GraphDiff perform() {
                    return filterChain.onSync(originatingContext, changes, syncType);
                }
            });
            break;
        default:
            throw new CayenneRuntimeException("Invalid synchronization type: %d", syncType);
    }
    callbackAction.applyPostCommit();
    return result;
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) DataChannelSyncCallbackAction(org.apache.cayenne.DataChannelSyncCallbackAction) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 4 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class CayenneContextIT method testCommitChangesNew.

@Test
public void testCommitChangesNew() {
    final CompoundDiff diff = new CompoundDiff();
    final Object newObjectId = ObjectId.of("test", "key", "generated");
    eventManager = new DefaultEventManager(0);
    // test that ids that are passed back are actually propagated to the
    // right
    // objects...
    MockDataChannel channel = new MockDataChannel() {

        @Override
        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            return diff;
        }

        // must provide a channel with working event manager
        @Override
        public EventManager getEventManager() {
            return eventManager;
        }
    };
    CayenneContext context = new CayenneContext(channel);
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    Persistent object = context.newObject(MockPersistentObject.class);
    // record change here to make it available to the anonymous connector
    // method..
    diff.add(new NodeIdChangeOperation(object.getObjectId(), newObjectId));
    // check that a generated object ID is assigned back to the object...
    assertNotSame(newObjectId, object.getObjectId());
    context.commitChanges();
    assertSame(newObjectId, object.getObjectId());
    assertSame(object, context.graphManager.getNode(newObjectId));
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation) GraphDiff(org.apache.cayenne.graph.GraphDiff) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 5 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class NestedCayenneContextIT method testCommitChangesToParent.

@Test
public void testCommitChangesToParent() {
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    List<ClientMtTable1> objects = ObjectSelect.query(ClientMtTable1.class).select(child);
    assertEquals(4, objects.size());
    final ClientMtTable1 childNew = child.newObject(ClientMtTable1.class);
    childNew.setGlobalAttribute1("NNN");
    final ClientMtTable1 childModified = objects.get(0);
    childModified.setGlobalAttribute1("MMM");
    final ClientMtTable1 childCommitted = objects.get(1);
    final ClientMtTable1 childHollow = objects.get(3);
    child.invalidateObjects(childHollow);
    queryInterceptor.runWithQueriesBlocked(() -> {
        child.commitChangesToParent();
        // * all modified child objects must be in committed state now
        // * all modifications should be propagated to the parent
        // * no actual commit should occur.
        assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
        assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
        ClientMtTable1 parentNew = (ClientMtTable1) clientContext.getGraphManager().getNode(childNew.getObjectId());
        final ClientMtTable1 parentModified = (ClientMtTable1) clientContext.getGraphManager().getNode(childModified.getObjectId());
        ClientMtTable1 parentCommitted = (ClientMtTable1) clientContext.getGraphManager().getNode(childCommitted.getObjectId());
        ClientMtTable1 parentHollow = (ClientMtTable1) clientContext.getGraphManager().getNode(childHollow.getObjectId());
        assertNotNull(parentNew);
        assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
        assertEquals("NNN", parentNew.getGlobalAttribute1());
        assertNotNull(parentModified);
        assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
        assertEquals("MMM", parentModified.getGlobalAttribute1());
        assertNotNull(parentCommitted);
        assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
        assertNotNull(parentHollow);
        // check that arc changes got recorded in the parent context
        GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
        final int[] modifiedProperties = new int[1];
        diffs.apply(new GraphChangeHandler() {

            @Override
            public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
            }

            @Override
            public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
            }

            @Override
            public void nodeCreated(Object nodeId) {
            }

            @Override
            public void nodeIdChanged(Object nodeId, Object newId) {
            }

            @Override
            public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
                if (nodeId.equals(parentModified.getObjectId())) {
                    modifiedProperties[0]++;
                }
            }

            @Override
            public void nodeRemoved(Object nodeId) {
            }
        });
        assertEquals(1, modifiedProperties[0]);
    });
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ArcId(org.apache.cayenne.graph.ArcId) GraphDiff(org.apache.cayenne.graph.GraphDiff) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Aggregations

GraphDiff (org.apache.cayenne.graph.GraphDiff)32 Test (org.junit.Test)16 ObjectContext (org.apache.cayenne.ObjectContext)8 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)7 ObjectId (org.apache.cayenne.ObjectId)5 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)5 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)5 ArcId (org.apache.cayenne.graph.ArcId)4 CayenneContext (org.apache.cayenne.CayenneContext)3 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)3 DataChannel (org.apache.cayenne.DataChannel)3 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)3 MockEventManager (org.apache.cayenne.event.MockEventManager)3 EntityResolver (org.apache.cayenne.map.EntityResolver)3 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)3 Artist (org.apache.cayenne.testdo.testmap.Artist)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 QueryResponse (org.apache.cayenne.QueryResponse)2 QueryCache (org.apache.cayenne.cache.QueryCache)2