Search in sources :

Example 41 with BytesStreamOutput

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

the class ExceptionSerializationTests method serialize.

private <T extends Exception> T serialize(T exception, Version version) throws IOException {
    ElasticsearchAssertions.assertVersionSerializable(version, exception);
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(version);
    out.writeException(exception);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(version);
    return in.readException();
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 42 with BytesStreamOutput

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

the class ExplainRequestTests method testSerialize50Request.

// BWC test for changes from #20916
public void testSerialize50Request() throws IOException {
    ExplainRequest request = new ExplainRequest("index", "type", "id");
    request.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1.*" }, new String[] { "field2.*" }));
    request.filteringAlias(new AliasFilter(QueryBuilders.termQuery("filter_field", "value"), new String[] { "alias0", "alias1" }));
    request.preference("the_preference");
    request.query(QueryBuilders.termQuery("field", "value"));
    request.storedFields(new String[] { "field1", "field2" });
    request.routing("some_routing");
    BytesArray requestBytes = new BytesArray(Base64.getDecoder().decode("AAABBWluZGV4BHR5cGUCaWQBDHNvbWVfcm91dGluZwEOdGhlX3ByZWZlcmVuY2UEdGVybT" + "+AAAAABWZpZWxkFQV2YWx1ZQIGYWxpYXMwBmFsaWFzMQECBmZpZWxkMQZmaWVsZDIBAQEIZmllbGQxLioBCGZpZWxkMi4qAA"));
    try (StreamInput in = new NamedWriteableAwareStreamInput(requestBytes.streamInput(), namedWriteableRegistry)) {
        in.setVersion(Version.V_5_0_0);
        ExplainRequest readRequest = new ExplainRequest();
        readRequest.readFrom(in);
        assertEquals(0, in.available());
        assertArrayEquals(request.filteringAlias().getAliases(), readRequest.filteringAlias().getAliases());
        expectThrows(IllegalStateException.class, () -> readRequest.filteringAlias().getQueryBuilder());
        assertArrayEquals(request.storedFields(), readRequest.storedFields());
        assertEquals(request.preference(), readRequest.preference());
        assertEquals(request.query(), readRequest.query());
        assertEquals(request.routing(), readRequest.routing());
        assertEquals(request.fetchSourceContext(), readRequest.fetchSourceContext());
        BytesStreamOutput output = new BytesStreamOutput();
        output.setVersion(Version.V_5_0_0);
        readRequest.writeTo(output);
        assertEquals(output.bytes().toBytesRef(), requestBytes.toBytesRef());
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) BytesArray(org.elasticsearch.common.bytes.BytesArray) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 43 with BytesStreamOutput

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

the class BulkRequestTests method testSmileIsSupported.

public void testSmileIsSupported() throws IOException {
    XContentType xContentType = XContentType.SMILE;
    BytesReference data;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.startObject("index");
            builder.field("_index", "index");
            builder.field("_type", "type");
            builder.field("_id", "test");
            builder.endObject();
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.field("field", "value");
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        data = out.bytes();
    }
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(data, null, null, xContentType);
    assertEquals(1, bulkRequest.requests().size());
    DocWriteRequest docWriteRequest = bulkRequest.requests().get(0);
    assertEquals(DocWriteRequest.OpType.INDEX, docWriteRequest.opType());
    assertEquals("index", docWriteRequest.index());
    assertEquals("type", docWriteRequest.type());
    assertEquals("test", docWriteRequest.id());
    assertThat(docWriteRequest, instanceOf(IndexRequest.class));
    IndexRequest request = (IndexRequest) docWriteRequest;
    assertEquals(1, request.sourceAsMap().size());
    assertEquals("value", request.sourceAsMap().get("field"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 44 with BytesStreamOutput

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

the class PutIndexTemplateRequestTests method testPutIndexTemplateRequestSerializationXContent.

public void testPutIndexTemplateRequestSerializationXContent() throws IOException {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest("foo");
    String mapping = YamlXContent.contentBuilder().startObject().field("foo", "bar").endObject().string();
    request.patterns(Collections.singletonList("foo"));
    request.mapping("bar", mapping, XContentType.YAML);
    assertNotEquals(mapping, request.mappings().get("bar"));
    assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), request.mappings().get("bar"));
    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);
    StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
    PutIndexTemplateRequest serialized = new PutIndexTemplateRequest();
    serialized.readFrom(in);
    assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), serialized.mappings().get("bar"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 45 with BytesStreamOutput

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

the class GetFieldMappingsResponseTests method testSerialization.

public void testSerialization() throws IOException {
    Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings = new HashMap<>();
    FieldMappingMetaData fieldMappingMetaData = new FieldMappingMetaData("my field", new BytesArray("{}"));
    mappings.put("index", Collections.singletonMap("type", Collections.singletonMap("field", fieldMappingMetaData)));
    GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        response.writeTo(out);
        GetFieldMappingsResponse serialized = new GetFieldMappingsResponse();
        try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) {
            serialized.readFrom(in);
            FieldMappingMetaData metaData = serialized.fieldMappings("index", "type", "field");
            assertNotNull(metaData);
            assertEquals(new BytesArray("{}"), metaData.getSource());
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) HashMap(java.util.HashMap) 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