db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r397313 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
Date Wed, 26 Apr 2006 21:51:21 GMT
Author: djd
Date: Wed Apr 26 14:51:20 2006
New Revision: 397313

URL: http://svn.apache.org/viewcvs?rev=397313&view=rev
Log:
DERBY-438 (partial) Optimize Blob.length() to avoid reading the whole stream if the length
is encoded in the stream from the store or the length has already been obtained once.

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

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java?rev=397313&r1=397312&r2=397313&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java Wed Apr 26 14:51:20
2006
@@ -70,9 +70,15 @@
 
 final class EmbedBlob extends ConnectionChild implements Blob
 {
-    // clob is either bytes or stream
+    // blob is either bytes or stream
     private boolean         isBytes;
     private InputStream     myStream;
+    /*
+     * Length of the BLOB if known. Set to -1 if
+     * the current length of the BLOB is not known.
+     */
+    private long myLength = -1;
+    
     private byte[]          myBytes;
     // note: cannot control position of the stream since user can do a getBinaryStream
     private long            pos;
@@ -107,6 +113,7 @@
             if (SanityManager.DEBUG)
                 SanityManager.ASSERT(dvdBytes != null,"blob has a null value underneath");
 
+            myLength = dvdBytes.length;
             myBytes = new byte[dvdBytes.length];
             System.arraycopy(dvdBytes, 0, myBytes, 0, dvdBytes.length);
         }
@@ -208,12 +215,13 @@
     public long length()
         throws SQLException
     {
+        if (myLength != -1)
+            return myLength;
+        
         boolean pushStack = false;
         try
         {
-            if (isBytes)
-                return myBytes.length;
-            // we have a stream
+           // we have a stream
             synchronized (getConnectionSynchronization())
             {
                 pushStack = !getEmbedConnection().isClosed();
@@ -221,6 +229,14 @@
                     setupContextStack();
 
                 setPosition(0);
+                // If possible get the length from the encoded
+                // length at the front of the raw stream.
+                if ((myLength = biStream.getLength()) != -1) {
+                    biStream.close();
+                   return myLength;
+                }
+                
+                // Otherwise have to read the entire stream!
                 for (;;)
                 {
                     int size = biStream.read(buf);
@@ -228,6 +244,9 @@
                         break;
                     pos += size;
                 }
+                // Save for future uses.
+                myLength = pos;
+                biStream.close();
                 return pos;
             }
         }



Mime
View raw message