recator source rest building into common code

This commit is contained in:
kimchy 2010-08-19 20:24:12 +03:00
parent 7b1093fb16
commit 8d0e5b239c
3 changed files with 36 additions and 38 deletions

View File

@ -24,12 +24,14 @@ import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.compress.lzf.LZFDecoder;
import org.elasticsearch.common.io.stream.*;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException;
import java.util.Iterator;
@ -230,23 +232,7 @@ public class GetResponse implements ActionResponse, Streamable, Iterable<GetFiel
builder.field("_type", type);
builder.field("_id", id);
if (source != null) {
if (LZFDecoder.isCompressed(source)) {
BytesStreamInput siBytes = new BytesStreamInput(source);
LZFStreamInput siLzf = CachedStreamInput.cachedLzf(siBytes);
XContentType contentType = XContentFactory.xContentType(siLzf);
siLzf.resetToBufferStart();
if (contentType == builder.contentType()) {
builder.rawField("_source", siLzf);
} else {
builder.field("_source", XContentFactory.xContent(builder.contentType()).createParser(siLzf).map());
}
} else {
if (XContentFactory.xContentType(source) == builder.contentType()) {
builder.rawField("_source", source);
} else {
builder.field("_source", XContentFactory.xContent(builder.contentType()).createParser(source).map());
}
}
RestXContentBuilder.restDocumentSource(source, builder, params);
}
if (fields != null && !fields.isEmpty()) {

View File

@ -19,6 +19,11 @@
package org.elasticsearch.rest.action.support;
import org.elasticsearch.common.compress.lzf.LZFDecoder;
import org.elasticsearch.common.io.stream.BytesStreamInput;
import org.elasticsearch.common.io.stream.CachedStreamInput;
import org.elasticsearch.common.io.stream.LZFStreamInput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.builder.BinaryXContentBuilder;
@ -58,4 +63,26 @@ public class RestXContentBuilder {
}
return builder;
}
public static void restDocumentSource(byte[] source, XContentBuilder builder, ToXContent.Params params) throws IOException {
if (LZFDecoder.isCompressed(source)) {
BytesStreamInput siBytes = new BytesStreamInput(source);
LZFStreamInput siLzf = CachedStreamInput.cachedLzf(siBytes);
XContentType contentType = XContentFactory.xContentType(siLzf);
siLzf.resetToBufferStart();
if (contentType == builder.contentType()) {
builder.rawField("_source", siLzf);
} else {
// TODO, should we just return it as binary and not auto convert it?
builder.field("_source", XContentFactory.xContent(builder.contentType()).createParser(siLzf).map());
}
} else {
if (XContentFactory.xContentType(source) == builder.contentType()) {
builder.rawField("_source", source);
} else {
// TODO, should we just return it as binary and not auto convert it?
builder.field("_source", XContentFactory.xContent(builder.contentType()).createParser(source).map());
}
}
}
}

View File

@ -24,12 +24,13 @@ import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.compress.lzf.LZFDecoder;
import org.elasticsearch.common.io.stream.*;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.trove.TIntObjectHashMap;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchShardTarget;
@ -260,23 +261,7 @@ public class InternalSearchHit implements SearchHit {
builder.field("_score", score);
}
if (source != null) {
if (LZFDecoder.isCompressed(source)) {
BytesStreamInput siBytes = new BytesStreamInput(source);
LZFStreamInput siLzf = CachedStreamInput.cachedLzf(siBytes);
XContentType contentType = XContentFactory.xContentType(siLzf);
siLzf.resetToBufferStart();
if (contentType == builder.contentType()) {
builder.rawField("_source", siLzf);
} else {
builder.field("_source", XContentFactory.xContent(builder.contentType()).createParser(siLzf).map());
}
} else {
if (XContentFactory.xContentType(source) == builder.contentType()) {
builder.rawField("_source", source);
} else {
builder.field("_source", XContentFactory.xContent(builder.contentType()).createParser(source).map());
}
}
RestXContentBuilder.restDocumentSource(source, builder, params);
}
if (fields != null && !fields.isEmpty()) {
builder.startObject("fields");