Search in sources :

Example 21 with NamedWriteableAwareStreamInput

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

the class ShardValidateQueryRequestTests method testSerialize.

public void testSerialize() throws IOException {
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest("indices");
        validateQueryRequest.query(QueryBuilders.termQuery("field", "value"));
        validateQueryRequest.rewrite(true);
        validateQueryRequest.explain(false);
        validateQueryRequest.types("type1", "type2");
        ShardValidateQueryRequest request = new ShardValidateQueryRequest(new ShardId("index", "foobar", 1), new AliasFilter(QueryBuilders.termQuery("filter_field", "value"), new String[] { "alias0", "alias1" }), validateQueryRequest);
        request.writeTo(output);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
            ShardValidateQueryRequest readRequest = new ShardValidateQueryRequest();
            readRequest.readFrom(in);
            assertEquals(request.filteringAliases(), readRequest.filteringAliases());
            assertArrayEquals(request.types(), readRequest.types());
            assertEquals(request.explain(), readRequest.explain());
            assertEquals(request.query(), readRequest.query());
            assertEquals(request.rewrite(), readRequest.rewrite());
            assertEquals(request.shardId(), readRequest.shardId());
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ShardValidateQueryRequest) AliasFilter(org.elasticsearch.search.internal.AliasFilter) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) ShardValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ShardValidateQueryRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 22 with NamedWriteableAwareStreamInput

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

the class ExplainRequestTests method testSerialize.

public void testSerialize() throws IOException {
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        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");
        request.writeTo(output);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
            ExplainRequest readRequest = new ExplainRequest();
            readRequest.readFrom(in);
            assertEquals(request.filteringAlias(), readRequest.filteringAlias());
            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());
        }
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) 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 23 with NamedWriteableAwareStreamInput

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

the class MetaDataTests method testSerializationWithIndexGraveyard.

public void testSerializationWithIndexGraveyard() throws IOException {
    final IndexGraveyard graveyard = IndexGraveyardTests.createRandom();
    final MetaData originalMeta = MetaData.builder().indexGraveyard(graveyard).build();
    final BytesStreamOutput out = new BytesStreamOutput();
    originalMeta.writeTo(out);
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
    final MetaData fromStreamMeta = MetaData.readFrom(new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry));
    assertThat(fromStreamMeta.indexGraveyard(), equalTo(fromStreamMeta.indexGraveyard()));
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 24 with NamedWriteableAwareStreamInput

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

the class DocValueFormatTests method testSerialization.

public void testSerialization() throws Exception {
    List<Entry> entries = new ArrayList<>();
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.BOOLEAN.getWriteableName(), in -> DocValueFormat.BOOLEAN));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::new));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::new));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.GEOHASH.getWriteableName(), in -> DocValueFormat.GEOHASH));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.IP.getWriteableName(), in -> DocValueFormat.IP));
    entries.add(new Entry(DocValueFormat.class, DocValueFormat.RAW.getWriteableName(), in -> DocValueFormat.RAW));
    NamedWriteableRegistry registry = new NamedWriteableRegistry(entries);
    BytesStreamOutput out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.BOOLEAN);
    StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.BOOLEAN, in.readNamedWriteable(DocValueFormat.class));
    DocValueFormat.Decimal decimalFormat = new DocValueFormat.Decimal("###.##");
    out = new BytesStreamOutput();
    out.writeNamedWriteable(decimalFormat);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    DocValueFormat vf = in.readNamedWriteable(DocValueFormat.class);
    assertEquals(DocValueFormat.Decimal.class, vf.getClass());
    assertEquals("###.##", ((DocValueFormat.Decimal) vf).pattern);
    DocValueFormat.DateTime dateFormat = new DocValueFormat.DateTime(Joda.forPattern("epoch_second"), DateTimeZone.forOffsetHours(1));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(dateFormat);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    vf = in.readNamedWriteable(DocValueFormat.class);
    assertEquals(DocValueFormat.DateTime.class, vf.getClass());
    assertEquals("epoch_second", ((DocValueFormat.DateTime) vf).formatter.format());
    assertEquals(DateTimeZone.forOffsetHours(1), ((DocValueFormat.DateTime) vf).timeZone);
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.GEOHASH);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.GEOHASH, in.readNamedWriteable(DocValueFormat.class));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.IP);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.IP, in.readNamedWriteable(DocValueFormat.class));
    out = new BytesStreamOutput();
    out.writeNamedWriteable(DocValueFormat.RAW);
    in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry);
    assertSame(DocValueFormat.RAW, in.readNamedWriteable(DocValueFormat.class));
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Joda(org.elasticsearch.common.joda.Joda) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) BytesRef(org.apache.lucene.util.BytesRef) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ArrayList(java.util.ArrayList) InetAddresses(org.elasticsearch.common.network.InetAddresses) List(java.util.List) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ESTestCase(org.elasticsearch.test.ESTestCase) Entry(org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Entry(org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)

Example 25 with NamedWriteableAwareStreamInput

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

the class TribeService method marshal.

/**
     * Since custom metadata can be loaded by a plugin class loader that resides in a sub-node, we need to
     * marshal this object into something the tribe node can work with
     */
private MetaData.Custom marshal(MetaData.Custom custom) {
    try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) {
        bytesStreamOutput.writeNamedWriteable(custom);
        try (StreamInput input = bytesStreamOutput.bytes().streamInput()) {
            StreamInput namedInput = new NamedWriteableAwareStreamInput(input, namedWriteableRegistry);
            MetaData.Custom marshaled = namedInput.readNamedWriteable(MetaData.Custom.class);
            return marshaled;
        }
    } catch (IOException ex) {
        throw new IllegalStateException("cannot marshal object with type " + custom.getWriteableName() + " to tribe node");
    }
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)28 StreamInput (org.elasticsearch.common.io.stream.StreamInput)27 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)23 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)9 AliasFilter (org.elasticsearch.search.internal.AliasFilter)5 IOException (java.io.IOException)4 BytesArray (org.elasticsearch.common.bytes.BytesArray)4 Version (org.elasticsearch.Version)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 ShardId (org.elasticsearch.index.shard.ShardId)3 ArrayList (java.util.ArrayList)2 ShardValidateQueryRequest (org.elasticsearch.action.admin.indices.validate.query.ShardValidateQueryRequest)2 ValidateQueryRequest (org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest)2 ExplainRequest (org.elasticsearch.action.explain.ExplainRequest)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 AllocateEmptyPrimaryAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 SearchModule (org.elasticsearch.search.SearchModule)2