reduce bytes allocation when doing http
This commit is contained in:
parent
fbde2c84ac
commit
6cd3fc92ed
|
@ -131,11 +131,10 @@ public class IndexRequest extends ShardReplicationOperationRequest {
|
|||
* @param id The id of 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.type = type;
|
||||
this.id = id;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override public ActionRequestValidationException validate() {
|
||||
|
|
|
@ -42,7 +42,7 @@ public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest
|
|||
|
||||
private final String path;
|
||||
|
||||
private byte[] data;
|
||||
private byte[] cachedData;
|
||||
|
||||
public NettyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest request) {
|
||||
this.request = request;
|
||||
|
@ -91,13 +91,27 @@ public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest
|
|||
return request.getContent().readableBytes() > 0;
|
||||
}
|
||||
|
||||
@Override public byte[] contentAsBytes() {
|
||||
if (this.data != null) {
|
||||
return this.data;
|
||||
@Override public int contentLength() {
|
||||
return request.getContent().readableBytes();
|
||||
}
|
||||
|
||||
@Override public byte[] contentByteArray() {
|
||||
if (request.getContent().hasArray()) {
|
||||
return request.getContent().array();
|
||||
}
|
||||
data = new byte[request.getContent().readableBytes()];
|
||||
request.getContent().getBytes(request.getContent().readerIndex(), data);
|
||||
return data;
|
||||
if (cachedData != null) {
|
||||
return cachedData;
|
||||
}
|
||||
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");
|
||||
|
|
|
@ -50,7 +50,11 @@ public interface RestRequest extends ToXContent.Params {
|
|||
|
||||
boolean hasContent();
|
||||
|
||||
byte[] contentAsBytes();
|
||||
byte[] contentByteArray();
|
||||
|
||||
int contentByteArrayOffset();
|
||||
|
||||
int contentLength();
|
||||
|
||||
String contentAsString();
|
||||
|
||||
|
|
|
@ -57,8 +57,8 @@ public class RestIndicesAliasesAction extends BaseRestHandler {
|
|||
// { remove : { index : "test1", alias : "alias1" } }
|
||||
// ]
|
||||
// }
|
||||
byte[] content = request.contentAsBytes();
|
||||
XContentParser parser = XContentFactory.xContent(content).createParser(content);
|
||||
XContentParser parser = XContentFactory.xContent(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength())
|
||||
.createParser(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
|
||||
XContentParser.Token token = parser.nextToken();
|
||||
if (token == null) {
|
||||
throw new ElasticSearchIllegalArgumentException("No action is specified");
|
||||
|
|
|
@ -64,7 +64,16 @@ public class RestCountAction extends BaseRestHandler {
|
|||
operationThreading = BroadcastOperationThreading.SINGLE_THREAD;
|
||||
}
|
||||
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.queryHint(request.param("query_hint"));
|
||||
countRequest.minScore(request.paramAsFloat("min_score", DEFAULT_MIN_SCORE));
|
||||
|
|
|
@ -54,7 +54,16 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
|
|||
// we just build a response and send it, no need to fork a thread
|
||||
deleteByQueryRequest.listenerThreaded(false);
|
||||
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"));
|
||||
String typesParam = request.param("type");
|
||||
if (typesParam != null) {
|
||||
|
|
|
@ -56,7 +56,8 @@ public class RestIndexAction extends BaseRestHandler {
|
|||
}
|
||||
|
||||
@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));
|
||||
String sOpType = request.param("op_type");
|
||||
if (sOpType != null) {
|
||||
|
|
|
@ -26,7 +26,6 @@ import org.elasticsearch.action.search.SearchType;
|
|||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.rest.*;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
import org.elasticsearch.util.Unicode;
|
||||
import org.elasticsearch.util.inject.Inject;
|
||||
import org.elasticsearch.util.settings.Settings;
|
||||
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||
|
@ -73,11 +72,11 @@ public class RestMoreLikeThisAction extends BaseRestHandler {
|
|||
mltRequest.searchScroll(new Scroll(parseTimeValue(searchScroll, null)));
|
||||
}
|
||||
if (request.hasContent()) {
|
||||
mltRequest.searchSource(request.contentAsBytes());
|
||||
mltRequest.searchSource(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
|
||||
} else {
|
||||
String searchSource = request.param("search_source");
|
||||
if (searchSource != null) {
|
||||
mltRequest.searchSource(Unicode.fromStringAsBytes(searchSource));
|
||||
mltRequest.searchSource(searchSource);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -31,7 +31,6 @@ import org.elasticsearch.rest.*;
|
|||
import org.elasticsearch.rest.action.support.RestActions;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.util.Unicode;
|
||||
import org.elasticsearch.util.inject.Inject;
|
||||
import org.elasticsearch.util.settings.Settings;
|
||||
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||
|
@ -116,11 +115,11 @@ public class RestSearchAction extends BaseRestHandler {
|
|||
SearchRequest searchRequest = new SearchRequest(indices);
|
||||
// get the content, and put it in the body
|
||||
if (request.hasContent()) {
|
||||
searchRequest.source(request.contentAsBytes());
|
||||
searchRequest.source(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
|
||||
} else {
|
||||
String source = request.param("source");
|
||||
if (source != null) {
|
||||
searchRequest.source(Unicode.fromStringAsBytes(source));
|
||||
searchRequest.source(source);
|
||||
}
|
||||
}
|
||||
// add extra source based on the request parameters
|
||||
|
|
|
@ -26,7 +26,6 @@ import org.elasticsearch.index.query.xcontent.QueryBuilders;
|
|||
import org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder;
|
||||
import org.elasticsearch.rest.RestRequest;
|
||||
import org.elasticsearch.util.Strings;
|
||||
import org.elasticsearch.util.Unicode;
|
||||
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -65,13 +64,6 @@ public class RestActions {
|
|||
}
|
||||
|
||||
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");
|
||||
if (queryString == null) {
|
||||
throw new ElasticSearchIllegalArgumentException("No query to execute, not in body, and not bounded to 'q' parameter");
|
||||
|
|
|
@ -37,7 +37,7 @@ public class RestXContentBuilder {
|
|||
if (contentType == null) {
|
||||
// try and guess it from the body, if exists
|
||||
if (request.hasContent()) {
|
||||
contentType = XContentFactory.xContentType(request.contentAsBytes());
|
||||
contentType = XContentFactory.xContentType(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
|
||||
}
|
||||
}
|
||||
if (contentType == null) {
|
||||
|
|
|
@ -109,10 +109,18 @@ public class MemcachedRestRequest extends AbstractRestRequest {
|
|||
return data != null;
|
||||
}
|
||||
|
||||
@Override public byte[] contentAsBytes() {
|
||||
@Override public byte[] contentByteArray() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override public int contentByteArrayOffset() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override public int contentLength() {
|
||||
return dataSize;
|
||||
}
|
||||
|
||||
@Override public String contentAsString() {
|
||||
return Unicode.fromBytes(data);
|
||||
}
|
||||
|
|
|
@ -180,10 +180,6 @@ public class MemcachedDecoder extends FrameDecoder {
|
|||
return null;
|
||||
}
|
||||
|
||||
private void readTextHeader(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
|
||||
this.request = null;
|
||||
sb.setLength(0);
|
||||
|
|
Loading…
Reference in New Issue