Fix and simplify serialization of shard profile results

This commit is contained in:
Zachary Tong 2015-11-03 18:52:05 -05:00
parent 461da25080
commit 22e631fe64
3 changed files with 23 additions and 25 deletions

View file

@ -82,29 +82,12 @@ public class InternalProfileShardResult implements ProfileShardResult, Streamabl
return builder;
}
public static List<InternalProfileShardResult> readProfileShardResults(StreamInput in) throws IOException {
int size = in.readInt();
List<InternalProfileShardResult> results = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
InternalProfileShardResult result = readProfileShardResult(in);
results.add(result);
}
return results;
}
public static InternalProfileShardResult readProfileShardResult(StreamInput in) throws IOException {
InternalProfileShardResult newShardResults = new InternalProfileShardResult();
newShardResults.readFrom(in);
return newShardResults;
}
public static void writeProfileShardResults(List<InternalProfileShardResult> results, StreamOutput out) throws IOException {
out.writeInt(results.size());
for (InternalProfileShardResult result : results) {
result.writeTo(out);
}
}
@Override
public void readFrom(StreamInput in) throws IOException {

View file

@ -7,10 +7,7 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
public final class InternalProfileShardResults implements Streamable, ToXContent{
@ -45,11 +42,18 @@ public final class InternalProfileShardResults implements Streamable, ToXContent
@Override
public void readFrom(StreamInput in) throws IOException {
int size = in.readInt();
shardResults = new HashMap<>(size);
for (int i = 0; i < size; i++) {
String key = in.readString();
List<InternalProfileShardResult> shardResult = InternalProfileShardResult.readProfileShardResults(in);
int shardResultsSize = in.readInt();
List<InternalProfileShardResult> shardResult = new ArrayList<>(shardResultsSize);
for (int j = 0; j < shardResultsSize; j++) {
InternalProfileShardResult result = InternalProfileShardResult.readProfileShardResult(in);
shardResult.add(result);
}
shardResults.put(key, shardResult);
}
}
@ -59,6 +63,8 @@ public final class InternalProfileShardResults implements Streamable, ToXContent
out.writeInt(shardResults.size());
for (Map.Entry<String, List<InternalProfileShardResult>> entry : shardResults.entrySet()) {
out.writeString(entry.getKey());
out.writeInt(entry.getValue().size());
for (InternalProfileShardResult result : entry.getValue()) {
result.writeTo(out);
}

View file

@ -31,6 +31,7 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams;
import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator;
import org.elasticsearch.search.profile.InternalProfileShardResult;
import org.elasticsearch.search.profile.InternalProfileShardResults;
import org.elasticsearch.search.suggest.Suggest;
import java.io.IOException;
@ -214,7 +215,12 @@ public class QuerySearchResult extends QuerySearchResultProvider {
// nocommit TODO need version check here?
if (in.readBoolean()) {
profileShardResults = InternalProfileShardResult.readProfileShardResults(in);
int profileSize = in.readVInt();
profileShardResults = new ArrayList<>(profileSize);
for (int i = 0; i < profileSize; i++) {
InternalProfileShardResult result = InternalProfileShardResult.readProfileShardResult(in);
profileShardResults.add(result);
}
}
}
@ -260,7 +266,10 @@ public class QuerySearchResult extends QuerySearchResultProvider {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
InternalProfileShardResult.writeProfileShardResults(profileShardResults, out);
out.writeVInt(profileShardResults.size());
for (InternalProfileShardResult shardResult : profileShardResults) {
shardResult.writeTo(out);
}
}
}
}