Search in sources :

Example 96 with BytesStreamOutput

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

the class ShardUpsertRequestTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    ShardId shardId = new ShardId("test", 1);
    String[] assignmentColumns = new String[] { "id", "name" };
    UUID jobId = UUID.randomUUID();
    Reference[] missingAssignmentColumns = new Reference[] { ID_REF, NAME_REF };
    ShardUpsertRequest request = new ShardUpsertRequest.Builder(false, false, assignmentColumns, missingAssignmentColumns, jobId, false).newRequest(shardId, "42");
    request.validateConstraints(false);
    request.add(123, new ShardUpsertRequest.Item("99", null, new Object[] { 99, new BytesRef("Marvin") }, null));
    request.add(5, new ShardUpsertRequest.Item("42", new Symbol[] { Literal.of(42), Literal.of("Deep Thought") }, null, 2L));
    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);
    StreamInput in = StreamInput.wrap(out.bytes());
    ShardUpsertRequest request2 = new ShardUpsertRequest();
    request2.readFrom(in);
    assertThat(request, equalTo(request2));
}
Also used : Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ShardId(org.elasticsearch.index.shard.ShardId) StreamInput(org.elasticsearch.common.io.stream.StreamInput) UUID(java.util.UUID) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 97 with BytesStreamOutput

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

the class BaseAggregationTestCase method testSerialization.

/**
     * Test serialization and deserialization of the test AggregatorFactory.
     */
public void testSerialization() throws IOException {
    AB testAgg = createTestAggregatorBuilder();
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        output.writeNamedWriteable(testAgg);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
            AggregationBuilder deserialized = in.readNamedWriteable(AggregationBuilder.class);
            assertEquals(testAgg, deserialized);
            assertEquals(testAgg.hashCode(), deserialized.hashCode());
            assertNotSame(testAgg, deserialized);
        }
    }
}
Also used : 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 98 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 99 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 100 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)

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