Fix IllegalArgumentException if there is no text between two delimiters

patch by Mick Semb Wever; reviewed by Jason Brown for CASSANDRA-14450


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/7068ef62
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/7068ef62
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/7068ef62

Branch: refs/heads/trunk
Commit: 7068ef62548c1ff8d17be7d0a1e71a5f098010e6
Parents: 0d4aacc
Author: Mick Semb Wever <m...@apache.org>
Authored: Thu May 17 21:30:02 2018 +1000
Committer: Mick Semb Wever <m...@apache.org>
Committed: Fri May 18 12:29:39 2018 +1000

----------------------------------------------------------------------
 .../index/sasi/analyzer/DelimiterAnalyzer.java  | 11 ++++++---
 .../sasi/analyzer/DelimiterAnalyzerTest.java    | 25 +++++++++++++++++++-
 2 files changed, 32 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/7068ef62/src/java/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzer.java
----------------------------------------------------------------------
diff --git 
a/src/java/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzer.java 
b/src/java/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzer.java
index 794a8b9..24acef4 100644
--- a/src/java/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzer.java
+++ b/src/java/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzer.java
@@ -85,12 +85,17 @@ public class DelimiterAnalyzer extends AbstractAnalyzer
 
                 CharBuffer readahead = cb.duplicate();
                 // loop until we see the next delimiter character, or reach 
end of data
-                while (readahead.hasRemaining() && readahead.get() != 
delimiter);
+                boolean readaheadRemaining;
+                while ((readaheadRemaining = readahead.hasRemaining()) && 
readahead.get() != delimiter);
 
-                char[] chars = new char[readahead.position() - cb.position() - 
(readahead.hasRemaining() ? 1 : 0)];
+                char[] chars = new char[readahead.position() - cb.position() - 
(readaheadRemaining ? 1 : 0)];
                 cb.get(chars);
                 Preconditions.checkState(!cb.hasRemaining() || cb.get() == 
delimiter);
-                return charset.encode(CharBuffer.wrap(chars));
+
+                return 0 < chars.length
+                        ? charset.encode(CharBuffer.wrap(chars))
+                        // blank partition keys not permitted, ref 
ConcurrentRadixTree.putIfAbsent(..)
+                        : computeNext();
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/7068ef62/test/unit/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzerTest.java
----------------------------------------------------------------------
diff --git 
a/test/unit/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzerTest.java 
b/test/unit/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzerTest.java
index 15bbb84..16f9f06 100644
--- 
a/test/unit/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzerTest.java
+++ 
b/test/unit/org/apache/cassandra/index/sasi/analyzer/DelimiterAnalyzerTest.java
@@ -54,7 +54,30 @@ public class DelimiterAnalyzerTest
         while (analyzer.hasNext())
             output.append(ByteBufferUtil.string(analyzer.next()) + 
(analyzer.hasNext() ? ' ' : ""));
 
-        Assert.assertTrue(testString.equals(output.toString()));
+        Assert.assertEquals(testString, output.toString());
+        Assert.assertFalse(testString.toLowerCase().equals(output.toString()));
+    }
+
+    @Test
+    public void testBlankEntries() throws Exception
+    {
+        DelimiterAnalyzer analyzer = new DelimiterAnalyzer();
+
+        analyzer.init(
+            new HashMap()
+                {{
+                    put(DelimiterTokenizingOptions.DELIMITER, ",");
+                }},
+            UTF8Type.instance);
+
+        String testString = ",Nip,,,,it,,,in,,the,bud,,,";
+        ByteBuffer toAnalyze = ByteBuffer.wrap(testString.getBytes());
+        analyzer.reset(toAnalyze);
+        StringBuilder output = new StringBuilder();
+        while (analyzer.hasNext())
+            output.append(ByteBufferUtil.string(analyzer.next()) + 
(analyzer.hasNext() ? ',' : ""));
+
+        Assert.assertEquals("Nip,it,in,the,bud", output.toString());
         Assert.assertFalse(testString.toLowerCase().equals(output.toString()));
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to