db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r547296 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc: ClobUpdateableReader.java EmbedClob.java
Date Thu, 14 Jun 2007 16:30:47 GMT
Author: kristwaa
Date: Thu Jun 14 09:30:41 2007
New Revision: 547296

URL: http://svn.apache.org/viewvc?view=rev&rev=547296
Log:
DERBY-2818: Rewrite ClobUpdateableReader constructors. Preparation for DERBY-2806.
Patch file: derby-2818-1a.diff

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

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java?view=diff&rev=547296&r1=547295&r2=547296
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java Thu
Jun 14 09:30:41 2007
@@ -48,7 +48,7 @@
     /** Character position of this reader. */
     private long pos;
     /** Underlying stream of byte data. */
-    private InputStream stream;
+    private InputStream stream = null;
     /** Connection object used to obtain synchronization-object. */
     private ConnectionChild conChild;
     /** flag to indicate if its associated with materialized clob */
@@ -73,10 +73,11 @@
         materialized = true;
         this.conChild = conChild;
         this.stream = stream;
-        init (stream, 0);
         //The subset of the Clob has not been requested. 
         //Hence set maxPos to -1.
         this.maxPos = -1;
+
+        init (stream, 0);
     }
 
     /**
@@ -86,18 +87,30 @@
      * @throws SQLException
      */
     ClobUpdateableReader (EmbedClob clob) throws IOException, SQLException {
-        materialized = clob.isWritable();        
         this.clob = clob;
         this.conChild = clob;
-        //getting bytelength make some time leave exisitng streams
-        //unusable
-        long byteLength = clob.getByteLength();
-        this.stream = clob.getInternalStream ();
-        init (0, byteLength);
-        //The subset of the Clob
-        //has not been requested.
-        //Hence set maxPos to -1.
+        // A subset of the Clob has not been requested.
+        // Hence set maxPos to -1.
         this.maxPos = -1;
+
+        InternalClob internalClob = clob.getInternalClob();
+        materialized = internalClob.isWritable();        
+        if (materialized) {
+            long byteLength = internalClob.getByteLength();
+            this.stream = internalClob.getRawByteStream();
+            init ((LOBInputStream)stream, 0);
+        } else {
+            if (SanityManager.DEBUG) {
+                SanityManager.ASSERT(internalClob instanceof StoreStreamClob,
+                        "Wrong type of internal clob representation: " +
+                        internalClob.toString());
+            }
+            // Since this representation is read-only, the stream never has to
+            // update itself, until the Clob representation itself has been
+            // changed. That even will be detected by {@link #updateIfRequired}.
+            this.streamReader = internalClob.getReader(1L);
+            this.pos = 0L;
+        }
     }
     
     /**
@@ -115,22 +128,29 @@
      */
     ClobUpdateableReader (EmbedClob clob, long pos, long len) 
     throws IOException, SQLException {
-        materialized = clob.isWritable();        
         this.clob = clob;
         this.conChild = clob;
-        //Get the Byte length from the Clob which can be 
-        //passes to the init method.
-        long byteLength = clob.getByteLength();
-        //Initialize the InputStream with the underlying 
-        //InputStream of the Clob.
-        this.stream = clob.getInternalStream ();
-        //position the stream on pos using the init method.
-        init (pos, byteLength);
-        //The length requested cannot exceed the length
-        //of the underlying Clob object. Hence chose the
-        //minimum of the length of the underlying Clob
-        //object and requested length.
-        maxPos = Math.min(clob.length(), pos + len);
+        this.maxPos = pos + len;
+
+        InternalClob internalClob = clob.getInternalClob();
+        materialized = internalClob.isWritable();        
+        if (materialized) {
+            long byteLength = internalClob.getByteLength();
+            this.stream = internalClob.getRawByteStream();
+            // Position the stream on pos using the init method.
+            init ((LOBInputStream)stream, pos);
+        } else {
+            if (SanityManager.DEBUG) {
+                SanityManager.ASSERT(internalClob instanceof StoreStreamClob,
+                        "Wrong type of internal clob representation: " +
+                        internalClob.toString());
+            }
+            // Since this representation is read-only, the stream never has to
+            // update itself, until the Clob representation itself has been
+            // changed. That even will be detected by {@link #updateIfRequired}.
+            this.streamReader = internalClob.getReader(1L);
+            this.pos = 0L;
+        }
     }
         
     /**
@@ -204,18 +224,6 @@
         pos = skip;
     }    
 
-    private void init (long skip, long streamLength) throws IOException {
-        streamReader = new UTF8Reader (stream, 0, streamLength,
-                                        conChild, 
-                                conChild.getConnectionSynchronization());
-        long remainToSkip = skip;
-        while (remainToSkip > 0) {
-            long skipBy = streamReader.skip(remainToSkip);
-            remainToSkip -= skipBy;
-        }
-        pos = skip;
-    }
-
     /**
      * Updates the stream if underlying clob is modified since
      * this reader was created. 
@@ -241,9 +249,9 @@
                 SanityManager.ASSERT (!(clob == null), 
                         "Internal error while updating stream");
             }
-            if (clob.isWritable ()) {
+            if (clob.getInternalClob().isWritable()) {
                 try {
-                    stream = clob.getInternalStream();
+                    stream = clob.getInternalClob().getRawByteStream();
                 }
                 catch (SQLException e) {
                     IOException ioe = new IOException (e.getMessage());

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java?view=diff&rev=547296&r1=547295&r2=547296
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java Thu Jun 14 09:30:41
2007
@@ -771,30 +771,17 @@
     }
 
     /**
-     * Returns if the internal clob is a writable clob.
-     * @return true if internal clob is writable
+     * Returns the current internal Clob representation.
+     * <p>
+     * Care should be taken, as the representation can change when the user
+     * performs operations on the Clob. An example is if the Clob content is
+     * served from a store stream and the user updates the content. The
+     * internal representation will then be changed to a temporary Clob copy
+     * that allows updates.
+     *
+     * @return The current internal Clob representation.
      */
-    boolean isWritable() {
-        return clob.isWritable();
-    }
-
-    /**
-     * Returns the internal InputStream associated with this clob.
-     * @return internal InputStream
-     * @throws IOException
-     */
-    InputStream getInternalStream () 
-                    throws IOException, SQLException  {
-        return clob.getRawByteStream();
-    }
-
-    /**
-     * Returns byte length of the clob
-     * @return byte length of the clob
-     * @throws IOException
-     * @throws SQLException
-     */
-    long getByteLength() throws IOException, SQLException {
-        return clob.getByteLength();
+    InternalClob getInternalClob() {
+        return this.clob;
     }
 }



Mime
View raw message