reduce bytes allocation when doing http

This commit is contained in:
kimchy 2010-05-13 20:42:59 +03:00
parent fbde2c84ac
commit 6cd3fc92ed
13 changed files with 65 additions and 35 deletions

View File

@ -131,11 +131,10 @@ public class IndexRequest extends ShardReplicationOperationRequest {
* @param id The id of document * @param id The id of document
* @param source The JSON source document * @param source The JSON source document
*/ */
public IndexRequest(String index, String type, String id, byte[] source) { public IndexRequest(String index, String type, String id) {
this.index = index; this.index = index;
this.type = type; this.type = type;
this.id = id; this.id = id;
this.source = source;
} }
@Override public ActionRequestValidationException validate() { @Override public ActionRequestValidationException validate() {

View File

@ -42,7 +42,7 @@ public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest
private final String path; private final String path;
private byte[] data; private byte[] cachedData;
public NettyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest request) { public NettyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest request) {
this.request = request; this.request = request;
@ -91,13 +91,27 @@ public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest
return request.getContent().readableBytes() > 0; return request.getContent().readableBytes() > 0;
} }
@Override public byte[] contentAsBytes() { @Override public int contentLength() {
if (this.data != null) { return request.getContent().readableBytes();
return this.data; }
@Override public byte[] contentByteArray() {
if (request.getContent().hasArray()) {
return request.getContent().array();
} }
data = new byte[request.getContent().readableBytes()]; if (cachedData != null) {
request.getContent().getBytes(request.getContent().readerIndex(), data); return cachedData;
return data; }
cachedData = new byte[request.getContent().readableBytes()];
request.getContent().getBytes(request.getContent().readerIndex(), cachedData);
return cachedData;
}
@Override public int contentByteArrayOffset() {
if (request.getContent().hasArray()) {
return request.getContent().arrayOffset();
}
return 0;
} }
private static Charset UTF8 = Charset.forName("UTF-8"); private static Charset UTF8 = Charset.forName("UTF-8");

View File

@ -50,7 +50,11 @@ public interface RestRequest extends ToXContent.Params {
boolean hasContent(); boolean hasContent();
byte[] contentAsBytes(); byte[] contentByteArray();
int contentByteArrayOffset();
int contentLength();
String contentAsString(); String contentAsString();

View File

@ -57,8 +57,8 @@ public class RestIndicesAliasesAction extends BaseRestHandler {
// { remove : { index : "test1", alias : "alias1" } } // { remove : { index : "test1", alias : "alias1" } }
// ] // ]
// } // }
byte[] content = request.contentAsBytes(); XContentParser parser = XContentFactory.xContent(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength())
XContentParser parser = XContentFactory.xContent(content).createParser(content); .createParser(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
XContentParser.Token token = parser.nextToken(); XContentParser.Token token = parser.nextToken();
if (token == null) { if (token == null) {
throw new ElasticSearchIllegalArgumentException("No action is specified"); throw new ElasticSearchIllegalArgumentException("No action is specified");

View File

@ -64,7 +64,16 @@ public class RestCountAction extends BaseRestHandler {
operationThreading = BroadcastOperationThreading.SINGLE_THREAD; operationThreading = BroadcastOperationThreading.SINGLE_THREAD;
} }
countRequest.operationThreading(operationThreading); countRequest.operationThreading(operationThreading);
countRequest.query(RestActions.parseQuerySource(request)); if (request.hasContent()) {
countRequest.query(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} else {
String source = request.param("source");
if (source != null) {
countRequest.query(source);
} else {
countRequest.query(RestActions.parseQuerySource(request));
}
}
countRequest.queryParserName(request.param("query_parser_name")); countRequest.queryParserName(request.param("query_parser_name"));
countRequest.queryHint(request.param("query_hint")); countRequest.queryHint(request.param("query_hint"));
countRequest.minScore(request.paramAsFloat("min_score", DEFAULT_MIN_SCORE)); countRequest.minScore(request.paramAsFloat("min_score", DEFAULT_MIN_SCORE));

View File

@ -54,7 +54,16 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
// we just build a response and send it, no need to fork a thread // we just build a response and send it, no need to fork a thread
deleteByQueryRequest.listenerThreaded(false); deleteByQueryRequest.listenerThreaded(false);
try { try {
deleteByQueryRequest.query(RestActions.parseQuerySource(request)); if (request.hasContent()) {
deleteByQueryRequest.query(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} else {
String source = request.param("source");
if (source != null) {
deleteByQueryRequest.query(source);
} else {
deleteByQueryRequest.query(RestActions.parseQuerySource(request));
}
}
deleteByQueryRequest.queryParserName(request.param("query_parser_name")); deleteByQueryRequest.queryParserName(request.param("query_parser_name"));
String typesParam = request.param("type"); String typesParam = request.param("type");
if (typesParam != null) { if (typesParam != null) {

View File

@ -56,7 +56,8 @@ public class RestIndexAction extends BaseRestHandler {
} }
@Override public void handleRequest(final RestRequest request, final RestChannel channel) { @Override public void handleRequest(final RestRequest request, final RestChannel channel) {
IndexRequest indexRequest = new IndexRequest(request.param("index"), request.param("type"), request.param("id"), request.contentAsBytes()); IndexRequest indexRequest = new IndexRequest(request.param("index"), request.param("type"), request.param("id"));
indexRequest.source(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
indexRequest.timeout(request.paramAsTime("timeout", IndexRequest.DEFAULT_TIMEOUT)); indexRequest.timeout(request.paramAsTime("timeout", IndexRequest.DEFAULT_TIMEOUT));
String sOpType = request.param("op_type"); String sOpType = request.param("op_type");
if (sOpType != null) { if (sOpType != null) {

View File

@ -26,7 +26,6 @@ import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.search.Scroll; import org.elasticsearch.search.Scroll;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.inject.Inject; import org.elasticsearch.util.inject.Inject;
import org.elasticsearch.util.settings.Settings; import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder; import org.elasticsearch.util.xcontent.builder.XContentBuilder;
@ -73,11 +72,11 @@ public class RestMoreLikeThisAction extends BaseRestHandler {
mltRequest.searchScroll(new Scroll(parseTimeValue(searchScroll, null))); mltRequest.searchScroll(new Scroll(parseTimeValue(searchScroll, null)));
} }
if (request.hasContent()) { if (request.hasContent()) {
mltRequest.searchSource(request.contentAsBytes()); mltRequest.searchSource(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} else { } else {
String searchSource = request.param("search_source"); String searchSource = request.param("search_source");
if (searchSource != null) { if (searchSource != null) {
mltRequest.searchSource(Unicode.fromStringAsBytes(searchSource)); mltRequest.searchSource(searchSource);
} }
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -31,7 +31,6 @@ import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions; import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.search.Scroll; import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.inject.Inject; import org.elasticsearch.util.inject.Inject;
import org.elasticsearch.util.settings.Settings; import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder; import org.elasticsearch.util.xcontent.builder.XContentBuilder;
@ -116,11 +115,11 @@ public class RestSearchAction extends BaseRestHandler {
SearchRequest searchRequest = new SearchRequest(indices); SearchRequest searchRequest = new SearchRequest(indices);
// get the content, and put it in the body // get the content, and put it in the body
if (request.hasContent()) { if (request.hasContent()) {
searchRequest.source(request.contentAsBytes()); searchRequest.source(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} else { } else {
String source = request.param("source"); String source = request.param("source");
if (source != null) { if (source != null) {
searchRequest.source(Unicode.fromStringAsBytes(source)); searchRequest.source(source);
} }
} }
// add extra source based on the request parameters // add extra source based on the request parameters

View File

@ -26,7 +26,6 @@ import org.elasticsearch.index.query.xcontent.QueryBuilders;
import org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder; import org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.util.Strings; import org.elasticsearch.util.Strings;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.xcontent.builder.XContentBuilder; import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -65,13 +64,6 @@ public class RestActions {
} }
public static byte[] parseQuerySource(RestRequest request) { public static byte[] parseQuerySource(RestRequest request) {
if (request.hasContent()) {
return request.contentAsBytes();
}
String source = request.param("source");
if (source != null) {
return Unicode.fromStringAsBytes(source);
}
String queryString = request.param("q"); String queryString = request.param("q");
if (queryString == null) { if (queryString == null) {
throw new ElasticSearchIllegalArgumentException("No query to execute, not in body, and not bounded to 'q' parameter"); throw new ElasticSearchIllegalArgumentException("No query to execute, not in body, and not bounded to 'q' parameter");

View File

@ -37,7 +37,7 @@ public class RestXContentBuilder {
if (contentType == null) { if (contentType == null) {
// try and guess it from the body, if exists // try and guess it from the body, if exists
if (request.hasContent()) { if (request.hasContent()) {
contentType = XContentFactory.xContentType(request.contentAsBytes()); contentType = XContentFactory.xContentType(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} }
} }
if (contentType == null) { if (contentType == null) {

View File

@ -109,10 +109,18 @@ public class MemcachedRestRequest extends AbstractRestRequest {
return data != null; return data != null;
} }
@Override public byte[] contentAsBytes() { @Override public byte[] contentByteArray() {
return data; return data;
} }
@Override public int contentByteArrayOffset() {
return 0;
}
@Override public int contentLength() {
return dataSize;
}
@Override public String contentAsString() { @Override public String contentAsString() {
return Unicode.fromBytes(data); return Unicode.fromBytes(data);
} }

View File

@ -180,10 +180,6 @@ public class MemcachedDecoder extends FrameDecoder {
return null; return null;
} }
private void readTextHeader(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
}
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
this.request = null; this.request = null;
sb.setLength(0); sb.setLength(0);