Package org.apache.lucene.store
Class ByteBuffersDataOutput
- java.lang.Object
-
- org.apache.lucene.store.DataOutput
-
- org.apache.lucene.store.ByteBuffersDataOutput
-
- All Implemented Interfaces:
Accountable
public final class ByteBuffersDataOutput extends DataOutput implements Accountable
ADataOutputstoring data in a list ofByteBuffers.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classByteBuffersDataOutput.ByteBufferRecyclerAn implementation of aByteBufferallocation and recycling policy.
-
Field Summary
Fields Modifier and Type Field Description static java.util.function.IntFunction<java.nio.ByteBuffer>ALLOCATE_BB_ON_HEAPprivate java.util.function.IntFunction<java.nio.ByteBuffer>blockAllocateByteBuffersupplier.private intblockBitsCurrent block size:2^bits.private java.util.function.Consumer<java.nio.ByteBuffer>blockReuseByteBufferrecycler onreset().private java.util.ArrayDeque<java.nio.ByteBuffer>blocksBlocks storing data.private java.nio.ByteBuffercurrentBlockThe current-or-next write block.static intDEFAULT_MAX_BITS_PER_BLOCKstatic intDEFAULT_MIN_BITS_PER_BLOCKprivate static java.nio.ByteBufferEMPTYprivate static byte[]EMPTY_BYTE_ARRAYprivate static longHALF_SHIFT(package private) static intMAX_BLOCKS_BEFORE_BLOCK_EXPANSIONMaximum number of blocks at the currentblockBitsblock size before we increase the block size (and thus decrease the number of blocks).private intmaxBitsPerBlockMaximum block size:2^bits.static java.util.function.Consumer<java.nio.ByteBuffer>NO_REUSEA singleton instance of "no-reuse" buffer strategy.private longramBytesUsedCumulative RAM usage across all blocks.private static intSURROGATE_OFFSET-
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
-
-
Constructor Summary
Constructors Constructor Description ByteBuffersDataOutput()ByteBuffersDataOutput(int minBitsPerBlock, int maxBitsPerBlock, java.util.function.IntFunction<java.nio.ByteBuffer> blockAllocate, java.util.function.Consumer<java.nio.ByteBuffer> blockReuse)ByteBuffersDataOutput(long expectedSize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidappendBlock()private intblockSize()private static intcomputeBlockSizeBitsFor(long bytes)voidcopyTo(DataOutput output)Copy the current content of this object into anotherDataOutput.static ByteBuffersDataOutputnewResettableInstance()longramBytesUsed()Return the memory usage of this object in bytes.voidreset()This method resets this object to a clean (zero-size) state and publishes any currently allocated buffers for reuse to the reuse strategy provided in the constructor.private voidrewriteToBlockSize(int targetBlockBits)longsize()byte[]toArrayCopy()Return a contiguous array with the current content written to the output.java.util.ArrayList<java.nio.ByteBuffer>toBufferList()Return a list of read-only view ofByteBufferblocks over the current content written to the output.ByteBuffersDataInputtoDataInput()Return aByteBuffersDataInputfor the set of current buffers (toBufferList()).java.lang.StringtoString()java.util.ArrayList<java.nio.ByteBuffer>toWriteableBufferList()Returns a list of writeable blocks over the (source) content buffers.private static intUTF16toUTF8(java.lang.CharSequence s, int offset, int length, byte[] buf, java.util.function.IntConsumer bufferFlusher)A consumer-based UTF16-UTF8 encoder (writes the input string in smaller buffers.).voidwriteByte(byte b)Writes a single byte.voidwriteBytes(byte[] b)voidwriteBytes(byte[] b, int length)Writes an array of bytes.voidwriteBytes(byte[] src, int offset, int length)Writes an array of bytes.voidwriteBytes(java.nio.ByteBuffer buffer)voidwriteInt(int v)Writes an int as four bytes.voidwriteLong(long v)Writes a long as eight bytes.voidwriteMapOfStrings(java.util.Map<java.lang.String,java.lang.String> map)Writes a String map.voidwriteSetOfStrings(java.util.Set<java.lang.String> set)Writes a String set.voidwriteShort(short v)Writes a short as two bytes.voidwriteString(java.lang.String v)Writes a string.-
Methods inherited from class org.apache.lucene.store.DataOutput
copyBytes, writeVInt, writeVLong, writeZInt, writeZLong
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.lucene.util.Accountable
getChildResources
-
-
-
-
Field Detail
-
EMPTY
private static final java.nio.ByteBuffer EMPTY
-
EMPTY_BYTE_ARRAY
private static final byte[] EMPTY_BYTE_ARRAY
-
ALLOCATE_BB_ON_HEAP
public static final java.util.function.IntFunction<java.nio.ByteBuffer> ALLOCATE_BB_ON_HEAP
-
NO_REUSE
public static final java.util.function.Consumer<java.nio.ByteBuffer> NO_REUSE
A singleton instance of "no-reuse" buffer strategy.
-
DEFAULT_MIN_BITS_PER_BLOCK
public static final int DEFAULT_MIN_BITS_PER_BLOCK
- See Also:
- Constant Field Values
-
DEFAULT_MAX_BITS_PER_BLOCK
public static final int DEFAULT_MAX_BITS_PER_BLOCK
- See Also:
- Constant Field Values
-
MAX_BLOCKS_BEFORE_BLOCK_EXPANSION
static final int MAX_BLOCKS_BEFORE_BLOCK_EXPANSION
Maximum number of blocks at the currentblockBitsblock size before we increase the block size (and thus decrease the number of blocks).- See Also:
- Constant Field Values
-
maxBitsPerBlock
private final int maxBitsPerBlock
Maximum block size:2^bits.
-
blockAllocate
private final java.util.function.IntFunction<java.nio.ByteBuffer> blockAllocate
ByteBuffersupplier.
-
blockReuse
private final java.util.function.Consumer<java.nio.ByteBuffer> blockReuse
ByteBufferrecycler onreset().
-
blockBits
private int blockBits
Current block size:2^bits.
-
blocks
private final java.util.ArrayDeque<java.nio.ByteBuffer> blocks
Blocks storing data.
-
ramBytesUsed
private long ramBytesUsed
Cumulative RAM usage across all blocks.
-
currentBlock
private java.nio.ByteBuffer currentBlock
The current-or-next write block.
-
HALF_SHIFT
private static final long HALF_SHIFT
- See Also:
- Constant Field Values
-
SURROGATE_OFFSET
private static final int SURROGATE_OFFSET
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ByteBuffersDataOutput
public ByteBuffersDataOutput(long expectedSize)
-
ByteBuffersDataOutput
public ByteBuffersDataOutput()
-
ByteBuffersDataOutput
public ByteBuffersDataOutput(int minBitsPerBlock, int maxBitsPerBlock, java.util.function.IntFunction<java.nio.ByteBuffer> blockAllocate, java.util.function.Consumer<java.nio.ByteBuffer> blockReuse)
-
-
Method Detail
-
writeByte
public void writeByte(byte b)
Description copied from class:DataOutputWrites a single byte.The most primitive data type is an eight-bit byte. Files are accessed as sequences of bytes. All other data types are defined as sequences of bytes, so file formats are byte-order independent.
- Specified by:
writeBytein classDataOutput- See Also:
DataInput.readByte()
-
writeBytes
public void writeBytes(byte[] src, int offset, int length)Description copied from class:DataOutputWrites an array of bytes.- Specified by:
writeBytesin classDataOutput- Parameters:
src- the bytes to writeoffset- the offset in the byte arraylength- the number of bytes to write- See Also:
DataInput.readBytes(byte[],int,int)
-
writeBytes
public void writeBytes(byte[] b, int length)Description copied from class:DataOutputWrites an array of bytes.- Overrides:
writeBytesin classDataOutput- Parameters:
b- the bytes to writelength- the number of bytes to write- See Also:
DataInput.readBytes(byte[],int,int)
-
writeBytes
public void writeBytes(byte[] b)
-
writeBytes
public void writeBytes(java.nio.ByteBuffer buffer)
-
toBufferList
public java.util.ArrayList<java.nio.ByteBuffer> toBufferList()
Return a list of read-only view ofByteBufferblocks over the current content written to the output.
-
toWriteableBufferList
public java.util.ArrayList<java.nio.ByteBuffer> toWriteableBufferList()
Returns a list of writeable blocks over the (source) content buffers. This method returns the raw content of source buffers that may change over the lifetime of this object (blocks can be recycled or discarded, for example). Most applications should favor callingtoBufferList()which returns a read-only view over the content of the source buffers. The difference betweentoBufferList()andtoWriteableBufferList()is that read-only view of source buffers will always returnfalsefromByteBuffer.hasArray()(which sometimes may be required to avoid double copying).
-
toDataInput
public ByteBuffersDataInput toDataInput()
Return aByteBuffersDataInputfor the set of current buffers (toBufferList()).
-
toArrayCopy
public byte[] toArrayCopy()
Return a contiguous array with the current content written to the output. The returned array is always a copy (can be mutated). If thesize()of the underlying buffers exceeds maximum size of Java array, anRuntimeExceptionwill be thrown.
-
copyTo
public void copyTo(DataOutput output) throws java.io.IOException
Copy the current content of this object into anotherDataOutput.- Throws:
java.io.IOException
-
size
public long size()
- Returns:
- The number of bytes written to this output so far.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
writeShort
public void writeShort(short v)
Description copied from class:DataOutputWrites a short as two bytes.- Overrides:
writeShortin classDataOutput- See Also:
DataInput.readShort()
-
writeInt
public void writeInt(int v)
Description copied from class:DataOutputWrites an int as four bytes.32-bit unsigned integer written as four bytes, high-order bytes first.
- Overrides:
writeIntin classDataOutput- See Also:
DataInput.readInt()
-
writeLong
public void writeLong(long v)
Description copied from class:DataOutputWrites a long as eight bytes.64-bit unsigned integer written as eight bytes, high-order bytes first.
- Overrides:
writeLongin classDataOutput- See Also:
DataInput.readLong()
-
writeString
public void writeString(java.lang.String v)
Description copied from class:DataOutputWrites a string.Writes strings as UTF-8 encoded bytes. First the length, in bytes, is written as a
VInt, followed by the bytes.- Overrides:
writeStringin classDataOutput- See Also:
DataInput.readString()
-
writeMapOfStrings
public void writeMapOfStrings(java.util.Map<java.lang.String,java.lang.String> map)
Description copied from class:DataOutputWrites a String map.First the size is written as an
vInt, followed by each key-value pair written as two consecutiveStrings.- Overrides:
writeMapOfStringsin classDataOutput- Parameters:
map- Input map.
-
writeSetOfStrings
public void writeSetOfStrings(java.util.Set<java.lang.String> set)
Description copied from class:DataOutputWrites a String set.First the size is written as an
vInt, followed by each value written as aString.- Overrides:
writeSetOfStringsin classDataOutput- Parameters:
set- Input set.
-
ramBytesUsed
public long ramBytesUsed()
Description copied from interface:AccountableReturn the memory usage of this object in bytes. Negative values are illegal.- Specified by:
ramBytesUsedin interfaceAccountable
-
reset
public void reset()
This method resets this object to a clean (zero-size) state and publishes any currently allocated buffers for reuse to the reuse strategy provided in the constructor. Sharing byte buffers for reads and writes is dangerous and will very likely lead to hard-to-debug issues, use with great care.
-
newResettableInstance
public static ByteBuffersDataOutput newResettableInstance()
- Returns:
- Returns a new
ByteBuffersDataOutputwith thereset()capability.
-
blockSize
private int blockSize()
-
appendBlock
private void appendBlock()
-
rewriteToBlockSize
private void rewriteToBlockSize(int targetBlockBits)
-
computeBlockSizeBitsFor
private static int computeBlockSizeBitsFor(long bytes)
-
UTF16toUTF8
private static int UTF16toUTF8(java.lang.CharSequence s, int offset, int length, byte[] buf, java.util.function.IntConsumer bufferFlusher)A consumer-based UTF16-UTF8 encoder (writes the input string in smaller buffers.).
-
-