Search in sources :

Example 26 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class BasePipelineAggregationTestCase method testSerialization.

/**
     * Test serialization and deserialization of the test AggregatorFactory.
     */
public void testSerialization() throws IOException {
    AF testAgg = createTestAggregatorFactory();
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        output.writeNamedWriteable(testAgg);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
            PipelineAggregationBuilder deserializedQuery = in.readNamedWriteable(PipelineAggregationBuilder.class);
            assertEquals(deserializedQuery, testAgg);
            assertEquals(deserializedQuery.hashCode(), testAgg.hashCode());
            assertNotSame(deserializedQuery, testAgg);
        }
    }
}
Also used : AbstractPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 27 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class SearchRequestTests method testSerialization.

public void testSerialization() throws Exception {
    SearchRequest searchRequest = createSearchRequest();
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        searchRequest.writeTo(output);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
            SearchRequest deserializedRequest = new SearchRequest();
            deserializedRequest.readFrom(in);
            assertEquals(deserializedRequest, searchRequest);
            assertEquals(deserializedRequest.hashCode(), searchRequest.hashCode());
            assertNotSame(deserializedRequest, searchRequest);
        }
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 28 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput 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 29 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class SearchSortValuesTests method testSerialization.

public void testSerialization() throws IOException {
    SearchSortValues sortValues = createTestItem();
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        sortValues.writeTo(output);
        try (StreamInput in = output.bytes().streamInput()) {
            SearchSortValues deserializedCopy = new SearchSortValues(in);
            assertEquals(sortValues, deserializedCopy);
            assertEquals(sortValues.hashCode(), deserializedCopy.hashCode());
            assertNotSame(sortValues, deserializedCopy);
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 30 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class ExtendedBoundsTests method testTransportRoundTrip.

public void testTransportRoundTrip() throws IOException {
    ExtendedBounds orig = randomExtendedBounds();
    BytesReference origBytes;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        orig.writeTo(out);
        origBytes = out.bytes();
    }
    ExtendedBounds read;
    try (StreamInput in = origBytes.streamInput()) {
        read = new ExtendedBounds(in);
        assertEquals("read fully", 0, in.available());
    }
    assertEquals(orig, read);
    BytesReference readBytes;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        read.writeTo(out);
        readBytes = out.bytes();
    }
    assertEquals(origBytes, readBytes);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)222 StreamInput (org.elasticsearch.common.io.stream.StreamInput)147 Test (org.junit.Test)45 CrateUnitTest (io.crate.test.integration.CrateUnitTest)36 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)24 IOException (java.io.IOException)21 BytesArray (org.elasticsearch.common.bytes.BytesArray)21 BytesReference (org.elasticsearch.common.bytes.BytesReference)18 Version (org.elasticsearch.Version)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ArrayList (java.util.ArrayList)12 BytesRef (org.apache.lucene.util.BytesRef)11 Map (java.util.Map)10 UUID (java.util.UUID)9 Symbol (io.crate.analyze.symbol.Symbol)8 HashMap (java.util.HashMap)8 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)7 List (java.util.List)6