Search in sources :

Example 6 with InputStreamStreamInput

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));
        }
    }
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 7 with InputStreamStreamInput

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));
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Index(org.elasticsearch.index.Index) Text(org.elasticsearch.common.text.Text) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 8 with InputStreamStreamInput

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()));
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Explanation(org.apache.lucene.search.Explanation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 9 with InputStreamStreamInput

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());
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 10 with InputStreamStreamInput

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()));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Aggregations

InputStreamStreamInput (org.elasticsearch.common.io.stream.InputStreamStreamInput)17 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)13 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)6 CrateUnitTest (io.crate.test.integration.CrateUnitTest)5 Test (org.junit.Test)5 ThreadPools (io.crate.monitor.ThreadPools)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Version (org.elasticsearch.Version)2 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 ShardId (org.elasticsearch.index.shard.ShardId)2 VersionUtils.randomVersion (org.elasticsearch.test.VersionUtils.randomVersion)2 BufferedInputStream (java.io.BufferedInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1