You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by kr...@apache.org on 2009/01/20 13:24:54 UTC

svn commit: r736000 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc: EmbedCallableStatement20.java UTF8Reader.java

Author: kristwaa
Date: Tue Jan 20 04:24:54 2009
New Revision: 736000

URL: http://svn.apache.org/viewvc?rev=736000&view=rev
Log:
DERBY-3934: Improve performance of reading modified Clobs.
Removed deprecated constructor and adjusted calling code.
Patch file: derby-3934-5a-UTF8Reader_cleanup.diff


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java?rev=736000&r1=735999&r2=736000&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java Tue Jan 20 04:24:54 2009
@@ -35,7 +35,6 @@
 import java.sql.Clob;
 import java.sql.Ref;
 
-import java.net.URL;
 import java.util.Map;
 
 import java.io.ByteArrayInputStream;
@@ -49,11 +48,12 @@
 
 import org.apache.derby.iapi.error.StandardException;
 
+import org.apache.derby.iapi.jdbc.CharacterStreamDescriptor;
 import org.apache.derby.iapi.services.io.StreamStorable;
-import org.apache.derby.iapi.sql.conn.StatementContext;
 import org.apache.derby.iapi.reference.JDBC30Translation;
 import org.apache.derby.iapi.reference.SQLState;
 import org.apache.derby.iapi.types.DataValueDescriptor;
+import org.apache.derby.iapi.types.StringDataValue;
 
 import org.apache.derby.impl.jdbc.Util;
 import org.apache.derby.impl.jdbc.EmbedConnection;
@@ -1124,7 +1124,7 @@
                 Object syncObject = getConnectionSynchronization();
                 synchronized (syncObject) {
                 try {
-                    DataValueDescriptor param = 
+                    StringDataValue param = (StringDataValue)
                         getParms().getParameterForGet(parameterIndex -1);
                     if (param.isNull()) {
                         break;
@@ -1132,12 +1132,12 @@
                     pushStack = true;
                     setupContextStack();
 
-                    StreamStorable ss = (StreamStorable)param;
-                    InputStream stream = ss.returnStream();
-                    if (stream == null) {
+                    CharacterStreamDescriptor csd =
+                            param.getStreamWithDescriptor();
+                    if (csd == null) {
                         reader = new StringReader(param.getString());
                     } else {
-                        reader = new UTF8Reader(stream, 0, this, syncObject);
+                        reader = new UTF8Reader(csd, this, syncObject);
                     }
                 } catch (Throwable t) {
                     throw EmbedResultSet.noStateChangeException(t);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java?rev=736000&r1=735999&r2=736000&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UTF8Reader.java Tue Jan 20 04:24:54 2009
@@ -33,7 +33,6 @@
 import org.apache.derby.iapi.jdbc.CharacterStreamDescriptor;
 import org.apache.derby.iapi.services.sanity.SanityManager;
 import org.apache.derby.iapi.types.PositionedStream;
-import org.apache.derby.iapi.types.Resetable;
 
 /**
  * Class for reading characters from streams encoded in the modified UTF-8
@@ -100,86 +99,14 @@
     private final CharacterStreamDescriptor csd;
 
     /**
-     * TODO: This constructor will be removed! Is is currently retrofitted to
-     *  use a CharacterStreamDescriptor.
+     * Constructs a reader on top of the source UTF-8 encoded stream.
      *
-     * Constructs a reader and consumes the encoded length bytes from the
-     * stream.
-     * <p>
-     * The encoded length bytes either state the number of bytes in the stream,
-     * or it is <code>0</code> which informs us the length is unknown or could
-     * not be represented and that we have to look for the Derby-specific
-     * end of stream marker.
-     * 
-     * @param in the underlying stream
-     * @param maxFieldSize the maximum allowed column length in characters
-     * @param parent the parent object / connection child
-     * @param synchronization synchronization object used when accessing the
-     *      underlying data stream
-     * 
+     * @param csd a description of and reference to the source stream
+     * @param conChild the parent object / connection child
+     * @param sync synchronization object used when accessing the underlying
+     *      data stream
      * @throws IOException if reading from the underlying stream fails
-     * @throws SQLException if setting up or restoring the context stack fails
      */
-    public UTF8Reader(
-        InputStream in,
-        long maxFieldSize,
-        ConnectionChild parent,
-        Object synchronization)
-            throws IOException, SQLException
-    {
-        super(synchronization);
-        this.parent = parent;
-        long utfLen = 0;
-
-        parent.setupContextStack();
-        try {
-            synchronized (lock) { // Synchronize access to store.
-                this.in = in; // Note the possible reassignment below.
-                if (in instanceof PositionedStoreStream) {
-                    this.positionedIn = (PositionedStoreStream)in;
-                    // This stream is already buffered, and buffering it again
-                    // this high up complicates the handling a lot. Must
-                    // implement a special buffered reader to buffer again.
-                    // Note that buffering this UTF8Reader again, does not
-                    // cause any trouble...
-                    try {
-                        ((Resetable)this.positionedIn).resetStream();
-                    } catch (StandardException se) {
-                        throw Util.newIOException(se);
-                    }
-                } else {
-                    this.positionedIn = null;
-                }
-                utfLen = readUnsignedShort();
-                // Even if we are reading the encoded length, the stream may
-                // not be a positioned stream. This is currently true when a
-                // stream is passed in after a ResultSet.getXXXStream method.
-                if (this.positionedIn != null) {
-                    this.rawStreamPos = this.positionedIn.getPosition();
-                }
-            } // End synchronized block
-        } finally {
-            parent.restoreContextStack();
-        }
-        // Setup buffering.
-        int bufferSize = calculateBufferSize(utfLen, maxFieldSize);
-        this.buffer = new char[bufferSize];
-        if (this.positionedIn == null) {
-            // Buffer this for improved performance.
-            // Note that the stream buffers bytes, whereas the internal buffer
-            // buffers characters. In worst case, the stream buffer must be
-            // filled three times to fill the internal character buffer.
-            this.in = new BufferedInputStream(in, bufferSize);
-        }
-        this.csd = new CharacterStreamDescriptor.Builder().
-                bufferable(positionedIn == null).
-                positionAware(positionedIn != null).
-                byteLength(utfLen == 0 ? 0 : utfLen +2). // Add header bytes
-                dataOffset(2).curBytePos(2).stream(in).
-                build();
-        utfCount = 2;
-    }
-
     public UTF8Reader(CharacterStreamDescriptor csd, ConnectionChild conChild,
             Object sync)
             throws IOException {
@@ -685,29 +612,6 @@
     }
 
     /**
-     * TODO: Remove this when CSD is fully integrated.
-     *
-     * Calculates an optimized buffer size.
-     * <p>
-     * The maximum size allowed is returned if the specified values don't give
-     * enough information to say a smaller buffer size is preferable.
-     *
-     * @param encodedSize data length in bytes
-     * @param maxFieldSize maximum data length in bytes
-     * @return An (sub)optimal buffer size.
-     */
-    private final int calculateBufferSize(long encodedSize, long maxFieldSize) {
-        int bufferSize = MAXIMUM_BUFFER_SIZE;
-        if (encodedSize > 0 && encodedSize < bufferSize) {
-            bufferSize = (int)encodedSize;
-        }
-        if (maxFieldSize > 0 && maxFieldSize < bufferSize) {
-            bufferSize = (int)maxFieldSize;
-        }
-        return bufferSize;
-    }
-
-    /**
      * Calculates an optimized buffer size.
      * <p>
      * The maximum size allowed is returned if the specified values don't give