use of org.elasticsearch.common.io.stream.InputStreamStreamInput in project elasticsearch by elastic.
the class ScriptTests method testScriptSerialization.
public void testScriptSerialization() throws IOException {
Script expectedScript = createScript();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
expectedScript.writeTo(new OutputStreamStreamOutput(out));
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
Script actualScript = new Script(new InputStreamStreamInput(in));
assertThat(actualScript, equalTo(expectedScript));
}
}
}
use of org.elasticsearch.common.io.stream.InputStreamStreamInput in project elasticsearch by elastic.
the class SearchHitTests method testSerializeShardTarget.
public void testSerializeShardTarget() throws Exception {
SearchShardTarget target = new SearchShardTarget("_node_id", new Index("_index", "_na_"), 0);
Map<String, SearchHits> innerHits = new HashMap<>();
SearchHit innerHit1 = new SearchHit(0, "_id", new Text("_type"), null);
innerHit1.shard(target);
SearchHit innerInnerHit2 = new SearchHit(0, "_id", new Text("_type"), null);
innerInnerHit2.shard(target);
innerHits.put("1", new SearchHits(new SearchHit[] { innerInnerHit2 }, 1, 1f));
innerHit1.setInnerHits(innerHits);
SearchHit innerHit2 = new SearchHit(0, "_id", new Text("_type"), null);
innerHit2.shard(target);
SearchHit innerHit3 = new SearchHit(0, "_id", new Text("_type"), null);
innerHit3.shard(target);
innerHits = new HashMap<>();
SearchHit hit1 = new SearchHit(0, "_id", new Text("_type"), null);
innerHits.put("1", new SearchHits(new SearchHit[] { innerHit1, innerHit2 }, 1, 1f));
innerHits.put("2", new SearchHits(new SearchHit[] { innerHit3 }, 1, 1f));
hit1.shard(target);
hit1.setInnerHits(innerHits);
SearchHit hit2 = new SearchHit(0, "_id", new Text("_type"), null);
hit2.shard(target);
SearchHits hits = new SearchHits(new SearchHit[] { hit1, hit2 }, 2, 1f);
BytesStreamOutput output = new BytesStreamOutput();
hits.writeTo(output);
InputStream input = output.bytes().streamInput();
SearchHits results = SearchHits.readSearchHits(new InputStreamStreamInput(input));
assertThat(results.getAt(0).getShard(), equalTo(target));
assertThat(results.getAt(0).getInnerHits().get("1").getAt(0).getShard(), notNullValue());
assertThat(results.getAt(0).getInnerHits().get("1").getAt(0).getInnerHits().get("1").getAt(0).getShard(), notNullValue());
assertThat(results.getAt(0).getInnerHits().get("1").getAt(1).getShard(), notNullValue());
assertThat(results.getAt(0).getInnerHits().get("2").getAt(0).getShard(), notNullValue());
assertThat(results.getAt(1).getShard(), equalTo(target));
}
use of org.elasticsearch.common.io.stream.InputStreamStreamInput in project elasticsearch by elastic.
the class ExplainActionIT method testStreamExplain.
public void testStreamExplain() throws Exception {
Explanation exp = Explanation.match(2f, "some explanation");
// write
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
Lucene.writeExplanation(out, exp);
// read
ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer);
Explanation result = Lucene.readExplanation(esBuffer);
assertThat(exp.toString(), equalTo(result.toString()));
exp = Explanation.match(2.0f, "some explanation", Explanation.match(2.0f, "another explanation"));
// write complex
outBuffer = new ByteArrayOutputStream();
out = new OutputStreamStreamOutput(outBuffer);
Lucene.writeExplanation(out, exp);
// read complex
esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
esBuffer = new InputStreamStreamInput(esInBuffer);
result = Lucene.readExplanation(esBuffer);
assertThat(exp.toString(), equalTo(result.toString()));
}
use of org.elasticsearch.common.io.stream.InputStreamStreamInput in project elasticsearch by elastic.
the class TermVectorsUnitTests method testStreamRequest.
public void testStreamRequest() throws IOException {
for (int i = 0; i < 10; i++) {
TermVectorsRequest request = new TermVectorsRequest("index", "type", "id");
request.offsets(random().nextBoolean());
request.fieldStatistics(random().nextBoolean());
request.payloads(random().nextBoolean());
request.positions(random().nextBoolean());
request.termStatistics(random().nextBoolean());
String parent = random().nextBoolean() ? "someParent" : null;
request.parent(parent);
String pref = random().nextBoolean() ? "somePreference" : null;
request.preference(pref);
request.doc(new BytesArray("{}"), randomBoolean(), XContentType.JSON);
// write
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
request.writeTo(out);
// read
ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer);
TermVectorsRequest req2 = new TermVectorsRequest(null, null, null);
req2.readFrom(esBuffer);
assertThat(request.offsets(), equalTo(req2.offsets()));
assertThat(request.fieldStatistics(), equalTo(req2.fieldStatistics()));
assertThat(request.payloads(), equalTo(req2.payloads()));
assertThat(request.positions(), equalTo(req2.positions()));
assertThat(request.termStatistics(), equalTo(req2.termStatistics()));
assertThat(request.preference(), equalTo(pref));
assertThat(request.routing(), equalTo(null));
assertEquals(new BytesArray("{}"), request.doc());
assertEquals(XContentType.JSON, request.xContentType());
}
}
use of org.elasticsearch.common.io.stream.InputStreamStreamInput in project elasticsearch by elastic.
the class StartRecoveryRequestTests method testSerialization.
public void testSerialization() throws Exception {
final Version targetNodeVersion = randomVersion(random());
final StartRecoveryRequest outRequest = new StartRecoveryRequest(new ShardId("test", "_na_", 0), new DiscoveryNode("a", buildNewFakeTransportAddress(), emptyMap(), emptySet(), targetNodeVersion), new DiscoveryNode("b", buildNewFakeTransportAddress(), emptyMap(), emptySet(), targetNodeVersion), Store.MetadataSnapshot.EMPTY, randomBoolean(), randomNonNegativeLong(), randomBoolean() ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomNonNegativeLong());
final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
final OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
out.setVersion(targetNodeVersion);
outRequest.writeTo(out);
final ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
in.setVersion(targetNodeVersion);
final StartRecoveryRequest inRequest = new StartRecoveryRequest();
inRequest.readFrom(in);
assertThat(outRequest.shardId(), equalTo(inRequest.shardId()));
assertThat(outRequest.sourceNode(), equalTo(inRequest.sourceNode()));
assertThat(outRequest.targetNode(), equalTo(inRequest.targetNode()));
assertThat(outRequest.metadataSnapshot().asMap(), equalTo(inRequest.metadataSnapshot().asMap()));
assertThat(outRequest.isPrimaryRelocation(), equalTo(inRequest.isPrimaryRelocation()));
assertThat(outRequest.recoveryId(), equalTo(inRequest.recoveryId()));
if (targetNodeVersion.onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)) {
assertThat(outRequest.startingSeqNo(), equalTo(inRequest.startingSeqNo()));
} else {
assertThat(SequenceNumbersService.UNASSIGNED_SEQ_NO, equalTo(inRequest.startingSeqNo()));
}
}
Aggregations