org.objectstyle.cayenne.access.DataDomainFlushAction Maven / Gradle / Ivy
/* ====================================================================
*
* The ObjectStyle Group Software License, version 1.1
* ObjectStyle Group - http://objectstyle.org/
*
* Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
* of the software. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if any,
* must include the following acknowlegement:
* "This product includes software developed by independent contributors
* and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
* or promote products derived from this software without prior written
* permission. For written permission, email
* "andrus at objectstyle dot org".
*
* 5. Products derived from this software may not be called "ObjectStyle"
* or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
* names without prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals and hosted on ObjectStyle Group web site. For more
* information on the ObjectStyle Group, please see
* .
*/
package org.objectstyle.cayenne.access;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.objectstyle.cayenne.CayenneRuntimeException;
import org.objectstyle.cayenne.ObjectId;
import org.objectstyle.cayenne.PersistenceState;
import org.objectstyle.cayenne.Persistent;
import org.objectstyle.cayenne.graph.CompoundDiff;
import org.objectstyle.cayenne.graph.GraphDiff;
import org.objectstyle.cayenne.map.DbEntity;
import org.objectstyle.cayenne.map.ObjEntity;
import org.objectstyle.cayenne.query.BatchQuery;
/**
* A stateful commit handler used by DataContext to perform commit operation.
* DataContextCommitAction resolves primary key dependencies, referential integrity
* dependencies (including multi-reflexive entities), generates primary keys, creates
* batches for massive data modifications, assigns operations to data nodes.
*
* @author Andrus Adamchik
* @since 1.2
*/
class DataDomainFlushAction {
private final DataDomain domain;
private Map changesByObjectId;
private CompoundDiff resultDiff;
private Collection resultDeletedIds;
private Map resultModifiedSnapshots;
private Collection resultIndirectlyModifiedIds;
private DataDomainInsertBucket insertBucket;
private DataDomainUpdateBucket updateBucket;
private DataDomainDeleteBucket deleteBucket;
private DataDomainFlattenedBucket flattenedBucket;
private List queries;
DataDomainFlushAction(DataDomain domain) {
this.domain = domain;
}
DataDomain getDomain() {
return domain;
}
Collection getResultDeletedIds() {
return resultDeletedIds;
}
CompoundDiff getResultDiff() {
return resultDiff;
}
Collection getResultIndirectlyModifiedIds() {
return resultIndirectlyModifiedIds;
}
Map getResultModifiedSnapshots() {
return resultModifiedSnapshots;
}
ObjectDiff objectDiff(Object objectId) {
return (ObjectDiff) changesByObjectId.get(objectId);
}
void addFlattenedInsert(DbEntity flattenedEntity, FlattenedArcKey flattenedInsertInfo) {
flattenedBucket.addFlattenedInsert(flattenedEntity, flattenedInsertInfo);
}
void addFlattenedDelete(DbEntity flattenedEntity, FlattenedArcKey flattenedDeleteInfo) {
flattenedBucket.addFlattenedDelete(flattenedEntity, flattenedDeleteInfo);
}
GraphDiff flush(DataContext context, GraphDiff changes) {
if (changes == null) {
return new CompoundDiff();
}
// TODO: Andrus, 3/13/2006 - support categorizing an arbitrary diff
if (!(changes instanceof ObjectStoreGraphDiff)) {
throw new IllegalArgumentException("Expected 'ObjectStoreGraphDiff', got: "
+ changes.getClass().getName());
}
// ObjectStoreGraphDiff contains changes already categorized by objectId...
this.changesByObjectId = ((ObjectStoreGraphDiff) changes).getChangesByObjectId();
this.insertBucket = new DataDomainInsertBucket(this);
this.deleteBucket = new DataDomainDeleteBucket(this);
this.updateBucket = new DataDomainUpdateBucket(this);
this.flattenedBucket = new DataDomainFlattenedBucket(this);
this.queries = new ArrayList();
// note that there is no syncing on the object store itself. This is caller's
// responsibility.
synchronized (context.getObjectStore().getDataRowCache()) {
this.resultIndirectlyModifiedIds = new HashSet();
preprocess(context, changes);
if (queries.isEmpty()) {
return new CompoundDiff();
}
this.resultDiff = new CompoundDiff();
this.resultDeletedIds = new ArrayList();
this.resultModifiedSnapshots = new HashMap();
runQueries();
postprocess(context);
return resultDiff;
}
}
private void preprocess(DataContext context, GraphDiff changes) {
// categorize dirty objects by state
ObjectStore objectStore = context.getObjectStore();
Iterator it = changesByObjectId.keySet().iterator();
while (it.hasNext()) {
ObjectId id = (ObjectId) it.next();
Persistent object = (Persistent) objectStore.getNode(id);
ObjEntity entity = context.getEntityResolver().lookupObjEntity(
id.getEntityName());
switch (object.getPersistenceState()) {
case PersistenceState.NEW:
insertBucket.addDirtyObject(object, entity);
break;
case PersistenceState.MODIFIED:
updateBucket.addDirtyObject(object, entity);
break;
case PersistenceState.DELETED:
deleteBucket.addDirtyObject(object, entity);
break;
}
}
new DataDomainIndirectDiffBuilder(this).processIndirectChanges(changes);
insertBucket.appendQueries(queries);
flattenedBucket.appendInserts(queries);
updateBucket.appendQueries(queries);
flattenedBucket.appendDeletes(queries);
deleteBucket.appendQueries(queries);
}
private void runQueries() {
DataDomainFlushObserver observer = new DataDomainFlushObserver();
// split query list by spanned nodes and run each single node range individually.
// Since connections are reused per node within an open transaction, there should
// not be much overhead in accessing the same node multiple times (may happen due
// to imperfect sorting)
try {
DataNode lastNode = null;
DbEntity lastEntity = null;
int rangeStart = 0;
int len = queries.size();
for (int i = 0; i < len; i++) {
BatchQuery query = (BatchQuery) queries.get(i);
if (query.getDbEntity() != lastEntity) {
lastEntity = query.getDbEntity();
DataNode node = domain.lookupDataNode(lastEntity.getDataMap());
if (node != lastNode) {
if (i - rangeStart > 0) {
lastNode.performQueries(
queries.subList(rangeStart, i),
observer);
}
rangeStart = i;
lastNode = node;
}
}
}
// process last segment of the query list...
lastNode.performQueries(queries.subList(rangeStart, len), observer);
}
catch (Throwable th) {
Transaction.getThreadTransaction().setRollbackOnly();
throw new CayenneRuntimeException("Transaction was rolledback.", th);
}
}
/*
* Sends notification of changes to the DataRowStore, returns GraphDiff with replaced
* ObjectIds.
*/
private void postprocess(DataContext context) {
deleteBucket.postprocess();
updateBucket.postprocess();
insertBucket.postprocess();
// notify cache...
if (!resultDeletedIds.isEmpty()
|| !resultModifiedSnapshots.isEmpty()
|| !resultIndirectlyModifiedIds.isEmpty()) {
context.getObjectStore().getDataRowCache().processSnapshotChanges(
context.getObjectStore(),
resultModifiedSnapshots,
resultDeletedIds,
Collections.EMPTY_LIST,
resultIndirectlyModifiedIds);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy