Author: mprudhom
Date: Fri May 18 16:22:10 2007
New Revision: 539627
URL: http://svn.apache.org/viewvc?view=rev&rev=539627
Log:
OPENJPA-235 Reverted patch since it was causing TCK failures
Modified:
incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/OperationOrderUpdateManager.java
Modified:
incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/OperationOrderUpdateManager.java
URL:
http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/OperationOrderUpdateManager.java?view=diff&rev=539627&r1=539626&r2=539627
==============================================================================
---
incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/OperationOrderUpdateManager.java
(original)
+++
incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/OperationOrderUpdateManager.java
Fri May 18 16:22:10 2007
@@ -14,7 +14,7 @@
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
- * under the License.
+ * under the License.
*/
package org.apache.openjpa.jdbc.kernel;
@@ -22,13 +22,8 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
-import java.util.Stack;
-import java.util.Map;
-import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ForeignKey;
import org.apache.openjpa.jdbc.sql.PrimaryRow;
import org.apache.openjpa.jdbc.sql.Row;
@@ -39,7 +34,7 @@
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
- * Update manager that writes SQL in object-level operation order
+ * Update manager that writes SQL in object-level operation order.
*
* @author Abe White
*/
@@ -71,20 +66,13 @@
// now do any 'all row' updates, which typically null keys
flush(rmimpl.getAllRowUpdates(), psMgr);
-
- // map statemanagers to primaryrows
- Map smMap = mapStateManagers(rmimpl.getOrdered());
-
- // order rows to avoid constraint violations
- List orderedRows = orderRows(rmimpl, smMap);
// gather any updates we need to avoid fk constraints on deletes
Collection constraintUpdates = null;
- for (Iterator itr = orderedRows.iterator(); itr.hasNext();) {
+ for (Iterator itr = rmimpl.getDeletes().iterator(); itr.hasNext();) {
try {
constraintUpdates = analyzeDeleteConstraints(rmimpl,
- (PrimaryRow) itr.next(), constraintUpdates, smMap,
- orderedRows);
+ (PrimaryRow) itr.next(), constraintUpdates);
} catch (SQLException se) {
exceps = addException(exceps, SQLExceptions.getStore
(se, dict));
@@ -94,18 +82,17 @@
flush(constraintUpdates, psMgr);
constraintUpdates.clear();
}
-
+
// flush primary rows in order
- for (Iterator itr = orderedRows.iterator(); itr.hasNext();) {
+ for (Iterator itr = rmimpl.getOrdered().iterator(); itr.hasNext();) {
try {
constraintUpdates = flushPrimaryRow(rmimpl, (PrimaryRow)
- itr.next(), psMgr, constraintUpdates, smMap, orderedRows);
+ itr.next(), psMgr, constraintUpdates);
} catch (SQLException se) {
exceps = addException(exceps, SQLExceptions.getStore
(se, dict));
}
}
-
if (constraintUpdates != null)
flush(constraintUpdates, psMgr);
@@ -120,121 +107,13 @@
}
/**
- * Reorders all rows provided by the specified RowManagerImpl such that
- * no foreign key constraints are violated (assuming a proper schema).
- * @param rmimpl RowManagerImpl
- */
- private List orderRows(RowManagerImpl rmimpl, Map smMap) {
- List orderedRows = new ArrayList();
- if (rmimpl.getOrdered().size() > 0) {
- List inserts = new ArrayList(rmimpl.getInserts());
- List updates = new ArrayList(rmimpl.getUpdates());
- List deletes = new ArrayList(rmimpl.getDeletes());
-
- orderedRows.addAll(orderRows(inserts, smMap));
- orderedRows.addAll(updates);
- orderedRows.addAll(orderRows(deletes, smMap));
- }
- return orderedRows;
- }
-
- private List orderRows(List unorderedList, Map smMap) {
- List orderedList = new ArrayList();
- // this iterates in a while loop instead of with an iterator to
- // avoid ConcurrentModificationExceptions, as unorderedList is
- // mutated in the orderRow() invocation.
- while (!unorderedList.isEmpty()) {
- PrimaryRow nextRow = (PrimaryRow) unorderedList.get(0);
- orderRow(nextRow, unorderedList, orderedList, smMap, new Stack());
- }
- return orderedList;
- }
-
- private void orderRow(PrimaryRow currentRow, Collection unordered,
- List orderedList, Map smMap, Stack visitedRows) {
- if (orderedList.contains(currentRow)) {
- return;
- }
-
- // a circular reference found which means there is a problem
- // with the underlying database schema and/or class metadata
- // definitions. nothing can be done here to correct the problem.
- if (visitedRows.contains(currentRow)) {
- orderedList.addAll(unordered);
- unordered.clear();
- return;
- }
-
- if (currentRow.getAction() == Row.ACTION_INSERT) {
- ForeignKey[] fks = currentRow.getTable().getForeignKeys();
- OpenJPAStateManager sm;
- for (int i = 0; i < fks.length; i++) {
- sm = currentRow.getForeignKeySet(fks[i]);
- if (sm == null)
- continue;
- // if the foreign key is new and it's primary key is
- // auto assigned
- PrimaryRow fkRow = (PrimaryRow) smMap.get(sm);
- if (fkRow.getAction() == Row.ACTION_INSERT) {
- boolean nullable = true;
- Column[] columns = fks[i].getColumns();
- for (int j = 0; j < columns.length; j++) {
- if (columns[j].isNotNull()) {
- nullable = false;
- break;
- }
- }
- if (!nullable) {
- visitedRows.push(currentRow);
- PrimaryRow nextRow = (PrimaryRow) smMap.get(sm);
- orderRow(nextRow, unordered, orderedList, smMap,
- visitedRows);
- visitedRows.pop();
- }
- }
- }
- if (!orderedList.contains(currentRow)) {
- unordered.remove(currentRow);
- orderedList.add(currentRow);
- }
- } else if (currentRow.getAction() == Row.ACTION_DELETE) {
- ForeignKey[] fks = currentRow.getTable().getForeignKeys();
- OpenJPAStateManager sm;
- for (int i = 0; i < fks.length; i++) {
- sm = currentRow.getForeignKeySet(fks[i]);
- if (sm == null)
- continue;
- PrimaryRow fkRow = (PrimaryRow) smMap.get(sm);
- // if the foreign key is going to be deleted
- if (!orderedList.contains(fkRow)
- && fkRow.getAction() == Row.ACTION_DELETE) {
- visitedRows.add(currentRow);
- orderRow(fkRow, unordered, orderedList, smMap,
visitedRows);
- visitedRows.remove(currentRow);
- }
- }
- unordered.remove(currentRow);
- orderedList.add(0, currentRow);
- }
- }
-
- private Map mapStateManagers(List rowList) {
- Map smMap = new HashMap();
- for (Iterator iter = rowList.iterator(); iter.hasNext();) {
- PrimaryRow row = (PrimaryRow) iter.next();
- smMap.put(row.getPrimaryKey(), row);
- }
- return smMap;
- }
-
- /**
* Analyze the delete constraints on the given row, gathering necessary
* updates to null fks before deleting.
*/
private Collection analyzeDeleteConstraints(RowManagerImpl rowMgr,
- PrimaryRow row, Collection updates, Map smMap, List orderedRows)
+ PrimaryRow row, Collection updates)
throws SQLException {
- if (!row.isValid() || row.getAction() != Row.ACTION_DELETE)
+ if (!row.isValid())
return updates;
ForeignKey[] fks = row.getTable().getForeignKeys();
@@ -248,11 +127,6 @@
sm = row.getForeignKeyWhere(fks[i]);
if (sm == null)
continue;
- PrimaryRow fkRow = (PrimaryRow) smMap.get(sm);
- int fkIndex = orderedRows.indexOf(fkRow);
- int rIndex = orderedRows.indexOf(row);
- if (fkIndex > rIndex)
- continue;
// only need an update if we have an fk to a row that's being
// deleted before we are
@@ -278,8 +152,7 @@
* Flush the given row, creating deferred updates for dependencies.
*/
private Collection flushPrimaryRow(RowManagerImpl rowMgr, PrimaryRow row,
- PreparedStatementManager psMgr, Collection updates, Map smMap,
- List orderedRows)
+ PreparedStatementManager psMgr, Collection updates)
throws SQLException {
if (!row.isValid())
return updates;
@@ -297,13 +170,6 @@
for (int i = 0; i < fks.length; i++) {
sm = row.getForeignKeySet(fks[i]);
if (sm == null)
- continue;
-
- PrimaryRow fkRow = (PrimaryRow) smMap.get(sm);
- int fkIndex = orderedRows.indexOf(fkRow);
- int rIndex = orderedRows.indexOf(row);
- // consider sm flushed, no need to defer
- if (rIndex > fkIndex)
continue;
// only need an update if we have an fk to a row that's being