[Commits] Rev 405: Use arrays rather than lists to represent resultset metadata and cached result set row, for array are more efficient (faster element access, less space) in file:///H:/bzr/mariadb-java-client/

Vladislav Vaintroub wlad at montyprogram.com
Tue Feb 12 23:29:07 EET 2013


At file:///H:/bzr/mariadb-java-client/

------------------------------------------------------------
revno: 405
revision-id: wlad at montyprogram.com-20130212212901-1bgu8wqjjzm1g4mf
parent: wlad at montyprogram.com-20130212145623-af1bwz5guqm8alvd
committer: Vladislav Vaintroub <wlad at montyprogram.com>
branch nick: mariadb-java-client
timestamp: Tue 2013-02-12 22:29:01 +0100
message:
  Use arrays rather than lists to represent resultset metadata and cached result set row, for array are more  efficient (faster element access, less space)
-------------- next part --------------
=== modified file 'src/main/java/org/mariadb/jdbc/MySQLPreparedStatement.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLPreparedStatement.java	2012-12-21 18:15:36 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLPreparedStatement.java	2013-02-12 21:29:01 +0000
@@ -322,8 +322,9 @@ public class MySQLPreparedStatement exte
      * @since 1.2
      */
     public ResultSetMetaData getMetaData() throws SQLException {
-        if (super.getResultSet() != null) {
-            return super.getResultSet().getMetaData();
+        ResultSet rs = getResultSet();
+        if (rs != null) {
+            return rs.getMetaData();
         }
         return null;
     }

=== modified file 'src/main/java/org/mariadb/jdbc/MySQLResultSet.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLResultSet.java	2013-02-12 14:56:23 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLResultSet.java	2013-02-12 21:29:01 +0000
@@ -69,7 +69,6 @@ import java.net.URL;
 import java.sql.*;
 import java.text.ParseException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.List;
@@ -96,8 +95,8 @@ public class MySQLResultSet implements R
     }
 
     private static MySQLResultSet createEmptyResultSet() {
-        List<ColumnInformation> colList = Collections.emptyList();
-        List<List<ValueObject>> voList = Collections.emptyList();
+        ColumnInformation[] colList = new ColumnInformation[0];
+        List<ValueObject[]> voList = Collections.emptyList();
         QueryResult qr = new CachedSelectResult(colList, voList, (short) 0);
         return new MySQLResultSet(qr, null, null);
     }
@@ -3701,22 +3700,22 @@ public class MySQLResultSet implements R
         
         byte[] BOOL_TRUE = {1};
         byte[] BOOL_FALSE ={0};
-        List<List<ValueObject>> rows  = new ArrayList<List<ValueObject>>();
+        List<ValueObject[]> rows  = new ArrayList<ValueObject[]>();
         for(String[] rowData : data) {
-            List<ValueObject> row = new ArrayList<ValueObject>();
+            ValueObject[] row = new ValueObject[N];
  
             if (rowData.length != N) {
                 throw new RuntimeException("Number of elements in the row != number of columns :" + rowData[0]);
             }
             for(int i = 0; i < N; i++){
                 if (columnTypes[i] == MySQLType.Type.BIT)
-                    row.add(new MySQLValueObject(rowData[i].equals("0")?BOOL_FALSE:BOOL_TRUE,columns[i]));
+                    row[i] = new MySQLValueObject(rowData[i].equals("0")?BOOL_FALSE:BOOL_TRUE,columns[i]);
                 else
-                    row.add(new MySQLValueObject(rowData[i].getBytes(),columns[i]));
+                    row[i] = new MySQLValueObject(rowData[i].getBytes(),columns[i]);
             }
             rows.add(row);
         }
-        return new MySQLResultSet(new CachedSelectResult(Arrays.asList(columns), rows, (short)0),
+        return new MySQLResultSet(new CachedSelectResult(columns , rows, (short)0),
                 null, protocol);
     }
 }

=== modified file 'src/main/java/org/mariadb/jdbc/MySQLResultSetMetaData.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLResultSetMetaData.java	2013-02-03 20:39:59 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLResultSetMetaData.java	2013-02-12 21:29:01 +0000
@@ -59,15 +59,14 @@ import java.math.BigInteger;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Types;
-import java.util.List;
 
 
 public class MySQLResultSetMetaData implements ResultSetMetaData {
 
-    private List<ColumnInformation> fieldPackets;
+    private ColumnInformation[] fieldPackets;
     private int datatypeMappingflags;
 
-    public MySQLResultSetMetaData(List<ColumnInformation> fieldPackets, int datatypeMappingFlags) {
+    public MySQLResultSetMetaData(ColumnInformation[] fieldPackets, int datatypeMappingFlags) {
         this.fieldPackets = fieldPackets;
         this.datatypeMappingflags = datatypeMappingFlags;
     }
@@ -79,7 +78,7 @@ public class MySQLResultSetMetaData impl
      * @throws java.sql.SQLException if a database access error occurs
      */
     public int getColumnCount() throws SQLException {
-        return fieldPackets.size();
+        return fieldPackets.length;
     }
 
     /**
@@ -460,8 +459,8 @@ public class MySQLResultSetMetaData impl
     }
 
     private ColumnInformation getColumnInformation(int column) throws SQLException {
-        if (column >= 1 && column <= fieldPackets.size()) {
-            return fieldPackets.get(column - 1);
+        if (column >= 1 && column <= fieldPackets.length) {
+            return fieldPackets[column - 1];
         }
         throw SQLExceptionMapper.getSQLException("No such column");
     }

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/CachedSelectResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/CachedSelectResult.java	2012-12-21 18:15:36 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/CachedSelectResult.java	2013-02-12 21:29:01 +0000
@@ -59,11 +59,11 @@ import java.util.List;
 
 public  class CachedSelectResult extends SelectQueryResult {
 
-    private  List<List<ValueObject>> resultSet;
+    private  List<ValueObject[]> resultSet;
     protected short warningCount;
     private int rowPointer;
 
-    public CachedSelectResult(List<ColumnInformation> ci, List<List<ValueObject>> result, short warningCount) {
+    public CachedSelectResult(ColumnInformation[] ci, List<ValueObject[]> result, short warningCount) {
         this.columnInformation = ci;
         this.resultSet = result;
         this.warningCount = warningCount;
@@ -72,7 +72,7 @@ public  class CachedSelectResult extends
 
 
     public static CachedSelectResult createCachedSelectResult(StreamingSelectResult streamingResult) throws IOException, QueryException {
-         final List<List<ValueObject>> valueObjects = new ArrayList<List<ValueObject>>();
+         final List<ValueObject[]> valueObjects = new ArrayList<ValueObject[]>();
 
         while(streamingResult.next()){
            valueObjects.add(streamingResult.values);
@@ -96,7 +96,7 @@ public  class CachedSelectResult extends
         return null;
     }
 
-    public List<ColumnInformation> getColumnInformation() {
+    public ColumnInformation[] getColumnInformation() {
         return columnInformation;
     }
 
@@ -113,10 +113,11 @@ public  class CachedSelectResult extends
         if (rowPointer >= resultSet.size()) {
             throw new NoSuchColumnException("Current position is after the last row");
         }
-        if (i < 0 || i > resultSet.get(rowPointer).size()) {
+        ValueObject[] row = resultSet.get(rowPointer);
+        if (i < 0 || i > row.length) {
             throw new NoSuchColumnException("No such column: " + i);
         }
-        return resultSet.get(rowPointer).get(i);
+        return row[i];
     }
 
     public int getRows() {

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/ColumnNameMap.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/ColumnNameMap.java	2013-01-15 01:18:28 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/ColumnNameMap.java	2013-02-12 21:29:01 +0000
@@ -4,17 +4,16 @@ import org.mariadb.jdbc.internal.common.
 
 import java.sql.SQLException;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 
 public class ColumnNameMap {
     Map<String, Integer> map;
     Map<String, Integer> labelMap;
-    List<ColumnInformation> columnInfo;
+    ColumnInformation[] columnInfo;
 
-    public ColumnNameMap(List<ColumnInformation> columnInfo) {
-       this.columnInfo = columnInfo;
+    public ColumnNameMap(ColumnInformation[] columnInformations) {
+       this.columnInfo = columnInformations;
     }
 
     public int getIndex(String name) throws SQLException {

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/InsertIdQueryResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/InsertIdQueryResult.java	2013-02-12 14:56:23 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/InsertIdQueryResult.java	2013-02-12 21:29:01 +0000
@@ -54,16 +54,13 @@ import org.mariadb.jdbc.internal.common.
 import org.mariadb.jdbc.internal.mysql.MySQLColumnInformation;
 import org.mariadb.jdbc.internal.mysql.MySQLType;
 
-import java.util.ArrayList;
-import java.util.List;
-
 
 public class InsertIdQueryResult extends SelectQueryResult {
 
     private final long insertId;
     private int rowPointer = 0;
     private final long rows;
-    private static List<ColumnInformation> ci;
+    private static ColumnInformation[] ci;
 
 
     public InsertIdQueryResult(final long insertId, final long rows) {
@@ -94,16 +91,12 @@ public class InsertIdQueryResult extends
         return rowPointer++ < rows;
     }
 
-    public synchronized List<ColumnInformation> getColumnInformation() {
+    public synchronized ColumnInformation[] getColumnInformation() {
         if (ci != null)
             return ci;
-
         MySQLColumnInformation info = MySQLColumnInformation.create("insert_id", MySQLType.Type.BIGINT);
-
-
-         ci =  new ArrayList<ColumnInformation>();
-         ci.add(info);
-         return ci;
+        ci = new ColumnInformation[] {info};
+        return ci;
     }
 
 

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/QueryResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/QueryResult.java	2012-12-21 18:15:36 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/QueryResult.java	2013-02-12 21:29:01 +0000
@@ -51,8 +51,6 @@ package org.mariadb.jdbc.internal.common
 
 import org.mariadb.jdbc.internal.common.ColumnInformation;
 
-import java.util.List;
-
 
 public abstract class QueryResult {
     public abstract ResultSetType getResultSetType();
@@ -69,7 +67,7 @@ public abstract class QueryResult {
 
     public abstract String getMessage();
 
-    public abstract List<ColumnInformation> getColumnInformation();
+    public abstract ColumnInformation[] getColumnInformation();
 
     public abstract int getRows();
 }

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/SelectQueryResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/SelectQueryResult.java	2012-12-21 18:15:36 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/SelectQueryResult.java	2013-02-12 21:29:01 +0000
@@ -56,15 +56,14 @@ import org.mariadb.jdbc.internal.common.
 import java.io.IOException;
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
-import java.util.List;
 
 
 public abstract class SelectQueryResult extends QueryResult {
 
-    List<ColumnInformation> columnInformation;
+    ColumnInformation[] columnInformation;
     short warningCount;
 
-    public List<ColumnInformation> getColumnInformation() {
+    public ColumnInformation[] getColumnInformation() {
         return columnInformation;
     }
 

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/StreamingSelectResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/StreamingSelectResult.java	2013-02-07 22:13:02 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/StreamingSelectResult.java	2013-02-12 21:29:01 +0000
@@ -6,23 +6,22 @@ import org.mariadb.jdbc.internal.common.
 import org.mariadb.jdbc.internal.common.ValueObject;
 import org.mariadb.jdbc.internal.common.packet.*;
 import org.mariadb.jdbc.internal.common.packet.buffer.ReadUtil;
+import org.mariadb.jdbc.internal.mysql.MySQLColumnInformation;
 import org.mariadb.jdbc.internal.mysql.MySQLProtocol;
-import org.mariadb.jdbc.internal.mysql.packet.MySQLFieldPacket;
 import org.mariadb.jdbc.internal.mysql.packet.MySQLRowPacket;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.List;
 
 public class StreamingSelectResult extends SelectQueryResult {
     PacketFetcher packetFetcher;
-    public List<ValueObject> values;
+    public ValueObject[] values;
     MySQLProtocol protocol;
     boolean isEOF;
     boolean beforeFirst;
 
 
-    private StreamingSelectResult(List<ColumnInformation> info, MySQLProtocol protocol, PacketFetcher fetcher) throws QueryException {
+    private StreamingSelectResult(ColumnInformation[] info, MySQLProtocol protocol, PacketFetcher fetcher) throws QueryException {
         this.columnInformation = info;
         this.protocol = protocol;
         this.packetFetcher = fetcher;
@@ -46,9 +45,10 @@ public class StreamingSelectResult exten
             throw new  QueryException("There is an active result set on the current connection, "+
                     "which must be closed prior to opening a new one");
         }
-
-        final List<ColumnInformation> ci = new ArrayList<ColumnInformation>();
-        for (int i = 0; i < packet.getFieldCount(); i++) {
+        long fieldCount = packet.getFieldCount();
+        ColumnInformation[] ci = new ColumnInformation[(int)fieldCount];
+        
+        for (int i = 0; i < fieldCount; i++) {
             final RawPacket rawPacket = packetFetcher.getRawPacket();
 
             // We do not expect an error packet, but check it just for safety
@@ -60,14 +60,13 @@ public class StreamingSelectResult exten
             // We do not expect OK or EOF packets either
             byte b = rawPacket.getByteBuffer().get(0);
             if (b == 0 || b == (byte)0xfe) {
-                // We do not expect OK or EOF packets here
                 throw new QueryException("Packets out of order when trying to read field packet - " +
                     "got packet starting with byte " + b + "packet content (hex) = "
                         + MySQLProtocol.hexdump(rawPacket.getByteBuffer(), 0));
             }
+            
             try {
-                ColumnInformation columnInfo = MySQLFieldPacket.columnInformationFactory(rawPacket);
-                ci.add(columnInfo);
+                ci[i] = new MySQLColumnInformation(rawPacket);
             } catch (Exception e) {
                 throw new QueryException("Error when trying to parse field packet : " + e + ",packet content (hex) = " +
                         MySQLProtocol.hexdump(rawPacket.getByteBuffer(), 0) , 0, "HY000", e);
@@ -151,7 +150,7 @@ public class StreamingSelectResult exten
      */
     @Override
     public ValueObject getValueObject(int i) throws NoSuchColumnException {
-        return values.get(i);
+        return values[i];
     }
 
     public int getRows() {

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/UpdateResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/UpdateResult.java	2012-12-21 18:15:36 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/UpdateResult.java	2013-02-12 21:29:01 +0000
@@ -51,8 +51,6 @@ package org.mariadb.jdbc.internal.common
 
 import org.mariadb.jdbc.internal.common.ColumnInformation;
 
-import java.util.List;
-
 
 public class UpdateResult extends ModifyQueryResult {
     private final long updateCount;
@@ -89,7 +87,7 @@ public class UpdateResult extends Modify
         return message;
     }
 
-    public List<ColumnInformation> getColumnInformation() {
+    public ColumnInformation[] getColumnInformation() {
         return null;
     }
 

=== modified file 'src/main/java/org/mariadb/jdbc/internal/mysql/packet/MySQLRowPacket.java'
--- a/src/main/java/org/mariadb/jdbc/internal/mysql/packet/MySQLRowPacket.java	2013-02-12 14:56:23 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/mysql/packet/MySQLRowPacket.java	2013-02-12 21:29:01 +0000
@@ -56,20 +56,18 @@ import org.mariadb.jdbc.internal.common.
 import org.mariadb.jdbc.internal.common.packet.buffer.Reader;
 import org.mariadb.jdbc.internal.mysql.MySQLValueObject;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.io.IOException;
 
 
 public class MySQLRowPacket {
-    private final List<ValueObject> columns;
+    private final ValueObject[] columns;
     private final Reader reader;
-    private final List<ColumnInformation> columnInformation;
+    private final ColumnInformation[] columnInformation;
 
-    public MySQLRowPacket(final RawPacket rawPacket, final List<ColumnInformation> columnInformation) throws IOException {
-        columns = new ArrayList<ValueObject>(columnInformation.size());
+    public MySQLRowPacket(final RawPacket rawPacket, final ColumnInformation[] columnInformation2) throws IOException {
+        columns = new ValueObject[columnInformation2.length];
         reader = new Reader(rawPacket);
-        this.columnInformation = columnInformation;
+        this.columnInformation = columnInformation2;
     }
 
     public boolean isPacketComplete() throws IOException {
@@ -83,13 +80,12 @@ public class MySQLRowPacket {
         reader.appendPacket(rawPacket);
     }
 
-    public List<ValueObject> getRow(PacketFetcher packetFetcher) throws IOException {
-        for (final ColumnInformation currentColumn : columnInformation) {
+    public ValueObject[] getRow(PacketFetcher packetFetcher) throws IOException {
+        for (int i = 0; i < columnInformation.length; i++) {
             while(!isPacketComplete()) {
                 appendPacket(packetFetcher.getRawPacket());
             }
-            final ValueObject dvo = new MySQLValueObject(reader.getLengthEncodedBytes(), currentColumn);
-            columns.add(dvo);
+            columns[i] = new MySQLValueObject(reader.getLengthEncodedBytes(), columnInformation[i]);
         }
         return columns;
     }

=== modified file 'src/test/java/org/mariadb/jdbc/ResultSetMetaDataTest.java'
--- a/src/test/java/org/mariadb/jdbc/ResultSetMetaDataTest.java	2013-02-02 23:32:30 +0000
+++ b/src/test/java/org/mariadb/jdbc/ResultSetMetaDataTest.java	2013-02-12 21:29:01 +0000
@@ -7,7 +7,6 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertTrue;
 
 
 public class ResultSetMetaDataTest extends BaseTest {



More information about the commits mailing list