Search in sources :

Example 1 with ReleasablePagedBytesReference

use of org.elasticsearch.common.bytes.ReleasablePagedBytesReference in project elasticsearch by elastic.

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)} 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);
            ReleasablePagedBytesReference bytes = out.bytes();
            bytes.writeTo(outStream);
        }
    } finally {
        Releasables.close(out.bytes());
    }
}
Also used : ReleasablePagedBytesReference(org.elasticsearch.common.bytes.ReleasablePagedBytesReference) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 2 with ReleasablePagedBytesReference

use of org.elasticsearch.common.bytes.ReleasablePagedBytesReference in project elasticsearch by elastic.

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);
    try {
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        final long start = out.position();
        out.skip(Integer.BYTES);
        writeOperationNoSize(checksumStreamOutput, operation);
        final long end = out.position();
        final int operationSize = (int) (end - Integer.BYTES - start);
        out.seek(start);
        out.writeInt(operationSize);
        out.seek(end);
        final ReleasablePagedBytesReference bytes = out.bytes();
        try (ReleasableLock ignored = readLock.acquire()) {
            ensureOpen();
            return current.add(bytes, operation.seqNo());
        }
    } catch (final AlreadyClosedException | IOException ex) {
        try {
            closeOnTragicEvent(ex);
        } catch (final Exception inner) {
            ex.addSuppressed(inner);
        }
        throw ex;
    } catch (final Exception e) {
        try {
            closeOnTragicEvent(e);
        } catch (final Exception inner) {
            e.addSuppressed(inner);
        }
        throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", e);
    } finally {
        Releasables.close(out.bytes());
    }
}
Also used : ReleasablePagedBytesReference(org.elasticsearch.common.bytes.ReleasablePagedBytesReference) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Aggregations

ReleasablePagedBytesReference (org.elasticsearch.common.bytes.ReleasablePagedBytesReference)2 ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 ReleasableLock (org.elasticsearch.common.util.concurrent.ReleasableLock)1