diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java index c48841513c4..9ceb4c608e7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/type/TransportSearchHelper.java @@ -28,6 +28,7 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.Base64; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.Unicode; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.collect.Maps; @@ -39,7 +40,6 @@ import org.elasticsearch.search.internal.InternalSearchRequest; import java.io.IOException; import java.util.Collection; import java.util.Map; -import java.util.regex.Pattern; /** * @author kimchy (Shay Banon) @@ -47,12 +47,6 @@ import java.util.regex.Pattern; public abstract class TransportSearchHelper { - private final static Pattern scrollIdPattern; - - static { - scrollIdPattern = Pattern.compile(";"); - } - /** * Builds the shard failures, and releases the cache (meaning this should only be called once!). */ @@ -119,7 +113,7 @@ public abstract class TransportSearchHelper { } catch (IOException e) { throw new ElasticSearchIllegalArgumentException("Failed to decode scrollId", e); } - String[] elements = scrollIdPattern.split(scrollId); + String[] elements = Strings.splitStringToArray(scrollId, ';'); int index = 0; String type = elements[index++]; int contextSize = Integer.parseInt(elements[index++]); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 1eb8f71e119..b03a9f8b7ac 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -46,7 +46,6 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; import static org.elasticsearch.common.collect.Lists.*; import static org.elasticsearch.common.collect.MapBuilder.*; @@ -64,8 +63,6 @@ public class MetaData implements Iterable { private final long version; - public final static Pattern routingPattern = Pattern.compile(","); - private final ImmutableMap indices; private final ImmutableMap templates; @@ -137,7 +134,7 @@ public class MetaData implements Iterable { tmpAliasToIndexToSearchRoutingMap.put(aliasMd.alias(), indexToSearchRoutingMap); } if (aliasMd.searchRouting() != null) { - indexToSearchRoutingMap.put(indexMetaData.index(), ImmutableSet.copyOf(routingPattern.split(aliasMd.searchRouting()))); + indexToSearchRoutingMap.put(indexMetaData.index(), ImmutableSet.copyOf(Strings.splitStringByCommaToSet(aliasMd.searchRouting()))); } else { indexToSearchRoutingMap.put(indexMetaData.index(), ImmutableSet.of()); } @@ -272,7 +269,7 @@ public class MetaData implements Iterable { */ private Map> resolveSearchRoutingAllIndices(String routing) { if (routing != null) { - THashSet r = new THashSet(Arrays.asList(routingPattern.split(routing))); + Set r = Strings.splitStringByCommaToSet(routing); Map> routings = newHashMap(); String[] concreteIndices = concreteAllIndices(); for (String index : concreteIndices) { @@ -285,9 +282,9 @@ public class MetaData implements Iterable { public Map> resolveSearchRouting(@Nullable String routing, String aliasOrIndex) { Map> routings = null; - List paramRouting = null; + Set paramRouting = null; if (routing != null) { - paramRouting = Arrays.asList(routingPattern.split(routing)); + paramRouting = Strings.splitStringByCommaToSet(routing); } ImmutableMap> indexToRoutingMap = aliasToIndexToSearchRoutingMap.get(aliasOrIndex); @@ -320,11 +317,7 @@ public class MetaData implements Iterable { } else { // It's an index if (paramRouting != null) { - Set r = new THashSet(paramRouting); - if (routings == null) { - routings = newHashMap(); - } - routings.put(aliasOrIndex, r); + routings = ImmutableMap.of(aliasOrIndex, paramRouting); } } return routings; @@ -336,14 +329,6 @@ public class MetaData implements Iterable { return resolveSearchRoutingAllIndices(routing); } - Map> routings = null; - List paramRouting = null; - // List of indices that don't require any routing - Set norouting = newHashSet(); - if (routing != null) { - paramRouting = Arrays.asList(routingPattern.split(routing)); - } - if (aliasesOrIndices.length == 1) { if (aliasesOrIndices[0].equals("_all")) { return resolveSearchRoutingAllIndices(routing); @@ -352,6 +337,14 @@ public class MetaData implements Iterable { } } + Map> routings = null; + Set paramRouting = null; + // List of indices that don't require any routing + Set norouting = newHashSet(); + if (routing != null) { + paramRouting = Strings.splitStringByCommaToSet(routing); + } + for (String aliasOrIndex : aliasesOrIndices) { ImmutableMap> indexToRoutingMap = aliasToIndexToSearchRoutingMap.get(aliasOrIndex); if (indexToRoutingMap != null && !indexToRoutingMap.isEmpty()) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/PlainShardsIterator.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/PlainShardsIterator.java index fbeb2be2d76..8f1bd74ceac 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/PlainShardsIterator.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/PlainShardsIterator.java @@ -106,7 +106,7 @@ public class PlainShardsIterator implements ShardsIterator { return shardRouting; } - @Override public ShardRouting nextActiveOrNull() throws NoSuchElementException { + @Override public ShardRouting nextActiveOrNull() { int counter = this.counter; int index = this.index; while (counter++ < size()) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/operation/plain/PlainOperationRouting.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/operation/plain/PlainOperationRouting.java index cbf9971f3f7..2299c1ea23e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/operation/plain/PlainOperationRouting.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/operation/plain/PlainOperationRouting.java @@ -42,15 +42,12 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; /** * @author kimchy (shay.banon) */ public class PlainOperationRouting extends AbstractComponent implements OperationRouting { - public final static Pattern routingPattern = Pattern.compile(","); - private final HashFunction hashFunction; private final boolean useType; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/Strings.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/Strings.java index 22c91e208f7..0e6e309832f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/Strings.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/Strings.java @@ -21,6 +21,7 @@ package org.elasticsearch.common; import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.Iterables; +import org.elasticsearch.common.trove.set.hash.THashSet; import java.util.*; @@ -968,6 +969,78 @@ public class Strings { return toStringArray(set); } + public static Set splitStringByCommaToSet(final String s) { + return splitStringToSet(s, ','); + } + + public static String[] splitStringByCommaToArray(final String s) { + return splitStringToArray(s, ','); + } + + public static Set splitStringToSet(final String s, final char c) { + final char[] chars = s.toCharArray(); + int count = 1; + for (final char x : chars) { + if (x == c) { + count++; + } + } + final THashSet result = new THashSet(count); + final int len = chars.length; + int start = 0; // starting index in chars of the current substring. + int pos = 0; // current index in chars. + int i = 0; // number of the current substring. + for (; pos < len; pos++) { + if (chars[pos] == c) { + int size = pos - start; + if (size > 0) { // only add non empty strings + result.add(new String(chars, start, size)); + } + start = pos + 1; + } + } + int size = pos - start; + if (size > 0) { + result.add(new String(chars, start, size)); + } + return result; + } + + public static String[] splitStringToArray(final String s, final char c) { + final char[] chars = s.toCharArray(); + int count = 1; + for (final char x : chars) { + if (x == c) { + count++; + } + } + final String[] result = new String[count]; + final int len = chars.length; + int start = 0; // starting index in chars of the current substring. + int pos = 0; // current index in chars. + int i = 0; // number of the current substring. + for (; pos < len; pos++) { + if (chars[pos] == c) { + int size = pos - start; + if (size > 0) { + result[i++] = new String(chars, start, size); + } + start = pos + 1; + } + } + int size = pos - start; + if (size > 0) { + result[i++] = new String(chars, start, size); + } + if (i != count) { + // we have empty strings, copy over to a new array + String[] result1 = new String[i]; + System.arraycopy(result, 0, result1, 0, i); + return result1; + } + return result; + } + /** * Split a String at the first occurrence of the delimiter. * Does not include the delimiter in the result. diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/path/PathTrie.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/path/PathTrie.java index 0ff9097ff40..0a8e2a04e5e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/path/PathTrie.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/path/PathTrie.java @@ -19,10 +19,10 @@ package org.elasticsearch.common.path; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.ImmutableMap; import java.util.Map; -import java.util.regex.Pattern; import static org.elasticsearch.common.collect.MapBuilder.*; @@ -43,21 +43,21 @@ public class PathTrie { private final Decoder decoder; private final TrieNode root; - private final Pattern pattern; + private final char separator; private T rootValue; public PathTrie() { - this("/", "*", NO_DECODER); + this('/', "*", NO_DECODER); } public PathTrie(Decoder decoder) { - this("/", "*", decoder); + this('/', "*", decoder); } - public PathTrie(String separator, String wildcard, Decoder decoder) { + public PathTrie(char separator, String wildcard, Decoder decoder) { this.decoder = decoder; - pattern = Pattern.compile(separator); - root = new TrieNode(separator, null, null, wildcard); + this.separator = separator; + root = new TrieNode(new String(new char[]{separator}), null, null, wildcard); } public class TrieNode { @@ -193,7 +193,7 @@ public class PathTrie { } public void insert(String path, T value) { - String[] strings = pattern.split(path); + String[] strings = Strings.splitStringToArray(path, separator); if (strings.length == 0) { rootValue = value; return; @@ -214,7 +214,7 @@ public class PathTrie { if (path.length() == 0) { return rootValue; } - String[] strings = pattern.split(path); + String[] strings = Strings.splitStringToArray(path, separator); if (strings.length == 0) { return rootValue; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java index 575c93106e6..b4808d126d3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java @@ -23,6 +23,7 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.Client; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -34,7 +35,6 @@ import org.elasticsearch.rest.XContentRestResponse; import org.elasticsearch.rest.XContentThrowableRestResponse; import java.io.IOException; -import java.util.regex.Pattern; import static org.elasticsearch.rest.RestRequest.Method.*; import static org.elasticsearch.rest.RestStatus.*; @@ -45,12 +45,6 @@ import static org.elasticsearch.rest.action.support.RestXContentBuilder.*; */ public class RestGetAction extends BaseRestHandler { - private final static Pattern fieldsPattern; - - static { - fieldsPattern = Pattern.compile(","); - } - @Inject public RestGetAction(Settings settings, Client client, RestController controller) { super(settings, client); controller.registerHandler(GET, "/{index}/{type}/{id}", this); @@ -69,7 +63,7 @@ public class RestGetAction extends BaseRestHandler { String sField = request.param("fields"); if (sField != null) { - String[] sFields = fieldsPattern.split(sField); + String[] sFields = Strings.splitStringByCommaToArray(sField); if (sFields != null) { getRequest.fields(sFields); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java index 3d83509e845..9a880b67a9f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java @@ -43,7 +43,6 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; import java.io.IOException; -import java.util.regex.Pattern; import static org.elasticsearch.common.unit.TimeValue.*; import static org.elasticsearch.rest.RestRequest.Method.*; @@ -55,15 +54,6 @@ import static org.elasticsearch.rest.action.support.RestXContentBuilder.*; */ public class RestSearchAction extends BaseRestHandler { - private final static Pattern fieldsPattern; - - private final static Pattern indicesBoostPattern; - - static { - fieldsPattern = Pattern.compile(","); - indicesBoostPattern = Pattern.compile(","); - } - @Inject public RestSearchAction(Settings settings, Client client, RestController controller) { super(settings, client); controller.registerHandler(GET, "/_search", this); @@ -192,7 +182,7 @@ public class RestSearchAction extends BaseRestHandler { if (!Strings.hasText(sField)) { searchSourceBuilder.noFields(); } else { - String[] sFields = fieldsPattern.split(sField); + String[] sFields = Strings.splitStringByCommaToArray(sField); if (sFields != null) { for (String field : sFields) { searchSourceBuilder.field(field); @@ -203,7 +193,7 @@ public class RestSearchAction extends BaseRestHandler { String sSorts = request.param("sort"); if (sSorts != null) { - String[] sorts = fieldsPattern.split(sSorts); + String[] sorts = Strings.splitStringByCommaToArray(sSorts); for (String sort : sorts) { int delimiter = sort.lastIndexOf(":"); if (delimiter != -1) { @@ -222,7 +212,7 @@ public class RestSearchAction extends BaseRestHandler { String sIndicesBoost = request.param("indices_boost"); if (sIndicesBoost != null) { - String[] indicesBoost = indicesBoostPattern.split(sIndicesBoost); + String[] indicesBoost = Strings.splitStringByCommaToArray(sIndicesBoost); for (String indexBoost : indicesBoost) { int divisor = indexBoost.indexOf(','); if (divisor == -1) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/support/RestActions.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/support/RestActions.java index 59cacc0dd2c..cddac5d09a0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/support/RestActions.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/support/RestActions.java @@ -29,18 +29,12 @@ import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.rest.RestRequest; import java.io.IOException; -import java.util.regex.Pattern; /** * @author kimchy (Shay Banon) */ public class RestActions { - public final static Pattern indicesPattern = Pattern.compile(","); - public final static Pattern typesPattern = Pattern.compile(","); - public final static Pattern nodesIdsPattern = Pattern.compile(","); - public final static Pattern genericPattern = Pattern.compile(","); - public static long parseVersion(RestRequest request) { if (request.hasParam("version")) { return request.paramAsLong("version", 0); @@ -100,20 +94,20 @@ public class RestActions { if (indices == null) { return Strings.EMPTY_ARRAY; } - return indicesPattern.split(indices); + return Strings.splitStringByCommaToArray(indices); } public static String[] splitTypes(String typeNames) { if (typeNames == null) { return Strings.EMPTY_ARRAY; } - return typesPattern.split(typeNames); + return Strings.splitStringByCommaToArray(typeNames); } public static String[] splitNodes(String nodes) { if (nodes == null) { return Strings.EMPTY_ARRAY; } - return nodesIdsPattern.split(nodes); + return Strings.splitStringByCommaToArray(nodes); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/support/AbstractRestRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/support/AbstractRestRequest.java index be124faa5d8..0e3aa343662 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/support/AbstractRestRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/support/AbstractRestRequest.java @@ -21,12 +21,11 @@ package org.elasticsearch.rest.support; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.Booleans; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.rest.RestRequest; -import java.util.regex.Pattern; - import static org.elasticsearch.common.unit.ByteSizeValue.*; import static org.elasticsearch.common.unit.TimeValue.*; @@ -35,8 +34,6 @@ import static org.elasticsearch.common.unit.TimeValue.*; */ public abstract class AbstractRestRequest implements RestRequest { - private static final Pattern commaPattern = Pattern.compile(","); - @Override public final String path() { return RestUtils.decodeComponent(rawPath()); } @@ -102,6 +99,6 @@ public abstract class AbstractRestRequest implements RestRequest { if (value == null) { return defaultValue; } - return commaPattern.split(value); + return Strings.splitStringByCommaToArray(value); } }