Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed open/close index api when using wildcard only #4606

Merged
merged 1 commit into from
Jan 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions rest-api-spec/test/indices.open/20_multiple_indices.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
setup:
- do:
indices.create:
index: test_index1
- do:
indices.create:
index: test_index2
- do:
indices.create:
index: test_index3
- do:
cluster.health:
wait_for_status: yellow

---
"All indices":
- do:
indices.close:
index: _all

- do:
catch: forbidden
search:
index: test_index2

- do:
indices.open:
index: _all

- do:
cluster.health:
wait_for_status: yellow

- do:
search:
index: test_index2

---
"Trailing wildcard":
- do:
indices.close:
index: test_*

- do:
catch: forbidden
search:
index: test_index2

- do:
indices.open:
index: test_*

- do:
cluster.health:
wait_for_status: yellow

- do:
search:
index: test_index2

---
"Only wildcard":
- do:
indices.close:
index: '*'

- do:
catch: forbidden
search:
index: test_index3

- do:
indices.open:
index: '*'

- do:
cluster.health:
wait_for_status: yellow

- do:
search:
index: test_index3
25 changes: 11 additions & 14 deletions src/main/java/org/elasticsearch/common/path/PathTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,42 +157,39 @@ public T retrieve(String[] path, int index, Map<String, String> params) {

String token = path[index];
TrieNode<T> node = children.get(token);
boolean usedWildcard = false;
boolean usedWildcard;
if (node == null) {
node = children.get(wildcard);
if (node == null) {
return null;
} else {
usedWildcard = true;
if (params != null && node.isNamedWildcard()) {
put(params, node.namedWildcard(), token);
}
}
usedWildcard = true;
} else {
usedWildcard = token.equals(wildcard);
}

put(params, node, token);

if (index == (path.length - 1)) {
if (params != null && node.isNamedWildcard()) {
put(params, node.namedWildcard(), token);
}
return node.value;
}

T res = node.retrieve(path, index + 1, params);
if (res == null && !usedWildcard) {
node = children.get(wildcard);
if (node != null) {
if (params != null && node.isNamedWildcard()) {
put(params, node.namedWildcard(), token);
}
put(params, node, token);
res = node.retrieve(path, index + 1, params);
}
}

return res;
}

private void put(Map<String, String> params, String key, String value) {
params.put(key, decoder.decode(value));
private void put(Map<String, String> params, TrieNode<T> node, String value) {
if (params != null && node.isNamedWildcard()) {
params.put(node.namedWildcard(), decoder.decode(value));
}
}
}

Expand Down
24 changes: 22 additions & 2 deletions src/test/java/org/elasticsearch/common/path/PathTrieTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

Expand Down Expand Up @@ -113,11 +112,32 @@ public void testPreferNonWildcardExecution() {
}

@Test
public void testEndWithNamedWildcardAndLookupWithWildcard() {
public void testNamedWildcardAndLookupWithWildcard() {
PathTrie<String> trie = new PathTrie<String>();
trie.insert("x/{test}", "test1");
trie.insert("{test}/a", "test2");
trie.insert("/{test}", "test3");
trie.insert("/{test}/_endpoint", "test4");
trie.insert("/*/{test}/_endpoint", "test5");

Map<String, String> params = newHashMap();
assertThat(trie.retrieve("/x/*", params), equalTo("test1"));
assertThat(params.get("test"), equalTo("*"));

params = newHashMap();
assertThat(trie.retrieve("/b/a", params), equalTo("test2"));
assertThat(params.get("test"), equalTo("b"));

params = newHashMap();
assertThat(trie.retrieve("/*", params), equalTo("test3"));
assertThat(params.get("test"), equalTo("*"));

params = newHashMap();
assertThat(trie.retrieve("/*/_endpoint", params), equalTo("test4"));
assertThat(params.get("test"), equalTo("*"));

params = newHashMap();
assertThat(trie.retrieve("a/*/_endpoint", params), equalTo("test5"));
assertThat(params.get("test"), equalTo("*"));
}
}