Search in sources :

Example 11 with ReleasableBytesStreamOutput

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

the class CompositeBytesReferenceTests method newRefList.

private List<BytesReference> newRefList(int length) throws IOException {
    List<BytesReference> referenceList = new ArrayList<>();
    for (int i = 0; i < length; ) {
        int remaining = length - i;
        int sliceLength = randomIntBetween(1, remaining);
        ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(sliceLength, bigarrays);
        for (int j = 0; j < sliceLength; j++) {
            out.writeByte((byte) random().nextInt(1 << 8));
        }
        assertEquals(sliceLength, out.size());
        referenceList.add(out.bytes());
        i += sliceLength;
    }
    return referenceList;
}
Also used : ArrayList(java.util.ArrayList) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 12 with ReleasableBytesStreamOutput

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

the class Translog method add.

/**
 * Adds an operation to the transaction log.
 *
 * @param operation the operation to add
 * @return the location of the operation in the translog
 * @throws IOException if adding the operation to the translog resulted in an I/O exception
 */
public Location add(final Operation operation) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(bigArrays);
    boolean successfullySerialized = false;
    try {
        final long start = out.position();
        out.skip(Integer.BYTES);
        writeOperationNoSize(new BufferedChecksumStreamOutput(out), operation);
        final long end = out.position();
        final int operationSize = (int) (end - Integer.BYTES - start);
        out.seek(start);
        out.writeInt(operationSize);
        out.seek(end);
        successfullySerialized = true;
        try (ReleasableBytesReference bytes = new ReleasableBytesReference(out.bytes(), out);
            ReleasableLock ignored = readLock.acquire()) {
            ensureOpen();
            if (operation.primaryTerm() > current.getPrimaryTerm()) {
                assert false : "Operation term is newer than the current term; " + "current term[" + current.getPrimaryTerm() + "], operation term[" + operation + "]";
                throw new IllegalArgumentException("Operation term is newer than the current term; " + "current term[" + current.getPrimaryTerm() + "], operation term[" + operation + "]");
            }
            return current.add(bytes, operation.seqNo());
        }
    } catch (final AlreadyClosedException | IOException ex) {
        closeOnTragicEvent(ex);
        throw ex;
    } catch (final Exception ex) {
        closeOnTragicEvent(ex);
        throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", ex);
    } finally {
        if (successfullySerialized == false) {
            Releasables.close(out);
        }
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EOFException(java.io.EOFException) IOException(java.io.IOException) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 13 with ReleasableBytesStreamOutput

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

the class ReleasableBytesReferenceTests method newBytesReferenceWithOffsetOfZero.

@Override
protected BytesReference newBytesReferenceWithOffsetOfZero(int length) throws IOException {
    BytesReference delegate;
    String composite = "composite";
    String paged = "paged";
    String array = "array";
    String type = randomFrom(composite, paged, array);
    if (array.equals(type)) {
        final BytesStreamOutput out = new BytesStreamOutput(length);
        for (int i = 0; i < length; i++) {
            out.writeByte((byte) random().nextInt(1 << 8));
        }
        assertThat(length, equalTo(out.size()));
        BytesArray ref = new BytesArray(out.bytes().toBytesRef().bytes, 0, length);
        assertThat(length, equalTo(ref.length()));
        assertThat(ref.length(), Matchers.equalTo(length));
        delegate = ref;
    } else if (paged.equals(type)) {
        ByteArray byteArray = bigarrays.newByteArray(length);
        for (int i = 0; i < length; i++) {
            byteArray.set(i, (byte) random().nextInt(1 << 8));
        }
        assertThat(byteArray.size(), Matchers.equalTo((long) length));
        BytesReference ref = new PagedBytesReference(byteArray, length);
        assertThat(ref.length(), Matchers.equalTo(length));
        delegate = ref;
    } else {
        assert composite.equals(type);
        List<BytesReference> referenceList = new ArrayList<>();
        for (int i = 0; i < length; ) {
            int remaining = length - i;
            int sliceLength = randomIntBetween(1, remaining);
            try (ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(sliceLength, bigarrays)) {
                for (int j = 0; j < sliceLength; j++) {
                    out.writeByte((byte) random().nextInt(1 << 8));
                }
                assertThat(sliceLength, equalTo(out.size()));
                referenceList.add(out.bytes());
            }
            i += sliceLength;
        }
        BytesReference ref = new CompositeBytesReference(referenceList.toArray(new BytesReference[0]));
        assertThat(length, equalTo(ref.length()));
        delegate = ref;
    }
    return ReleasableBytesReference.wrap(delegate);
}
Also used : ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) ByteArray(org.elasticsearch.common.util.ByteArray) List(java.util.List) ArrayList(java.util.ArrayList) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

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