use of org.apache.cayenne.Persistent 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.Persistent in project cayenne by apache.
the class DataDomainUpdateBucket method appendQueriesInternal.
@Override
void appendQueriesInternal(Collection<Query> queries) {
DataDomainDBDiffBuilder diffBuilder = new DataDomainDBDiffBuilder();
DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor();
for (DbEntity dbEntity : dbEntities) {
Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
Map<Object, Query> batches = new LinkedHashMap<>();
for (DbEntityClassDescriptor descriptor : descriptors) {
ObjEntity entity = descriptor.getEntity();
diffBuilder.reset(descriptor);
qualifierBuilder.reset(descriptor);
boolean isRootDbEntity = entity.getDbEntity() == dbEntity;
for (Persistent o : objectsByDescriptor.get(descriptor.getClassDescriptor())) {
ObjectDiff diff = parent.objectDiff(o.getObjectId());
Map<String, Object> snapshot = diffBuilder.buildDBDiff(diff);
// check whether MODIFIED object has real db-level modifications
if (snapshot == null) {
continue;
}
// after we filtered out "fake" modifications, check if an
// attempt is made to modify a read only entity
checkReadOnly(entity);
Map<String, Object> qualifierSnapshot = qualifierBuilder.createQualifierSnapshot(diff);
// organize batches by the updated columns + nulls in qualifier
Set<String> snapshotSet = snapshot.keySet();
Set<String> nullQualifierNames = new HashSet<>();
for (Map.Entry<String, Object> entry : qualifierSnapshot.entrySet()) {
if (entry.getValue() == null) {
nullQualifierNames.add(entry.getKey());
}
}
List<Set<String>> batchKey = Arrays.asList(snapshotSet, nullQualifierNames);
UpdateBatchQuery batch = (UpdateBatchQuery) batches.get(batchKey);
if (batch == null) {
batch = new UpdateBatchQuery(dbEntity, qualifierBuilder.getAttributes(), updatedAttributes(dbEntity, snapshot), nullQualifierNames, 10);
batch.setUsingOptimisticLocking(qualifierBuilder.isUsingOptimisticLocking());
batches.put(batchKey, batch);
}
batch.add(qualifierSnapshot, snapshot, o.getObjectId());
// update replacement id with meaningful PK changes
if (isRootDbEntity) {
Map<String, Object> replacementId = o.getObjectId().getReplacementIdMap();
for (DbAttribute pk : dbEntity.getPrimaryKeys()) {
String name = pk.getName();
if (snapshot.containsKey(name) && !replacementId.containsKey(name)) {
replacementId.put(name, snapshot.get(name));
}
}
}
}
}
queries.addAll(batches.values());
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class HierarchicalObjectResolverNode method objectsFromDataRows.
@Override
List<Persistent> objectsFromDataRows(List<? extends DataRow> rows) {
if (rows == null || rows.size() == 0) {
return new ArrayList<>(1);
}
List<Persistent> results = new ArrayList<>(rows.size());
for (DataRow row : rows) {
// determine entity to use
ClassDescriptor classDescriptor = descriptorResolutionStrategy.descriptorForRow(row);
// not using DataRow.createObjectId for performance reasons -
// ObjectResolver
// has all needed metadata already cached.
ObjectId anId = createObjectId(row, classDescriptor.getEntity(), null);
Persistent object = objectFromDataRow(row, anId, classDescriptor);
if (object == null) {
throw new CayenneRuntimeException("Can't build Object from row: %s", row);
}
// keep the dupe objects (and data rows) around, as there maybe an
// attached
// joint prefetch...
results.add(object);
node.getParentAttachmentStrategy().linkToParent(row, object);
}
// now deal with snapshots
// TODO: refactoring: dupes will clutter the lists and cause extra
// processing...
// removal of dupes happens only downstream, as we need the objects
// matching
// fetched rows for joint prefetch resolving... maybe pushback unique
// and
// non-unique lists to the "node", instead of returning a single list
// from this
// method
cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
return results;
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class JoinedIdParentAttachementStrategy method linkToParent.
public void linkToParent(DataRow row, Persistent object) {
Persistent parentObject = null;
for (ObjEntity entity : sourceEntities) {
if (entity.isAbstract()) {
continue;
}
ObjectId id = node.getResolver().createObjectId(row, entity, relatedIdPrefix);
if (id == null) {
throw new CayenneRuntimeException("Can't build ObjectId from row: %s, entity: %s, prefix: %s", row, entity.getName(), relatedIdPrefix);
}
parentObject = (Persistent) graphManager.getNode(id);
if (parentObject != null) {
break;
}
}
node.linkToParent(object, parentObject);
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ObjectDiff method getArcSnapshotValue.
ObjectId getArcSnapshotValue(String propertyName) {
Object value = arcSnapshot != null ? arcSnapshot.get(propertyName) : null;
if (value instanceof Fault) {
Persistent target = (Persistent) ((Fault) value).resolveFault(object, propertyName);
value = target != null ? target.getObjectId() : null;
arcSnapshot.put(propertyName, value);
}
return (ObjectId) value;
}
Aggregations