From e75301b781f2ee73f0782d9b90b5fa085052ad9a Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sat, 27 Oct 2012 23:20:43 +0200 Subject: [PATCH] lucene 4: optimize bytes on XContentParser also, does not seem like we need to reuse bytes buffer, if we need to, we can always add it later --- .../common/xcontent/XContentParser.java | 12 ++++----- .../support/AbstractXContentParser.java | 26 ++++++++----------- .../index/query/RangeFilterParser.java | 12 ++++----- .../index/query/RangeQueryParser.java | 12 ++++----- .../index/query/TypeFilterParser.java | 2 +- 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java b/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java index 3872eb5e5fa..597901676da 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java +++ b/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java @@ -19,12 +19,12 @@ package org.elasticsearch.common.xcontent; +import org.apache.lucene.util.BytesRef; + import java.io.Closeable; import java.io.IOException; import java.util.Map; -import org.apache.lucene.util.BytesRef; - /** * */ @@ -130,10 +130,10 @@ public interface XContentParser extends Closeable { String text() throws IOException; String textOrNull() throws IOException; - - BytesRef bytesOrNull(BytesRef spare) throws IOException; - - BytesRef bytes(BytesRef spare) throws IOException; + + BytesRef bytesOrNull() throws IOException; + + BytesRef bytes() throws IOException; boolean hasTextCharacters(); diff --git a/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java b/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java index e6a89bb475f..945b6ac617e 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java +++ b/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.common.xcontent.support; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.UnicodeUtil; import org.elasticsearch.common.Booleans; import org.elasticsearch.common.xcontent.XContentParser; @@ -106,26 +107,21 @@ public abstract class AbstractXContentParser implements XContentParser { } return text(); } - - + @Override - public BytesRef bytesOrNull(BytesRef spare) throws IOException { - if (currentToken() == Token.VALUE_NULL) { - return null; - } - return bytes(spare); + public BytesRef bytesOrNull() throws IOException { + if (currentToken() == Token.VALUE_NULL) { + return null; + } + return bytes(); } @Override - public BytesRef bytes(BytesRef spare) throws IOException { - // LUCENE 4 UPGRADE: we can possibly make this more efficient for now I just forward to text - if (spare == null) { - return new BytesRef(text()); - } else { - spare.copyChars(text()); - return spare; - } + public BytesRef bytes() throws IOException { + BytesRef bytes = new BytesRef(); + UnicodeUtil.UTF16toUTF8(textCharacters(), textOffset(), textLength(), bytes); + return bytes; } @Override diff --git a/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java b/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java index b24f72fd13f..f35cb3194a6 100644 --- a/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java @@ -72,24 +72,24 @@ public class RangeFilterParser implements FilterParser { currentFieldName = parser.currentName(); } else { if ("from".equals(currentFieldName)) { - from = parser.bytesOrNull(from); + from = parser.bytesOrNull(); } else if ("to".equals(currentFieldName)) { - to = parser.bytesOrNull(to); + to = parser.bytesOrNull(); } else if ("include_lower".equals(currentFieldName) || "includeLower".equals(currentFieldName)) { includeLower = parser.booleanValue(); } else if ("include_upper".equals(currentFieldName) || "includeUpper".equals(currentFieldName)) { includeUpper = parser.booleanValue(); } else if ("gt".equals(currentFieldName)) { - from = parser.bytesOrNull(from); + from = parser.bytesOrNull(); includeLower = false; } else if ("gte".equals(currentFieldName) || "ge".equals(currentFieldName)) { - from = parser.bytesOrNull(from); + from = parser.bytesOrNull(); includeLower = true; } else if ("lt".equals(currentFieldName)) { - to = parser.bytesOrNull(to); + to = parser.bytesOrNull(); includeUpper = false; } else if ("lte".equals(currentFieldName) || "le".equals(currentFieldName)) { - to = parser.bytesOrNull(to); + to = parser.bytesOrNull(); includeUpper = true; } else { throw new QueryParsingException(parseContext.index(), "[range] filter does not support [" + currentFieldName + "]"); diff --git a/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java b/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java index b79b3a07ee3..5cbaccf9db4 100644 --- a/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java @@ -72,9 +72,9 @@ public class RangeQueryParser implements QueryParser { currentFieldName = parser.currentName(); } else { if ("from".equals(currentFieldName)) { - from = parser.bytesOrNull(from); + from = parser.bytesOrNull(); } else if ("to".equals(currentFieldName)) { - to = parser.bytesOrNull(to); + to = parser.bytesOrNull(); } else if ("include_lower".equals(currentFieldName) || "includeLower".equals(currentFieldName)) { includeLower = parser.booleanValue(); } else if ("include_upper".equals(currentFieldName) || "includeUpper".equals(currentFieldName)) { @@ -82,16 +82,16 @@ public class RangeQueryParser implements QueryParser { } else if ("boost".equals(currentFieldName)) { boost = parser.floatValue(); } else if ("gt".equals(currentFieldName)) { - from = parser.bytesOrNull(from); + from = parser.bytesOrNull(); includeLower = false; } else if ("gte".equals(currentFieldName) || "ge".equals(currentFieldName)) { - from = parser.bytesOrNull(from); + from = parser.bytesOrNull(); includeLower = true; } else if ("lt".equals(currentFieldName)) { - to = parser.bytesOrNull(to); + to = parser.bytesOrNull(); includeUpper = false; } else if ("lte".equals(currentFieldName) || "le".equals(currentFieldName)) { - to = parser.bytesOrNull(to); + to = parser.bytesOrNull(); includeUpper = true; } else { throw new QueryParsingException(parseContext.index(), "[range] query does not support [" + currentFieldName + "]"); diff --git a/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java b/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java index f1b5c227130..754295562e6 100644 --- a/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java @@ -59,7 +59,7 @@ public class TypeFilterParser implements FilterParser { if (token != XContentParser.Token.VALUE_STRING) { throw new QueryParsingException(parseContext.index(), "[type] filter should have a value field, and the type name"); } - BytesRef type = parser.bytes(null); + BytesRef type = parser.bytes(); // move to the next token parser.nextToken();