Changeset: 6ba465efcea0 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6ba465efcea0
Modified Files:
        java/src/nl/cwi/monetdb/jdbc/MonetBlob.java
        java/src/nl/cwi/monetdb/jdbc/MonetClob.java
        java/src/nl/cwi/monetdb/jdbc/MonetConnection.java
        java/src/nl/cwi/monetdb/jdbc/MonetDataSource.java
        java/src/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java
        java/src/nl/cwi/monetdb/jdbc/MonetDriver.java.in
        java/src/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
        java/src/nl/cwi/monetdb/jdbc/MonetResultSet.java
        java/src/nl/cwi/monetdb/jdbc/MonetSavepoint.java
        java/src/nl/cwi/monetdb/jdbc/MonetStatement.java
        java/src/nl/cwi/monetdb/jdbc/MonetWrapper.java
Branch: Apr2012
Log Message:

jdbc: misc cosmetic and performance fixes

- cosmetic: use return without parenthesis
- use Arrays.copyOfRange instead of manual loop
- use StringBuilder for performance
- use stronger typing for collections
- MonetStatement: reduce synchronisation
- use easier collections looping


diffs (truncated from 5457 to 300 lines):

diff --git a/java/src/nl/cwi/monetdb/jdbc/MonetBlob.java 
b/java/src/nl/cwi/monetdb/jdbc/MonetBlob.java
--- a/java/src/nl/cwi/monetdb/jdbc/MonetBlob.java
+++ b/java/src/nl/cwi/monetdb/jdbc/MonetBlob.java
@@ -20,12 +20,13 @@
 package nl.cwi.monetdb.jdbc;
 
 import java.sql.*;
+import java.util.Arrays;
 import java.io.*;
 
 /**
- * The MonetBlob class implements the java.sql.Blob interface.  Because
+ * The MonetBlob class implements the {@link java.sql.Blob} interface.  Because
  * MonetDB/SQL currently has no support for streams, this class is a
- * shallow wrapper of a StringBuffer.  It is more or less supplied to
+ * shallow wrapper of a {@link StringBuilder}.  It is more or less supplied to
  * enable an application that depends on it to run.  It may be obvious
  * that it is a real resource expensive workaround that contradicts the
  * benefits for a Blob: avoidance of huge resource consumption.
@@ -35,12 +36,18 @@ import java.io.*;
 public class MonetBlob implements Blob {
        private byte[] buf;
 
-       protected MonetBlob(String in) {
+       protected MonetBlob(byte[] data) {
+               buf = data;
+       }
+       
+       static MonetBlob create(String in) {
                int len = in.length() / 2;
-               buf = new byte[len];
+               byte[] buf = new byte[len];
                for (int i = 0; i < len; i++)
                        buf[i] = (byte)Integer.parseInt(in.substring(2 * i, (2 
* i) + 2), 16);
+               return new MonetBlob(buf);
        }
+       
 
        //== begin interface Blob
        
@@ -74,7 +81,7 @@ public class MonetBlob implements Blob {
        public InputStream getBinaryStream() throws SQLException {
                if (buf == null)
                        throw new SQLException("This Blob object has been 
freed", "M1M20");
-               return(new ByteArrayInputStream(buf));
+               return new ByteArrayInputStream(buf);
        }
 
        /**
@@ -105,7 +112,7 @@ public class MonetBlob implements Blob {
                        throw new SQLException("pos is greater than the number 
of bytes in the Blob", "M1M05");
                if (pos - 1 + length > buf.length)
                        throw new SQLException("pos + length is greater than 
the number of bytes in the Blob", "M1M05");
-               return(new ByteArrayInputStream(buf, (int)(pos - 1), 
(int)length));
+               return new ByteArrayInputStream(buf, (int)(pos - 1), 
(int)length);
        }
 
        /**
@@ -126,10 +133,7 @@ public class MonetBlob implements Blob {
                if (buf == null)
                        throw new SQLException("This Blob object has been 
freed", "M1M20");
                try {
-                       byte[] r = new byte[length];
-                       for (int i = 0; i < length; i++)
-                               r[i] = buf[(int)pos - 1 + i];
-                       return(r);
+                       return Arrays.copyOfRange(buf, (int) pos - 1, (int) pos 
- 1 + length);
                } catch (IndexOutOfBoundsException e) {
                        throw new SQLException(e.getMessage(), "M0M10");
                }
@@ -146,7 +150,7 @@ public class MonetBlob implements Blob {
        public long length() throws SQLException {
                if (buf == null)
                        throw new SQLException("This Blob object has been 
freed", "M1M20");
-               return((long)buf.length);
+               return (long)buf.length;
        }
 
        /**
@@ -163,7 +167,7 @@ public class MonetBlob implements Blob {
         *         BLOB value
         */
        public long position(Blob pattern, long start) throws SQLException {
-               return(position(pattern.getBytes(1L, (int)pattern.length()), 
start));
+               return position(pattern.getBytes(1L, (int)pattern.length()), 
start);
        }
 
        /**
@@ -189,12 +193,12 @@ public class MonetBlob implements Blob {
                                                break;
                                }
                                if (j == pattern.length)
-                                       return(i);
+                                       return i;
                        }
                } catch (IndexOutOfBoundsException e) {
                        throw new SQLException(e.getMessage(), "M0M10");
                }
-               return(-1);
+               return -1;
        }
 
        /**
@@ -237,7 +241,7 @@ public class MonetBlob implements Blob {
         *         BLOB value
         */
        public int setBytes(long pos, byte[] bytes) throws SQLException {
-               return(setBytes(pos, bytes, 1, bytes.length));
+               return setBytes(pos, bytes, 1, bytes.length);
        }
 
        /**
@@ -270,7 +274,7 @@ public class MonetBlob implements Blob {
                } catch (IndexOutOfBoundsException e) {
                        throw new SQLException(e.getMessage(), "M0M10");
                }
-               return(len);
+               return len;
        }
 
        /**
diff --git a/java/src/nl/cwi/monetdb/jdbc/MonetClob.java 
b/java/src/nl/cwi/monetdb/jdbc/MonetClob.java
--- a/java/src/nl/cwi/monetdb/jdbc/MonetClob.java
+++ b/java/src/nl/cwi/monetdb/jdbc/MonetClob.java
@@ -23,9 +23,9 @@ import java.sql.*;
 import java.io.*;
 
 /**
- * The MonetClob class implements the java.sql.Clob interface.  Because
+ * The MonetClob class implements the {@link java.sql.Clob} interface.  Because
  * MonetDB/SQL currently has no support for streams, this class is a
- * shallow wrapper of a StringBuffer.  It is more or less supplied to
+ * shallow wrapper of a {@link StringBuilder}.  It is more or less supplied to
  * enable an application that depends on it to run.  It may be obvious
  * that it is a real resource expensive workaround that contradicts the
  * sole reason for a Clob: avoidance of huge resource consumption.
@@ -34,10 +34,11 @@ import java.io.*;
  * @author Fabian Groffen <[email protected]>
  */
 public class MonetClob implements Clob {
-       private StringBuffer buf;
+       
+       private StringBuilder buf;
 
        protected MonetClob(String in) {
-               buf = new StringBuffer(in);
+               buf = new StringBuilder(in);
        }
 
        //== begin interface Clob
@@ -115,7 +116,7 @@ public class MonetClob implements Clob {
                if (buf == null)
                        throw new SQLException("This Clob has been freed", 
"M1M20");
                try {
-                       return(buf.substring((int)(pos - 1), (int)(pos - 1 + 
length)));
+                       return buf.substring((int)(pos - 1), (int)(pos - 1 + 
length));
                } catch (IndexOutOfBoundsException e) {
                        throw new SQLException(e.getMessage());
                }
@@ -132,7 +133,7 @@ public class MonetClob implements Clob {
        public long length() throws SQLException {
                if (buf == null)
                        throw new SQLException("This Clob has been freed", 
"M1M20");
-               return((long)buf.length());
+               return (long)buf.length();
        }
 
        /**
@@ -149,7 +150,7 @@ public class MonetClob implements Clob {
         *         CLOB value
         */
        public long position(Clob searchstr, long start) throws SQLException {
-               return(position(searchstr.getSubString(1L, 
(int)(searchstr.length())), start));
+               return position(searchstr.getSubString(1L, 
(int)(searchstr.length())), start);
        }
 
        /**
@@ -168,7 +169,7 @@ public class MonetClob implements Clob {
        public long position(String searchstr, long start) throws SQLException {
                if (buf == null)
                        throw new SQLException("This Clob has been freed", 
"M1M20");
-               return((long)(buf.indexOf(searchstr, (int)(start - 1))));
+               return (long)(buf.indexOf(searchstr, (int)(start - 1)));
        }
 
        public OutputStream setAsciiStream(long pos) throws SQLException {
@@ -196,7 +197,7 @@ public class MonetClob implements Clob {
         *         CLOB value
         */
        public int setString(long pos, String str) throws SQLException {
-               return(setString(pos, str, 1, str.length()));
+               return setString(pos, str, 1, str.length());
        }
 
        /**
@@ -225,9 +226,9 @@ public class MonetClob implements Clob {
                
                if (retlen > 0) {
                        buf.replace((int)(pos - 1), (int)(pos + retlen), 
str.substring(offset - 1, (offset + len)));
-                       return(retlen);
+                       return retlen;
                } else {
-                       return(0);
+                       return 0;
                }
        }
 
@@ -255,7 +256,7 @@ public class MonetClob implements Clob {
         */
        public String toString() {
                if (buf == null)
-                       return("<a freed MonetClob instance>");
-               return(buf.toString());
+                       return "<a freed MonetClob instance>";
+               return buf.toString();
        }
 }
diff --git a/java/src/nl/cwi/monetdb/jdbc/MonetConnection.java 
b/java/src/nl/cwi/monetdb/jdbc/MonetConnection.java
--- a/java/src/nl/cwi/monetdb/jdbc/MonetConnection.java
+++ b/java/src/nl/cwi/monetdb/jdbc/MonetConnection.java
@@ -19,21 +19,49 @@
 
 package nl.cwi.monetdb.jdbc;
 
-import java.sql.*;
-import java.util.*;
-import java.io.*;
-import java.nio.*;
-import java.security.*;
-import java.util.concurrent.Executor;
+import java.io.File;
+import java.io.IOException;
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
-import nl.cwi.monetdb.mcl.io.*;
-import nl.cwi.monetdb.mcl.net.*;
-import nl.cwi.monetdb.mcl.parser.*;
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.CallableStatement;
+import java.sql.Clob;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.NClob;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.SQLWarning;
+import java.sql.SQLXML;
+import java.sql.Savepoint;
+import java.sql.Statement;
+import java.sql.Struct;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.WeakHashMap;
+import java.util.concurrent.Executor;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
 import nl.cwi.monetdb.mcl.MCLException;
+import nl.cwi.monetdb.mcl.io.BufferedMCLReader;
+import nl.cwi.monetdb.mcl.io.BufferedMCLWriter;
+import nl.cwi.monetdb.mcl.net.MapiSocket;
+import nl.cwi.monetdb.mcl.parser.HeaderLineParser;
+import nl.cwi.monetdb.mcl.parser.MCLParseException;
+import nl.cwi.monetdb.mcl.parser.StartOfHeaderParser;
 
 /**
- * A Connection suitable for the MonetDB database.
+ * A {@link Connection} suitable for the MonetDB database.
  * <br /><br />
  * This connection represents a connection (session) to a MonetDB
  * database. SQL statements are executed and results are returned within
@@ -87,12 +115,12 @@ public class MonetConnection extends Mon
        private SQLWarning warnings = null;
        /** The Connection specific mapping of user defined types to Java
         * types (not used) */
-       private Map typeMap = new HashMap();
+       private Map<String,Class<?>> typeMap = new HashMap<String,Class<?>>();
 
        // See javadoc for documentation about WeakHashMap if you don't know 
what
        // it does !!!NOW!!! (only when you deal with it of course)
        /** A Map containing all (active) Statements created from this 
Connection */
-       private Map statements = new WeakHashMap();
+       private Map<Statement,?> statements = new WeakHashMap<Statement, 
Object>();
 
        /** The number of results we receive from the server at once */
        private int curReplySize = -1;  // the server by default uses -1 (all)
@@ -126,8 +154,7 @@ public class MonetConnection extends Mon
         * @throws SQLException if a database error occurs
         * @throws IllegalArgumentException is one of the arguments is null or 
empty
         */
-       MonetConnection(
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to