From e816782da110f94be59f0d153feb8b65c29bd711 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 8 Mar 2018 17:17:36 -0700 Subject: [PATCH] Remove FastStringReader in favor of vanilla StringReader (#28944) This allows us to remove another dependency in the decoupling of the XContent code. Rather than move this class over or decouple it, it can simply be removed. Relates tangentially to #28504 --- .../forbidden/es-server-signatures.txt | 2 - .../script/mustache/MustacheScriptEngine.java | 4 +- .../analyze/TransportAnalyzeAction.java | 8 +- .../org/elasticsearch/common/Strings.java | 4 +- .../common/geo/parsers/GeoWKTParser.java | 4 +- .../common/io/FastStringReader.java | 208 ------------------ .../lucene/search/MoreLikeThisQuery.java | 4 +- .../common/lucene/search/XMoreLikeThis.java | 4 +- .../common/xcontent/cbor/CborXContent.java | 4 +- .../common/xcontent/json/JsonXContent.java | 4 +- .../common/xcontent/smile/SmileXContent.java | 4 +- .../common/xcontent/yaml/YamlXContent.java | 4 +- .../analysis/SynonymTokenFilterFactory.java | 4 +- 13 files changed, 24 insertions(+), 234 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/common/io/FastStringReader.java diff --git a/buildSrc/src/main/resources/forbidden/es-server-signatures.txt b/buildSrc/src/main/resources/forbidden/es-server-signatures.txt index 89179350174a6..9db17aaac0e93 100644 --- a/buildSrc/src/main/resources/forbidden/es-server-signatures.txt +++ b/buildSrc/src/main/resources/forbidden/es-server-signatures.txt @@ -29,8 +29,6 @@ java.util.concurrent.Executors#privilegedThreadFactory() java.lang.Character#codePointBefore(char[],int) @ Implicit start offset is error-prone when the char[] is a buffer and the first chars are random chars java.lang.Character#codePointAt(char[],int) @ Implicit end offset is error-prone when the char[] is a buffer and the last chars are random chars -java.io.StringReader#(java.lang.String) @ Use FastStringReader instead - @defaultMessage Reference management is tricky, leave it to SearcherManager org.apache.lucene.index.IndexReader#decRef() org.apache.lucene.index.IndexReader#incRef() diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java index 16081b3dd1b12..5a0b2e15460c5 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java @@ -21,11 +21,11 @@ import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheFactory; +import java.io.StringReader; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.script.GeneralScriptException; import org.elasticsearch.script.Script; @@ -65,7 +65,7 @@ public T compile(String templateName, String templateSource, ScriptContext new MustacheExecutableScript(template, params); return context.factoryClazz.cast(compiled); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java index 99fe07a1f49cc..b17d2c3f2cd01 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java @@ -41,7 +41,6 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexService; @@ -65,6 +64,7 @@ import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -317,12 +317,12 @@ private static DetailAnalyzeResponse detailAnalyze(AnalyzeRequest request, Analy for (int textIndex = 0; textIndex < request.text().length; textIndex++) { String charFilteredSource = request.text()[textIndex]; - Reader reader = new FastStringReader(charFilteredSource); + Reader reader = new StringReader(charFilteredSource); if (charFilterFactories != null) { for (int charFilterIndex = 0; charFilterIndex < charFilterFactories.length; charFilterIndex++) { reader = charFilterFactories[charFilterIndex].create(reader); - Reader readerForWriteOut = new FastStringReader(charFilteredSource); + Reader readerForWriteOut = new StringReader(charFilteredSource); readerForWriteOut = charFilterFactories[charFilterIndex].create(readerForWriteOut); charFilteredSource = writeCharStream(readerForWriteOut); charFiltersTexts[charFilterIndex][textIndex] = charFilteredSource; @@ -382,7 +382,7 @@ private static DetailAnalyzeResponse detailAnalyze(AnalyzeRequest request, Analy } private static TokenStream createStackedTokenStream(String source, CharFilterFactory[] charFilterFactories, TokenizerFactory tokenizerFactory, TokenFilterFactory[] tokenFilterFactories, int current) { - Reader reader = new FastStringReader(source); + Reader reader = new StringReader(source); for (CharFilterFactory charFilterFactory : charFilterFactories) { reader = charFilterFactory.create(reader); } diff --git a/server/src/main/java/org/elasticsearch/common/Strings.java b/server/src/main/java/org/elasticsearch/common/Strings.java index 02a0852b0a03a..8c823f401a0f8 100644 --- a/server/src/main/java/org/elasticsearch/common/Strings.java +++ b/server/src/main/java/org/elasticsearch/common/Strings.java @@ -23,7 +23,6 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -31,6 +30,7 @@ import java.io.BufferedReader; import java.io.IOException; +import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -51,7 +51,7 @@ public class Strings { public static final String[] EMPTY_ARRAY = new String[0]; public static void spaceify(int spaces, String from, StringBuilder to) throws Exception { - try (BufferedReader reader = new BufferedReader(new FastStringReader(from))) { + try (BufferedReader reader = new BufferedReader(new StringReader(from))) { String line; while ((line = reader.readLine()) != null) { for (int i = 0; i < spaces; i++) { diff --git a/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java b/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java index 38643df017943..2a8110c5f4dc2 100644 --- a/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java +++ b/server/src/main/java/org/elasticsearch/common/geo/parsers/GeoWKTParser.java @@ -22,6 +22,7 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.geo.GeoShapeType; +import java.io.StringReader; import org.elasticsearch.common.geo.builders.CoordinatesBuilder; import org.elasticsearch.common.geo.builders.EnvelopeBuilder; import org.elasticsearch.common.geo.builders.GeometryCollectionBuilder; @@ -32,7 +33,6 @@ import org.elasticsearch.common.geo.builders.PointBuilder; import org.elasticsearch.common.geo.builders.PolygonBuilder; import org.elasticsearch.common.geo.builders.ShapeBuilder; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.xcontent.XContentParser; @@ -69,7 +69,7 @@ public static ShapeBuilder parse(XContentParser parser) /** throws an exception if the parsed geometry type does not match the expected shape type */ public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType) throws IOException, ElasticsearchParseException { - FastStringReader reader = new FastStringReader(parser.text()); + StringReader reader = new StringReader(parser.text()); try { // setup the tokenizer; configured to read words w/o numbers StreamTokenizer tokenizer = new StreamTokenizer(reader); diff --git a/server/src/main/java/org/elasticsearch/common/io/FastStringReader.java b/server/src/main/java/org/elasticsearch/common/io/FastStringReader.java deleted file mode 100644 index 2ac7e9022e687..0000000000000 --- a/server/src/main/java/org/elasticsearch/common/io/FastStringReader.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.io; - -import java.io.IOException; -import java.io.Reader; - -/** - * A character stream whose source is a string that is not thread safe - *

- * (shay.banon - * ) - */ -public class FastStringReader extends Reader implements CharSequence { - - private String str; - private int length; - private int next = 0; - private int mark = 0; - private boolean closed = false; - - /** - * Creates a new string reader. - * - * @param s String providing the character stream. - */ - public FastStringReader(String s) { - this.str = s; - this.length = s.length(); - } - - /** - * Check to make sure that the stream has not been closed - */ - private void ensureOpen() throws IOException { - if (closed) { - throw new IOException("Stream closed"); - } - } - - @Override - public int length() { - return length; - } - - @Override - public char charAt(int index) { - return str.charAt(index); - } - - @Override - public CharSequence subSequence(int start, int end) { - return str.subSequence(start, end); - } - - /** - * Reads a single character. - * - * @return The character read, or -1 if the end of the stream has been - * reached - * @throws IOException If an I/O error occurs - */ - @Override - public int read() throws IOException { - ensureOpen(); - if (next >= length) - return -1; - return str.charAt(next++); - } - - /** - * Reads characters into a portion of an array. - * - * @param cbuf Destination buffer - * @param off Offset at which to start writing characters - * @param len Maximum number of characters to read - * @return The number of characters read, or -1 if the end of the - * stream has been reached - * @throws IOException If an I/O error occurs - */ - @Override - public int read(char cbuf[], int off, int len) throws IOException { - ensureOpen(); - if (len == 0) { - return 0; - } - if (next >= length) - return -1; - int n = Math.min(length - next, len); - str.getChars(next, next + n, cbuf, off); - next += n; - return n; - } - - /** - * Skips the specified number of characters in the stream. Returns - * the number of characters that were skipped. - *

- * The ns parameter may be negative, even though the - * skip method of the {@link Reader} superclass throws - * an exception in this case. Negative values of ns cause the - * stream to skip backwards. Negative return values indicate a skip - * backwards. It is not possible to skip backwards past the beginning of - * the string. - *

- * If the entire string has been read or skipped, then this method has - * no effect and always returns 0. - * - * @throws IOException If an I/O error occurs - */ - @Override - public long skip(long ns) throws IOException { - ensureOpen(); - if (next >= length) - return 0; - // Bound skip by beginning and end of the source - long n = Math.min(length - next, ns); - n = Math.max(-next, n); - next += n; - return n; - } - - /** - * Tells whether this stream is ready to be read. - * - * @return True if the next read() is guaranteed not to block for input - * @throws IOException If the stream is closed - */ - @Override - public boolean ready() throws IOException { - ensureOpen(); - return true; - } - - /** - * Tells whether this stream supports the mark() operation, which it does. - */ - @Override - public boolean markSupported() { - return true; - } - - /** - * Marks the present position in the stream. Subsequent calls to reset() - * will reposition the stream to this point. - * - * @param readAheadLimit Limit on the number of characters that may be - * read while still preserving the mark. Because - * the stream's input comes from a string, there - * is no actual limit, so this argument must not - * be negative, but is otherwise ignored. - * @throws IllegalArgumentException If readAheadLimit is < 0 - * @throws IOException If an I/O error occurs - */ - @Override - public void mark(int readAheadLimit) throws IOException { - if (readAheadLimit < 0) { - throw new IllegalArgumentException("Read-ahead limit < 0"); - } - ensureOpen(); - mark = next; - } - - /** - * Resets the stream to the most recent mark, or to the beginning of the - * string if it has never been marked. - * - * @throws IOException If an I/O error occurs - */ - @Override - public void reset() throws IOException { - ensureOpen(); - next = mark; - } - - /** - * Closes the stream and releases any system resources associated with - * it. Once the stream has been closed, further read(), - * ready(), mark(), or reset() invocations will throw an IOException. - * Closing a previously closed stream has no effect. - */ - @Override - public void close() { - closed = true; - } - - @Override - public String toString() { - return str; - } -} diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java b/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java index 28971fc9ca45e..f79f45f3b62bd 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java @@ -35,10 +35,10 @@ import org.apache.lucene.search.similarities.TFIDFSimilarity; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.FastStringReader; import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -166,7 +166,7 @@ private Query createQuery(XMoreLikeThis mlt) throws IOException { if (this.likeText != null) { Reader[] readers = new Reader[likeText.length]; for (int i = 0; i < readers.length; i++) { - readers[i] = new FastStringReader(likeText[i]); + readers[i] = new StringReader(likeText[i]); } //LUCENE 4 UPGRADE this mapps the 3.6 behavior (only use the first field) Query mltQuery = mlt.like(moreLikeFields[0], readers); diff --git a/server/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java b/server/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java index e973689615ed7..5d1e4537f6561 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/search/XMoreLikeThis.java @@ -58,10 +58,10 @@ import org.apache.lucene.util.CharsRefBuilder; import org.apache.lucene.util.PriorityQueue; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.io.FastStringReader; import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -815,7 +815,7 @@ private PriorityQueue retrieveTerms(int docNum) throws IOException { for (IndexableField field : fields) { final String stringValue = field.stringValue(); if (stringValue != null) { - addTermFrequencies(new FastStringReader(stringValue), termFreqMap, fieldName); + addTermFrequencies(new StringReader(stringValue), termFreqMap, fieldName); } } } else { diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java b/server/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java index 222cf8e98bd32..58a9e9a98f833 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java @@ -24,7 +24,6 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContent; @@ -37,6 +36,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; +import java.io.StringReader; import java.util.Set; /** @@ -81,7 +81,7 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, @Override public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) throws IOException { - return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(new FastStringReader(content))); + return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(new StringReader(content))); } @Override diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java b/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java index 813aa64d9ffa3..b2aac37abe57d 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java @@ -23,7 +23,6 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContent; @@ -36,6 +35,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; +import java.io.StringReader; import java.util.Set; /** @@ -82,7 +82,7 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, @Override public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) throws IOException { - return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(new FastStringReader(content))); + return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(new StringReader(content))); } @Override diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java b/server/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java index bbe3542f29c5a..caf6488eea398 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java @@ -24,7 +24,6 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.smile.SmileGenerator; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContent; @@ -37,6 +36,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; +import java.io.StringReader; import java.util.Set; /** @@ -82,7 +82,7 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, @Override public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) throws IOException { - return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(new FastStringReader(content))); + return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(new StringReader(content))); } @Override diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java b/server/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java index 3c466e59093be..5c335276bc024 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java @@ -22,7 +22,6 @@ import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContent; @@ -35,6 +34,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; +import java.io.StringReader; import java.util.Set; /** @@ -76,7 +76,7 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, @Override public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) throws IOException { - return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(new FastStringReader(content))); + return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(new StringReader(content))); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/analysis/SynonymTokenFilterFactory.java b/server/src/main/java/org/elasticsearch/index/analysis/SynonymTokenFilterFactory.java index 37e96cbb54a57..56bae57198829 100644 --- a/server/src/main/java/org/elasticsearch/index/analysis/SynonymTokenFilterFactory.java +++ b/server/src/main/java/org/elasticsearch/index/analysis/SynonymTokenFilterFactory.java @@ -25,13 +25,13 @@ import org.apache.lucene.analysis.synonym.SynonymFilter; import org.apache.lucene.analysis.synonym.SynonymMap; import org.apache.lucene.analysis.synonym.WordnetSynonymParser; -import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.util.List; public class SynonymTokenFilterFactory extends AbstractTokenFilterFactory { @@ -68,7 +68,7 @@ protected Reader getRulesFromSettings(Environment env) { for (String line : rulesList) { sb.append(line).append(System.lineSeparator()); } - rulesReader = new FastStringReader(sb.toString()); + rulesReader = new StringReader(sb.toString()); } else if (settings.get("synonyms_path") != null) { rulesReader = Analysis.getReaderFromFile(env, settings, "synonyms_path"); } else {