Repository: empire-db Updated Branches: refs/heads/master 914c76985 -> 95a93b71c
EMPIREDB-213 use a field index map to improve performance Project: http://git-wip-us.apache.org/repos/asf/empire-db/repo Commit: http://git-wip-us.apache.org/repos/asf/empire-db/commit/95a93b71 Tree: http://git-wip-us.apache.org/repos/asf/empire-db/tree/95a93b71 Diff: http://git-wip-us.apache.org/repos/asf/empire-db/diff/95a93b71 Branch: refs/heads/master Commit: 95a93b71ccbdba5021d9121ab6ba4e5d434355cc Parents: 914c769 Author: Rainer Döbele <[email protected]> Authored: Thu Feb 25 12:52:05 2016 +0100 Committer: Rainer Döbele <[email protected]> Committed: Thu Feb 25 12:52:05 2016 +0100 ---------------------------------------------------------------------- .../java/org/apache/empire/db/DBReader.java | 134 ++++++++++++++----- .../java/org/apache/empire/db/DBRowSet.java | 2 +- 2 files changed, 100 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/empire-db/blob/95a93b71/empire-db/src/main/java/org/apache/empire/db/DBReader.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBReader.java b/empire-db/src/main/java/org/apache/empire/db/DBReader.java index 80189c2..d480564 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBReader.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBReader.java @@ -238,18 +238,29 @@ public class DBReader extends DBRecordData private static ThreadLocal<Map<DBReader, Exception>> threadLocalOpenResultSets = new ThreadLocal<Map<DBReader, Exception>>(); // Object references - protected DBDatabase db = null; - protected DBColumnExpr[] colList = null; - - // Direct column access - protected ResultSet rset = null; + private DBDatabase db = null; + private DBColumnExpr[] colList = null; + private ResultSet rset = null; + // the field index map + private Map<ColumnExpr, Integer> fieldIndexMap = null; /** - * Constructs an empty DBRecordSet object. + * Constructs a default DBReader object with the fieldIndexMap enabled. */ public DBReader() { // Default Constructor + this(true); + } + + /** + * Constructs an empty DBRecordSet object. + * @param useFieldIndexMap + */ + public DBReader(boolean useFieldIndexMap) + { + if (useFieldIndexMap) + fieldIndexMap = new HashMap<ColumnExpr, Integer>(); } /** @@ -282,30 +293,20 @@ public class DBReader extends DBRecordData * @return the index value */ @Override - public int getFieldIndex(ColumnExpr column) + public int getFieldIndex(ColumnExpr column) { - if (colList != null) - { - // First chance: Try to find an exact match - for (int i = 0; i < colList.length; i++) - { - if (colList[i].equals(column)) - return i; - } - // Second chance: Try Update Column - if (column instanceof DBColumn) - { - for (int i = 0; i < colList.length; i++) - { - DBColumn updColumn = colList[i].getUpdateColumn(); - if (updColumn!=null && updColumn.equals(column)) - return i; - } - } + if (fieldIndexMap==null) + return findFieldIndex(column); + // Use fieldIndexMap + Integer index = fieldIndexMap.get(column); + if (index==null) + { // add to field Index map + index = findFieldIndex(column); + fieldIndexMap.put(column, index); } - return -1; + return index; } - + /** Get the column Expression at position */ @Override public DBColumnExpr getColumnExpr(int iColumn) @@ -394,7 +395,7 @@ public class DBReader extends DBRecordData { return (rset != null); } - + /** * Opens the reader by executing the given SQL command.<BR> * After the reader is open, the reader's position is before the first record.<BR> @@ -425,14 +426,12 @@ public class DBReader extends DBRecordData paramValues = subqueryParamValues.get(0); } // Execute the query - db = cmd.getDatabase(); - rset = db.executeQuery(sqlCmd, paramValues, scrollable, conn); - if (rset==null) + DBDatabase queryDb = cmd.getDatabase(); + ResultSet queryRset = queryDb.executeQuery(sqlCmd, paramValues, scrollable, conn); + if (queryRset==null) throw new QueryNoResultException(sqlCmd); - // successfully opened - colList = cmd.getSelectExprList(); - // add to tracking list (if enabled) - trackThisResultSet(); + // init + init(queryDb, cmd.getSelectExprList(), queryRset); } /** @@ -495,6 +494,9 @@ public class DBReader extends DBRecordData // Detach columns colList = null; rset = null; + // clear FieldIndexMap + if (fieldIndexMap!=null) + fieldIndexMap.clear(); // Done } catch (Exception e) { // What's wrong here? @@ -822,6 +824,68 @@ public class DBReader extends DBRecordData } /** + * Initialize the reader from an open JDBC-ResultSet + * @param db the database + * @param colList the query column expressions + * @param rset the JDBC-ResultSet + */ + protected void init(DBDatabase db, DBColumnExpr[] colList, ResultSet rset) + { + this.db = db; + this.colList = colList; + this.rset = rset; + // add to tracking list (if enabled) + trackThisResultSet(); + } + + /** + * Access the column expression list + * @return the column expression list + */ + protected final DBColumnExpr[] getColumnExprList() + { + return colList; + } + + /** + * Access the JDBC-ResultSet + * @return the JDBC-ResultSet + */ + protected final ResultSet getResultSet() + { + return rset; + } + + /** + * finds the field Index of a given column expression + * Internally used as helper for getFieldIndex() + * @return the index value + */ + protected int findFieldIndex(ColumnExpr column) + { + if (colList == null) + return -1; + // First chance: Try to find an exact match + for (int i = 0; i < colList.length; i++) + { + if (colList[i].equals(column)) + return i; + } + // Second chance: Try Update Column + if (column instanceof DBColumn) + { + for (int i = 0; i < colList.length; i++) + { + DBColumn updColumn = colList[i].getUpdateColumn(); + if (updColumn!=null && updColumn.equals(column)) + return i; + } + } + // not found! + return -1; + } + + /** * internal helper function to find parameterized subqueries * @param cmd the command * @return a list of parameter arrays, one for each subquery http://git-wip-us.apache.org/repos/asf/empire-db/blob/95a93b71/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java index 0c46e12..25d4b02 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java @@ -609,7 +609,7 @@ public abstract class DBRowSet extends DBExpr DBReader reader = null; try { // read record using a DBReader - reader = new DBReader(); + reader = new DBReader(false); reader.getRecordData(cmd, conn); initRecord(rec, reader);
