diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-01-12 23:28:56 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-01-12 23:28:56 +0000 |
commit | 641e9f232ba188979ec33c528a1d14e2824f76d1 (patch) | |
tree | 0683e89511e82cf832f24888edc7918c0920709b /java/das/rdb | |
parent | f082eb1c7580078fa390e78254b2ed3faf7edfca (diff) |
TUSCANY-2767 - Appling performance enhancements patch from Florian Pinel
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@733965 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/das/rdb')
9 files changed, 268 insertions, 192 deletions
diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/RelationshipWrapper.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/RelationshipWrapper.java index 0e4447bf8b..af2ab7ca5b 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/RelationshipWrapper.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/RelationshipWrapper.java @@ -29,19 +29,22 @@ import org.apache.tuscany.das.rdb.config.Relationship; public class RelationshipWrapper { private Relationship relationship; + private List foreignKeys; public RelationshipWrapper(Relationship r) { this.relationship = r; } public Collection getForeignKeys() { - List keys = new ArrayList(); - Iterator i = this.relationship.getKeyPair().iterator(); - while (i.hasNext()) { - KeyPair pair = (KeyPair) i.next(); - keys.add(pair.getForeignKeyColumn()); - } - return keys; + if (foreignKeys == null) { + foreignKeys = new ArrayList(); + Iterator i = this.relationship.getKeyPair().iterator(); + while (i.hasNext()) { + KeyPair pair = (KeyPair) i.next(); + foreignKeys.add(pair.getForeignKeyColumn()); + } + } + return foreignKeys; } - + } diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/DataObjectMaker.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/DataObjectMaker.java index 58e1fd3ff0..495d401947 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/DataObjectMaker.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/DataObjectMaker.java @@ -27,11 +27,11 @@ import commonj.sdo.Property; import commonj.sdo.Type; import commonj.sdo.helper.DataFactory; -public class DataObjectMaker { +public final class DataObjectMaker { private final DataObject rootObject; - private final Logger logger = Logger.getLogger(DataObjectMaker.class); + private static final Logger logger = Logger.getLogger(DataObjectMaker.class); public DataObjectMaker(DataObject root) { this.rootObject = root; diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java index 8559b63b78..5be56d77d8 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java @@ -41,15 +41,17 @@ import commonj.sdo.impl.HelperProvider; /** */ -public class GraphBuilderMetadata { +public final class GraphBuilderMetadata { - private final Logger logger = Logger.getLogger(GraphBuilderMetadata.class); + private static final Logger logger = Logger.getLogger(GraphBuilderMetadata.class); private MappingWrapper configWrapper; private final Collection resultSets = new ArrayList(); - + private String typeURI; + + private List definedTypes; private Type rootType; @@ -57,7 +59,7 @@ public class GraphBuilderMetadata { private HelperContext helperContext = HelperProvider.getInstance().getDefaultContext(); - public GraphBuilderMetadata(Collection results, Config model, ResultSetShape shape) throws SQLException { + public GraphBuilderMetadata(List results, Config model, ResultSetShape shape) throws SQLException { this.configWrapper = new MappingWrapper(model); if (model != null) { this.typeURI = model.getDataObjectModel(); @@ -70,7 +72,7 @@ public class GraphBuilderMetadata { resultSets.add(resultMetadata); } - } + } /** * Returns the collection of ResultMetadata objects @@ -78,7 +80,7 @@ public class GraphBuilderMetadata { public Collection getResultMetadata() { return this.resultSets; } - + /** * Returns the set of defined relationships */ @@ -122,55 +124,55 @@ public class GraphBuilderMetadata { ResultMetadata resultMetadata = (ResultMetadata) iter.next(); - // Create a Type for each Table represented in the ResultSet - Iterator names = resultMetadata.getAllTablePropertyNames().iterator(); - while (names.hasNext()) { - String tableName = (String) names.next(); - if (root.getProperty(tableName) == null) { - Type tableType = SDOUtil.createType(helperContext, getDefaultURI(), tableName, false); - Property property = SDOUtil.createProperty(root, tableName, tableType); - SDOUtil.setMany(property, true); - SDOUtil.setContainment(property, true); - if (this.logger.isDebugEnabled()) { - this.logger.debug("GBMD.createDynamicTypes():CREATING NEW TABLE TYPE & PROPERTY :"+tableName); - } - } - } + // Create a Type for each Table represented in the ResultSet + Iterator names = resultMetadata.getAllTablePropertyNames().iterator(); + while (names.hasNext()) { + String tableName = (String) names.next(); + if (root.getProperty(tableName) == null) { + Type tableType = SDOUtil.createType(helperContext, getDefaultURI(), tableName, false); + Property property = SDOUtil.createProperty(root, tableName, tableType); + SDOUtil.setMany(property, true); + SDOUtil.setContainment(property, true); + if (this.logger.isDebugEnabled()) { + this.logger.debug("GBMD.createDynamicTypes():CREATING NEW TABLE TYPE & PROPERTY :"+tableName); + } + } + } - // TODO tablePropertyMap is temporary until Tuscany-203 is fixed - Map tablePropertyMap = new HashMap(); + // TODO tablePropertyMap is temporary until Tuscany-203 is fixed + Map tablePropertyMap = new HashMap(); - for (int i = 1; i <= resultMetadata.getResultSetSize(); i++) { + for (int i = 1; i <= resultMetadata.getResultSetSize(); i++) { - Property ref = root.getProperty(resultMetadata.getTablePropertyName(i)); + Property ref = root.getProperty(resultMetadata.getTablePropertyName(i)); - if (ref == null) { - throw new RuntimeException("Could not find table " + resultMetadata.getTablePropertyName(i) - + " in the SDO model"); - } - - // TODO Temporary code to check to see if a property has already been added. - // Replace when Tuscany-203 is fixed - List addedProperties = (List) tablePropertyMap.get(ref.getName()); - if (addedProperties == null) { - addedProperties = new ArrayList(); - tablePropertyMap.put(ref.getName(), addedProperties); - } + if (ref == null) { + throw new RuntimeException("Could not find table " + resultMetadata.getTablePropertyName(i) + + " in the SDO model"); + } - + // TODO Temporary code to check to see if a property has already been added. + // Replace when Tuscany-203 is fixed + List addedProperties = (List) tablePropertyMap.get(ref.getName()); + if (addedProperties == null) { + addedProperties = new ArrayList(); + tablePropertyMap.put(ref.getName(), addedProperties); + } - String columnName = resultMetadata.getColumnPropertyName(i); - // TODO temporary check until Tuscany-203 is fixed - if (!addedProperties.contains(columnName)) { - addedProperties.add(columnName); - Type atype = resultMetadata.getDataType(i); - SDOUtil.createProperty(ref.getType(), columnName, atype); + String columnName = resultMetadata.getColumnPropertyName(i); - } + // TODO temporary check until Tuscany-203 is fixed + if (!addedProperties.contains(columnName)) { + addedProperties.add(columnName); + Type atype = resultMetadata.getDataType(i); - } + SDOUtil.createProperty(ref.getType(), columnName, atype); + + } + + } } MappingWrapper wrapper = getConfigWrapper(); @@ -243,37 +245,39 @@ public class GraphBuilderMetadata { } public List getDefinedTypes() { - List types = null; - List defaultTypes = null; - if (this.typeURI == null) { - types = SDOUtil.getTypes(helperContext, getDefaultURI()); - defaultTypes = SDOUtil.getTypes(defaultHelperContext, getDefaultURI()); - if(defaultTypes != null){ - if(types == null) { - types = defaultTypes; - } - else { - types.addAll(defaultTypes); - } - } - return types; - } - - types = SDOUtil.getTypes(helperContext, typeURI); - defaultTypes = SDOUtil.getTypes(defaultHelperContext, typeURI); - if(defaultTypes != null){ - if(types == null) { - types = defaultTypes; - } - else { - types.addAll(defaultTypes); - } - } - - if(rootType != null) - types.add(rootType); - return types; - + if (this.definedTypes == null) { + List types = null; + List defaultTypes = null; + if (this.typeURI == null) { + types = SDOUtil.getTypes(helperContext, getDefaultURI()); + defaultTypes = SDOUtil.getTypes(defaultHelperContext, getDefaultURI()); + if(defaultTypes != null){ + if(types == null) { + types = defaultTypes; + } + else { + types.addAll(defaultTypes); + } + } + this.definedTypes = types; + } else { + types = SDOUtil.getTypes(helperContext, typeURI); + defaultTypes = SDOUtil.getTypes(defaultHelperContext, typeURI); + if(defaultTypes != null){ + if(types == null) { + types = defaultTypes; + } + else { + types.addAll(defaultTypes); + } + } + + if(rootType != null) + types.add(rootType); + this.definedTypes = types; + } + } + return this.definedTypes; } } diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/MultiTableRegistry.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/MultiTableRegistry.java index 15ac74ab45..bc3ca440b9 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/MultiTableRegistry.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/MultiTableRegistry.java @@ -33,7 +33,7 @@ import commonj.sdo.DataObject; * efficient if we could use LinkedHashMap from JDK 1.4 */ public class MultiTableRegistry implements TableRegistry { - private final Logger logger = Logger.getLogger(MultiTableRegistry.class); + private static final Logger logger = Logger.getLogger(MultiTableRegistry.class); private Map tableNameMap; diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultMetadata.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultMetadata.java index e94f3e3b3b..2b3e8b20a6 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultMetadata.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultMetadata.java @@ -39,7 +39,7 @@ import org.apache.tuscany.das.rdb.impl.ResultSetShape; import commonj.sdo.Type; -public class ResultMetadata { +public final class ResultMetadata { private Map tableToPropertyMap = new HashMap(); diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java index 7722c3c4fd..431af18d62 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java @@ -31,13 +31,15 @@ import commonj.sdo.DataObject; * A ResultSetProcessor is used to transform the data in a ResultSet into a set of inter-related EDataObjects. */ public class ResultSetProcessor { - private final Logger logger = Logger.getLogger(ResultSetProcessor.class); + private static final Logger logger = Logger.getLogger(ResultSetProcessor.class); private TableRegistry registry; private GraphBuilderMetadata metadata; private final DataObjectMaker doMaker; + + private final RowObjects tableObjects; public ResultSetProcessor(DataObject g, GraphBuilderMetadata gbmd) { @@ -49,6 +51,8 @@ public class ResultSetProcessor { } doMaker = new DataObjectMaker(g); + + tableObjects = new RowObjects(metadata, registry); if (this.logger.isDebugEnabled()) { this.logger.debug(metadata); @@ -80,20 +84,19 @@ public class ResultSetProcessor { // results.getStatement().close(); results.close(); } - } private void processResultSet(ResultSet rs, ResultMetadata rsMetadata, int start, int end) throws SQLException { - + ResultSetRow rsr = new ResultSetRow(rsMetadata); if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) { while (rs.next() && start < end) { - ResultSetRow rsr = new ResultSetRow(rs, rsMetadata); + rsr.processRow(rs); addRowToGraph(rsr, rsMetadata); ++start; } } else { while (rs.absolute(start) && start < end) { - ResultSetRow rsr = new ResultSetRow(rs, rsMetadata); + rsr.processRow(rs); addRowToGraph(rsr, rsMetadata); ++start; } @@ -105,8 +108,8 @@ public class ResultSetProcessor { * @param resultMetadata */ private void addRowToGraph(ResultSetRow row, ResultMetadata resultMetadata) { - RowObjects tableObjects = new RowObjects(metadata, registry); - Iterator tables = row.getAllTableData().iterator(); + tableObjects.clear(); + Iterator tables = row.getAllTableData().iterator(); while (tables.hasNext()) { TableData rawDataFromRow = (TableData) tables.next(); @@ -120,7 +123,8 @@ public class ResultSetProcessor { String tableName = rawDataFromRow.getTableName(); DataObject tableObject = registry.get(tableName, rawDataFromRow.getPrimaryKeyValues()); - if (tableObject == null + boolean newlyCreated = (tableObject == null); + if (newlyCreated && !rawDataFromRow.hasNullPrimaryKey()) {//2nd check for null data in PK, //as TableData.addData() - hasValidPrimaryKey=false is commented for a reason //with this, DataObjs with null PK will not be added to registry and tableObjects @@ -143,7 +147,7 @@ public class ResultSetProcessor { if (this.logger.isDebugEnabled()) { this.logger.debug("Do not allow any Null tableObject in tableObjects"); } - tableObjects.put(tableName, tableObject); + tableObjects.put(tableName, tableObject, newlyCreated); } } diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetRow.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetRow.java index a44713b677..6ef815530d 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetRow.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetRow.java @@ -27,89 +27,117 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.log4j.Logger; /** * - * A ResultSetRow is used to transform a single row of a ResultSet into a set of EDataObjects. + * A ResultSetRow is used to transform a row of a ResultSet into a set of EDataObjects. */ public class ResultSetRow { - private final Logger logger = Logger.getLogger(ResultSetRow.class); + private static final Logger logger = Logger.getLogger(ResultSetRow.class); private final ResultMetadata metadata; - + private final boolean recursive; + private final int resultSetSize; + private Collection allTableNames; + private Set tablesWithNoPK = new HashSet(); + private String[] tablePropertyNames; + private String[] columnPropertyNames; + private boolean[] isPKColumn; private Map tableMap = new HashMap(); - private List allTableData; /** * Method ResultSetRow. * + * @param m + * the result metadata + */ + public ResultSetRow(ResultMetadata m) throws SQLException { + this.metadata = m; + this.recursive = m.isRecursive(); + this.resultSetSize = m.getResultSetSize(); + cacheMetadata(); + getAllTableNamesForRS(); + getTablesWithNoPK(); + } + + /** + * Processes a single row in the ResultSet. + * * @param rs * A ResultSet positioned on the desired row - * @param ePackage - * The package used to create EDataObjects */ - public ResultSetRow(ResultSet rs, ResultMetadata m) throws SQLException { - this.metadata = m; - if (m.isRecursive()) { + public void processRow(ResultSet rs) throws SQLException { + // clear previous data + for (Iterator itTableData = tableMap.values().iterator(); itTableData.hasNext(); ) { + TableData tableData = (TableData) itTableData.next(); + tableData.clear(); + } + allTableData = null; + + // process row + if (recursive) { processRecursiveRow(rs); } else { - processRow(rs); + processNonRecursiveRow(rs); } } - /** - * Processes a single row in the ResultSet Method processRow. - * - * @param rs - */ - private void processRow(ResultSet rs) throws SQLException { + private void processNonRecursiveRow(ResultSet rs) throws SQLException { if (this.logger.isDebugEnabled()) { this.logger.debug(""); } - for (int i = 1; i <= metadata.getResultSetSize(); i++) { - Object data = getObject(rs, i); - - TableData table = getRawData(metadata.getTablePropertyName(i)); + for (int i = 1; i <= resultSetSize; i++) { + Object data = getObject(rs, i); + TableData table = getRawData(tablePropertyNames[i]); if (this.logger.isDebugEnabled()) { - this.logger.debug("Adding column: " + metadata.getColumnPropertyName(i) + "\tValue: " + this.logger.debug("Adding column: " + columnPropertyNames[i] + "\tValue: " + data + "\tTable: " - + metadata.getTablePropertyName(i)); + + tablePropertyNames[i]); } - table.addData(metadata.getColumnPropertyName(i), metadata.isPKColumn(i), data); - } - + table.addData(columnPropertyNames[i], isPKColumn[i], data); + } checkResultSetMissesPK(); - } + } //get all table names involved in current result set //can not use metadata.getAllTablePropertyNames() //as it gives table names for all tables from Config - public Collection getAllTableNamesForRS(){ - Collection allTableNamesForRS = new HashSet(); - for (int i = 1; i <= metadata.getResultSetSize(); i++) { - allTableNamesForRS.add(metadata.getTablePropertyName(i)); + private void getAllTableNamesForRS(){ + allTableNames = new HashSet(resultSetSize); + for (int i = 1; i <= resultSetSize; i++) { + allTableNames.add(tablePropertyNames[i]); } - return allTableNamesForRS; + } + + private void cacheMetadata() { + tablePropertyNames = new String[resultSetSize + 1]; + columnPropertyNames = new String[resultSetSize + 1]; + isPKColumn = new boolean[resultSetSize + 1]; + for (int i = 1; i <= resultSetSize; i++) { + tablePropertyNames[i] = metadata.getTablePropertyName(i); + columnPropertyNames[i] = metadata.getColumnPropertyName(i); + isPKColumn[i] = metadata.isPKColumn(i); + } } - //case when result set omits PK column, take care of compound PKs too - public void checkResultSetMissesPK(){ + private void getTablesWithNoPK(){ + //case when result set omits PK column, take care of compound PKs too boolean tableRSHasPK; - Collection allTableNames = getAllTableNamesForRS(); Iterator itr = allTableNames.iterator(); while(itr.hasNext()){ tableRSHasPK = false; String currentTableName = (String)itr.next(); HashSet pks = metadata.getAllPKsForTable(currentTableName); HashSet pksInRS = new HashSet(); - for(int j=1; j<=metadata.getResultSetSize(); j++){ - if(currentTableName.equals(metadata.getTablePropertyName(j)) && - metadata.isPKColumn(j) ){ - pksInRS.add(metadata.getColumnPropertyName(j)); + for(int j=1; j<=resultSetSize; j++){ + if(currentTableName.equals(tablePropertyNames[j]) && + isPKColumn[j] ){ + pksInRS.add(columnPropertyNames[j]); } } @@ -148,49 +176,51 @@ public class ResultSetRow { } } - //Default is TRUE(from TableData), so consider only FALSE case - if(!tableRSHasPK){ - TableData table = getRawData(currentTableName); - table.setValidPrimaryKey(tableRSHasPK); - } + if (!tableRSHasPK) tablesWithNoPK.add(currentTableName); + + if (this.logger.isDebugEnabled()) { + this.logger.debug("table "+currentTableName+" hasValidPK "+tableRSHasPK); + } + } + } + + private void checkResultSetMissesPK(){ + //Default is TRUE(from TableData), so consider only FALSE case + Iterator itr = tablesWithNoPK.iterator(); + while(itr.hasNext()){ + String currentTableName = (String)itr.next(); + TableData table = getRawData(currentTableName); + table.setValidPrimaryKey(false); } - - //for testing - if (this.logger.isDebugEnabled()) { - for (int i = 1; i <= metadata.getResultSetSize(); i++) { - TableData table = getRawData(metadata.getTablePropertyName(i)); - this.logger.debug("table "+table.getTableName()+" hasValidPK "+table.hasValidPrimaryKey()); - } - } } - public void processRecursiveRow(ResultSet rs) throws SQLException { + private void processRecursiveRow(ResultSet rs) throws SQLException { this.allTableData = new ArrayList(); int i = 1; - while (i <= metadata.getResultSetSize()) { - TableData table = new TableData(metadata.getTablePropertyName(i)); + while (i <= resultSetSize) { + TableData table = new TableData(tablePropertyNames[i]); this.allTableData.add(table); - while ((i <= metadata.getResultSetSize()) && (metadata.isPKColumn(i))) { + while ((i <= resultSetSize) && (isPKColumn[i])) { Object data = getObject(rs, i); if (this.logger.isDebugEnabled()) { - this.logger.debug("Adding column: " + metadata.getColumnPropertyName(i) + this.logger.debug("Adding column: " + columnPropertyNames[i] + "\tValue: " + data + "\tTable: " - + metadata.getTablePropertyName(i)); + + tablePropertyNames[i]); } - table.addData(metadata.getColumnPropertyName(i), true, data); + table.addData(columnPropertyNames[i], true, data); i++; } - while ((i <= metadata.getResultSetSize()) && (!metadata.isPKColumn(i))) { + while ((i <= resultSetSize) && (!isPKColumn[i])) { Object data = getObject(rs, i); if (this.logger.isDebugEnabled()) { - this.logger.debug("Adding column: " + metadata.getColumnPropertyName(i) + this.logger.debug("Adding column: " + columnPropertyNames[i] + "\tValue: " + data + "\tTable: " - + metadata.getTablePropertyName(i)); + + tablePropertyNames[i]); } - table.addData(metadata.getColumnPropertyName(i), false, data); + table.addData(columnPropertyNames[i], false, data); i++; } } @@ -247,7 +277,7 @@ public class ResultSetRow { return table; } - + public List getAllTableData() { if (this.allTableData == null) { this.allTableData = new ArrayList(); diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/RowObjects.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/RowObjects.java index c7f200cb1e..13060a3ca6 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/RowObjects.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/RowObjects.java @@ -20,9 +20,11 @@ package org.apache.tuscany.das.rdb.graphbuilder.impl; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.log4j.Logger; import org.apache.tuscany.das.rdb.config.KeyPair; @@ -33,36 +35,63 @@ import commonj.sdo.DataObject; import commonj.sdo.Property; public class RowObjects { - private final Logger logger = Logger.getLogger(RowObjects.class); + private static final Logger logger = Logger.getLogger(RowObjects.class); private Map objectsByTableName; private List tableObjects; + + private Set newTableObjectNames; + + private Map tableTypeNames; private final GraphBuilderMetadata metadata; private final TableRegistry registry; + + private final MappingWrapper configWrapper; + + private final boolean hasRecursiveRelationships; public RowObjects(GraphBuilderMetadata metadata, TableRegistry registry) { objectsByTableName = new HashMap(); tableObjects = new ArrayList(); + newTableObjectNames = new HashSet(); + tableTypeNames = new HashMap(); this.metadata = metadata; this.registry = registry; + this.configWrapper = metadata.getConfigWrapper(); + this.hasRecursiveRelationships = configWrapper.hasRecursiveRelationships(); + } + + public void clear() { + objectsByTableName.clear(); + tableObjects.clear(); + newTableObjectNames.clear(); } - public void put(String key, DataObject value) { + public void put(String key, DataObject value, boolean newlyCreated) { objectsByTableName.put(key, value); tableObjects.add(value); + if (newlyCreated) newTableObjectNames.add(key); } public DataObject get(String tablePropertyName) { return (DataObject) objectsByTableName.get(tablePropertyName); } + + private String getTableTypeName(String tableName) { + String typeName = (String) tableTypeNames.get(tableName); + if (typeName == null) { + typeName = configWrapper.getTableTypeName(tableName); + tableTypeNames.put(tableName, typeName); + } + return typeName; + } - void processRelationships() { - MappingWrapper wrapper = metadata.getConfigWrapper(); - if (wrapper.hasRecursiveRelationships()) { - processRecursiveRelationships(wrapper); + public void processRelationships() { + if (hasRecursiveRelationships) { + processRecursiveRelationships(configWrapper); return; } @@ -70,20 +99,22 @@ public class RowObjects { while (i.hasNext()) { Relationship r = (Relationship) i.next(); - DataObject parentTable = get(wrapper.getTableTypeName(r.getPrimaryKeyTable())); - DataObject childTable = get(wrapper.getTableTypeName(r.getForeignKeyTable())); + String parentTypeName = getTableTypeName(r.getPrimaryKeyTable()); + String childTypeName = getTableTypeName(r.getForeignKeyTable()); - if (this.logger.isDebugEnabled()) { - this.logger.debug("Parent table: " + parentTable); - this.logger.debug("Child table: " + childTable); - } - if ((parentTable == null) || (childTable == null)) { - continue; - } + // the relationship needs to be set only if the parent or the child is newly created + // otherwise the relationship has already been set + if (newTableObjectNames.contains(parentTypeName) || newTableObjectNames.contains(childTypeName)) { + DataObject parent = get(parentTypeName); + DataObject child = get(childTypeName); - Property p = parentTable.getType().getProperty(r.getName()); - setOrAdd(parentTable, childTable, p); + if (this.logger.isDebugEnabled()) { + this.logger.debug("Parent table: " + parent); + this.logger.debug("Child table: " + child); + } + setOrAdd(parent, child, r.getName()); + } } } @@ -98,18 +129,16 @@ public class RowObjects { DataObject parentTable = findParentTable(table, r, wrapper); - if (parentTable == null) { - continue; - } - Property p = parentTable.getType().getProperty(r.getName()); - setOrAdd(parentTable, table, p); + setOrAdd(parentTable, table, r.getName()); } } } - private void setOrAdd(DataObject parent, DataObject child, Property p) { + private void setOrAdd(DataObject parent, DataObject child, String propertyName) { + if (parent == null || child == null) return; + Property p = parent.getType().getProperty(propertyName); if (p.isMany()) { parent.getList(p).add(child); } else { diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/TableData.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/TableData.java index f74370f003..959a07b26f 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/TableData.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/TableData.java @@ -25,12 +25,12 @@ import java.util.Map; import org.apache.log4j.Logger; -public class TableData { - private final Logger logger = Logger.getLogger(TableData.class); +public final class TableData { + private static final Logger logger = Logger.getLogger(TableData.class); - private Map columnData = new HashMap(); + private Map columnData; - private List primaryKey = new ArrayList(); + private List primaryKey;; private final String name; @@ -44,6 +44,15 @@ public class TableData { } this.name = tableName; + this.columnData = new HashMap(); + this.primaryKey = new ArrayList(); + } + + public void clear() { + columnData.clear(); + primaryKey.clear(); + hasValidPrimaryKey = true; + hasNullPrimaryKey = false; } public void addData(String columnName, boolean isPrimaryKeyColumn, Object data) { @@ -58,14 +67,11 @@ public class TableData { if (this.logger.isDebugEnabled()) { this.logger.debug("Column " + columnName + " is a primary key column and is null"); } + hasNullPrimaryKey = true; //hasValidPrimaryKey = false; - if uncommented and JIRA-1464, RecursiveTests.testReadEngineParts() will fail - } - if(data != null){ + } else { primaryKey.add(data); } - else{ - hasNullPrimaryKey = true; - } } } |