Search in sources :

Example 81 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class MultiGetShardRequestTests method testSerialization.

public void testSerialization() throws IOException {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    if (randomBoolean()) {
        multiGetRequest.preference(randomAlphaOfLength(randomIntBetween(1, 10)));
    }
    if (randomBoolean()) {
        multiGetRequest.realtime(false);
    }
    if (randomBoolean()) {
        multiGetRequest.refresh(true);
    }
    MultiGetShardRequest multiGetShardRequest = new MultiGetShardRequest(multiGetRequest, "index", 0);
    int numItems = iterations(10, 30);
    for (int i = 0; i < numItems; i++) {
        MultiGetRequest.Item item = new MultiGetRequest.Item("alias-" + randomAlphaOfLength(randomIntBetween(1, 10)), "id-" + i);
        if (randomBoolean()) {
            int numFields = randomIntBetween(1, 5);
            String[] fields = new String[numFields];
            for (int j = 0; j < fields.length; j++) {
                fields[j] = randomAlphaOfLength(randomIntBetween(1, 10));
            }
            item.storedFields(fields);
        }
        if (randomBoolean()) {
            item.version(randomIntBetween(1, Integer.MAX_VALUE));
            item.versionType(randomFrom(VersionType.values()));
        }
        if (randomBoolean()) {
            item.fetchSourceContext(new FetchSourceContext(randomBoolean()));
        }
        multiGetShardRequest.add(0, item);
    }
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(randomVersion(random()));
    multiGetShardRequest.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(out.getVersion());
    MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest(in);
    assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index()));
    assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference()));
    assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime()));
    assertThat(multiGetShardRequest2.refresh(), equalTo(multiGetShardRequest.refresh()));
    assertThat(multiGetShardRequest2.items.size(), equalTo(multiGetShardRequest.items.size()));
    for (int i = 0; i < multiGetShardRequest2.items.size(); i++) {
        MultiGetRequest.Item item = multiGetShardRequest.items.get(i);
        MultiGetRequest.Item item2 = multiGetShardRequest2.items.get(i);
        assertThat(item2.index(), equalTo(item.index()));
        assertThat(item2.id(), equalTo(item.id()));
        assertThat(item2.storedFields(), equalTo(item.storedFields()));
        assertThat(item2.version(), equalTo(item.version()));
        assertThat(item2.versionType(), equalTo(item.versionType()));
        assertThat(item2.fetchSourceContext(), equalTo(item.fetchSourceContext()));
    }
    assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices()));
    assertThat(multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions()));
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 82 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class PutPipelineRequestTests method testSerializationWithXContent.

public void testSerializationWithXContent() throws IOException {
    PutPipelineRequest request = new PutPipelineRequest("1", new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON);
    assertEquals(XContentType.JSON, request.getXContentType());
    BytesStreamOutput output = new BytesStreamOutput();
    request.writeTo(output);
    StreamInput in = StreamInput.wrap(output.bytes().toBytesRef().bytes);
    PutPipelineRequest serialized = new PutPipelineRequest(in);
    assertEquals(XContentType.JSON, serialized.getXContentType());
    assertEquals("{}", serialized.getSource().utf8ToString());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 83 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class DiscoveryNodeTests method testDiscoveryNodeSerializationKeepsHost.

public void testDiscoveryNodeSerializationKeepsHost() throws Exception {
    InetAddress inetAddress = InetAddress.getByAddress("name1", new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 });
    TransportAddress transportAddress = new TransportAddress(inetAddress, randomIntBetween(0, 65535));
    DiscoveryNode node = new DiscoveryNode("name1", "id1", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
    BytesStreamOutput streamOutput = new BytesStreamOutput();
    streamOutput.setVersion(Version.CURRENT);
    node.writeTo(streamOutput);
    StreamInput in = StreamInput.wrap(streamOutput.bytes().toBytesRef().bytes);
    DiscoveryNode serialized = new DiscoveryNode(in);
    assertEquals(transportAddress.address().getHostString(), serialized.getHostName());
    assertEquals(transportAddress.address().getHostString(), serialized.getAddress().address().getHostString());
    assertEquals(transportAddress.getAddress(), serialized.getHostAddress());
    assertEquals(transportAddress.getAddress(), serialized.getAddress().getAddress());
    assertEquals(transportAddress.getPort(), serialized.getAddress().getPort());
}
Also used : TransportAddress(org.opensearch.common.transport.TransportAddress) StreamInput(org.opensearch.common.io.stream.StreamInput) InetAddress(java.net.InetAddress) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 84 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class GeometryIOTests method testRandomSerialization.

public void testRandomSerialization() throws Exception {
    for (int i = 0; i < randomIntBetween(1, 20); i++) {
        boolean hasAlt = randomBoolean();
        Geometry geometry = randomGeometry(hasAlt);
        if (shapeSupported(geometry) && randomBoolean()) {
            // Shape builder conversion doesn't support altitude
            ShapeBuilder<?, ?, ?> shapeBuilder = geometryToShapeBuilder(geometry);
            if (randomBoolean()) {
                Geometry actual = shapeBuilder.buildGeometry();
                assertEquals(geometry, actual);
            }
            if (randomBoolean()) {
                // Test ShapeBuilder -> Geometry Serialization
                try (BytesStreamOutput out = new BytesStreamOutput()) {
                    out.writeNamedWriteable(shapeBuilder);
                    try (StreamInput in = out.bytes().streamInput()) {
                        Geometry actual = GeometryIO.readGeometry(in);
                        assertEquals(geometry, actual);
                        assertEquals(0, in.available());
                    }
                }
            } else {
                // Test Geometry -> ShapeBuilder Serialization
                try (BytesStreamOutput out = new BytesStreamOutput()) {
                    GeometryIO.writeGeometry(out, geometry);
                    try (StreamInput in = out.bytes().streamInput()) {
                        try (StreamInput nin = new NamedWriteableAwareStreamInput(in, this.writableRegistry())) {
                            ShapeBuilder<?, ?, ?> actual = nin.readNamedWriteable(ShapeBuilder.class);
                            assertEquals(shapeBuilder, actual);
                            assertEquals(0, in.available());
                        }
                    }
                }
            }
            // Test Geometry -> Geometry
            try (BytesStreamOutput out = new BytesStreamOutput()) {
                GeometryIO.writeGeometry(out, geometry);
                ;
                try (StreamInput in = out.bytes().streamInput()) {
                    Geometry actual = GeometryIO.readGeometry(in);
                    assertEquals(geometry, actual);
                    assertEquals(0, in.available());
                }
            }
        }
    }
}
Also used : GeometryTestUtils.randomGeometry(org.opensearch.geo.GeometryTestUtils.randomGeometry) Geometry(org.opensearch.geometry.Geometry) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 85 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class FieldMemoryStatsTests method testSerialize.

public void testSerialize() throws IOException {
    FieldMemoryStats stats = randomFieldMemoryStats();
    BytesStreamOutput out = new BytesStreamOutput();
    stats.writeTo(out);
    StreamInput input = out.bytes().streamInput();
    FieldMemoryStats read = new FieldMemoryStats(input);
    assertEquals(-1, input.read());
    assertEquals(stats, read);
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Aggregations

StreamInput (org.opensearch.common.io.stream.StreamInput)266 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)179 IOException (java.io.IOException)48 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)43 Version (org.opensearch.Version)30 Test (org.junit.Test)27 OpenSearchException (org.opensearch.OpenSearchException)22 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)19 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)19 Matchers.containsString (org.hamcrest.Matchers.containsString)18 BytesReference (org.opensearch.common.bytes.BytesReference)18 HashMap (java.util.HashMap)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)17 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)17 UncheckedIOException (java.io.UncheckedIOException)15 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 TransportException (org.opensearch.transport.TransportException)14 ArrayList (java.util.ArrayList)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13