[DS-734] Cache columnNames in TableRowIterator, reduce database accesses in Item.update (getting metadatafields)

git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@5721 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2010-11-03 20:10:11 +00:00
parent e0da343294
commit 6d6c309371
3 changed files with 74 additions and 7 deletions

View File

@@ -48,6 +48,7 @@ import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.util.AuthorizeUtil;
import org.dspace.authorize.AuthorizeConfiguration;
@@ -1770,10 +1771,29 @@ public class Item extends DSpaceObject
}
}
private transient MetadataField[] allMetadataFields = null;
private MetadataField getMetadataField(DCValue dcv) throws SQLException, AuthorizeException
{
return MetadataField.findByElement(ourContext,
getMetadataSchemaID(dcv), dcv.element, dcv.qualifier);
if (allMetadataFields == null)
{
allMetadataFields = MetadataField.findAll(ourContext);
}
if (allMetadataFields != null)
{
int schemaID = getMetadataSchemaID(dcv);
for (MetadataField field : allMetadataFields)
{
if (field.getSchemaID() == schemaID &&
StringUtils.equals(field.getElement(), dcv.element) &&
StringUtils.equals(field.getQualifier(), dcv.qualifier))
{
return field;
}
}
}
return null;
}
private int getMetadataSchemaID(DCValue dcv) throws SQLException

View File

@@ -915,7 +915,7 @@ public class DatabaseManager
* @exception SQLException
* If a database error occurs
*/
protected static List<String> getColumnNames(String table) throws SQLException
static List<String> getColumnNames(String table) throws SQLException
{
List<String> results = new ArrayList<String>();
Collection<ColumnInfo> info = getColumnInfo(table);
@@ -938,8 +938,7 @@ public class DatabaseManager
* @exception SQLException
* If a database error occurs
*/
protected static List<String> getColumnNames(ResultSetMetaData meta)
throws SQLException
static List<String> getColumnNames(ResultSetMetaData meta) throws SQLException
{
List<String> results = new ArrayList<String>();
int columns = meta.getColumnCount();
@@ -1170,11 +1169,31 @@ public class DatabaseManager
* If a database error occurs
*/
static TableRow process(ResultSet results, String table) throws SQLException
{
return process(results, table, null);
}
/**
* Convert the current row in a ResultSet into a TableRow object.
*
* @param results
* A ResultSet to process
* @param table
* The name of the table
* @param pColumnNames
* The name of the columns in this resultset
* @return A TableRow object with the data from the ResultSet
* @exception SQLException
* If a database error occurs
*/
static TableRow process(ResultSet results, String table, List<String> pColumnNames) throws SQLException
{
ResultSetMetaData meta = results.getMetaData();
int columns = meta.getColumnCount() + 1;
List<String> columnNames = (table == null) ? getColumnNames(meta) : getColumnNames(table);
// If we haven't been passed the column names try to generate them from the metadata / table
List<String> columnNames = pColumnNames != null ? pColumnNames :
((table == null) ? getColumnNames(meta) : getColumnNames(table));
TableRow row = new TableRow(canonicalize(table), columnNames);

View File

@@ -45,6 +45,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -81,6 +82,11 @@ public class TableRowIterator
*/
private boolean hasAdvanced = false;
/**
* Column names for the results in this table
*/
List<String> columnNames = null;
/**
* Constructor
*
@@ -102,9 +108,31 @@ public class TableRowIterator
* The name of the table
*/
TableRowIterator(ResultSet results, String table)
{
this(results, table, null);
statemt = null;
}
TableRowIterator(ResultSet results, String table, List<String> columnNames)
{
this.results = results;
this.table = table;
if (columnNames == null)
{
try
{
this.columnNames = (table == null) ? DatabaseManager.getColumnNames(results.getMetaData()) : DatabaseManager.getColumnNames(table);
}
catch (SQLException e)
{
this.columnNames = null;
}
}
else
{
this.columnNames = Collections.unmodifiableList(columnNames);
}
statemt = null;
}
@@ -152,7 +180,7 @@ public class TableRowIterator
hasAdvanced = false;
return DatabaseManager.process(results, table);
return DatabaseManager.process(results, table, columnNames);
}
/**