public static class HFileBlock.Writer extends Object
HFile
block writer. The intended usage pattern
is as follows:
HFileBlock.Writer
, providing a compression algorithm.
startWriting(org.apache.hadoop.hbase.io.hfile.BlockType)
and get a data stream to write to.
writeHeaderAndData(FSDataOutputStream)
as many times as you need to.
store the serialized block into an external stream.
Modifier and Type | Class and Description |
---|---|
static class |
HFileBlock.Writer.BufferGrabbingByteArrayOutputStream |
private static class |
HFileBlock.Writer.State |
Modifier and Type | Field and Description |
---|---|
private ByteArrayOutputStream |
baosInMemory
The stream we use to accumulate data in uncompressed format for each
block.
|
private BlockType |
blockType
Current block type.
|
private HFileDataBlockEncoder |
dataBlockEncoder
Data block encoder used for data blocks
|
private HFileBlockEncodingContext |
dataBlockEncodingCtx |
private HFileBlockDefaultEncodingContext |
defaultBlockEncodingCtx
block encoding context for non-data blocks
|
private HFileContext |
fileContext
Meta data that holds information about the hfileblock
|
private byte[] |
onDiskBytesWithHeader
Bytes to be written to the file system, including the header.
|
private byte[] |
onDiskChecksum
The size of the checksum data on disk.
|
private long |
prevOffset
The offset of the previous block of the same type
|
private long[] |
prevOffsetByType
Offset of previous block by block type.
|
private long |
startOffset
Current block's start offset in the
HFile . |
private HFileBlock.Writer.State |
state
Writer state.
|
private byte[] |
uncompressedBytesWithHeader
Valid in the READY state.
|
private int |
unencodedDataSizeWritten |
private DataOutputStream |
userDataStream
A stream that we write uncompressed bytes to, which compresses them and
writes them to
baosInMemory . |
Constructor and Description |
---|
HFileBlock.Writer(HFileDataBlockEncoder dataBlockEncoder,
HFileContext fileContext) |
Modifier and Type | Method and Description |
---|---|
int |
blockSizeWritten()
Returns the number of bytes written into the current block so far, or
zero if not writing the block at the moment.
|
(package private) void |
ensureBlockReady()
Transitions the block writer from the "writing" state to the "block
ready" state.
|
private void |
expectState(HFileBlock.Writer.State expectedState) |
private void |
finishBlock()
An internal method that flushes the compressing stream (if using
compression), serializes the header, and takes care of the separate
uncompressed stream for caching on write, if applicable.
|
protected void |
finishBlockAndWriteHeaderAndData(DataOutputStream out)
Writes the header and the compressed data of this block (or uncompressed
data when not using compression) into the given stream.
|
HFileBlock |
getBlockForCaching(CacheConfig cacheConf)
Creates a new HFileBlock.
|
(package private) byte[] |
getHeaderAndDataForTest()
Returns the header or the compressed data (or uncompressed data when not
using compression) as a byte array.
|
(package private) ByteBuffer |
getOnDiskBufferWithHeader()
Returns the header followed by the on-disk (compressed/encoded/encrypted) data.
|
(package private) int |
getOnDiskSizeWithHeader()
Returns the on-disk size of the block.
|
(package private) int |
getOnDiskSizeWithoutHeader()
Returns the on-disk size of the data portion of the block.
|
(package private) ByteBuffer |
getUncompressedBufferWithHeader()
Returns the header followed by the uncompressed data, even if using
compression.
|
(package private) int |
getUncompressedSizeWithHeader()
The uncompressed size of the block data, including header size.
|
(package private) int |
getUncompressedSizeWithoutHeader()
The uncompressed size of the block data.
|
(package private) DataOutputStream |
getUserDataStream()
Returns the stream for the user to write to.
|
boolean |
isWriting() |
private void |
putHeader(byte[] dest,
int offset,
int onDiskSize,
int uncompressedSize,
int onDiskDataSize)
Put the header into the given byte array at the given offset.
|
void |
release()
Releases resources used by this writer.
|
DataOutputStream |
startWriting(BlockType newBlockType)
Starts writing into the block.
|
void |
write(Cell cell)
Writes the Cell to this block
|
void |
writeBlock(HFileBlock.BlockWritable bw,
org.apache.hadoop.fs.FSDataOutputStream out)
Takes the given
HFileBlock.BlockWritable instance, creates a new block of
its appropriate type, writes the writable into this block, and flushes
the block into the output stream. |
void |
writeHeaderAndData(org.apache.hadoop.fs.FSDataOutputStream out)
Similar to
writeHeaderAndData(FSDataOutputStream) , but records
the offset of this block so that it can be referenced in the next block
of the same type. |
private HFileBlock.Writer.State state
private final HFileDataBlockEncoder dataBlockEncoder
private HFileBlockEncodingContext dataBlockEncodingCtx
private HFileBlockDefaultEncodingContext defaultBlockEncodingCtx
private ByteArrayOutputStream baosInMemory
HConstants.HFILEBLOCK_HEADER_SIZE
bytes into this
stream.private BlockType blockType
startWriting(BlockType)
. Could be
changed in finishBlock()
from BlockType.DATA
to BlockType.ENCODED_DATA
.private DataOutputStream userDataStream
baosInMemory
.private int unencodedDataSizeWritten
private byte[] onDiskBytesWithHeader
private byte[] onDiskChecksum
private byte[] uncompressedBytesWithHeader
HFileBlock.uncompressedSizeWithoutHeader
+
HConstants.HFILEBLOCK_HEADER_SIZE
.
Does not store checksums.private long startOffset
HFile
. Set in
writeHeaderAndData(FSDataOutputStream)
.private long[] prevOffsetByType
private long prevOffset
private HFileContext fileContext
public HFileBlock.Writer(HFileDataBlockEncoder dataBlockEncoder, HFileContext fileContext)
dataBlockEncoder
- data block encoding algorithm to usepublic DataOutputStream startWriting(BlockType newBlockType) throws IOException
IOException
public void write(Cell cell) throws IOException
cell
- IOException
DataOutputStream getUserDataStream()
void ensureBlockReady() throws IOException
IOException
private void finishBlock() throws IOException
IOException
private void putHeader(byte[] dest, int offset, int onDiskSize, int uncompressedSize, int onDiskDataSize)
onDiskSize
- size of the block on disk header + data + checksumuncompressedSize
- size of the block after decompression (but
before optional data block decoding) including headeronDiskDataSize
- size of the block on disk with header
and data but not including the checksumspublic void writeHeaderAndData(org.apache.hadoop.fs.FSDataOutputStream out) throws IOException
writeHeaderAndData(FSDataOutputStream)
, but records
the offset of this block so that it can be referenced in the next block
of the same type.out
- IOException
protected void finishBlockAndWriteHeaderAndData(DataOutputStream out) throws IOException
out
- the output stream to write theIOException
byte[] getHeaderAndDataForTest() throws IOException
IOException
public void release()
int getOnDiskSizeWithoutHeader()
int getOnDiskSizeWithHeader()
int getUncompressedSizeWithoutHeader()
int getUncompressedSizeWithHeader()
public boolean isWriting()
public int blockSizeWritten()
ByteBuffer getUncompressedBufferWithHeader()
ByteBuffer getOnDiskBufferWithHeader()
getUncompressedBufferWithHeader()
. Returns only the header and data,
Does not include checksum data.private void expectState(HFileBlock.Writer.State expectedState)
public void writeBlock(HFileBlock.BlockWritable bw, org.apache.hadoop.fs.FSDataOutputStream out) throws IOException
HFileBlock.BlockWritable
instance, creates a new block of
its appropriate type, writes the writable into this block, and flushes
the block into the output stream. The writer is instructed not to buffer
uncompressed bytes for cache-on-write.bw
- the block-writable object to write as a blockout
- the file system output streamIOException
public HFileBlock getBlockForCaching(CacheConfig cacheConf)
Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.