db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpendle...@apache.org
Subject svn commit: r493262 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/jdbc/EmbedClob.java testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobClob4BlobTest.java
Date Sat, 06 Jan 2007 00:58:39 GMT
Author: bpendleton
Date: Fri Jan  5 16:58:39 2007
New Revision: 493262

URL: http://svn.apache.org/viewvc?view=rev&rev=493262
Log:
DERBY-1917: Clob.position fails with search strings longer than 256 chars

This patch was contributed by V. Narayanan (v.narayanan@sun.com)

The position algorithm proceeds in a chunked fashion, searching for 256
byte chunks of the search string at a time. The chunking algorithm contained
two flaws:

  - tmpPatternS = searchStr.substring(patternIndex, 256);
  + tmpPatternS = searchStr.substring(patternIndex , patternIndex + 256);

searchStr.substring(patternIndex , patternIndex + 256); has to actually
return 256 characters starting from patternIndex. This was resulting in
an empty string being returned when the string length exceeded 256.

  - tmpPatternS = searchStr;
  + tmpPatternS = searchStr.substring(patternIndex , patternLength);

Assume that the string length is 258 then in the first iteration it
returned 256. In the the second it was returning the whole string instead
of the remaining two characters. Doing a tmpPatternS =
searchStr.substring(patternIndex , patternLength); corrected this problem.



Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobClob4BlobTest.java

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=493262&r1=493261&r2=493262
==============================================================================
--- 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 Fri Jan  5 16:58:39
2007
@@ -371,7 +371,57 @@
   /**
    * Determines the character position at which the specified substring
    * <code>searchstr</code> appears in the <code>CLOB</code>.  The
search
-   * begins at position <code>start</code>.
+	* begins at position <code>start</code>. The method uses the following
+	* algorithm for the search
+	*
+	*
+	* 1)Is the length of the current pattern string to be matched greater than 256 ?
+	*
+	*	1.1)If "YES"
+	*		Extract the first 256 bytes as the current pattern to be matched
+	*
+	*		If "NO"
+	*		Make the pattern string itself as the current pattern to be matched
+	*
+	*	1.2)Initialize a variable that will indicate the character in the pattern
+	*		String being matched to zero. (say currPatternPos)
+	*
+	* 2)Read the 256 bytes of the Clob from the database
+	*
+	*	2.1)Initialize a variable that will indicate the current index in this array
+	*		to zero. (say currClobPos)
+	*	2.2)Exit if there are no more characters to be read in the Clob
+	*
+	* 3)Initialize a bestMatchPosition that will keep storing the next occurence of the 
+	*	first character in the pattern.This will be useful when we want to go back and 
+	*	start searching in the Clob array when a mismatch occurs.
+	*
+	* 4)Do the characters in currPatternPos and currClobPos match ?
+	*	4.1)If "YES" 
+	*
+	*		Increment currPatternPos and currClobPos. 
+	*
+	*		If currPatternPos is not 0 and the character in the 
+	*		currentClobPos is the same as the first character in the
+	*		pattern set bestMatchPosition = currentClobPos
+	*
+	*	4.2)If "No" 
+	*
+	*		set currClobPos = bestMatchPosition
+	*		set currPatternPos = 0
+	*
+	*	4.3)If currPatternPos > 256 
+	*		4.3.1)If "YES" 
+	*			  Return the current position in the Clob if all characters 
+	*			  have been matched otherwise perform step 1 to fetch the
+	*			  next 256 characters and increment matchCount
+	*		4.3.2)If "NO" repeat Step 4
+	*
+	*	4.4)If currClobPos > 256
+	*		4.4.1)If "YES"
+	*			  Repeat step 2 to fetch next 256 characters
+	*		4.4.2)If "NO"
+	*			  Repeat step 4
    * @param searchStr the substring for which to search
    * @param start the position at which to begin searching; the first position
    *              is 1
@@ -459,10 +509,15 @@
 							if (needPattern) {
 
 								String tmpPatternS;
+								//Keep extracting substrings of length 256 from the pattern string
+								//and use these substrings for comparison with the data from the Clob
+								//if the subString remaining has a length > 256 then extract 256 bytes
+								//and return it
+								//otherwise return the remaining string 
 								if ((patternLength - patternIndex) > 256)
-									tmpPatternS = searchStr.substring(patternIndex, 256);
+									tmpPatternS = searchStr.substring(patternIndex , patternIndex + 256);
 								else
-									tmpPatternS = searchStr;
+									tmpPatternS = searchStr.substring(patternIndex , patternLength);
 
 								tmpPattern = tmpPatternS.toCharArray();
 								needPattern = false;
@@ -514,6 +569,11 @@
 										}
 
 										needPattern = true;
+										//We need to increment clobOffset
+										//to start comparison from the 
+										//next character since the current
+										//character has already been compared
+										clobOffset++;
 										continue search;
 
 									}

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobClob4BlobTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobClob4BlobTest.java?view=diff&rev=493262&r1=493261&r2=493262
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobClob4BlobTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobClob4BlobTest.java
Fri Jan  5 16:58:39 2007
@@ -457,9 +457,7 @@
                 for (int i=0; i<10; i++) {
                     // find a random string to search for
                     start = Math.max(random.nextInt(clobLength - 1), 1);
-                    // length = random.nextInt(clobLength - start) + 1;
-                    // use 256 until DERBY-1917 is fixed
-                    length = random.nextInt(256) + 1;
+                    length = random.nextInt(clobLength - start) + 1;
                     println("start:" + start + " length:" + length);
                     searchString = clob.getSubString(start, length);
                     // get random position to start the search from



Mime
View raw message