Search in sources :

Example 1 with StreamInput

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

the class AbstractBytesReferenceTestCase method testSliceStreamInput.

public void testSliceStreamInput() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    // test stream input over slice (upper half of original)
    int sliceOffset = randomIntBetween(1, length / 2);
    int sliceLength = length - sliceOffset;
    BytesReference slice = pbr.slice(sliceOffset, sliceLength);
    StreamInput sliceInput = slice.streamInput();
    assertEquals(sliceInput.available(), sliceLength);
    // single reads
    assertEquals(slice.get(0), sliceInput.readByte());
    assertEquals(slice.get(1), sliceInput.readByte());
    assertEquals(slice.get(2), sliceInput.readByte());
    assertEquals(sliceInput.available(), sliceLength - 3);
    // reset the slice stream for bulk reading
    sliceInput.reset();
    assertEquals(sliceInput.available(), sliceLength);
    // bulk read
    byte[] sliceBytes = new byte[sliceLength];
    sliceInput.readFully(sliceBytes);
    assertEquals(sliceInput.available(), 0);
    // compare slice content with upper half of original
    byte[] pbrSliceBytes = Arrays.copyOfRange(BytesReference.toBytes(pbr), sliceOffset, length);
    assertArrayEquals(pbrSliceBytes, sliceBytes);
    // compare slice bytes with bytes read from slice via streamInput :D
    byte[] sliceToBytes = BytesReference.toBytes(slice);
    assertEquals(sliceBytes.length, sliceToBytes.length);
    assertArrayEquals(sliceBytes, sliceToBytes);
    sliceInput.reset();
    assertEquals(sliceInput.available(), sliceLength);
    byte[] buffer = new byte[sliceLength + scaledRandomIntBetween(1, 100)];
    int offset = scaledRandomIntBetween(0, Math.max(1, buffer.length - sliceLength - 1));
    int read = sliceInput.read(buffer, offset, sliceLength / 2);
    assertEquals(sliceInput.available(), sliceLength - read);
    sliceInput.read(buffer, offset + read, sliceLength - read);
    assertArrayEquals(sliceBytes, Arrays.copyOfRange(buffer, offset, offset + sliceLength));
    assertEquals(sliceInput.available(), 0);
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput)

Example 2 with StreamInput

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

the class AbstractBytesReferenceTestCase method testStreamInputBulkReadWithOffset.

public void testStreamInputBulkReadWithOffset() throws IOException {
    final int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput si = pbr.streamInput();
    assertNotNull(si);
    // read a bunch of single bytes one by one
    int offset = randomIntBetween(1, length / 2);
    for (int i = 0; i < offset; i++) {
        assertEquals(si.available(), length - i);
        assertEquals(pbr.get(i), si.readByte());
    }
    // now do NOT reset the stream - keep the stream's offset!
    // buffer to compare remaining bytes against bulk read
    byte[] pbrBytesWithOffset = Arrays.copyOfRange(BytesReference.toBytes(pbr), offset, length);
    // randomized target buffer to ensure no stale slots
    byte[] targetBytes = new byte[pbrBytesWithOffset.length];
    random().nextBytes(targetBytes);
    // bulk-read all
    si.readFully(targetBytes);
    assertArrayEquals(pbrBytesWithOffset, targetBytes);
    assertEquals(si.available(), 0);
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput)

Example 3 with StreamInput

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

the class AbstractBytesReferenceTestCase method testStreamInputMarkAndReset.

public void testStreamInputMarkAndReset() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput si = pbr.streamInput();
    assertNotNull(si);
    StreamInput wrap = StreamInput.wrap(BytesReference.toBytes(pbr));
    while (wrap.available() > 0) {
        if (rarely()) {
            wrap.mark(Integer.MAX_VALUE);
            si.mark(Integer.MAX_VALUE);
        } else if (rarely()) {
            wrap.reset();
            si.reset();
        }
        assertEquals(si.readByte(), wrap.readByte());
        assertEquals(si.available(), wrap.available());
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput)

Example 4 with StreamInput

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

the class AbstractBytesReferenceTestCase method testRandomReads.

public void testRandomReads() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput streamInput = pbr.streamInput();
    BytesRefBuilder target = new BytesRefBuilder();
    while (target.length() < pbr.length()) {
        switch(randomIntBetween(0, 10)) {
            case 6:
            case 5:
                target.append(new BytesRef(new byte[] { streamInput.readByte() }));
                break;
            case 4:
            case 3:
                BytesRef bytesRef = streamInput.readBytesRef(scaledRandomIntBetween(1, pbr.length() - target.length()));
                target.append(bytesRef);
                break;
            default:
                byte[] buffer = new byte[scaledRandomIntBetween(1, pbr.length() - target.length())];
                int offset = scaledRandomIntBetween(0, buffer.length - 1);
                int read = streamInput.read(buffer, offset, buffer.length - offset);
                target.append(new BytesRef(buffer, offset, read));
                break;
        }
    }
    assertEquals(pbr.length(), target.length());
    BytesRef targetBytes = target.get();
    assertArrayEquals(BytesReference.toBytes(pbr), Arrays.copyOfRange(targetBytes.bytes, targetBytes.offset, targetBytes.length));
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with StreamInput

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

the class ClusterSearchShardsRequestTests method testSerialization.

public void testSerialization() throws Exception {
    ClusterSearchShardsRequest request = new ClusterSearchShardsRequest();
    if (randomBoolean()) {
        int numIndices = randomIntBetween(1, 5);
        String[] indices = new String[numIndices];
        for (int i = 0; i < numIndices; i++) {
            indices[i] = randomAsciiOfLengthBetween(3, 10);
        }
        request.indices(indices);
    }
    if (randomBoolean()) {
        request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()));
    }
    if (randomBoolean()) {
        request.preference(randomAsciiOfLengthBetween(3, 10));
    }
    if (randomBoolean()) {
        int numRoutings = randomIntBetween(1, 3);
        String[] routings = new String[numRoutings];
        for (int i = 0; i < numRoutings; i++) {
            routings[i] = randomAsciiOfLengthBetween(3, 10);
        }
        request.routing(routings);
    }
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.CURRENT);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.setVersion(version);
        request.writeTo(out);
        try (StreamInput in = out.bytes().streamInput()) {
            in.setVersion(version);
            ClusterSearchShardsRequest deserialized = new ClusterSearchShardsRequest();
            deserialized.readFrom(in);
            assertArrayEquals(request.indices(), deserialized.indices());
            assertSame(request.indicesOptions(), deserialized.indicesOptions());
            assertEquals(request.routing(), deserialized.routing());
            assertEquals(request.preference(), deserialized.preference());
        }
    }
}
Also used : Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

StreamInput (org.elasticsearch.common.io.stream.StreamInput)287 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)224 Test (org.junit.Test)124 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)38 Version (org.elasticsearch.Version)29 IOException (java.io.IOException)28 BytesArray (org.elasticsearch.common.bytes.BytesArray)25 CrateUnitTest (io.crate.test.integration.CrateUnitTest)17 UUID (java.util.UUID)16 BytesReference (org.elasticsearch.common.bytes.BytesReference)15 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 TransportException (org.elasticsearch.transport.TransportException)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ShardId (org.elasticsearch.index.shard.ShardId)12 InputColumn (io.crate.expression.symbol.InputColumn)11 Symbol (io.crate.expression.symbol.Symbol)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)9 Map (java.util.Map)8