Search in sources :

Example 1 with ReleasableBytesStreamOutput

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

the class AbstractBytesReferenceTestCase method testCompareTo.

public void testCompareTo() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
        BytesReference bytesReference = newBytesReference(length);
        assertTrue(bytesReference.compareTo(new BytesArray("")) > 0);
        assertTrue(new BytesArray("").compareTo(bytesReference) < 0);
        assertEquals(0, bytesReference.compareTo(bytesReference));
        int sliceFrom = randomIntBetween(0, bytesReference.length());
        int sliceLength = randomIntBetween(0, bytesReference.length() - sliceFrom);
        BytesReference slice = bytesReference.slice(sliceFrom, sliceLength);
        assertEquals(bytesReference.toBytesRef().compareTo(slice.toBytesRef()), new BytesArray(bytesReference.toBytesRef(), true).compareTo(new BytesArray(slice.toBytesRef(), true)));
        assertEquals(bytesReference.toBytesRef().compareTo(slice.toBytesRef()), bytesReference.compareTo(slice));
        assertEquals(slice.toBytesRef().compareTo(bytesReference.toBytesRef()), slice.compareTo(bytesReference));
        assertEquals(0, slice.compareTo(new BytesArray(slice.toBytesRef())));
        assertEquals(0, new BytesArray(slice.toBytesRef()).compareTo(slice));
        final int crazyLength = length + randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
        ReleasableBytesStreamOutput crazyStream = new ReleasableBytesStreamOutput(length, bigarrays);
        final int offset = randomIntBetween(0, crazyLength - length);
        for (int j = 0; j < offset; j++) {
            crazyStream.writeByte((byte) random().nextInt(1 << 8));
        }
        bytesReference.writeTo(crazyStream);
        for (int j = crazyStream.size(); j < crazyLength; j++) {
            crazyStream.writeByte((byte) random().nextInt(1 << 8));
        }
        PagedBytesReference crazyReference = crazyStream.bytes();
        assertFalse(crazyReference.compareTo(bytesReference) == 0);
        assertEquals(0, crazyReference.slice(offset, length).compareTo(bytesReference));
        assertEquals(0, bytesReference.compareTo(crazyReference.slice(offset, length)));
    }
}
Also used : ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 2 with ReleasableBytesStreamOutput

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

the class TcpTransport method sendRequestToChannel.

private void sendRequestToChannel(DiscoveryNode node, final Channel targetChannel, final long requestId, final String action, final TransportRequest request, TransportRequestOptions options, Version channelVersion, byte status) throws IOException, TransportException {
    if (compress) {
        options = TransportRequestOptions.builder(options).withCompress(true).build();
    }
    status = TransportStatus.setRequest(status);
    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
    // we wrap this in a release once since if the onRequestSent callback throws an exception
    // we might release things twice and this should be prevented
    final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
    boolean addedReleaseListener = false;
    StreamOutput stream = bStream;
    try {
        // the header part is compressed, and the "body" can't be extracted as compressed
        if (options.compress() && canCompress(request)) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.COMPRESSOR.streamOutput(stream);
        }
        // we pick the smallest of the 2, to support both backward and forward compatibility
        // note, this is the only place we need to do this, since from here on, we use the serialized version
        // as the version to use also when the node receiving this request will send the response with
        Version version = Version.min(getCurrentVersion(), channelVersion);
        stream.setVersion(version);
        threadPool.getThreadContext().writeTo(stream);
        stream.writeString(action);
        BytesReference message = buildMessage(requestId, status, node.getVersion(), request, stream, bStream);
        final TransportRequestOptions finalOptions = options;
        Runnable onRequestSent = () -> {
            // this might be called in a different thread
            try {
                toRelease.close();
            } finally {
                transportServiceAdapter.onRequestSent(node, requestId, action, request, finalOptions);
            }
        };
        addedReleaseListener = internalSendMessage(targetChannel, message, onRequestSent);
    } finally {
        IOUtils.close(stream);
        if (!addedReleaseListener) {
            toRelease.close();
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) Version(org.elasticsearch.Version) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) AbstractLifecycleRunnable(org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) Releasable(org.elasticsearch.common.lease.Releasable) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 3 with ReleasableBytesStreamOutput

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

the class PagedBytesReferenceTests method newBytesReference.

protected BytesReference newBytesReference(int length) throws IOException {
    // we know bytes stream output always creates a paged bytes reference, we use it to create randomized content
    ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
    for (int i = 0; i < length; i++) {
        out.writeByte((byte) random().nextInt(1 << 8));
    }
    assertThat(out.size(), Matchers.equalTo(length));
    BytesReference ref = out.bytes();
    assertThat(ref.length(), Matchers.equalTo(length));
    assertThat(ref, Matchers.instanceOf(PagedBytesReference.class));
    return ref;
}
Also used : ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 4 with ReleasableBytesStreamOutput

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

the class AbstractBytesReferenceTestCase method testCompareTo.

public void testCompareTo() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
        BytesReference bytesReference = newBytesReference(length);
        assertTrue(bytesReference.compareTo(new BytesArray("")) > 0);
        assertTrue(new BytesArray("").compareTo(bytesReference) < 0);
        assertEquals(0, bytesReference.compareTo(bytesReference));
        int sliceFrom = randomIntBetween(0, bytesReference.length());
        int sliceLength = randomIntBetween(0, bytesReference.length() - sliceFrom);
        BytesReference slice = bytesReference.slice(sliceFrom, sliceLength);
        assertEquals(bytesReference.toBytesRef().compareTo(slice.toBytesRef()), new BytesArray(bytesReference.toBytesRef(), true).compareTo(new BytesArray(slice.toBytesRef(), true)));
        assertEquals(bytesReference.toBytesRef().compareTo(slice.toBytesRef()), bytesReference.compareTo(slice));
        assertEquals(slice.toBytesRef().compareTo(bytesReference.toBytesRef()), slice.compareTo(bytesReference));
        assertEquals(0, slice.compareTo(new BytesArray(slice.toBytesRef())));
        assertEquals(0, new BytesArray(slice.toBytesRef()).compareTo(slice));
        final int crazyLength = length + randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
        ReleasableBytesStreamOutput crazyStream = new ReleasableBytesStreamOutput(length, bigarrays);
        final int offset = randomIntBetween(0, crazyLength - length);
        for (int j = 0; j < offset; j++) {
            crazyStream.writeByte((byte) random().nextInt(1 << 8));
        }
        bytesReference.writeTo(crazyStream);
        for (int j = crazyStream.size(); j < crazyLength; j++) {
            crazyStream.writeByte((byte) random().nextInt(1 << 8));
        }
        BytesReference crazyReference = crazyStream.bytes();
        assertFalse(crazyReference.compareTo(bytesReference) == 0);
        assertEquals(0, crazyReference.slice(offset, length).compareTo(bytesReference));
        assertEquals(0, bytesReference.compareTo(crazyReference.slice(offset, length)));
    }
}
Also used : ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 5 with ReleasableBytesStreamOutput

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

the class Translog method writeOperations.

/**
 * Writes all operations in the given iterable to the given output stream including the size of the array
 * use {@link #readOperations(StreamInput, String)} to read it back.
 */
public static void writeOperations(StreamOutput outStream, List<Operation> toWrite) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE);
    try {
        outStream.writeInt(toWrite.size());
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        for (Operation op : toWrite) {
            out.reset();
            final long start = out.position();
            out.skip(Integer.BYTES);
            writeOperationNoSize(checksumStreamOutput, op);
            long end = out.position();
            int operationSize = (int) (out.position() - Integer.BYTES - start);
            out.seek(start);
            out.writeInt(operationSize);
            out.seek(end);
            out.bytes().writeTo(outStream);
        }
    } finally {
        Releasables.close(out);
    }
}
Also used : ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Aggregations

ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)4 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 BytesRef (org.apache.lucene.util.BytesRef)2 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)2 ReleasablePagedBytesReference (org.elasticsearch.common.bytes.ReleasablePagedBytesReference)2 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)2 Releasable (org.elasticsearch.common.lease.Releasable)2 AbstractLifecycleRunnable (org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable)2 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)2 ReleasableLock (org.elasticsearch.common.util.concurrent.ReleasableLock)2 ByteBuf (io.netty.buffer.ByteBuf)1 List (java.util.List)1 Version (org.elasticsearch.Version)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)1