optimize get/delete/index response xcontent generation

This commit is contained in:
kimchy 2010-10-09 04:06:03 +02:00
parent 087b4e6f23
commit c15a612b8a
3 changed files with 37 additions and 18 deletions

View File

@ -27,10 +27,7 @@ import org.elasticsearch.common.compress.lzf.LZFDecoder;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -219,24 +216,31 @@ public class GetResponse implements ActionResponse, Streamable, Iterable<GetFiel
return fields.values().iterator(); return fields.values().iterator();
} }
static final class Fields {
static final XContentBuilderString _INDEX = new XContentBuilderString("_index");
static final XContentBuilderString _TYPE = new XContentBuilderString("_type");
static final XContentBuilderString _ID = new XContentBuilderString("_id");
static final XContentBuilderString FIELDS = new XContentBuilderString("fields");
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException { @Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
if (!exists()) { if (!exists()) {
builder.startObject(); builder.startObject();
builder.field("_index", index); builder.field(Fields._INDEX, index);
builder.field("_type", type); builder.field(Fields._TYPE, type);
builder.field("_id", id); builder.field(Fields._ID, id);
builder.endObject(); builder.endObject();
} else { } else {
builder.startObject(); builder.startObject();
builder.field("_index", index); builder.field(Fields._INDEX, index);
builder.field("_type", type); builder.field(Fields._TYPE, type);
builder.field("_id", id); builder.field(Fields._ID, id);
if (source != null) { if (source != null) {
RestXContentBuilder.restDocumentSource(source, builder, params); RestXContentBuilder.restDocumentSource(source, builder, params);
} }
if (fields != null && !fields.isEmpty()) { if (fields != null && !fields.isEmpty()) {
builder.startObject("fields"); builder.startObject(Fields.FIELDS);
for (GetField field : fields.values()) { for (GetField field : fields.values()) {
if (field.values().isEmpty()) { if (field.values().isEmpty()) {
continue; continue;

View File

@ -26,6 +26,7 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.indices.IndexMissingException; import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
@ -37,7 +38,7 @@ import static org.elasticsearch.common.unit.TimeValue.*;
import static org.elasticsearch.rest.RestResponse.Status.*; import static org.elasticsearch.rest.RestResponse.Status.*;
/** /**
* @author kimchy (Shay Banon) * @author kimchy (shay.banon)
*/ */
public class RestDeleteIndexAction extends BaseRestHandler { public class RestDeleteIndexAction extends BaseRestHandler {
@ -54,8 +55,8 @@ public class RestDeleteIndexAction extends BaseRestHandler {
try { try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject() builder.startObject()
.field("ok", true) .field(Fields.OK, true)
.field("acknowledged", response.acknowledged()) .field(Fields.ACKNOWLEDGED, response.acknowledged())
.endObject(); .endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder)); channel.sendResponse(new XContentRestResponse(request, OK, builder));
} catch (IOException e) { } catch (IOException e) {
@ -78,4 +79,9 @@ public class RestDeleteIndexAction extends BaseRestHandler {
} }
}); });
} }
static final class Fields {
static final XContentBuilderString OK = new XContentBuilderString("ok");
static final XContentBuilderString ACKNOWLEDGED = new XContentBuilderString("acknowledged");
}
} }

View File

@ -27,6 +27,7 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
@ -90,10 +91,10 @@ public class RestIndexAction extends BaseRestHandler {
try { try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject() builder.startObject()
.field("ok", true) .field(Fields.OK, true)
.field("_index", result.index()) .field(Fields._INDEX, result.index())
.field("_type", result.type()) .field(Fields._TYPE, result.type())
.field("_id", result.id()) .field(Fields._ID, result.id())
.endObject(); .endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder)); channel.sendResponse(new XContentRestResponse(request, OK, builder));
} catch (Exception e) { } catch (Exception e) {
@ -110,4 +111,12 @@ public class RestIndexAction extends BaseRestHandler {
} }
}); });
} }
static final class Fields {
static final XContentBuilderString OK = new XContentBuilderString("ok");
static final XContentBuilderString _INDEX = new XContentBuilderString("_index");
static final XContentBuilderString _TYPE = new XContentBuilderString("_type");
static final XContentBuilderString _ID = new XContentBuilderString("_id");
}
} }