Search in sources :

Example 21 with BytesStreamOutput

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

the class DistributionInfoTest method testStreamingDefaultImpl.

@Test
public void testStreamingDefaultImpl() throws Exception {
    DistributionInfo distributionInfo = DistributionInfo.DEFAULT_BROADCAST;
    BytesStreamOutput out = new BytesStreamOutput(10);
    distributionInfo.writeTo(out);
    StreamInput in = StreamInput.wrap(out.bytes());
    DistributionInfo streamed = DistributionInfo.fromStream(in);
    assertThat(streamed, equalTo(distributionInfo));
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 22 with BytesStreamOutput

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

the class ArrayMapper method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    /*
         * array mapping should look like:
         *
         * "fieldName": {
         *      "type": "array":
         *      "inner": {
         *          "type": "string"
         *          ...
         *      }
         * }
         *
         *
         * Use the innerMapper to generate the mapping for the inner type which will look like:
         *
         * "fieldName": {
         *      "type": "string",
         *      ...
         * }
         *
         * and then parse the contents of the object to set it into the "inner" field of the outer array type.
         */
    XContentBuilder innerBuilder = new XContentBuilder(builder.contentType().xContent(), new BytesStreamOutput(0));
    innerBuilder.startObject();
    innerBuilder = innerMapper.toXContent(innerBuilder, params);
    innerBuilder.endObject();
    innerBuilder.close();
    XContentParser parser = builder.contentType().xContent().createParser(innerBuilder.bytes());
    //noinspection StatementWithEmptyBody
    while ((parser.nextToken() != XContentParser.Token.START_OBJECT)) {
    // consume tokens until start of object
    }
    //noinspection unchecked
    Map<String, Object> innerMap = (Map<String, Object>) parser.mapOrdered().get(innerMapper.simpleName());
    assert innerMap != null : "innerMap was null";
    builder.startObject(simpleName());
    builder.field("type", contentType());
    builder.field(INNER, innerMap);
    return builder.endObject();
}
Also used : XContentBuilderString(org.elasticsearch.common.xcontent.XContentBuilderString) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 23 with BytesStreamOutput

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

the class JobRequestTest method testJobRequestStreaming.

@Test
public void testJobRequestStreaming() throws Exception {
    JobRequest r1 = new JobRequest(UUID.randomUUID(), "n1", Collections.<NodeOperation>emptyList());
    BytesStreamOutput out = new BytesStreamOutput();
    r1.writeTo(out);
    JobRequest r2 = new JobRequest();
    r2.readFrom(StreamInput.wrap(out.bytes()));
    assertThat(r1.coordinatorNodeId(), is(r2.coordinatorNodeId()));
    assertThat(r1.jobId(), is(r2.jobId()));
    assertThat(r1.nodeOperations().isEmpty(), is(true));
}
Also used : BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 24 with BytesStreamOutput

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

the class TDigestStateTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    TDigestState digestState1 = new TDigestState(250, new double[] { 0.5, 0.8 });
    BytesStreamOutput out = new BytesStreamOutput();
    TDigestStateType digestStateType = TDigestStateType.INSTANCE;
    Streamer streamer = digestStateType.create().streamer();
    streamer.writeValueTo(out, digestState1);
    StreamInput in = StreamInput.wrap(out.bytes());
    TDigestState digestState2 = (TDigestState) streamer.readValueFrom(in);
    assertEquals(digestState1.compression(), digestState2.compression(), 0.001d);
    assertEquals(digestState1.fractions()[0], digestState2.fractions()[0], 0.001d);
    assertEquals(digestState1.fractions()[1], digestState2.fractions()[1], 0.001d);
}
Also used : Streamer(io.crate.Streamer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 25 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)

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