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);
}
}
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;
}
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;
}
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));
}
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]);
});
}
Aggregations