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 2008/10/29 15:18:58 UTC

svn commit: r708912 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/StoreStreamClob.java

Author: kristwaa
Date: Wed Oct 29 07:18:57 2008
New Revision: 708912

URL: http://svn.apache.org/viewvc?rev=708912&view=rev
Log:
DERBY-2822: Add caching of store stream length in StoreStreamClob, if appropriate.
StoreStreamClob is a read-only representation of a Clob, and will now cache the character length after it has been obtained the first time. Getting the length initially is still expensive, as the UTF-8 data stream has to be decoded.
Patch file: derby-2822-1b-cacheCharLength.diff

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

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/StoreStreamClob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/StoreStreamClob.java?rev=708912&r1=708911&r2=708912&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/StoreStreamClob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/StoreStreamClob.java Wed Oct 29 07:18:57 2008
@@ -65,6 +65,14 @@
      */
     //@GuardedBy("synchronizationObject")
     private final PositionedStoreStream positionedStoreStream;
+    /**
+     * The cached length of the store stream in number of characters.
+     * A value of {@code 0} means the length is unknown, and zero is an invalid
+     * length for a store stream Clob. It is set to zero because that is the
+     * value encoded as length in the store stream (on disk format) when the
+     * length is unknown or cannot be represented.
+     */
+    private long cachedCharLength = 0;
     /** The connection (child) this Clob belongs to. */
     private final ConnectionChild conChild;
     /** Object used for synchronizing access to the store stream. */
@@ -134,17 +142,21 @@
     public long getCharLength()
             throws SQLException {
         checkIfValid();
-        synchronized (this.synchronizationObject) {
-            this.conChild.setupContextStack();
-            try {
-                return UTF8Util.skipUntilEOF(
-                                new BufferedInputStream(getRawByteStream()));
-            } catch (Throwable t) {
-                throw noStateChangeLOB(t);
-            } finally {
-                this.conChild.restoreContextStack();
+        if (this.cachedCharLength == 0) {
+            // Decode the stream to find the length.
+            synchronized (this.synchronizationObject) {
+                this.conChild.setupContextStack();
+                try {
+                    this.cachedCharLength = UTF8Util.skipUntilEOF(
+                            new BufferedInputStream(getRawByteStream()));
+                } catch (Throwable t) {
+                    throw noStateChangeLOB(t);
+                } finally {
+                    this.conChild.restoreContextStack();
+                }
             }
         }
+        return this.cachedCharLength;
     }
 
     /**