Class RecyclerBytesStreamOutput
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,Releasable
StreamOutput that uses a Recycler<org.apache.lucene.util.BytesRef> to acquire pages of bytes, which avoids frequent reallocation &
copying of the internal data. When close() is called, the bytes will be released.
Best only used for outputs which are either short-lived or large, because the resulting ReleasableBytesReference retains whole
pages and this overhead may be significant on small and long-lived objects.
A RecyclerBytesStreamOutput obtains pages (16kiB slices of a larger byte[]) from a Recycler<BytesRef> rather than
using the BigArrays abstraction that BytesStreamOutput and ReleasableBytesStreamOutput both use. This means it
can access the underlying byte[] directly and therefore avoids the intermediate buffer and the copy for almost all writes (the
exception being occasional writes that get too close to the end of a page).
It does not attempt to grow its collector slowly in the same way that BigArrays.resize(org.elasticsearch.common.util.ByteArray, long) does. Instead, it always obtains from the
recycler a whole new 16kiB page when the need arises. This works best when the serialized data has a short lifespan (e.g. it is an
outbound network message) so the overhead has limited impact and the savings on allocations and copying (due to the absence of resize
operations) are significant.
The resulting ReleasableBytesReference is a view over the underlying byte[] pages and involves no significant extra
allocation to obtain. It is oversized: The worst case for overhead is when the data is a single byte, since this takes up a whole 16kiB
page almost all of which is overhead. Nonetheless, if recycled pages are available then it may still be preferable to use them via a
RecyclerBytesStreamOutput. If the result is large then the overhead is less important, and if the result will only be used for
a short time, for instance soon being written to the network or to disk, then the imminent recycling of these pages may mean it is ok to
keep it as-is. For results which are both small and long-lived it may be better to copy them into a freshly-allocated byte[].
Any memory allocated in this way is tracked by the org.elasticsearch.common.breaker subsystem if and only if the caller passes in
a non-null CircuitBreaker at creation time. If the provided CircuitBreaker is null then the allocations performed
here are untracked by circuit-breakers, even if the Recycler<BytesRef> was obtained from BigArrays.bytesRefRecycler().
-
Field Summary
Fields inherited from class org.elasticsearch.common.io.stream.StreamOutput
GENERIC_LIST_HEADER -
Constructor Summary
ConstructorsConstructorDescriptionRecyclerBytesStreamOutput(Recycler<org.apache.lucene.util.BytesRef> recycler) RecyclerBytesStreamOutput(Recycler<org.apache.lucene.util.BytesRef> recycler, CircuitBreaker circuitBreaker) -
Method Summary
Modifier and TypeMethodDescriptionbytes()voidclose()Closes this stream to further operations.voidflush()Forces any buffered output to be written.voidlegacyWriteWithSizePrefix(Writeable writeable) Serializes a writable just likeWriteable.writeTo(StreamOutput)would but prefixes it with the serialized size of the result.Move the contents written to this stream to aReleasableBytesReference.longposition()voidseek(long position) intsize()Returns the current size of the buffer.voidskip(int length) toBase64String(Base64.Encoder encoder) Base64-encode the contents of the stream and convert to aString, avoiding unnecessary allocation and copying as much as possible.org.apache.lucene.util.BytesReftryGetPageForWrite(int bytes) Attempt to get one page to perform a write directly into the page.static intvIntLength(int value) voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidCopies the entire remaining contents of the givenInputStreamdirectly into this output, avoiding the need for any intermediate buffers.voidwriteByte(byte b) Writes a single byte.voidwriteBytes(byte[] b, int offset, int length) Writes an array of bytes.voidwriteGenericString(String value) Write aStringwithinStreamOutput.writeGenericValue(java.lang.Object), represented as the type code byte0followed by the string itself written as if withStreamOutput.writeString(java.lang.String).voidwriteInt(int i) Writes an int as four bytes.voidwriteIntLE(int i) Writes an int as four bytes, least significant bytes first.voidwriteLong(long i) Writes a long as eight bytes.voidwriteLongLE(long i) Writes a long as eight bytes.voidWrite a possibly-nullString, represented as awhich isinvalid reference
booleanfalseif the string is null, or elsetrueif it is not null, and in this latter case it is followed by the string itself written as if withStreamOutput.writeString(java.lang.String).voidwriteString(String str) Write aString: its length in Unicode code units (chars) (NB not bytes) written usingStreamOutput.writeVInt(int)followed by the sequence of characters themselves encoded as UTF-8.voidwriteVInt(int i) Writes an int in a variable-length format.Methods inherited from class org.elasticsearch.common.io.stream.BytesStream
writeWithSizePrefixMethods inherited from class org.elasticsearch.common.io.stream.StreamOutput
checkWriteable, getTransportVersion, setTransportVersion, write, writeArray, writeArray, writeBigInteger, writeBoolean, writeByteArray, writeBytes, writeBytes, writeBytesRef, writeBytesReference, writeCollection, writeCollection, writeDouble, writeDoubleArray, writeDoubleLE, writeEnum, writeEnumSet, writeException, writeFloat, writeFloatArray, writeGenericList, writeGenericMap, writeGenericNull, writeGenericValue, writeGeoPoint, writeInstant, writeIntArray, writeLongArray, writeMap, writeMap, writeMap, writeMapValues, writeMapValues, writeMapWithConsistentOrder, writeMissingString, writeMissingWriteable, writeNamedWriteable, writeNamedWriteableCollection, writeOptional, writeOptionalArray, writeOptionalArray, writeOptionalBoolean, writeOptionalByteArray, writeOptionalBytesReference, writeOptionalCollection, writeOptionalCollection, writeOptionalDouble, writeOptionalEnum, writeOptionalException, writeOptionalFloat, writeOptionalFloatArray, writeOptionalInstant, writeOptionalInt, writeOptionalLong, writeOptionalMap, writeOptionalNamedWriteable, writeOptionalNamedWriteableCollection, writeOptionalSecureString, writeOptionalStringArray, writeOptionalStringCollection, writeOptionalText, writeOptionalTimeValue, writeOptionalVInt, writeOptionalVLong, writeOptionalWriteable, writeOptionalZoneId, writeSecureString, writeShort, writeStringArray, writeStringArrayNullable, writeStringCollection, writeText, writeTimeValue, writeVIntArray, writeVLong, writeVLongArray, writeWriteable, writeZLong, writeZoneIdMethods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
RecyclerBytesStreamOutput
-
RecyclerBytesStreamOutput
public RecyclerBytesStreamOutput(Recycler<org.apache.lucene.util.BytesRef> recycler, @Nullable CircuitBreaker circuitBreaker)
-
-
Method Details
-
position
public long position()- Specified by:
positionin classStreamOutput- Returns:
- the current position of the stream, in bytes. Increases as data is written to the stream. If the stream is seekable then
the seek operation updates the current
position().
-
writeByte
public void writeByte(byte b) Description copied from class:StreamOutputWrites a single byte.- Specified by:
writeBytein classStreamOutput
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classStreamOutput- Throws:
IOException
-
writeBytes
public void writeBytes(byte[] b, int offset, int length) Description copied from class:StreamOutputWrites an array of bytes.- Specified by:
writeBytesin classStreamOutput- Parameters:
b- the bytes to writeoffset- the offset in the byte arraylength- the number of bytes to write
-
writeVInt
public void writeVInt(int i) Description copied from class:StreamOutputWrites an int in a variable-length format. Writes between one and five bytes. Smaller values take fewer bytes. Negative numbers will always use all 5 bytes and are therefore better serialized usingStreamOutput.writeInt(int)- Overrides:
writeVIntin classStreamOutput
-
vIntLength
public static int vIntLength(int value) -
writeInt
Description copied from class:StreamOutputWrites an int as four bytes.- Overrides:
writeIntin classStreamOutput- Throws:
IOException
-
writeIntLE
Description copied from class:StreamOutputWrites an int as four bytes, least significant bytes first.- Overrides:
writeIntLEin classStreamOutput- Throws:
IOException
-
writeLong
Description copied from class:StreamOutputWrites a long as eight bytes.- Overrides:
writeLongin classStreamOutput- Throws:
IOException
-
writeLongLE
Description copied from class:StreamOutputWrites a long as eight bytes.- Overrides:
writeLongLEin classStreamOutput- Throws:
IOException
-
legacyWriteWithSizePrefix
Description copied from class:StreamOutputSerializes a writable just likeWriteable.writeTo(StreamOutput)would but prefixes it with the serialized size of the result.- Overrides:
legacyWriteWithSizePrefixin classStreamOutput- Parameters:
writeable-Writeableto serialize- Throws:
IOException
-
tryGetPageForWrite
public org.apache.lucene.util.BytesRef tryGetPageForWrite(int bytes) Attempt to get one page to perform a write directly into the page. The page will only be returned if the requested bytes can fit. If requested bytes cannot fit, null will be returned. This will advance the current position in the stream.- Parameters:
bytes- the number of bytes for the single write- Returns:
- a direct page if there is enough space in current page, otherwise null
-
writeString
Description copied from class:StreamOutputWrite aString: its length in Unicode code units (chars) (NB not bytes) written usingStreamOutput.writeVInt(int)followed by the sequence of characters themselves encoded as UTF-8.May be performance-critical, so subclasses must specify an explicit implementation. If performance is unimportant, consider using
StreamOutputHelper.writeString(java.lang.String, java.io.OutputStream).- Specified by:
writeStringin classStreamOutput- Throws:
IOException
-
writeOptionalString
Description copied from class:StreamOutputWrite a possibly-nullString, represented as awhich isinvalid reference
booleanfalseif the string is null, or elsetrueif it is not null, and in this latter case it is followed by the string itself written as if withStreamOutput.writeString(java.lang.String).May be performance-critical, so subclasses must specify an explicit implementation. If performance is unimportant, consider using
StreamOutputHelper.writeOptionalString(java.lang.String, java.io.OutputStream).- Specified by:
writeOptionalStringin classStreamOutput- Throws:
IOException
-
writeGenericString
Description copied from class:StreamOutputWrite aStringwithinStreamOutput.writeGenericValue(java.lang.Object), represented as the type code byte0followed by the string itself written as if withStreamOutput.writeString(java.lang.String).May be performance-critical, so subclasses must specify an explicit implementation. If performance is unimportant, consider using
StreamOutputHelper.writeGenericString(java.lang.String, java.io.OutputStream).- Specified by:
writeGenericStringin classStreamOutput- Throws:
IOException
-
flush
public void flush()Description copied from class:StreamOutputForces any buffered output to be written.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classStreamOutput
-
seek
public void seek(long position) - Specified by:
seekin classBytesStream
-
skip
public void skip(int length) -
close
public void close()Description copied from class:StreamOutputCloses this stream to further operations.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceReleasable- Specified by:
closein classStreamOutput
-
moveToBytesReference
Move the contents written to this stream to aReleasableBytesReference. Closing this instance becomes a noop after this method returns successfully and its buffers need to be released by releasing the returned bytes reference.- Returns:
- a
ReleasableBytesReferencethat must be released once no longer needed
-
toBase64String
Base64-encode the contents of the stream and convert to aString, avoiding unnecessary allocation and copying as much as possible.- Parameters:
encoder- Encoder to use. Must not insert line-breaks.- Returns:
- Base64-encoded copy of the contents of the stream.
-
writeAllBytesFrom
Copies the entire remaining contents of the givenInputStreamdirectly into this output, avoiding the need for any intermediate buffers.- Throws:
IOException
-
size
public int size()Returns the current size of the buffer.- Returns:
- the value of the
countfield, which is the number of valid bytes in this output stream. - See Also:
-
bytes
- Specified by:
bytesin classBytesStream
-