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
This commit is contained in:
Shay Banon 2012-10-27 23:20:43 +02:00
parent 19ab1d0548
commit e75301b781
5 changed files with 30 additions and 34 deletions

View File

@ -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;
/**
*
*/
@ -131,9 +131,9 @@ public interface XContentParser extends Closeable {
String textOrNull() throws IOException;
BytesRef bytesOrNull(BytesRef spare) throws IOException;
BytesRef bytesOrNull() throws IOException;
BytesRef bytes(BytesRef spare) throws IOException;
BytesRef bytes() throws IOException;
boolean hasTextCharacters();

View File

@ -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;
@ -108,24 +109,19 @@ public abstract class AbstractXContentParser implements XContentParser {
}
@Override
public BytesRef bytesOrNull(BytesRef spare) throws IOException {
public BytesRef bytesOrNull() throws IOException {
if (currentToken() == Token.VALUE_NULL) {
return null;
}
return bytes(spare);
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

View File

@ -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 + "]");

View File

@ -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 + "]");

View File

@ -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();