Index name expressions should not be broken up

Closes #13665
This commit is contained in:
Martijn van Groningen 2015-09-21 20:12:28 +02:00
parent 1f5bd495ed
commit 93ad696966
4 changed files with 79 additions and 10 deletions

View File

@ -556,7 +556,6 @@ public class Strings {
count++;
}
}
// TODO (MvG): No push: hppc or jcf?
final Set<String> result = new HashSet<>(count);
final int len = chars.length;
int start = 0; // starting index in chars of the current substring.

View File

@ -22,6 +22,8 @@ package org.elasticsearch.common.path;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.common.Strings;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder;
@ -195,7 +197,7 @@ public class PathTrie<T> {
private void put(Map<String, String> params, TrieNode<T> node, String value) {
if (params != null && node.isNamedWildcard()) {
params.put(node.namedWildcard(), decoder.decode(value));
params.put(node.namedWildcard(), value);
}
}
}
@ -222,7 +224,7 @@ public class PathTrie<T> {
if (path.length() == 0) {
return rootValue;
}
String[] strings = Strings.splitStringToArray(path, separator);
String[] strings = splitPath(decoder.decode(path));
if (strings.length == 0) {
return rootValue;
}
@ -233,4 +235,50 @@ public class PathTrie<T> {
}
return root.retrieve(strings, index, params);
}
/*
Splits up the url path up by '/' and is aware of
index name expressions that appear between '<' and '>'.
*/
String[] splitPath(final String path) {
if (path == null || path.length() == 0) {
return Strings.EMPTY_ARRAY;
}
int count = 1;
boolean splitAllowed = true;
for (int i = 0; i < path.length(); i++) {
final char currentC = path.charAt(i);
if ('<' == currentC) {
splitAllowed = false;
} else if (currentC == '>') {
splitAllowed = true;
} else if (splitAllowed && currentC == separator) {
count++;
}
}
final List<String> result = new ArrayList<>(count);
final StringBuilder builder = new StringBuilder();
splitAllowed = true;
for (int i = 0; i < path.length(); i++) {
final char currentC = path.charAt(i);
if ('<' == currentC) {
splitAllowed = false;
} else if (currentC == '>') {
splitAllowed = true;
} else if (splitAllowed && currentC == separator) {
if (builder.length() > 0) {
result.add(builder.toString());
builder.setLength(0);
}
continue;
}
builder.append(currentC);
}
if (builder.length() > 0) {
result.add(builder.toString());
}
return result.toArray(new String[result.size()]);
}
}

View File

@ -25,6 +25,7 @@ import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@ -33,7 +34,6 @@ import static org.hamcrest.Matchers.nullValue;
*/
public class PathTrieTests extends ESTestCase {
@Test
public void testPath() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("/a/b/c", "walla");
@ -61,14 +61,12 @@ public class PathTrieTests extends ESTestCase {
assertThat(params.get("docId"), equalTo("12"));
}
@Test
public void testEmptyPath() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("/", "walla");
assertThat(trie.retrieve(""), equalTo("walla"));
}
@Test
public void testDifferentNamesOnDifferentPath() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("/a/{type}", "test1");
@ -83,7 +81,6 @@ public class PathTrieTests extends ESTestCase {
assertThat(params.get("name"), equalTo("testX"));
}
@Test
public void testSameNameOnDifferentPath() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("/a/c/{name}", "test1");
@ -98,7 +95,6 @@ public class PathTrieTests extends ESTestCase {
assertThat(params.get("name"), equalTo("testX"));
}
@Test
public void testPreferNonWildcardExecution() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("{test}", "test1");
@ -115,7 +111,6 @@ public class PathTrieTests extends ESTestCase {
assertThat(trie.retrieve("/v/x/c", params), equalTo("test6"));
}
@Test
public void testSamePathConcreteResolution() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("{x}/{y}/{z}", "test1");
@ -132,7 +127,6 @@ public class PathTrieTests extends ESTestCase {
assertThat(params.get("k"), equalTo("c"));
}
@Test
public void testNamedWildcardAndLookupWithWildcard() {
PathTrie<String> trie = new PathTrie<>();
trie.insert("x/{test}", "test1");
@ -161,4 +155,25 @@ public class PathTrieTests extends ESTestCase {
assertThat(trie.retrieve("a/*/_endpoint", params), equalTo("test5"));
assertThat(params.get("test"), equalTo("*"));
}
public void testSplitPath() {
PathTrie<String> trie = new PathTrie<>();
assertThat(trie.splitPath("/a/"), arrayContaining("a"));
assertThat(trie.splitPath("/a/b"),arrayContaining("a", "b"));
assertThat(trie.splitPath("/a/b/c"), arrayContaining("a", "b", "c"));
assertThat(trie.splitPath("/a/b/<c/d>"), arrayContaining("a", "b", "<c/d>"));
assertThat(trie.splitPath("/a/b/<c/d>/d"), arrayContaining("a", "b", "<c/d>", "d"));
assertThat(trie.splitPath("/<logstash-{now}>/_search"), arrayContaining("<logstash-{now}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/d}>/_search"), arrayContaining("<logstash-{now/d}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M{YYYY.MM}}>/_search"), arrayContaining("<logstash-{now/M{YYYY.MM}}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M{YYYY.MM}}>/_search"), arrayContaining("<logstash-{now/M{YYYY.MM}}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M{YYYY.MM|UTC}}>/log/_search"), arrayContaining("<logstash-{now/M{YYYY.MM|UTC}}>", "log", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M}>,<logstash-{now/M-1M}>/_search"), arrayContaining("<logstash-{now/M}>,<logstash-{now/M-1M}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M}>,<logstash-{now/M-1M}>/_search"), arrayContaining("<logstash-{now/M}>,<logstash-{now/M-1M}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M{YYYY.MM}}>,<logstash-{now/M-1M{YYYY.MM}}>/_search"), arrayContaining("<logstash-{now/M{YYYY.MM}}>,<logstash-{now/M-1M{YYYY.MM}}>", "_search"));
assertThat(trie.splitPath("/<logstash-{now/M{YYYY.MM|UTC}}>,<logstash-{now/M-1M{YYYY.MM|UTC}}>/_search"), arrayContaining("<logstash-{now/M{YYYY.MM|UTC}}>,<logstash-{now/M-1M{YYYY.MM|UTC}}>", "_search"));
}
}

View File

@ -0,0 +1,7 @@
---
"Missing index with catch":
- do:
catch: /index=logstash-\d{4}\.\d{2}\.\d{2}/
search:
index: <logstash-{now/M}>