use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectResolver method objectFromDataRow.
Persistent objectFromDataRow(DataRow row, ObjectId anId, ClassDescriptor classDescriptor) {
// details
if (anId == null) {
return null;
}
// this will create a HOLLOW object if it is not registered yet
Persistent object = context.findOrCreateObject(anId);
// deal with object state
int state = object.getPersistenceState();
switch(state) {
case PersistenceState.COMMITTED:
case PersistenceState.MODIFIED:
case PersistenceState.DELETED:
// process the above only if refresh is requested...
if (refreshObjects) {
DataRowUtils.mergeObjectWithSnapshot(context, classDescriptor, object, row);
if (object instanceof DataObject) {
((DataObject) object).setSnapshotVersion(row.getVersion());
}
}
break;
case PersistenceState.HOLLOW:
if (!refreshObjects) {
DataRow cachedRow = cache.getCachedSnapshot(anId);
if (cachedRow != null) {
row = cachedRow;
}
}
DataRowUtils.mergeObjectWithSnapshot(context, classDescriptor, object, row);
if (object instanceof DataObject) {
((DataObject) object).setSnapshotVersion(row.getVersion());
}
break;
default:
break;
}
return object;
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectStore method processIndirectlyModifiedIDs.
/**
* Requires external synchronization.
*
* @since 1.1
*/
void processIndirectlyModifiedIDs(Collection<ObjectId> indirectlyModifiedIDs) {
for (ObjectId oid : indirectlyModifiedIDs) {
// access object map directly - the method should be called in a synchronized context...
final DataObject object = (DataObject) objectMap.get(oid);
if (object == null || object.getPersistenceState() != PersistenceState.COMMITTED) {
continue;
}
// for now break all "independent" object relationships...
// in the future we may want to be more precise and go after modified
// relationships only, or even process updated lists without invalidating...
DataContextDelegate delegate = context.nonNullDelegate();
if (delegate.shouldMergeChanges(object, null)) {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(oid.getEntityName());
descriptor.visitProperties(new PropertyVisitor() {
@Override
public boolean visitToMany(ToManyProperty property) {
property.invalidate(object);
return true;
}
@Override
public boolean visitToOne(ToOneProperty property) {
if (property.getRelationship().isSourceIndependentFromTargetChange()) {
property.invalidate(object);
}
return true;
}
@Override
public boolean visitAttribute(AttributeProperty property) {
return true;
}
});
delegate.finishedProcessDelete(object);
}
}
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectStore method processUpdatedSnapshot.
/**
* Requires external synchronization.
*
* @since 1.1
*/
void processUpdatedSnapshot(ObjectId nodeId, DataRow diff) {
// access object map directly - the method should be called in a synchronized context...
DataObject object = (DataObject) objectMap.get(nodeId);
// no object, or HOLLOW object require no processing
if (object != null) {
int state = object.getPersistenceState();
if (state != PersistenceState.HOLLOW) {
// perform same steps as resolveHollow()
if (state == PersistenceState.COMMITTED) {
// consult delegate if it exists
DataContextDelegate delegate = context.nonNullDelegate();
if (delegate.shouldMergeChanges(object, diff)) {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(nodeId.getEntityName());
// TODO: andrus, 5/26/2006 - call to 'getSnapshot' is expensive,
// however my attempts to merge the 'diff' instead of snapshot
// via 'refreshObjectWithSnapshot' resulted in even worse performance.
// This sounds counterintuitive (Not sure if this is some HotSpot related glitch)...
// still keeping the old algorithm here until we switch from snapshot events
// to GraphEvents and all this code becomes obsolete.
DataRow snapshot = getSnapshot(object.getObjectId());
DataRowUtils.refreshObjectWithSnapshot(descriptor, object, snapshot, true);
delegate.finishedMergeChanges(object);
}
} else // merge modified and deleted
if (state == PersistenceState.DELETED || state == PersistenceState.MODIFIED) {
// consult delegate if it exists
DataContextDelegate delegate = context.nonNullDelegate();
if (delegate.shouldMergeChanges(object, diff)) {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(nodeId.getEntityName());
DataRowUtils.forceMergeWithSnapshot(context, descriptor, object, diff);
delegate.finishedMergeChanges(object);
}
}
}
}
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectStore method registerDiff.
/**
* Registers object change.
*
* @since 1.2
*/
synchronized ObjectDiff registerDiff(Object nodeId, NodeDiff diff) {
if (diff != null) {
diff.setDiffId(++currentDiffId);
}
ObjectDiff objectDiff = changes.get(nodeId);
if (objectDiff == null) {
Persistent object = objectMap.get(nodeId);
if (object == null) {
throw new CayenneRuntimeException("No object is registered in context with Id %s", nodeId);
}
if (object.getPersistenceState() == PersistenceState.COMMITTED) {
object.setPersistenceState(PersistenceState.MODIFIED);
// replacement yet, so we still need to handle them...
if (object instanceof DataObject) {
DataObject dataObject = (DataObject) object;
DataRow snapshot = getCachedSnapshot((ObjectId) nodeId);
if (snapshot != null && snapshot.getVersion() != dataObject.getSnapshotVersion()) {
DataContextDelegate delegate = context.nonNullDelegate();
if (delegate.shouldMergeChanges(dataObject, snapshot)) {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
DataRowUtils.forceMergeWithSnapshot(context, descriptor, dataObject, snapshot);
dataObject.setSnapshotVersion(snapshot.getVersion());
delegate.finishedMergeChanges(dataObject);
}
}
}
}
objectDiff = new ObjectDiff(object);
objectDiff.setDiffId(++currentDiffId);
changes.put(nodeId, objectDiff);
}
if (diff != null) {
objectDiff.addDiff(diff, this);
}
return objectDiff;
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectIdBatchFault method fetchObjects.
private Map<String, Object> fetchObjects() {
if (sources == null) {
return Collections.emptyMap();
}
EntityResolver resolver = context.getEntityResolver();
// reasons
if (sources.size() == 1) {
String uuid = sources.get(0).getId();
String entityName = EntityIdCoder.getEntityName(uuid);
ObjEntity entity = resolver.getObjEntity(entityName);
ObjectId id = new EntityIdCoder(entity).toObjectId(uuid);
Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
if (object == null) {
return Collections.emptyMap();
} else {
return Collections.singletonMap(uuid, object);
}
}
Map<String, SelectQuery<DataObject>> queriesByEntity = new HashMap<>();
Map<String, EntityIdCoder> codersByEntity = new HashMap<>();
for (ObjectIdBatchSourceItem source : sources) {
String uuid = source.getId();
String entityName = EntityIdCoder.getEntityName(uuid);
EntityIdCoder coder = codersByEntity.get(entityName);
SelectQuery<DataObject> query;
if (coder == null) {
coder = new EntityIdCoder(resolver.getObjEntity(entityName));
codersByEntity.put(entityName, coder);
query = new SelectQuery<>(entityName);
queriesByEntity.put(entityName, query);
} else {
query = queriesByEntity.get(entityName);
}
ObjectId id = coder.toObjectId(uuid);
Expression idExp = ExpressionFactory.matchAllDbExp(id.getIdSnapshot(), Expression.EQUAL_TO);
query.orQualifier(idExp);
}
int capacity = (int) Math.ceil(sources.size() / 0.75d);
Map<String, Object> results = new HashMap<>(capacity);
for (SelectQuery<DataObject> query : queriesByEntity.values()) {
EntityIdCoder coder = codersByEntity.get(query.getRoot());
@SuppressWarnings("unchecked") List<DataObject> objects = context.performQuery(query);
for (DataObject object : objects) {
String uuid = coder.toStringId(object.getObjectId());
results.put(uuid, object);
}
}
return results;
}
Aggregations