db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r544119 - in /db/derby/code/trunk/java/client/org/apache/derby/client/am: CallableLocatorProcedures.java Clob.java ClobLocatorInputStream.java ClobLocatorReader.java ClobLocatorWriter.java
Date Mon, 04 Jun 2007 10:55:30 GMT
Author: kahatlen
Date: Mon Jun  4 03:55:29 2007
New Revision: 544119

URL: http://svn.apache.org/viewvc?view=rev&rev=544119
Log:
DERBY-2604: Implement Clob support for locators

Fixed some bugs found when running JUnit tests with locators
enabled. Simplified handling of length in the reader and the input
stream. Wrapped streams/readers in buffered streams/readers.

Contributed by V. Narayanan.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableLocatorProcedures.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableLocatorProcedures.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableLocatorProcedures.java?view=diff&rev=544119&r1=544118&r2=544119
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableLocatorProcedures.java
(original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableLocatorProcedures.java
Mon Jun  4 03:55:29 2007
@@ -783,7 +783,7 @@
         }
 
         clobGetPositionFromStringCall.setIntX(2, locator);
-        clobGetPositionFromStringCall.setStringX(3, searchLiteral);
+        clobGetPositionFromStringCall.setStringX(3, stringToBeCompared);
         clobGetPositionFromStringCall.setLongX(4, fromPosition);
         try {
             clobGetPositionFromStringCall.executeX();

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java?view=diff&rev=544119&r1=544118&r2=544119
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java Mon Jun  4 03:55:29
2007
@@ -21,6 +21,8 @@
 
 package org.apache.derby.client.am;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.Reader;
@@ -368,7 +370,8 @@
         if (isLocator()) {
             //The Lob is locator enabled. Return an instance of the Locator
             //enabled Writer implementation
-            return new ClobLocatorReader(agent_.connection_, this);
+            return new BufferedReader
+                    (new ClobLocatorReader(agent_.connection_, this));
         }
         else if (isCharacterStream())  // this Lob is used for input
         {
@@ -415,7 +418,8 @@
             //The Lob is locator enabled. Return an instance
             //of the Locator enabled Clob specific InputStream
             //implementation.
-            return new ClobLocatorInputStream(agent_.connection_,this);
+            return new BufferedInputStream(
+                    new ClobLocatorInputStream(agent_.connection_,this));
         }
         else {
             return new AsciiStream(string_, new java.io.StringReader(string_));
@@ -541,13 +545,29 @@
                 return -1;
             }
 
-            index = string_.indexOf(searchstr.getSubString(1L, 
-                                                      (int) searchstr.length()),
-                                    (int) start - 1);
+            //Check if locator support is available for this LOB.
+            if (isLocator()) {
+                //Locator support is available. Hence call
+                //CLOBGETPOSITIONFROMLOCATOR to determine the position
+                //of the given Clob inside the LOB.
+                index = (int)agent_.connection_.locatorProcedureCall()
+                    .clobGetPositionFromLocator(locator_,
+                        ((Clob)searchstr).getLocator(),
+                        start);
+            } else {
+                //Locator support is not available.
+                index = string_.indexOf(searchstr.getSubString(1L,
+                                                    (int) searchstr.length()),
+                                        (int) start - 1);
+            }
         } catch (java.sql.SQLException e) {
             throw new SqlException(e);
         }
-        if (index != -1) {
+        //When the LOB is locator enabled then
+        //the stored procedure call returns the
+        //correct position. There is no need
+        //to increment by 1
+        if (index != -1 && !isLocator()) {
             index++; // api index starts at 1
         }
         return (long) index;
@@ -874,10 +894,21 @@
             Reader retVal = null;
             //check if the Lob is locator enabled.
             if(isLocator()) {
-                //The Lob is locator enabled. Return the locator enabled
-                //Implementation of a Clob Reader.
-                retVal = new ClobLocatorReader(agent_.connection_, this, 
-                        pos, length);
+                //1) The Lob is locator enabled. Return the locator
+                //   enabled Implementation of a Clob Reader.
+                //2) len is the number of characters in the
+                //   stream starting from pos.
+                //3) checkPosAndLength will ensure that pos and
+                //   length fall within the boundaries of the
+                //   Clob object.
+                try {
+                    retVal = new BufferedReader(
+                            new ClobLocatorReader(agent_.connection_, this,
+                            pos, length));
+                }
+                catch(SqlException sqle) {
+                    throw sqle.getSQLException();
+                }
             }
             else {
                 //The Lob is not locator enabled.

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java?view=diff&rev=544119&r1=544118&r2=544119
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
(original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorInputStream.java
Mon Jun  4 03:55:29 2007
@@ -60,9 +60,14 @@
      * Current position in the underlying Clob.
      * Clobs are indexed from 1
      */
-    private long currentPos = 1;
+    private long currentPos;
     
     /**
+     * Position in Clob where to stop reading.
+     */
+    private long maxPos;
+
+    /**
      * Create an <code>InputStream</code> for reading the
      * <code>Clob</code> value represented by the given locator based
      * <code>Clob</code> object.
@@ -71,13 +76,16 @@
      * @param clob <code>Clob</code> object that contains locator for
      *        the <code>Clob</code> value on the server.
      */
-    public ClobLocatorInputStream(Connection connection, Clob clob) {
+    public ClobLocatorInputStream(Connection connection, Clob clob)
+    throws SqlException{
         if (SanityManager.DEBUG) {
             SanityManager.ASSERT(clob.isLocator());
         }
         
         this.connection = connection;
         this.clob = clob;
+        this.currentPos = 1;
+        this.maxPos = clob.sqlLength();
     }
     
     /**
@@ -125,7 +133,7 @@
     private byte[] readBytes(int len) throws IOException {
         try {
             int actualLength
-                    = (int )Math.min(len, clob.sqlLength() - currentPos + 1);
+                    = (int )Math.min(len, maxPos - currentPos + 1);
             String resultStr = connection.locatorProcedureCall().
                     clobGetSubString(clob.getLocator(),
                     currentPos, actualLength);

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java?view=diff&rev=544119&r1=544118&r2=544119
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorReader.java Mon
Jun  4 03:55:29 2007
@@ -51,12 +51,12 @@
      * Current position in the underlying Clob.
      * Clobs are indexed from 1
      */
-    private long currentPos = 1;
+    private long currentPos;
     
     /**
-     * The length in characters of the partial value to be retrieved.
+     * Position in Clob where to stop reading.
      */
-    private long length = -1;
+    private long maxPos;
     
     /**
      * Stores the information to whether this Reader has been
@@ -74,13 +74,16 @@
      * @param clob <code>Clob</code> object that contains locator for
      *        the <code>Clob</code> value on the server.
      */
-    public ClobLocatorReader(Connection connection, Clob clob) {
+    public ClobLocatorReader(Connection connection, Clob clob)
+    throws SqlException {
         if (SanityManager.DEBUG) {
             SanityManager.ASSERT(clob.isLocator());
         }
         
         this.connection = connection;
         this.clob = clob;
+        this.currentPos = 1;
+        this.maxPos = clob.sqlLength();
     }
     
     /**
@@ -96,10 +99,10 @@
      * @param len The length in characters of the partial value to be retrieved.
      */
     public ClobLocatorReader(Connection connection, Clob clob, 
-            long pos, long len) {
+            long pos, long len) throws SqlException {
         this(connection, clob);
-        currentPos = pos;
-        length = len;
+        this.currentPos = pos;
+        this.maxPos = Math.min(clob.sqlLength(), pos + len - 1);
     }
     
     /**
@@ -180,7 +183,7 @@
     private char[] readCharacters(int len) throws IOException {
         try {
             int actualLength
-                    = (int )Math.min(len, getStreamLength() - currentPos + 1);
+                    = (int )Math.min(len, maxPos - currentPos + 1);
             String resultStr = connection.locatorProcedureCall().
                     clobGetSubString(clob.getLocator(),
                     currentPos, actualLength);
@@ -191,26 +194,6 @@
             IOException ioEx = new IOException();
             ioEx.initCause(ex);
             throw ioEx;
-        }
-    }
-    
-    /**
-     * Return the length of the stream.
-     *
-     * @return the length of the stream.
-     */
-    private long getStreamLength() throws SqlException {
-        //check to see if the length of the stream has been set
-        //during initialization
-        if(length != -1) {
-            //The length has been set. Hence return this as the
-            //length
-            return length;
-        }
-        else {
-            //The length has not been set. Obtain the length from
-            //the Clob.
-            return clob.sqlLength();
         }
     }
 }

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java?view=diff&rev=544119&r1=544118&r2=544119
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobLocatorWriter.java Mon
Jun  4 03:55:29 2007
@@ -163,9 +163,9 @@
     throws IOException {
         try {
             connection.locatorProcedureCall().clobSetString
-                    (clob.locator_, currentPos, c.length, 
+                    (clob.locator_, currentPos, len,
                     new String(c, off, len));
-            currentPos += c.length;
+            currentPos += len;
             if (currentPos-1 > clob.sqlLength()) {
                 // Wrote past the old end of the Clob value, update length
                 clob.setSqlLength(currentPos - 1);



Mime
View raw message