hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From git-site-r...@apache.org
Subject [04/51] [partial] hbase-site git commit: Published site at 82d554e3783372cc6b05489452c815b57c06f6cd.
Date Fri, 28 Apr 2017 14:59:03 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/6f2e75f2/devapidocs/src-html/org/apache/hadoop/hbase/io/hfile/HFileBlock.PrefetchedHeader.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/io/hfile/HFileBlock.PrefetchedHeader.html b/devapidocs/src-html/org/apache/hadoop/hbase/io/hfile/HFileBlock.PrefetchedHeader.html
index a665139..3fedd0b 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/io/hfile/HFileBlock.PrefetchedHeader.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/io/hfile/HFileBlock.PrefetchedHeader.html
@@ -879,1201 +879,1221 @@
 <span class="sourceLineNo">871</span>    // includes the header size also.<a name="line.871"></a>
 <span class="sourceLineNo">872</span>    private int unencodedDataSizeWritten;<a name="line.872"></a>
 <span class="sourceLineNo">873</span><a name="line.873"></a>
-<span class="sourceLineNo">874</span>    /**<a name="line.874"></a>
-<span class="sourceLineNo">875</span>     * Bytes to be written to the file system, including the header. Compressed<a name="line.875"></a>
-<span class="sourceLineNo">876</span>     * if compression is turned on. It also includes the checksum data that<a name="line.876"></a>
-<span class="sourceLineNo">877</span>     * immediately follows the block data. (header + data + checksums)<a name="line.877"></a>
-<span class="sourceLineNo">878</span>     */<a name="line.878"></a>
-<span class="sourceLineNo">879</span>    private ByteArrayOutputStream onDiskBlockBytesWithHeader;<a name="line.879"></a>
-<span class="sourceLineNo">880</span><a name="line.880"></a>
-<span class="sourceLineNo">881</span>    /**<a name="line.881"></a>
-<span class="sourceLineNo">882</span>     * The size of the checksum data on disk. It is used only if data is<a name="line.882"></a>
-<span class="sourceLineNo">883</span>     * not compressed. If data is compressed, then the checksums are already<a name="line.883"></a>
-<span class="sourceLineNo">884</span>     * part of onDiskBytesWithHeader. If data is uncompressed, then this<a name="line.884"></a>
-<span class="sourceLineNo">885</span>     * variable stores the checksum data for this block.<a name="line.885"></a>
-<span class="sourceLineNo">886</span>     */<a name="line.886"></a>
-<span class="sourceLineNo">887</span>    private byte[] onDiskChecksum = HConstants.EMPTY_BYTE_ARRAY;<a name="line.887"></a>
-<span class="sourceLineNo">888</span><a name="line.888"></a>
-<span class="sourceLineNo">889</span>    /**<a name="line.889"></a>
-<span class="sourceLineNo">890</span>     * Current block's start offset in the {@link HFile}. Set in<a name="line.890"></a>
-<span class="sourceLineNo">891</span>     * {@link #writeHeaderAndData(FSDataOutputStream)}.<a name="line.891"></a>
-<span class="sourceLineNo">892</span>     */<a name="line.892"></a>
-<span class="sourceLineNo">893</span>    private long startOffset;<a name="line.893"></a>
-<span class="sourceLineNo">894</span><a name="line.894"></a>
-<span class="sourceLineNo">895</span>    /**<a name="line.895"></a>
-<span class="sourceLineNo">896</span>     * Offset of previous block by block type. Updated when the next block is<a name="line.896"></a>
-<span class="sourceLineNo">897</span>     * started.<a name="line.897"></a>
-<span class="sourceLineNo">898</span>     */<a name="line.898"></a>
-<span class="sourceLineNo">899</span>    private long[] prevOffsetByType;<a name="line.899"></a>
-<span class="sourceLineNo">900</span><a name="line.900"></a>
-<span class="sourceLineNo">901</span>    /** The offset of the previous block of the same type */<a name="line.901"></a>
-<span class="sourceLineNo">902</span>    private long prevOffset;<a name="line.902"></a>
-<span class="sourceLineNo">903</span>    /** Meta data that holds information about the hfileblock**/<a name="line.903"></a>
-<span class="sourceLineNo">904</span>    private HFileContext fileContext;<a name="line.904"></a>
-<span class="sourceLineNo">905</span><a name="line.905"></a>
-<span class="sourceLineNo">906</span>    /**<a name="line.906"></a>
-<span class="sourceLineNo">907</span>     * @param dataBlockEncoder data block encoding algorithm to use<a name="line.907"></a>
-<span class="sourceLineNo">908</span>     */<a name="line.908"></a>
-<span class="sourceLineNo">909</span>    public Writer(HFileDataBlockEncoder dataBlockEncoder, HFileContext fileContext) {<a name="line.909"></a>
-<span class="sourceLineNo">910</span>      if (fileContext.getBytesPerChecksum() &lt; HConstants.HFILEBLOCK_HEADER_SIZE) {<a name="line.910"></a>
-<span class="sourceLineNo">911</span>        throw new RuntimeException("Unsupported value of bytesPerChecksum. " +<a name="line.911"></a>
-<span class="sourceLineNo">912</span>            " Minimum is " + HConstants.HFILEBLOCK_HEADER_SIZE + " but the configured value is " +<a name="line.912"></a>
-<span class="sourceLineNo">913</span>            fileContext.getBytesPerChecksum());<a name="line.913"></a>
-<span class="sourceLineNo">914</span>      }<a name="line.914"></a>
-<span class="sourceLineNo">915</span>      this.dataBlockEncoder = dataBlockEncoder != null?<a name="line.915"></a>
-<span class="sourceLineNo">916</span>          dataBlockEncoder: NoOpDataBlockEncoder.INSTANCE;<a name="line.916"></a>
-<span class="sourceLineNo">917</span>      this.dataBlockEncodingCtx = this.dataBlockEncoder.<a name="line.917"></a>
-<span class="sourceLineNo">918</span>          newDataBlockEncodingContext(HConstants.HFILEBLOCK_DUMMY_HEADER, fileContext);<a name="line.918"></a>
-<span class="sourceLineNo">919</span>      // TODO: This should be lazily instantiated since we usually do NOT need this default encoder<a name="line.919"></a>
-<span class="sourceLineNo">920</span>      this.defaultBlockEncodingCtx = new HFileBlockDefaultEncodingContext(null,<a name="line.920"></a>
-<span class="sourceLineNo">921</span>          HConstants.HFILEBLOCK_DUMMY_HEADER, fileContext);<a name="line.921"></a>
-<span class="sourceLineNo">922</span>      // TODO: Set BAOS initial size. Use fileContext.getBlocksize() and add for header/checksum<a name="line.922"></a>
-<span class="sourceLineNo">923</span>      baosInMemory = new ByteArrayOutputStream();<a name="line.923"></a>
-<span class="sourceLineNo">924</span>      prevOffsetByType = new long[BlockType.values().length];<a name="line.924"></a>
-<span class="sourceLineNo">925</span>      for (int i = 0; i &lt; prevOffsetByType.length; ++i) {<a name="line.925"></a>
-<span class="sourceLineNo">926</span>        prevOffsetByType[i] = UNSET;<a name="line.926"></a>
-<span class="sourceLineNo">927</span>      }<a name="line.927"></a>
-<span class="sourceLineNo">928</span>      // TODO: Why fileContext saved away when we have dataBlockEncoder and/or<a name="line.928"></a>
-<span class="sourceLineNo">929</span>      // defaultDataBlockEncoder?<a name="line.929"></a>
-<span class="sourceLineNo">930</span>      this.fileContext = fileContext;<a name="line.930"></a>
-<span class="sourceLineNo">931</span>    }<a name="line.931"></a>
-<span class="sourceLineNo">932</span><a name="line.932"></a>
-<span class="sourceLineNo">933</span>    /**<a name="line.933"></a>
-<span class="sourceLineNo">934</span>     * Starts writing into the block. The previous block's data is discarded.<a name="line.934"></a>
-<span class="sourceLineNo">935</span>     *<a name="line.935"></a>
-<span class="sourceLineNo">936</span>     * @return the stream the user can write their data into<a name="line.936"></a>
-<span class="sourceLineNo">937</span>     * @throws IOException<a name="line.937"></a>
-<span class="sourceLineNo">938</span>     */<a name="line.938"></a>
-<span class="sourceLineNo">939</span>    DataOutputStream startWriting(BlockType newBlockType)<a name="line.939"></a>
-<span class="sourceLineNo">940</span>        throws IOException {<a name="line.940"></a>
-<span class="sourceLineNo">941</span>      if (state == State.BLOCK_READY &amp;&amp; startOffset != -1) {<a name="line.941"></a>
-<span class="sourceLineNo">942</span>        // We had a previous block that was written to a stream at a specific<a name="line.942"></a>
-<span class="sourceLineNo">943</span>        // offset. Save that offset as the last offset of a block of that type.<a name="line.943"></a>
-<span class="sourceLineNo">944</span>        prevOffsetByType[blockType.getId()] = startOffset;<a name="line.944"></a>
-<span class="sourceLineNo">945</span>      }<a name="line.945"></a>
-<span class="sourceLineNo">946</span><a name="line.946"></a>
-<span class="sourceLineNo">947</span>      startOffset = -1;<a name="line.947"></a>
-<span class="sourceLineNo">948</span>      blockType = newBlockType;<a name="line.948"></a>
-<span class="sourceLineNo">949</span><a name="line.949"></a>
-<span class="sourceLineNo">950</span>      baosInMemory.reset();<a name="line.950"></a>
-<span class="sourceLineNo">951</span>      baosInMemory.write(HConstants.HFILEBLOCK_DUMMY_HEADER);<a name="line.951"></a>
-<span class="sourceLineNo">952</span><a name="line.952"></a>
-<span class="sourceLineNo">953</span>      state = State.WRITING;<a name="line.953"></a>
-<span class="sourceLineNo">954</span><a name="line.954"></a>
-<span class="sourceLineNo">955</span>      // We will compress it later in finishBlock()<a name="line.955"></a>
-<span class="sourceLineNo">956</span>      userDataStream = new ByteBufferWriterDataOutputStream(baosInMemory);<a name="line.956"></a>
-<span class="sourceLineNo">957</span>      if (newBlockType == BlockType.DATA) {<a name="line.957"></a>
-<span class="sourceLineNo">958</span>        this.dataBlockEncoder.startBlockEncoding(dataBlockEncodingCtx, userDataStream);<a name="line.958"></a>
-<span class="sourceLineNo">959</span>      }<a name="line.959"></a>
-<span class="sourceLineNo">960</span>      this.unencodedDataSizeWritten = 0;<a name="line.960"></a>
-<span class="sourceLineNo">961</span>      return userDataStream;<a name="line.961"></a>
-<span class="sourceLineNo">962</span>    }<a name="line.962"></a>
-<span class="sourceLineNo">963</span><a name="line.963"></a>
-<span class="sourceLineNo">964</span>    /**<a name="line.964"></a>
-<span class="sourceLineNo">965</span>     * Writes the Cell to this block<a name="line.965"></a>
-<span class="sourceLineNo">966</span>     * @param cell<a name="line.966"></a>
-<span class="sourceLineNo">967</span>     * @throws IOException<a name="line.967"></a>
-<span class="sourceLineNo">968</span>     */<a name="line.968"></a>
-<span class="sourceLineNo">969</span>    void write(Cell cell) throws IOException{<a name="line.969"></a>
-<span class="sourceLineNo">970</span>      expectState(State.WRITING);<a name="line.970"></a>
-<span class="sourceLineNo">971</span>      this.unencodedDataSizeWritten +=<a name="line.971"></a>
-<span class="sourceLineNo">972</span>          this.dataBlockEncoder.encode(cell, dataBlockEncodingCtx, this.userDataStream);<a name="line.972"></a>
-<span class="sourceLineNo">973</span>    }<a name="line.973"></a>
-<span class="sourceLineNo">974</span><a name="line.974"></a>
-<span class="sourceLineNo">975</span>    /**<a name="line.975"></a>
-<span class="sourceLineNo">976</span>     * Returns the stream for the user to write to. The block writer takes care<a name="line.976"></a>
-<span class="sourceLineNo">977</span>     * of handling compression and buffering for caching on write. Can only be<a name="line.977"></a>
-<span class="sourceLineNo">978</span>     * called in the "writing" state.<a name="line.978"></a>
-<span class="sourceLineNo">979</span>     *<a name="line.979"></a>
-<span class="sourceLineNo">980</span>     * @return the data output stream for the user to write to<a name="line.980"></a>
-<span class="sourceLineNo">981</span>     */<a name="line.981"></a>
-<span class="sourceLineNo">982</span>    DataOutputStream getUserDataStream() {<a name="line.982"></a>
-<span class="sourceLineNo">983</span>      expectState(State.WRITING);<a name="line.983"></a>
-<span class="sourceLineNo">984</span>      return userDataStream;<a name="line.984"></a>
-<span class="sourceLineNo">985</span>    }<a name="line.985"></a>
-<span class="sourceLineNo">986</span><a name="line.986"></a>
-<span class="sourceLineNo">987</span>    /**<a name="line.987"></a>
-<span class="sourceLineNo">988</span>     * Transitions the block writer from the "writing" state to the "block<a name="line.988"></a>
-<span class="sourceLineNo">989</span>     * ready" state.  Does nothing if a block is already finished.<a name="line.989"></a>
-<span class="sourceLineNo">990</span>     */<a name="line.990"></a>
-<span class="sourceLineNo">991</span>    void ensureBlockReady() throws IOException {<a name="line.991"></a>
-<span class="sourceLineNo">992</span>      Preconditions.checkState(state != State.INIT,<a name="line.992"></a>
-<span class="sourceLineNo">993</span>          "Unexpected state: " + state);<a name="line.993"></a>
-<span class="sourceLineNo">994</span><a name="line.994"></a>
-<span class="sourceLineNo">995</span>      if (state == State.BLOCK_READY) {<a name="line.995"></a>
-<span class="sourceLineNo">996</span>        return;<a name="line.996"></a>
-<span class="sourceLineNo">997</span>      }<a name="line.997"></a>
-<span class="sourceLineNo">998</span><a name="line.998"></a>
-<span class="sourceLineNo">999</span>      // This will set state to BLOCK_READY.<a name="line.999"></a>
-<span class="sourceLineNo">1000</span>      finishBlock();<a name="line.1000"></a>
-<span class="sourceLineNo">1001</span>    }<a name="line.1001"></a>
-<span class="sourceLineNo">1002</span><a name="line.1002"></a>
-<span class="sourceLineNo">1003</span>    /**<a name="line.1003"></a>
-<span class="sourceLineNo">1004</span>     * Finish up writing of the block.<a name="line.1004"></a>
-<span class="sourceLineNo">1005</span>     * Flushes the compressing stream (if using compression), fills out the header,<a name="line.1005"></a>
-<span class="sourceLineNo">1006</span>     * does any compression/encryption of bytes to flush out to disk, and manages<a name="line.1006"></a>
-<span class="sourceLineNo">1007</span>     * the cache on write content, if applicable. Sets block write state to "block ready".<a name="line.1007"></a>
-<span class="sourceLineNo">1008</span>     */<a name="line.1008"></a>
-<span class="sourceLineNo">1009</span>    private void finishBlock() throws IOException {<a name="line.1009"></a>
-<span class="sourceLineNo">1010</span>      if (blockType == BlockType.DATA) {<a name="line.1010"></a>
-<span class="sourceLineNo">1011</span>        this.dataBlockEncoder.endBlockEncoding(dataBlockEncodingCtx, userDataStream,<a name="line.1011"></a>
-<span class="sourceLineNo">1012</span>            baosInMemory.getBuffer(), blockType);<a name="line.1012"></a>
-<span class="sourceLineNo">1013</span>        blockType = dataBlockEncodingCtx.getBlockType();<a name="line.1013"></a>
-<span class="sourceLineNo">1014</span>      }<a name="line.1014"></a>
-<span class="sourceLineNo">1015</span>      userDataStream.flush();<a name="line.1015"></a>
-<span class="sourceLineNo">1016</span>      prevOffset = prevOffsetByType[blockType.getId()];<a name="line.1016"></a>
-<span class="sourceLineNo">1017</span><a name="line.1017"></a>
-<span class="sourceLineNo">1018</span>      // We need to set state before we can package the block up for cache-on-write. In a way, the<a name="line.1018"></a>
-<span class="sourceLineNo">1019</span>      // block is ready, but not yet encoded or compressed.<a name="line.1019"></a>
-<span class="sourceLineNo">1020</span>      state = State.BLOCK_READY;<a name="line.1020"></a>
-<span class="sourceLineNo">1021</span>      Bytes compressAndEncryptDat;<a name="line.1021"></a>
-<span class="sourceLineNo">1022</span>      if (blockType == BlockType.DATA || blockType == BlockType.ENCODED_DATA) {<a name="line.1022"></a>
-<span class="sourceLineNo">1023</span>        compressAndEncryptDat = dataBlockEncodingCtx.<a name="line.1023"></a>
-<span class="sourceLineNo">1024</span>            compressAndEncrypt(baosInMemory.getBuffer(), 0, baosInMemory.size());<a name="line.1024"></a>
-<span class="sourceLineNo">1025</span>      } else {<a name="line.1025"></a>
-<span class="sourceLineNo">1026</span>        compressAndEncryptDat = defaultBlockEncodingCtx.<a name="line.1026"></a>
-<span class="sourceLineNo">1027</span>            compressAndEncrypt(baosInMemory.getBuffer(), 0, baosInMemory.size());<a name="line.1027"></a>
-<span class="sourceLineNo">1028</span>      }<a name="line.1028"></a>
-<span class="sourceLineNo">1029</span>      if (compressAndEncryptDat == null) {<a name="line.1029"></a>
-<span class="sourceLineNo">1030</span>        compressAndEncryptDat = new Bytes(baosInMemory.getBuffer(), 0, baosInMemory.size());<a name="line.1030"></a>
-<span class="sourceLineNo">1031</span>      }<a name="line.1031"></a>
-<span class="sourceLineNo">1032</span>      if (onDiskBlockBytesWithHeader == null) {<a name="line.1032"></a>
-<span class="sourceLineNo">1033</span>        onDiskBlockBytesWithHeader = new ByteArrayOutputStream(compressAndEncryptDat.getLength());<a name="line.1033"></a>
-<span class="sourceLineNo">1034</span>      }<a name="line.1034"></a>
-<span class="sourceLineNo">1035</span>      onDiskBlockBytesWithHeader.reset();<a name="line.1035"></a>
-<span class="sourceLineNo">1036</span>      onDiskBlockBytesWithHeader.write(compressAndEncryptDat.get(),<a name="line.1036"></a>
-<span class="sourceLineNo">1037</span>            compressAndEncryptDat.getOffset(), compressAndEncryptDat.getLength());<a name="line.1037"></a>
-<span class="sourceLineNo">1038</span>      // Calculate how many bytes we need for checksum on the tail of the block.<a name="line.1038"></a>
-<span class="sourceLineNo">1039</span>      int numBytes = (int) ChecksumUtil.numBytes(<a name="line.1039"></a>
-<span class="sourceLineNo">1040</span>          onDiskBlockBytesWithHeader.size(),<a name="line.1040"></a>
-<span class="sourceLineNo">1041</span>          fileContext.getBytesPerChecksum());<a name="line.1041"></a>
-<span class="sourceLineNo">1042</span><a name="line.1042"></a>
-<span class="sourceLineNo">1043</span>      // Put the header for the on disk bytes; header currently is unfilled-out<a name="line.1043"></a>
-<span class="sourceLineNo">1044</span>      putHeader(onDiskBlockBytesWithHeader,<a name="line.1044"></a>
-<span class="sourceLineNo">1045</span>          onDiskBlockBytesWithHeader.size() + numBytes,<a name="line.1045"></a>
-<span class="sourceLineNo">1046</span>          baosInMemory.size(), onDiskBlockBytesWithHeader.size());<a name="line.1046"></a>
-<span class="sourceLineNo">1047</span>      if (onDiskChecksum.length != numBytes) {<a name="line.1047"></a>
-<span class="sourceLineNo">1048</span>        onDiskChecksum = new byte[numBytes];<a name="line.1048"></a>
-<span class="sourceLineNo">1049</span>      }<a name="line.1049"></a>
-<span class="sourceLineNo">1050</span>      ChecksumUtil.generateChecksums(<a name="line.1050"></a>
-<span class="sourceLineNo">1051</span>          onDiskBlockBytesWithHeader.getBuffer(), 0,onDiskBlockBytesWithHeader.size(),<a name="line.1051"></a>
-<span class="sourceLineNo">1052</span>          onDiskChecksum, 0, fileContext.getChecksumType(), fileContext.getBytesPerChecksum());<a name="line.1052"></a>
-<span class="sourceLineNo">1053</span>    }<a name="line.1053"></a>
-<span class="sourceLineNo">1054</span><a name="line.1054"></a>
-<span class="sourceLineNo">1055</span>    /**<a name="line.1055"></a>
-<span class="sourceLineNo">1056</span>     * Put the header into the given byte array at the given offset.<a name="line.1056"></a>
-<span class="sourceLineNo">1057</span>     * @param onDiskSize size of the block on disk header + data + checksum<a name="line.1057"></a>
-<span class="sourceLineNo">1058</span>     * @param uncompressedSize size of the block after decompression (but<a name="line.1058"></a>
-<span class="sourceLineNo">1059</span>     *          before optional data block decoding) including header<a name="line.1059"></a>
-<span class="sourceLineNo">1060</span>     * @param onDiskDataSize size of the block on disk with header<a name="line.1060"></a>
-<span class="sourceLineNo">1061</span>     *        and data but not including the checksums<a name="line.1061"></a>
-<span class="sourceLineNo">1062</span>     */<a name="line.1062"></a>
-<span class="sourceLineNo">1063</span>    private void putHeader(byte[] dest, int offset, int onDiskSize,<a name="line.1063"></a>
-<span class="sourceLineNo">1064</span>        int uncompressedSize, int onDiskDataSize) {<a name="line.1064"></a>
-<span class="sourceLineNo">1065</span>      offset = blockType.put(dest, offset);<a name="line.1065"></a>
-<span class="sourceLineNo">1066</span>      offset = Bytes.putInt(dest, offset, onDiskSize - HConstants.HFILEBLOCK_HEADER_SIZE);<a name="line.1066"></a>
-<span class="sourceLineNo">1067</span>      offset = Bytes.putInt(dest, offset, uncompressedSize - HConstants.HFILEBLOCK_HEADER_SIZE);<a name="line.1067"></a>
-<span class="sourceLineNo">1068</span>      offset = Bytes.putLong(dest, offset, prevOffset);<a name="line.1068"></a>
-<span class="sourceLineNo">1069</span>      offset = Bytes.putByte(dest, offset, fileContext.getChecksumType().getCode());<a name="line.1069"></a>
-<span class="sourceLineNo">1070</span>      offset = Bytes.putInt(dest, offset, fileContext.getBytesPerChecksum());<a name="line.1070"></a>
-<span class="sourceLineNo">1071</span>      Bytes.putInt(dest, offset, onDiskDataSize);<a name="line.1071"></a>
-<span class="sourceLineNo">1072</span>    }<a name="line.1072"></a>
-<span class="sourceLineNo">1073</span><a name="line.1073"></a>
-<span class="sourceLineNo">1074</span>    private void putHeader(ByteArrayOutputStream dest, int onDiskSize,<a name="line.1074"></a>
-<span class="sourceLineNo">1075</span>        int uncompressedSize, int onDiskDataSize) {<a name="line.1075"></a>
-<span class="sourceLineNo">1076</span>      putHeader(dest.getBuffer(),0, onDiskSize, uncompressedSize, onDiskDataSize);<a name="line.1076"></a>
-<span class="sourceLineNo">1077</span>    }<a name="line.1077"></a>
-<span class="sourceLineNo">1078</span><a name="line.1078"></a>
-<span class="sourceLineNo">1079</span>    /**<a name="line.1079"></a>
-<span class="sourceLineNo">1080</span>     * Similar to {@link #writeHeaderAndData(FSDataOutputStream)}, but records<a name="line.1080"></a>
-<span class="sourceLineNo">1081</span>     * the offset of this block so that it can be referenced in the next block<a name="line.1081"></a>
-<span class="sourceLineNo">1082</span>     * of the same type.<a name="line.1082"></a>
-<span class="sourceLineNo">1083</span>     *<a name="line.1083"></a>
-<span class="sourceLineNo">1084</span>     * @param out<a name="line.1084"></a>
-<span class="sourceLineNo">1085</span>     * @throws IOException<a name="line.1085"></a>
-<span class="sourceLineNo">1086</span>     */<a name="line.1086"></a>
-<span class="sourceLineNo">1087</span>    void writeHeaderAndData(FSDataOutputStream out) throws IOException {<a name="line.1087"></a>
-<span class="sourceLineNo">1088</span>      long offset = out.getPos();<a name="line.1088"></a>
-<span class="sourceLineNo">1089</span>      if (startOffset != UNSET &amp;&amp; offset != startOffset) {<a name="line.1089"></a>
-<span class="sourceLineNo">1090</span>        throw new IOException("A " + blockType + " block written to a "<a name="line.1090"></a>
-<span class="sourceLineNo">1091</span>            + "stream twice, first at offset " + startOffset + ", then at "<a name="line.1091"></a>
-<span class="sourceLineNo">1092</span>            + offset);<a name="line.1092"></a>
-<span class="sourceLineNo">1093</span>      }<a name="line.1093"></a>
-<span class="sourceLineNo">1094</span>      startOffset = offset;<a name="line.1094"></a>
-<span class="sourceLineNo">1095</span><a name="line.1095"></a>
-<span class="sourceLineNo">1096</span>      finishBlockAndWriteHeaderAndData((DataOutputStream) out);<a name="line.1096"></a>
-<span class="sourceLineNo">1097</span>    }<a name="line.1097"></a>
-<span class="sourceLineNo">1098</span><a name="line.1098"></a>
-<span class="sourceLineNo">1099</span>    /**<a name="line.1099"></a>
-<span class="sourceLineNo">1100</span>     * Writes the header and the compressed data of this block (or uncompressed<a name="line.1100"></a>
-<span class="sourceLineNo">1101</span>     * data when not using compression) into the given stream. Can be called in<a name="line.1101"></a>
-<span class="sourceLineNo">1102</span>     * the "writing" state or in the "block ready" state. If called in the<a name="line.1102"></a>
-<span class="sourceLineNo">1103</span>     * "writing" state, transitions the writer to the "block ready" state.<a name="line.1103"></a>
-<span class="sourceLineNo">1104</span>     *<a name="line.1104"></a>
-<span class="sourceLineNo">1105</span>     * @param out the output stream to write the<a name="line.1105"></a>
-<span class="sourceLineNo">1106</span>     * @throws IOException<a name="line.1106"></a>
-<span class="sourceLineNo">1107</span>     */<a name="line.1107"></a>
-<span class="sourceLineNo">1108</span>    protected void finishBlockAndWriteHeaderAndData(DataOutputStream out)<a name="line.1108"></a>
-<span class="sourceLineNo">1109</span>      throws IOException {<a name="line.1109"></a>
-<span class="sourceLineNo">1110</span>      ensureBlockReady();<a name="line.1110"></a>
-<span class="sourceLineNo">1111</span>      out.write(onDiskBlockBytesWithHeader.getBuffer(), 0, onDiskBlockBytesWithHeader.size());<a name="line.1111"></a>
-<span class="sourceLineNo">1112</span>      out.write(onDiskChecksum);<a name="line.1112"></a>
-<span class="sourceLineNo">1113</span>    }<a name="line.1113"></a>
-<span class="sourceLineNo">1114</span><a name="line.1114"></a>
-<span class="sourceLineNo">1115</span>    /**<a name="line.1115"></a>
-<span class="sourceLineNo">1116</span>     * Returns the header or the compressed data (or uncompressed data when not<a name="line.1116"></a>
-<span class="sourceLineNo">1117</span>     * using compression) as a byte array. Can be called in the "writing" state<a name="line.1117"></a>
-<span class="sourceLineNo">1118</span>     * or in the "block ready" state. If called in the "writing" state,<a name="line.1118"></a>
-<span class="sourceLineNo">1119</span>     * transitions the writer to the "block ready" state. This returns<a name="line.1119"></a>
-<span class="sourceLineNo">1120</span>     * the header + data + checksums stored on disk.<a name="line.1120"></a>
-<span class="sourceLineNo">1121</span>     *<a name="line.1121"></a>
-<span class="sourceLineNo">1122</span>     * @return header and data as they would be stored on disk in a byte array<a name="line.1122"></a>
-<span class="sourceLineNo">1123</span>     * @throws IOException<a name="line.1123"></a>
-<span class="sourceLineNo">1124</span>     */<a name="line.1124"></a>
-<span class="sourceLineNo">1125</span>    byte[] getHeaderAndDataForTest() throws IOException {<a name="line.1125"></a>
-<span class="sourceLineNo">1126</span>      ensureBlockReady();<a name="line.1126"></a>
-<span class="sourceLineNo">1127</span>      // This is not very optimal, because we are doing an extra copy.<a name="line.1127"></a>
-<span class="sourceLineNo">1128</span>      // But this method is used only by unit tests.<a name="line.1128"></a>
-<span class="sourceLineNo">1129</span>      byte[] output =<a name="line.1129"></a>
-<span class="sourceLineNo">1130</span>          new byte[onDiskBlockBytesWithHeader.size()<a name="line.1130"></a>
-<span class="sourceLineNo">1131</span>              + onDiskChecksum.length];<a name="line.1131"></a>
-<span class="sourceLineNo">1132</span>      System.arraycopy(onDiskBlockBytesWithHeader.getBuffer(), 0, output, 0,<a name="line.1132"></a>
-<span class="sourceLineNo">1133</span>          onDiskBlockBytesWithHeader.size());<a name="line.1133"></a>
-<span class="sourceLineNo">1134</span>      System.arraycopy(onDiskChecksum, 0, output,<a name="line.1134"></a>
-<span class="sourceLineNo">1135</span>          onDiskBlockBytesWithHeader.size(), onDiskChecksum.length);<a name="line.1135"></a>
-<span class="sourceLineNo">1136</span>      return output;<a name="line.1136"></a>
-<span class="sourceLineNo">1137</span>    }<a name="line.1137"></a>
-<span class="sourceLineNo">1138</span><a name="line.1138"></a>
-<span class="sourceLineNo">1139</span>    /**<a name="line.1139"></a>
-<span class="sourceLineNo">1140</span>     * Releases resources used by this writer.<a name="line.1140"></a>
-<span class="sourceLineNo">1141</span>     */<a name="line.1141"></a>
-<span class="sourceLineNo">1142</span>    void release() {<a name="line.1142"></a>
-<span class="sourceLineNo">1143</span>      if (dataBlockEncodingCtx != null) {<a name="line.1143"></a>
-<span class="sourceLineNo">1144</span>        dataBlockEncodingCtx.close();<a name="line.1144"></a>
-<span class="sourceLineNo">1145</span>        dataBlockEncodingCtx = null;<a name="line.1145"></a>
-<span class="sourceLineNo">1146</span>      }<a name="line.1146"></a>
-<span class="sourceLineNo">1147</span>      if (defaultBlockEncodingCtx != null) {<a name="line.1147"></a>
-<span class="sourceLineNo">1148</span>        defaultBlockEncodingCtx.close();<a name="line.1148"></a>
-<span class="sourceLineNo">1149</span>        defaultBlockEncodingCtx = null;<a name="line.1149"></a>
-<span class="sourceLineNo">1150</span>      }<a name="line.1150"></a>
-<span class="sourceLineNo">1151</span>    }<a name="line.1151"></a>
-<span class="sourceLineNo">1152</span><a name="line.1152"></a>
-<span class="sourceLineNo">1153</span>    /**<a name="line.1153"></a>
-<span class="sourceLineNo">1154</span>     * Returns the on-disk size of the data portion of the block. This is the<a name="line.1154"></a>
-<span class="sourceLineNo">1155</span>     * compressed size if compression is enabled. Can only be called in the<a name="line.1155"></a>
-<span class="sourceLineNo">1156</span>     * "block ready" state. Header is not compressed, and its size is not<a name="line.1156"></a>
-<span class="sourceLineNo">1157</span>     * included in the return value.<a name="line.1157"></a>
-<span class="sourceLineNo">1158</span>     *<a name="line.1158"></a>
-<span class="sourceLineNo">1159</span>     * @return the on-disk size of the block, not including the header.<a name="line.1159"></a>
-<span class="sourceLineNo">1160</span>     */<a name="line.1160"></a>
-<span class="sourceLineNo">1161</span>    int getOnDiskSizeWithoutHeader() {<a name="line.1161"></a>
-<span class="sourceLineNo">1162</span>      expectState(State.BLOCK_READY);<a name="line.1162"></a>
-<span class="sourceLineNo">1163</span>      return onDiskBlockBytesWithHeader.size() +<a name="line.1163"></a>
-<span class="sourceLineNo">1164</span>          onDiskChecksum.length - HConstants.HFILEBLOCK_HEADER_SIZE;<a name="line.1164"></a>
-<span class="sourceLineNo">1165</span>    }<a name="line.1165"></a>
-<span class="sourceLineNo">1166</span><a name="line.1166"></a>
-<span class="sourceLineNo">1167</span>    /**<a name="line.1167"></a>
-<span class="sourceLineNo">1168</span>     * Returns the on-disk size of the block. Can only be called in the<a name="line.1168"></a>
-<span class="sourceLineNo">1169</span>     * "block ready" state.<a name="line.1169"></a>
-<span class="sourceLineNo">1170</span>     *<a name="line.1170"></a>
-<span class="sourceLineNo">1171</span>     * @return the on-disk size of the block ready to be written, including the<a name="line.1171"></a>
-<span class="sourceLineNo">1172</span>     *         header size, the data and the checksum data.<a name="line.1172"></a>
-<span class="sourceLineNo">1173</span>     */<a name="line.1173"></a>
-<span class="sourceLineNo">1174</span>    int getOnDiskSizeWithHeader() {<a name="line.1174"></a>
-<span class="sourceLineNo">1175</span>      expectState(State.BLOCK_READY);<a name="line.1175"></a>
-<span class="sourceLineNo">1176</span>      return onDiskBlockBytesWithHeader.size() + onDiskChecksum.length;<a name="line.1176"></a>
-<span class="sourceLineNo">1177</span>    }<a name="line.1177"></a>
-<span class="sourceLineNo">1178</span><a name="line.1178"></a>
-<span class="sourceLineNo">1179</span>    /**<a name="line.1179"></a>
-<span class="sourceLineNo">1180</span>     * The uncompressed size of the block data. Does not include header size.<a name="line.1180"></a>
-<span class="sourceLineNo">1181</span>     */<a name="line.1181"></a>
-<span class="sourceLineNo">1182</span>    int getUncompressedSizeWithoutHeader() {<a name="line.1182"></a>
-<span class="sourceLineNo">1183</span>      expectState(State.BLOCK_READY);<a name="line.1183"></a>
-<span class="sourceLineNo">1184</span>      return baosInMemory.size() - HConstants.HFILEBLOCK_HEADER_SIZE;<a name="line.1184"></a>
-<span class="sourceLineNo">1185</span>    }<a name="line.1185"></a>
-<span class="sourceLineNo">1186</span><a name="line.1186"></a>
-<span class="sourceLineNo">1187</span>    /**<a name="line.1187"></a>
-<span class="sourceLineNo">1188</span>     * The uncompressed size of the block data, including header size.<a name="line.1188"></a>
-<span class="sourceLineNo">1189</span>     */<a name="line.1189"></a>
-<span class="sourceLineNo">1190</span>    int getUncompressedSizeWithHeader() {<a name="line.1190"></a>
-<span class="sourceLineNo">1191</span>      expectState(State.BLOCK_READY);<a name="line.1191"></a>
-<span class="sourceLineNo">1192</span>      return baosInMemory.size();<a name="line.1192"></a>
-<span class="sourceLineNo">1193</span>    }<a name="line.1193"></a>
-<span class="sourceLineNo">1194</span><a name="line.1194"></a>
-<span class="sourceLineNo">1195</span>    /** @return true if a block is being written  */<a name="line.1195"></a>
-<span class="sourceLineNo">1196</span>    boolean isWriting() {<a name="line.1196"></a>
-<span class="sourceLineNo">1197</span>      return state == State.WRITING;<a name="line.1197"></a>
-<span class="sourceLineNo">1198</span>    }<a name="line.1198"></a>
-<span class="sourceLineNo">1199</span><a name="line.1199"></a>
-<span class="sourceLineNo">1200</span>    /**<a name="line.1200"></a>
-<span class="sourceLineNo">1201</span>     * Returns the number of bytes written into the current block so far, or<a name="line.1201"></a>
-<span class="sourceLineNo">1202</span>     * zero if not writing the block at the moment. Note that this will return<a name="line.1202"></a>
-<span class="sourceLineNo">1203</span>     * zero in the "block ready" state as well.<a name="line.1203"></a>
-<span class="sourceLineNo">1204</span>     *<a name="line.1204"></a>
-<span class="sourceLineNo">1205</span>     * @return the number of bytes written<a name="line.1205"></a>
-<span class="sourceLineNo">1206</span>     */<a name="line.1206"></a>
-<span class="sourceLineNo">1207</span>    int blockSizeWritten() {<a name="line.1207"></a>
-<span class="sourceLineNo">1208</span>      if (state != State.WRITING) return 0;<a name="line.1208"></a>
-<span class="sourceLineNo">1209</span>      return this.unencodedDataSizeWritten;<a name="line.1209"></a>
-<span class="sourceLineNo">1210</span>    }<a name="line.1210"></a>
-<span class="sourceLineNo">1211</span><a name="line.1211"></a>
-<span class="sourceLineNo">1212</span>    /**<a name="line.1212"></a>
-<span class="sourceLineNo">1213</span>     * Clones the header followed by the uncompressed data, even if using<a name="line.1213"></a>
-<span class="sourceLineNo">1214</span>     * compression. This is needed for storing uncompressed blocks in the block<a name="line.1214"></a>
-<span class="sourceLineNo">1215</span>     * cache. Can be called in the "writing" state or the "block ready" state.<a name="line.1215"></a>
-<span class="sourceLineNo">1216</span>     * Returns only the header and data, does not include checksum data.<a name="line.1216"></a>
-<span class="sourceLineNo">1217</span>     *<a name="line.1217"></a>
-<span class="sourceLineNo">1218</span>     * @return Returns a copy of uncompressed block bytes for caching on write<a name="line.1218"></a>
-<span class="sourceLineNo">1219</span>     */<a name="line.1219"></a>
-<span class="sourceLineNo">1220</span>    @VisibleForTesting<a name="line.1220"></a>
-<span class="sourceLineNo">1221</span>    ByteBuffer cloneUncompressedBufferWithHeader() {<a name="line.1221"></a>
-<span class="sourceLineNo">1222</span>      expectState(State.BLOCK_READY);<a name="line.1222"></a>
-<span class="sourceLineNo">1223</span>      byte[] uncompressedBlockBytesWithHeader = baosInMemory.toByteArray();<a name="line.1223"></a>
-<span class="sourceLineNo">1224</span>      int numBytes = (int) ChecksumUtil.numBytes(<a name="line.1224"></a>
-<span class="sourceLineNo">1225</span>          onDiskBlockBytesWithHeader.size(),<a name="line.1225"></a>
-<span class="sourceLineNo">1226</span>          fileContext.getBytesPerChecksum());<a name="line.1226"></a>
-<span class="sourceLineNo">1227</span>      putHeader(uncompressedBlockBytesWithHeader, 0,<a name="line.1227"></a>
-<span class="sourceLineNo">1228</span>        onDiskBlockBytesWithHeader.size() + numBytes,<a name="line.1228"></a>
-<span class="sourceLineNo">1229</span>        baosInMemory.size(), onDiskBlockBytesWithHeader.size());<a name="line.1229"></a>
-<span class="sourceLineNo">1230</span>      return ByteBuffer.wrap(uncompressedBlockBytesWithHeader);<a name="line.1230"></a>
-<span class="sourceLineNo">1231</span>    }<a name="line.1231"></a>
-<span class="sourceLineNo">1232</span><a name="line.1232"></a>
-<span class="sourceLineNo">1233</span>    /**<a name="line.1233"></a>
-<span class="sourceLineNo">1234</span>     * Clones the header followed by the on-disk (compressed/encoded/encrypted) data. This is<a name="line.1234"></a>
-<span class="sourceLineNo">1235</span>     * needed for storing packed blocks in the block cache. Expects calling semantics identical to<a name="line.1235"></a>
-<span class="sourceLineNo">1236</span>     * {@link #getUncompressedBufferWithHeader()}. Returns only the header and data,<a name="line.1236"></a>
-<span class="sourceLineNo">1237</span>     * Does not include checksum data.<a name="line.1237"></a>
-<span class="sourceLineNo">1238</span>     *<a name="line.1238"></a>
-<span class="sourceLineNo">1239</span>     * @return Returns a copy of block bytes for caching on write<a name="line.1239"></a>
-<span class="sourceLineNo">1240</span>     */<a name="line.1240"></a>
-<span class="sourceLineNo">1241</span>    private ByteBuffer cloneOnDiskBufferWithHeader() {<a name="line.1241"></a>
+<span class="sourceLineNo">874</span>    // Size of actual data being written. considering the block encoding. This<a name="line.874"></a>
+<span class="sourceLineNo">875</span>    // includes the header size also.<a name="line.875"></a>
+<span class="sourceLineNo">876</span>    private int encodedDataSizeWritten;<a name="line.876"></a>
+<span class="sourceLineNo">877</span><a name="line.877"></a>
+<span class="sourceLineNo">878</span>    /**<a name="line.878"></a>
+<span class="sourceLineNo">879</span>     * Bytes to be written to the file system, including the header. Compressed<a name="line.879"></a>
+<span class="sourceLineNo">880</span>     * if compression is turned on. It also includes the checksum data that<a name="line.880"></a>
+<span class="sourceLineNo">881</span>     * immediately follows the block data. (header + data + checksums)<a name="line.881"></a>
+<span class="sourceLineNo">882</span>     */<a name="line.882"></a>
+<span class="sourceLineNo">883</span>    private ByteArrayOutputStream onDiskBlockBytesWithHeader;<a name="line.883"></a>
+<span class="sourceLineNo">884</span><a name="line.884"></a>
+<span class="sourceLineNo">885</span>    /**<a name="line.885"></a>
+<span class="sourceLineNo">886</span>     * The size of the checksum data on disk. It is used only if data is<a name="line.886"></a>
+<span class="sourceLineNo">887</span>     * not compressed. If data is compressed, then the checksums are already<a name="line.887"></a>
+<span class="sourceLineNo">888</span>     * part of onDiskBytesWithHeader. If data is uncompressed, then this<a name="line.888"></a>
+<span class="sourceLineNo">889</span>     * variable stores the checksum data for this block.<a name="line.889"></a>
+<span class="sourceLineNo">890</span>     */<a name="line.890"></a>
+<span class="sourceLineNo">891</span>    private byte[] onDiskChecksum = HConstants.EMPTY_BYTE_ARRAY;<a name="line.891"></a>
+<span class="sourceLineNo">892</span><a name="line.892"></a>
+<span class="sourceLineNo">893</span>    /**<a name="line.893"></a>
+<span class="sourceLineNo">894</span>     * Current block's start offset in the {@link HFile}. Set in<a name="line.894"></a>
+<span class="sourceLineNo">895</span>     * {@link #writeHeaderAndData(FSDataOutputStream)}.<a name="line.895"></a>
+<span class="sourceLineNo">896</span>     */<a name="line.896"></a>
+<span class="sourceLineNo">897</span>    private long startOffset;<a name="line.897"></a>
+<span class="sourceLineNo">898</span><a name="line.898"></a>
+<span class="sourceLineNo">899</span>    /**<a name="line.899"></a>
+<span class="sourceLineNo">900</span>     * Offset of previous block by block type. Updated when the next block is<a name="line.900"></a>
+<span class="sourceLineNo">901</span>     * started.<a name="line.901"></a>
+<span class="sourceLineNo">902</span>     */<a name="line.902"></a>
+<span class="sourceLineNo">903</span>    private long[] prevOffsetByType;<a name="line.903"></a>
+<span class="sourceLineNo">904</span><a name="line.904"></a>
+<span class="sourceLineNo">905</span>    /** The offset of the previous block of the same type */<a name="line.905"></a>
+<span class="sourceLineNo">906</span>    private long prevOffset;<a name="line.906"></a>
+<span class="sourceLineNo">907</span>    /** Meta data that holds information about the hfileblock**/<a name="line.907"></a>
+<span class="sourceLineNo">908</span>    private HFileContext fileContext;<a name="line.908"></a>
+<span class="sourceLineNo">909</span><a name="line.909"></a>
+<span class="sourceLineNo">910</span>    /**<a name="line.910"></a>
+<span class="sourceLineNo">911</span>     * @param dataBlockEncoder data block encoding algorithm to use<a name="line.911"></a>
+<span class="sourceLineNo">912</span>     */<a name="line.912"></a>
+<span class="sourceLineNo">913</span>    public Writer(HFileDataBlockEncoder dataBlockEncoder, HFileContext fileContext) {<a name="line.913"></a>
+<span class="sourceLineNo">914</span>      if (fileContext.getBytesPerChecksum() &lt; HConstants.HFILEBLOCK_HEADER_SIZE) {<a name="line.914"></a>
+<span class="sourceLineNo">915</span>        throw new RuntimeException("Unsupported value of bytesPerChecksum. " +<a name="line.915"></a>
+<span class="sourceLineNo">916</span>            " Minimum is " + HConstants.HFILEBLOCK_HEADER_SIZE + " but the configured value is " +<a name="line.916"></a>
+<span class="sourceLineNo">917</span>            fileContext.getBytesPerChecksum());<a name="line.917"></a>
+<span class="sourceLineNo">918</span>      }<a name="line.918"></a>
+<span class="sourceLineNo">919</span>      this.dataBlockEncoder = dataBlockEncoder != null?<a name="line.919"></a>
+<span class="sourceLineNo">920</span>          dataBlockEncoder: NoOpDataBlockEncoder.INSTANCE;<a name="line.920"></a>
+<span class="sourceLineNo">921</span>      this.dataBlockEncodingCtx = this.dataBlockEncoder.<a name="line.921"></a>
+<span class="sourceLineNo">922</span>          newDataBlockEncodingContext(HConstants.HFILEBLOCK_DUMMY_HEADER, fileContext);<a name="line.922"></a>
+<span class="sourceLineNo">923</span>      // TODO: This should be lazily instantiated since we usually do NOT need this default encoder<a name="line.923"></a>
+<span class="sourceLineNo">924</span>      this.defaultBlockEncodingCtx = new HFileBlockDefaultEncodingContext(null,<a name="line.924"></a>
+<span class="sourceLineNo">925</span>          HConstants.HFILEBLOCK_DUMMY_HEADER, fileContext);<a name="line.925"></a>
+<span class="sourceLineNo">926</span>      // TODO: Set BAOS initial size. Use fileContext.getBlocksize() and add for header/checksum<a name="line.926"></a>
+<span class="sourceLineNo">927</span>      baosInMemory = new ByteArrayOutputStream();<a name="line.927"></a>
+<span class="sourceLineNo">928</span>      prevOffsetByType = new long[BlockType.values().length];<a name="line.928"></a>
+<span class="sourceLineNo">929</span>      for (int i = 0; i &lt; prevOffsetByType.length; ++i) {<a name="line.929"></a>
+<span class="sourceLineNo">930</span>        prevOffsetByType[i] = UNSET;<a name="line.930"></a>
+<span class="sourceLineNo">931</span>      }<a name="line.931"></a>
+<span class="sourceLineNo">932</span>      // TODO: Why fileContext saved away when we have dataBlockEncoder and/or<a name="line.932"></a>
+<span class="sourceLineNo">933</span>      // defaultDataBlockEncoder?<a name="line.933"></a>
+<span class="sourceLineNo">934</span>      this.fileContext = fileContext;<a name="line.934"></a>
+<span class="sourceLineNo">935</span>    }<a name="line.935"></a>
+<span class="sourceLineNo">936</span><a name="line.936"></a>
+<span class="sourceLineNo">937</span>    /**<a name="line.937"></a>
+<span class="sourceLineNo">938</span>     * Starts writing into the block. The previous block's data is discarded.<a name="line.938"></a>
+<span class="sourceLineNo">939</span>     *<a name="line.939"></a>
+<span class="sourceLineNo">940</span>     * @return the stream the user can write their data into<a name="line.940"></a>
+<span class="sourceLineNo">941</span>     * @throws IOException<a name="line.941"></a>
+<span class="sourceLineNo">942</span>     */<a name="line.942"></a>
+<span class="sourceLineNo">943</span>    DataOutputStream startWriting(BlockType newBlockType)<a name="line.943"></a>
+<span class="sourceLineNo">944</span>        throws IOException {<a name="line.944"></a>
+<span class="sourceLineNo">945</span>      if (state == State.BLOCK_READY &amp;&amp; startOffset != -1) {<a name="line.945"></a>
+<span class="sourceLineNo">946</span>        // We had a previous block that was written to a stream at a specific<a name="line.946"></a>
+<span class="sourceLineNo">947</span>        // offset. Save that offset as the last offset of a block of that type.<a name="line.947"></a>
+<span class="sourceLineNo">948</span>        prevOffsetByType[blockType.getId()] = startOffset;<a name="line.948"></a>
+<span class="sourceLineNo">949</span>      }<a name="line.949"></a>
+<span class="sourceLineNo">950</span><a name="line.950"></a>
+<span class="sourceLineNo">951</span>      startOffset = -1;<a name="line.951"></a>
+<span class="sourceLineNo">952</span>      blockType = newBlockType;<a name="line.952"></a>
+<span class="sourceLineNo">953</span><a name="line.953"></a>
+<span class="sourceLineNo">954</span>      baosInMemory.reset();<a name="line.954"></a>
+<span class="sourceLineNo">955</span>      baosInMemory.write(HConstants.HFILEBLOCK_DUMMY_HEADER);<a name="line.955"></a>
+<span class="sourceLineNo">956</span><a name="line.956"></a>
+<span class="sourceLineNo">957</span>      state = State.WRITING;<a name="line.957"></a>
+<span class="sourceLineNo">958</span><a name="line.958"></a>
+<span class="sourceLineNo">959</span>      // We will compress it later in finishBlock()<a name="line.959"></a>
+<span class="sourceLineNo">960</span>      userDataStream = new ByteBufferWriterDataOutputStream(baosInMemory);<a name="line.960"></a>
+<span class="sourceLineNo">961</span>      if (newBlockType == BlockType.DATA) {<a name="line.961"></a>
+<span class="sourceLineNo">962</span>        this.dataBlockEncoder.startBlockEncoding(dataBlockEncodingCtx, userDataStream);<a name="line.962"></a>
+<span class="sourceLineNo">963</span>      }<a name="line.963"></a>
+<span class="sourceLineNo">964</span>      this.unencodedDataSizeWritten = 0;<a name="line.964"></a>
+<span class="sourceLineNo">965</span>      this.encodedDataSizeWritten = 0;<a name="line.965"></a>
+<span class="sourceLineNo">966</span>      return userDataStream;<a name="line.966"></a>
+<span class="sourceLineNo">967</span>    }<a name="line.967"></a>
+<span class="sourceLineNo">968</span><a name="line.968"></a>
+<span class="sourceLineNo">969</span>    /**<a name="line.969"></a>
+<span class="sourceLineNo">970</span>     * Writes the Cell to this block<a name="line.970"></a>
+<span class="sourceLineNo">971</span>     * @param cell<a name="line.971"></a>
+<span class="sourceLineNo">972</span>     * @throws IOException<a name="line.972"></a>
+<span class="sourceLineNo">973</span>     */<a name="line.973"></a>
+<span class="sourceLineNo">974</span>    void write(Cell cell) throws IOException{<a name="line.974"></a>
+<span class="sourceLineNo">975</span>      expectState(State.WRITING);<a name="line.975"></a>
+<span class="sourceLineNo">976</span>      int posBeforeEncode = this.userDataStream.size();<a name="line.976"></a>
+<span class="sourceLineNo">977</span>      this.unencodedDataSizeWritten +=<a name="line.977"></a>
+<span class="sourceLineNo">978</span>          this.dataBlockEncoder.encode(cell, dataBlockEncodingCtx, this.userDataStream);<a name="line.978"></a>
+<span class="sourceLineNo">979</span>      this.encodedDataSizeWritten += this.userDataStream.size() - posBeforeEncode;<a name="line.979"></a>
+<span class="sourceLineNo">980</span>    }<a name="line.980"></a>
+<span class="sourceLineNo">981</span><a name="line.981"></a>
+<span class="sourceLineNo">982</span>    /**<a name="line.982"></a>
+<span class="sourceLineNo">983</span>     * Returns the stream for the user to write to. The block writer takes care<a name="line.983"></a>
+<span class="sourceLineNo">984</span>     * of handling compression and buffering for caching on write. Can only be<a name="line.984"></a>
+<span class="sourceLineNo">985</span>     * called in the "writing" state.<a name="line.985"></a>
+<span class="sourceLineNo">986</span>     *<a name="line.986"></a>
+<span class="sourceLineNo">987</span>     * @return the data output stream for the user to write to<a name="line.987"></a>
+<span class="sourceLineNo">988</span>     */<a name="line.988"></a>
+<span class="sourceLineNo">989</span>    DataOutputStream getUserDataStream() {<a name="line.989"></a>
+<span class="sourceLineNo">990</span>      expectState(State.WRITING);<a name="line.990"></a>
+<span class="sourceLineNo">991</span>      return userDataStream;<a name="line.991"></a>
+<span class="sourceLineNo">992</span>    }<a name="line.992"></a>
+<span class="sourceLineNo">993</span><a name="line.993"></a>
+<span class="sourceLineNo">994</span>    /**<a name="line.994"></a>
+<span class="sourceLineNo">995</span>     * Transitions the block writer from the "writing" state to the "block<a name="line.995"></a>
+<span class="sourceLineNo">996</span>     * ready" state.  Does nothing if a block is already finished.<a name="line.996"></a>
+<span class="sourceLineNo">997</span>     */<a name="line.997"></a>
+<span class="sourceLineNo">998</span>    void ensureBlockReady() throws IOException {<a name="line.998"></a>
+<span class="sourceLineNo">999</span>      Preconditions.checkState(state != State.INIT,<a name="line.999"></a>
+<span class="sourceLineNo">1000</span>          "Unexpected state: " + state);<a name="line.1000"></a>
+<span class="sourceLineNo">1001</span><a name="line.1001"></a>
+<span class="sourceLineNo">1002</span>      if (state == State.BLOCK_READY) {<a name="line.1002"></a>
+<span class="sourceLineNo">1003</span>        return;<a name="line.1003"></a>
+<span class="sourceLineNo">1004</span>      }<a name="line.1004"></a>
+<span class="sourceLineNo">1005</span><a name="line.1005"></a>
+<span class="sourceLineNo">1006</span>      // This will set state to BLOCK_READY.<a name="line.1006"></a>
+<span class="sourceLineNo">1007</span>      finishBlock();<a name="line.1007"></a>
+<span class="sourceLineNo">1008</span>    }<a name="line.1008"></a>
+<span class="sourceLineNo">1009</span><a name="line.1009"></a>
+<span class="sourceLineNo">1010</span>    /**<a name="line.1010"></a>
+<span class="sourceLineNo">1011</span>     * Finish up writing of the block.<a name="line.1011"></a>
+<span class="sourceLineNo">1012</span>     * Flushes the compressing stream (if using compression), fills out the header,<a name="line.1012"></a>
+<span class="sourceLineNo">1013</span>     * does any compression/encryption of bytes to flush out to disk, and manages<a name="line.1013"></a>
+<span class="sourceLineNo">1014</span>     * the cache on write content, if applicable. Sets block write state to "block ready".<a name="line.1014"></a>
+<span class="sourceLineNo">1015</span>     */<a name="line.1015"></a>
+<span class="sourceLineNo">1016</span>    private void finishBlock() throws IOException {<a name="line.1016"></a>
+<span class="sourceLineNo">1017</span>      if (blockType == BlockType.DATA) {<a name="line.1017"></a>
+<span class="sourceLineNo">1018</span>        this.dataBlockEncoder.endBlockEncoding(dataBlockEncodingCtx, userDataStream,<a name="line.1018"></a>
+<span class="sourceLineNo">1019</span>            baosInMemory.getBuffer(), blockType);<a name="line.1019"></a>
+<span class="sourceLineNo">1020</span>        blockType = dataBlockEncodingCtx.getBlockType();<a name="line.1020"></a>
+<span class="sourceLineNo">1021</span>      }<a name="line.1021"></a>
+<span class="sourceLineNo">1022</span>      userDataStream.flush();<a name="line.1022"></a>
+<span class="sourceLineNo">1023</span>      prevOffset = prevOffsetByType[blockType.getId()];<a name="line.1023"></a>
+<span class="sourceLineNo">1024</span><a name="line.1024"></a>
+<span class="sourceLineNo">1025</span>      // We need to set state before we can package the block up for cache-on-write. In a way, the<a name="line.1025"></a>
+<span class="sourceLineNo">1026</span>      // block is ready, but not yet encoded or compressed.<a name="line.1026"></a>
+<span class="sourceLineNo">1027</span>      state = State.BLOCK_READY;<a name="line.1027"></a>
+<span class="sourceLineNo">1028</span>      Bytes compressAndEncryptDat;<a name="line.1028"></a>
+<span class="sourceLineNo">1029</span>      if (blockType == BlockType.DATA || blockType == BlockType.ENCODED_DATA) {<a name="line.1029"></a>
+<span class="sourceLineNo">1030</span>        compressAndEncryptDat = dataBlockEncodingCtx.<a name="line.1030"></a>
+<span class="sourceLineNo">1031</span>            compressAndEncrypt(baosInMemory.getBuffer(), 0, baosInMemory.size());<a name="line.1031"></a>
+<span class="sourceLineNo">1032</span>      } else {<a name="line.1032"></a>
+<span class="sourceLineNo">1033</span>        compressAndEncryptDat = defaultBlockEncodingCtx.<a name="line.1033"></a>
+<span class="sourceLineNo">1034</span>            compressAndEncrypt(baosInMemory.getBuffer(), 0, baosInMemory.size());<a name="line.1034"></a>
+<span class="sourceLineNo">1035</span>      }<a name="line.1035"></a>
+<span class="sourceLineNo">1036</span>      if (compressAndEncryptDat == null) {<a name="line.1036"></a>
+<span class="sourceLineNo">1037</span>        compressAndEncryptDat = new Bytes(baosInMemory.getBuffer(), 0, baosInMemory.size());<a name="line.1037"></a>
+<span class="sourceLineNo">1038</span>      }<a name="line.1038"></a>
+<span class="sourceLineNo">1039</span>      if (onDiskBlockBytesWithHeader == null) {<a name="line.1039"></a>
+<span class="sourceLineNo">1040</span>        onDiskBlockBytesWithHeader = new ByteArrayOutputStream(compressAndEncryptDat.getLength());<a name="line.1040"></a>
+<span class="sourceLineNo">1041</span>      }<a name="line.1041"></a>
+<span class="sourceLineNo">1042</span>      onDiskBlockBytesWithHeader.reset();<a name="line.1042"></a>
+<span class="sourceLineNo">1043</span>      onDiskBlockBytesWithHeader.write(compressAndEncryptDat.get(),<a name="line.1043"></a>
+<span class="sourceLineNo">1044</span>            compressAndEncryptDat.getOffset(), compressAndEncryptDat.getLength());<a name="line.1044"></a>
+<span class="sourceLineNo">1045</span>      // Calculate how many bytes we need for checksum on the tail of the block.<a name="line.1045"></a>
+<span class="sourceLineNo">1046</span>      int numBytes = (int) ChecksumUtil.numBytes(<a name="line.1046"></a>
+<span class="sourceLineNo">1047</span>          onDiskBlockBytesWithHeader.size(),<a name="line.1047"></a>
+<span class="sourceLineNo">1048</span>          fileContext.getBytesPerChecksum());<a name="line.1048"></a>
+<span class="sourceLineNo">1049</span><a name="line.1049"></a>
+<span class="sourceLineNo">1050</span>      // Put the header for the on disk bytes; header currently is unfilled-out<a name="line.1050"></a>
+<span class="sourceLineNo">1051</span>      putHeader(onDiskBlockBytesWithHeader,<a name="line.1051"></a>
+<span class="sourceLineNo">1052</span>          onDiskBlockBytesWithHeader.size() + numBytes,<a name="line.1052"></a>
+<span class="sourceLineNo">1053</span>          baosInMemory.size(), onDiskBlockBytesWithHeader.size());<a name="line.1053"></a>
+<span class="sourceLineNo">1054</span>      if (onDiskChecksum.length != numBytes) {<a name="line.1054"></a>
+<span class="sourceLineNo">1055</span>        onDiskChecksum = new byte[numBytes];<a name="line.1055"></a>
+<span class="sourceLineNo">1056</span>      }<a name="line.1056"></a>
+<span class="sourceLineNo">1057</span>      ChecksumUtil.generateChecksums(<a name="line.1057"></a>
+<span class="sourceLineNo">1058</span>          onDiskBlockBytesWithHeader.getBuffer(), 0,onDiskBlockBytesWithHeader.size(),<a name="line.1058"></a>
+<span class="sourceLineNo">1059</span>          onDiskChecksum, 0, fileContext.getChecksumType(), fileContext.getBytesPerChecksum());<a name="line.1059"></a>
+<span class="sourceLineNo">1060</span>    }<a name="line.1060"></a>
+<span class="sourceLineNo">1061</span><a name="line.1061"></a>
+<span class="sourceLineNo">1062</span>    /**<a name="line.1062"></a>
+<span class="sourceLineNo">1063</span>     * Put the header into the given byte array at the given offset.<a name="line.1063"></a>
+<span class="sourceLineNo">1064</span>     * @param onDiskSize size of the block on disk header + data + checksum<a name="line.1064"></a>
+<span class="sourceLineNo">1065</span>     * @param uncompressedSize size of the block after decompression (but<a name="line.1065"></a>
+<span class="sourceLineNo">1066</span>     *          before optional data block decoding) including header<a name="line.1066"></a>
+<span class="sourceLineNo">1067</span>     * @param onDiskDataSize size of the block on disk with header<a name="line.1067"></a>
+<span class="sourceLineNo">1068</span>     *        and data but not including the checksums<a name="line.1068"></a>
+<span class="sourceLineNo">1069</span>     */<a name="line.1069"></a>
+<span class="sourceLineNo">1070</span>    private void putHeader(byte[] dest, int offset, int onDiskSize,<a name="line.1070"></a>
+<span class="sourceLineNo">1071</span>        int uncompressedSize, int onDiskDataSize) {<a name="line.1071"></a>
+<span class="sourceLineNo">1072</span>      offset = blockType.put(dest, offset);<a name="line.1072"></a>
+<span class="sourceLineNo">1073</span>      offset = Bytes.putInt(dest, offset, onDiskSize - HConstants.HFILEBLOCK_HEADER_SIZE);<a name="line.1073"></a>
+<span class="sourceLineNo">1074</span>      offset = Bytes.putInt(dest, offset, uncompressedSize - HConstants.HFILEBLOCK_HEADER_SIZE);<a name="line.1074"></a>
+<span class="sourceLineNo">1075</span>      offset = Bytes.putLong(dest, offset, prevOffset);<a name="line.1075"></a>
+<span class="sourceLineNo">1076</span>      offset = Bytes.putByte(dest, offset, fileContext.getChecksumType().getCode());<a name="line.1076"></a>
+<span class="sourceLineNo">1077</span>      offset = Bytes.putInt(dest, offset, fileContext.getBytesPerChecksum());<a name="line.1077"></a>
+<span class="sourceLineNo">1078</span>      Bytes.putInt(dest, offset, onDiskDataSize);<a name="line.1078"></a>
+<span class="sourceLineNo">1079</span>    }<a name="line.1079"></a>
+<span class="sourceLineNo">1080</span><a name="line.1080"></a>
+<span class="sourceLineNo">1081</span>    private void putHeader(ByteArrayOutputStream dest, int onDiskSize,<a name="line.1081"></a>
+<span class="sourceLineNo">1082</span>        int uncompressedSize, int onDiskDataSize) {<a name="line.1082"></a>
+<span class="sourceLineNo">1083</span>      putHeader(dest.getBuffer(),0, onDiskSize, uncompressedSize, onDiskDataSize);<a name="line.1083"></a>
+<span class="sourceLineNo">1084</span>    }<a name="line.1084"></a>
+<span class="sourceLineNo">1085</span><a name="line.1085"></a>
+<span class="sourceLineNo">1086</span>    /**<a name="line.1086"></a>
+<span class="sourceLineNo">1087</span>     * Similar to {@link #writeHeaderAndData(FSDataOutputStream)}, but records<a name="line.1087"></a>
+<span class="sourceLineNo">1088</span>     * the offset of this block so that it can be referenced in the next block<a name="line.1088"></a>
+<span class="sourceLineNo">1089</span>     * of the same type.<a name="line.1089"></a>
+<span class="sourceLineNo">1090</span>     *<a name="line.1090"></a>
+<span class="sourceLineNo">1091</span>     * @param out<a name="line.1091"></a>
+<span class="sourceLineNo">1092</span>     * @throws IOException<a name="line.1092"></a>
+<span class="sourceLineNo">1093</span>     */<a name="line.1093"></a>
+<span class="sourceLineNo">1094</span>    void writeHeaderAndData(FSDataOutputStream out) throws IOException {<a name="line.1094"></a>
+<span class="sourceLineNo">1095</span>      long offset = out.getPos();<a name="line.1095"></a>
+<span class="sourceLineNo">1096</span>      if (startOffset != UNSET &amp;&amp; offset != startOffset) {<a name="line.1096"></a>
+<span class="sourceLineNo">1097</span>        throw new IOException("A " + blockType + " block written to a "<a name="line.1097"></a>
+<span class="sourceLineNo">1098</span>            + "stream twice, first at offset " + startOffset + ", then at "<a name="line.1098"></a>
+<span class="sourceLineNo">1099</span>            + offset);<a name="line.1099"></a>
+<span class="sourceLineNo">1100</span>      }<a name="line.1100"></a>
+<span class="sourceLineNo">1101</span>      startOffset = offset;<a name="line.1101"></a>
+<span class="sourceLineNo">1102</span><a name="line.1102"></a>
+<span class="sourceLineNo">1103</span>      finishBlockAndWriteHeaderAndData((DataOutputStream) out);<a name="line.1103"></a>
+<span class="sourceLineNo">1104</span>    }<a name="line.1104"></a>
+<span class="sourceLineNo">1105</span><a name="line.1105"></a>
+<span class="sourceLineNo">1106</span>    /**<a name="line.1106"></a>
+<span class="sourceLineNo">1107</span>     * Writes the header and the compressed data of this block (or uncompressed<a name="line.1107"></a>
+<span class="sourceLineNo">1108</span>     * data when not using compression) into the given stream. Can be called in<a name="line.1108"></a>
+<span class="sourceLineNo">1109</span>     * the "writing" state or in the "block ready" state. If called in the<a name="line.1109"></a>
+<span class="sourceLineNo">1110</span>     * "writing" state, transitions the writer to the "block ready" state.<a name="line.1110"></a>
+<span class="sourceLineNo">1111</span>     *<a name="line.1111"></a>
+<span class="sourceLineNo">1112</span>     * @param out the output stream to write the<a name="line.1112"></a>
+<span class="sourceLineNo">1113</span>     * @throws IOException<a name="line.1113"></a>
+<span class="sourceLineNo">1114</span>     */<a name="line.1114"></a>
+<span class="sourceLineNo">1115</span>    protected void finishBlockAndWriteHeaderAndData(DataOutputStream out)<a name="line.1115"></a>
+<span class="sourceLineNo">1116</span>      throws IOException {<a name="line.1116"></a>
+<span class="sourceLineNo">1117</span>      ensureBlockReady();<a name="line.1117"></a>
+<span class="sourceLineNo">1118</span>      out.write(onDiskBlockBytesWithHeader.getBuffer(), 0, onDiskBlockBytesWithHeader.size());<a name="line.1118"></a>
+<span class="sourceLineNo">1119</span>      out.write(onDiskChecksum);<a name="line.1119"></a>
+<span class="sourceLineNo">1120</span>    }<a name="line.1120"></a>
+<span class="sourceLineNo">1121</span><a name="line.1121"></a>
+<span class="sourceLineNo">1122</span>    /**<a name="line.1122"></a>
+<span class="sourceLineNo">1123</span>     * Returns the header or the compressed data (or uncompressed data when not<a name="line.1123"></a>
+<span class="sourceLineNo">1124</span>     * using compression) as a byte array. Can be called in the "writing" state<a name="line.1124"></a>
+<span class="sourceLineNo">1125</span>     * or in the "block ready" state. If called in the "writing" state,<a name="line.1125"></a>
+<span class="sourceLineNo">1126</span>     * transitions the writer to the "block ready" state. This returns<a name="line.1126"></a>
+<span class="sourceLineNo">1127</span>     * the header + data + checksums stored on disk.<a name="line.1127"></a>
+<span class="sourceLineNo">1128</span>     *<a name="line.1128"></a>
+<span class="sourceLineNo">1129</span>     * @return header and data as they would be stored on disk in a byte array<a name="line.1129"></a>
+<span class="sourceLineNo">1130</span>     * @throws IOException<a name="line.1130"></a>
+<span class="sourceLineNo">1131</span>     */<a name="line.1131"></a>
+<span class="sourceLineNo">1132</span>    byte[] getHeaderAndDataForTest() throws IOException {<a name="line.1132"></a>
+<span class="sourceLineNo">1133</span>      ensureBlockReady();<a name="line.1133"></a>
+<span class="sourceLineNo">1134</span>      // This is not very optimal, because we are doing an extra copy.<a name="line.1134"></a>
+<span class="sourceLineNo">1135</span>      // But this method is used only by unit tests.<a name="line.1135"></a>
+<span class="sourceLineNo">1136</span>      byte[] output =<a name="line.1136"></a>
+<span class="sourceLineNo">1137</span>          new byte[onDiskBlockBytesWithHeader.size()<a name="line.1137"></a>
+<span class="sourceLineNo">1138</span>              + onDiskChecksum.length];<a name="line.1138"></a>
+<span class="sourceLineNo">1139</span>      System.arraycopy(onDiskBlockBytesWithHeader.getBuffer(), 0, output, 0,<a name="line.1139"></a>
+<span class="sourceLineNo">1140</span>          onDiskBlockBytesWithHeader.size());<a name="line.1140"></a>
+<span class="sourceLineNo">1141</span>      System.arraycopy(onDiskChecksum, 0, output,<a name="line.1141"></a>
+<span class="sourceLineNo">1142</span>          onDiskBlockBytesWithHeader.size(), onDiskChecksum.length);<a name="line.1142"></a>
+<span class="sourceLineNo">1143</span>      return output;<a name="line.1143"></a>
+<span class="sourceLineNo">1144</span>    }<a name="line.1144"></a>
+<span class="sourceLineNo">1145</span><a name="line.1145"></a>
+<span class="sourceLineNo">1146</span>    /**<a name="line.1146"></a>
+<span class="sourceLineNo">1147</span>     * Releases resources used by this writer.<a name="line.1147"></a>
+<span class="sourceLineNo">1148</span>     */<a name="line.1148"></a>
+<span class="sourceLineNo">1149</span>    void release() {<a name="line.1149"></a>
+<span class="sourceLineNo">1150</span>      if (dataBlockEncodingCtx != null) {<a name="line.1150"></a>
+<span class="sourceLineNo">1151</span>        dataBlockEncodingCtx.close();<a name="line.1151"></a>
+<span class="sourceLineNo">1152</span>        dataBlockEncodingCtx = null;<a name="line.1152"></a>
+<span class="sourceLineNo">1153</span>      }<a name="line.1153"></a>
+<span class="sourceLineNo">1154</span>      if (defaultBlockEncodingCtx != null) {<a name="line.1154"></a>
+<span class="sourceLineNo">1155</span>        defaultBlockEncodingCtx.close();<a name="line.1155"></a>
+<span class="sourceLineNo">1156</span>        defaultBlockEncodingCtx = null;<a name="line.1156"></a>
+<span class="sourceLineNo">1157</span>      }<a name="line.1157"></a>
+<span class="sourceLineNo">1158</span>    }<a name="line.1158"></a>
+<span class="sourceLineNo">1159</span><a name="line.1159"></a>
+<span class="sourceLineNo">1160</span>    /**<a name="line.1160"></a>
+<span class="sourceLineNo">1161</span>     * Returns the on-disk size of the data portion of the block. This is the<a name="line.1161"></a>
+<span class="sourceLineNo">1162</span>     * compressed size if compression is enabled. Can only be called in the<a name="line.1162"></a>
+<span class="sourceLineNo">1163</span>     * "block ready" state. Header is not compressed, and its size is not<a name="line.1163"></a>
+<span class="sourceLineNo">1164</span>     * included in the return value.<a name="line.1164"></a>
+<span class="sourceLineNo">1165</span>     *<a name="line.1165"></a>
+<span class="sourceLineNo">1166</span>     * @return the on-disk size of the block, not including the header.<a name="line.1166"></a>
+<span class="sourceLineNo">1167</span>     */<a name="line.1167"></a>
+<span class="sourceLineNo">1168</span>    int getOnDiskSizeWithoutHeader() {<a name="line.1168"></a>
+<span class="sourceLineNo">1169</span>      expectState(State.BLOCK_READY);<a name="line.1169"></a>
+<span class="sourceLineNo">1170</span>      return onDiskBlockBytesWithHeader.size() +<a name="line.1170"></a>
+<span class="sourceLineNo">1171</span>          onDiskChecksum.length - HConstants.HFILEBLOCK_HEADER_SIZE;<a name="line.1171"></a>
+<span class="sourceLineNo">1172</span>    }<a name="line.1172"></a>
+<span class="sourceLineNo">1173</span><a name="line.1173"></a>
+<span class="sourceLineNo">1174</span>    /**<a name="line.1174"></a>
+<span class="sourceLineNo">1175</span>     * Returns the on-disk size of the block. Can only be called in the<a name="line.1175"></a>
+<span class="sourceLineNo">1176</span>     * "block ready" state.<a name="line.1176"></a>
+<span class="sourceLineNo">1177</span>     *<a name="line.1177"></a>
+<span class="sourceLineNo">1178</span>     * @return the on-disk size of the block ready to be written, including the<a name="line.1178"></a>
+<span class="sourceLineNo">1179</span>     *         header size, the data and the checksum data.<a name="line.1179"></a>
+<span class="sourceLineNo">1180</span>     */<a name="line.1180"></a>
+<span class="sourceLineNo">1181</span>    int getOnDiskSizeWithHeader() {<a name="line.1181"></a>
+<span class="sourceLineNo">1182</span>      expectState(State.BLOCK_READY);<a name="line.1182"></a>
+<span class="sourceLineNo">1183</span>      return onDiskBlockBytesWithHeader.size() + onDiskChecksum.length;<a name="line.1183"></a>
+<span class="sourceLineNo">1184</span>    }<a name="line.1184"></a>
+<span class="sourceLineNo">1185</span><a name="line.1185"></a>
+<span class="sourceLineNo">1186</span>    /**<a name="line.1186"></a>
+<span class="sourceLineNo">1187</span>     * The uncompressed size of the block data. Does not include header size.<a name="line.1187"></a>
+<span class="sourceLineNo">1188</span>     */<a name="line.1188"></a>
+<span class="sourceLineNo">1189</span>    int getUncompressedSizeWithoutHeader() {<a name="line.1189"></a>
+<span class="sourceLineNo">1190</span>      expectState(State.BLOCK_READY);<a name="line.1190"></a>
+<span class="sourceLineNo">1191</span>      return baosInMemory.size() - HConstants.HFILEBLOCK_HEADER_SIZE;<a name="line.1191"></a>
+<span class="sourceLineNo">1192</span>    }<a name="line.1192"></a>
+<span class="sourceLineNo">1193</span><a name="line.1193"></a>
+<span class="sourceLineNo">1194</span>    /**<a name="line.1194"></a>
+<span class="sourceLineNo">1195</span>     * The uncompressed size of the block data, including header size.<a name="line.1195"></a>
+<span class="sourceLineNo">1196</span>     */<a name="line.1196"></a>
+<span class="sourceLineNo">1197</span>    int getUncompressedSizeWithHeader() {<a name="line.1197"></a>
+<span class="sourceLineNo">1198</span>      expectState(State.BLOCK_READY);<a name="line.1198"></a>
+<span class="sourceLineNo">1199</span>      return baosInMemory.size();<a name="line.1199"></a>
+<span class="sourceLineNo">1200</span>    }<a name="line.1200"></a>
+<span class="sourceLineNo">1201</span><a name="line.1201"></a>
+<span class="sourceLineNo">1202</span>    /** @return true if a block is being written  */<a name="line.1202"></a>
+<span class="sourceLineNo">1203</span>    boolean isWriting() {<a name="line.1203"></a>
+<span class="sourceLineNo">1204</span>      return state == State.WRITING;<a name="line.1204"></a>
+<span class="sourceLineNo">1205</span>    }<a name="line.1205"></a>
+<span class="sourceLineNo">1206</span><a name="line.1206"></a>
+<span class="sourceLineNo">1207</span>    /**<a name="line.1207"></a>
+<span class="sourceLineNo">1208</span>     * Returns the number of bytes written into the current block so far, or<a name="line.1208"></a>
+<span class="sourceLineNo">1209</span>     * zero if not writing the block at the moment. Note that this will return<a name="line.1209"></a>
+<span class="sourceLineNo">1210</span>     * zero in the "block ready" state as well.<a name="line.1210"></a>
+<span class="sourceLineNo">1211</span>     *<a name="line.1211"></a>
+<span class="sourceLineNo">1212</span>     * @return the number of bytes written<a name="line.1212"></a>
+<span class="sourceLineNo">1213</span>     */<a name="line.1213"></a>
+<span class="sourceLineNo">1214</span>    public int encodedBlockSizeWritten() {<a name="line.1214"></a>
+<span class="sourceLineNo">1215</span>      if (state != State.WRITING)<a name="line.1215"></a>
+<span class="sourceLineNo">1216</span>        return 0;<a name="line.1216"></a>
+<span class="sourceLineNo">1217</span>      return this.encodedDataSizeWritten;<a name="line.1217"></a>
+<span class="sourceLineNo">1218</span>    }<a name="line.1218"></a>
+<span class="sourceLineNo">1219</span><a name="line.1219"></a>
+<span class="sourceLineNo">1220</span>    /**<a name="line.1220"></a>
+<span class="sourceLineNo">1221</span>     * Returns the number of bytes written into the current block so far, or<a name="line.1221"></a>
+<span class="sourceLineNo">1222</span>     * zero if not writing the block at the moment. Note that this will return<a name="line.1222"></a>
+<span class="sourceLineNo">1223</span>     * zero in the "block ready" state as well.<a name="line.1223"></a>
+<span class="sourceLineNo">1224</span>     *<a name="line.1224"></a>
+<span class="sourceLineNo">1225</span>     * @return the number of bytes written<a name="line.1225"></a>
+<span class="sourceLineNo">1226</span>     */<a name="line.1226"></a>
+<span class="sourceLineNo">1227</span>    int blockSizeWritten() {<a name="line.1227"></a>
+<span class="sourceLineNo">1228</span>      if (state != State.WRITING) return 0;<a name="line.1228"></a>
+<span class="sourceLineNo">1229</span>      return this.unencodedDataSizeWritten;<a name="line.1229"></a>
+<span class="sourceLineNo">1230</span>    }<a name="line.1230"></a>
+<span class="sourceLineNo">1231</span><a name="line.1231"></a>
+<span class="sourceLineNo">1232</span>    /**<a name="line.1232"></a>
+<span class="sourceLineNo">1233</span>     * Clones the header followed by the uncompressed data, even if using<a name="line.1233"></a>
+<span class="sourceLineNo">1234</span>     * compression. This is needed for storing uncompressed blocks in the block<a name="line.1234"></a>
+<span class="sourceLineNo">1235</span>     * cache. Can be called in the "writing" state or the "block ready" state.<a name="line.1235"></a>
+<span class="sourceLineNo">1236</span>     * Returns only the header and data, does not include checksum data.<a name="line.1236"></a>
+<span class="sourceLineNo">1237</span>     *<a name="line.1237"></a>
+<span class="sourceLineNo">1238</span>     * @return Returns a copy of uncompressed block bytes for caching on write<a name="line.1238"></a>
+<span class="sourceLineNo">1239</span>     */<a name="line.1239"></a>
+<span class="sourceLineNo">1240</span>    @VisibleForTesting<a name="line.1240"></a>
+<span class="sourceLineNo">1241</span>    ByteBuffer cloneUncompressedBufferWithHeader() {<a name="line.1241"></a>
 <span class="sourceLineNo">1242</span>      expectState(State.BLOCK_READY);<a name="line.1242"></a>
-<span class="sourceLineNo">1243</span>      return ByteBuffer.wrap(onDiskBlockBytesWithHeader.toByteArray());<a name="line.1243"></a>
-<span class="sourceLineNo">1244</span>    }<a name="line.1244"></a>
-<span class="sourceLineNo">1245</span><a name="line.1245"></a>
-<span class="sourceLineNo">1246</span>    private void expectState(State expectedState) {<a name="line.1246"></a>
-<span class="sourceLineNo">1247</span>      if (state != expectedState) {<a name="line.1247"></a>
-<span class="sourceLineNo">1248</span>        throw new IllegalStateException("Expected state: " + expectedState +<a name="line.1248"></a>
-<span class="sourceLineNo">1249</span>            ", actual state: " + state);<a name="line.1249"></a>
-<span class="sourceLineNo">1250</span>      }<a name="line.1250"></a>
+<span class="sourceLineNo">1243</span>      byte[] uncompressedBlockBytesWithHeader = baosInMemory.toByteArray();<a name="line.1243"></a>
+<span class="sourceLineNo">1244</span>      int numBytes = (int) ChecksumUtil.numBytes(<a name="line.1244"></a>
+<span class="sourceLineNo">1245</span>          onDiskBlockBytesWithHeader.size(),<a name="line.1245"></a>
+<span class="sourceLineNo">1246</span>          fileContext.getBytesPerChecksum());<a name="line.1246"></a>
+<span class="sourceLineNo">1247</span>      putHeader(uncompressedBlockBytesWithHeader, 0,<a name="line.1247"></a>
+<span class="sourceLineNo">1248</span>        onDiskBlockBytesWithHeader.size() + numBytes,<a name="line.1248"></a>
+<span class="sourceLineNo">1249</span>        baosInMemory.size(), onDiskBlockBytesWithHeader.size());<a name="line.1249"></a>
+<span class="sourceLineNo">1250</span>      return ByteBuffer.wrap(uncompressedBlockBytesWithHeader);<a name="line.1250"></a>
 <span class="sourceLineNo">1251</span>    }<a name="line.1251"></a>
 <span class="sourceLineNo">1252</span><a name="line.1252"></a>
 <span class="sourceLineNo">1253</span>    /**<a name="line.1253"></a>
-<span class="sourceLineNo">1254</span>     * Takes the given {@link BlockWritable} instance, creates a new block of<a name="line.1254"></a>
-<span class="sourceLineNo">1255</span>     * its appropriate type, writes the writable into this block, and flushes<a name="line.1255"></a>
-<span class="sourceLineNo">1256</span>     * the block into the output stream. The writer is instructed not to buffer<a name="line.1256"></a>
-<span class="sourceLineNo">1257</span>     * uncompressed bytes for cache-on-write.<a name="line.1257"></a>
+<span class="sourceLineNo">1254</span>     * Clones the header followed by the on-disk (compressed/encoded/encrypted) data. This is<a name="line.1254"></a>
+<span class="sourceLineNo">1255</span>     * needed for storing packed blocks in the block cache. Expects calling semantics identical to<a name="line.1255"></a>
+<span class="sourceLineNo">1256</span>     * {@link #getUncompressedBufferWithHeader()}. Returns only the header and data,<a name="line.1256"></a>
+<span class="sourceLineNo">1257</span>     * Does not include checksum data.<a name="line.1257"></a>
 <span class="sourceLineNo">1258</span>     *<a name="line.1258"></a>
-<span class="sourceLineNo">1259</span>     * @param bw the block-writable object to write as a block<a name="line.1259"></a>
-<span class="sourceLineNo">1260</span>     * @param out the file system output stream<a name="line.1260"></a>
-<span class="sourceLineNo">1261</span>     * @throws IOException<a name="line.1261"></a>
-<span class="sourceLineNo">1262</span>     */<a name="line.1262"></a>
-<span class="sourceLineNo">1263</span>    void writeBlock(BlockWritable bw, FSDataOutputStream out)<a name="line.1263"></a>
-<span class="sourceLineNo">1264</span>        throws IOException {<a name="line.1264"></a>
-<span class="sourceLineNo">1265</span>      bw.writeToBlock(startWriting(bw.getBlockType()));<a name="line.1265"></a>
-<span class="sourceLineNo">1266</span>      writeHeaderAndData(out);<a name="line.1266"></a>
-<span class="sourceLineNo">1267</span>    }<a name="line.1267"></a>
-<span class="sourceLineNo">1268</span><a name="line.1268"></a>
-<span class="sourceLineNo">1269</span>    /**<a name="line.1269"></a>
-<span class="sourceLineNo">1270</span>     * Creates a new HFileBlock. Checksums have already been validated, so<a name="line.1270"></a>
-<span class="sourceLineNo">1271</span>     * the byte buffer passed into the constructor of this newly created<a name="line.1271"></a>
-<span class="sourceLineNo">1272</span>     * block does not have checksum data even though the header minor<a name="line.1272"></a>
-<span class="sourceLineNo">1273</span>     * version is MINOR_VERSION_WITH_CHECKSUM. This is indicated by setting a<a name="line.1273"></a>
-<span class="sourceLineNo">1274</span>     * 0 value in bytesPerChecksum. This method copies the on-disk or<a name="line.1274"></a>
-<span class="sourceLineNo">1275</span>     * uncompressed data to build the HFileBlock which is used only<a name="line.1275"></a>
-<span class="sourceLineNo">1276</span>     * while writing blocks and caching.<a name="line.1276"></a>
-<span class="sourceLineNo">1277</span>     *<a name="line.1277"></a>
-<span class="sourceLineNo">1278</span>     * &lt;p&gt;TODO: Should there be an option where a cache can ask that hbase preserve block<a name="line.1278"></a>
-<span class="sourceLineNo">1279</span>     * checksums for checking after a block comes out of the cache? Otehrwise, cache is responsible<a name="line.1279"></a>
-<span class="sourceLineNo">1280</span>     * for blocks being wholesome (ECC memory or if file-backed, it does checksumming).<a name="line.1280"></a>
-<span class="sourceLineNo">1281</span>     */<a name="line.1281"></a>
-<span class="sourceLineNo">1282</span>    HFileBlock getBlockForCaching(CacheConfig cacheConf) {<a name="line.1282"></a>
-<span class="sourceLineNo">1283</span>      HFileContext newContext = new HFileContextBuilder()<a name="line.1283"></a>
-<span class="sourceLineNo">1284</span>                                .withBlockSize(fileContext.getBlocksize())<a name="line.1284"></a>
-<span class="sourceLineNo">1285</span>                                .withBytesPerCheckSum(0)<a name="line.1285"></a>
-<span class="sourceLineNo">1286</span>                                .withChecksumType(ChecksumType.NULL) // no checksums in cached data<a name="line.1286"></a>
-<span class="sourceLineNo">1287</span>                                .withCompression(fileContext.getCompression())<a name="line.1287"></a>
-<span class="sourceLineNo">1288</span>                                .withDataBlockEncoding(fileContext.getDataBlockEncoding())<a name="line.1288"></a>
-<span class="sourceLineNo">1289</span>                                .withHBaseCheckSum(fileContext.isUseHBaseChecksum())<a name="line.1289"></a>
-<span class="sourceLineNo">1290</span>                                .withCompressTags(fileContext.isCompressTags())<a name="line.1290"></a>
-<span class="sourceLineNo">1291</span>                                .withIncludesMvcc(fileContext.isIncludesMvcc())<a name="line.1291"></a>
-<span class="sourceLineNo">1292</span>                                .withIncludesTags(fileContext.isIncludesTags())<a name="line.1292"></a>
-<span class="sourceLineNo">1293</span>                                .build();<a name="line.1293"></a>
-<span class="sourceLineNo">1294</span>       return new HFileBlock(blockType, getOnDiskSizeWithoutHeader(),<a name="line.1294"></a>
-<span class="sourceLineNo">1295</span>          getUncompressedSizeWithoutHeader(), prevOffset,<a name="line.1295"></a>
-<span class="sourceLineNo">1296</span>          cacheConf.shouldCacheCompressed(blockType.getCategory())?<a name="line.1296"></a>
-<span class="sourceLineNo">1297</span>            cloneOnDiskBufferWithHeader() :<a name="line.1297"></a>
-<span class="sourceLineNo">1298</span>            cloneUncompressedBufferWithHeader(),<a name="line.1298"></a>
-<span class="sourceLineNo">1299</span>          FILL_HEADER, startOffset, UNSET,<a name="line.1299"></a>
-<span class="sourceLineNo">1300</span>          onDiskBlockBytesWithHeader.size() + onDiskChecksum.length, newContext);<a name="line.1300"></a>
-<span class="sourceLineNo">1301</span>    }<a name="line.1301"></a>
-<span class="sourceLineNo">1302</span>  }<a name="line.1302"></a>
-<span class="sourceLineNo">1303</span><a name="line.1303"></a>
-<span class="sourceLineNo">1304</span>  /** Something that can be written into a block. */<a name="line.1304"></a>
-<span class="sourceLineNo">1305</span>  interface BlockWritable {<a name="line.1305"></a>
-<span class="sourceLineNo">1306</span><a name="line.1306"></a>
-<span class="sourceLineNo">1307</span>    /** The type of block this data should use. */<a name="line.1307"></a>
-<span class="sourceLineNo">1308</span>    BlockType getBlockType();<a name="line.1308"></a>
-<span class="sourceLineNo">1309</span><a name="line.1309"></a>
-<span class="sourceLineNo">1310</span>    /**<a name="line.1310"></a>
-<span class="sourceLineNo">1311</span>     * Writes the block to the provided stream. Must not write any magic<a name="line.1311"></a>
-<span class="sourceLineNo">1312</span>     * records.<a name="line.1312"></a>
-<span class="sourceLineNo">1313</span>     *<a name="line.1313"></a>
-<span class="sourceLineNo">1314</span>     * @param out a stream to write uncompressed data into<a name="line.1314"></a>
-<span class="sourceLineNo">1315</span>     */<a name="line.1315"></a>
-<span class="sourceLineNo">1316</span>    void writeToBlock(DataOutput out) throws IOException;<a name="line.1316"></a>
-<span class="sourceLineNo">1317</span>  }<a name="line.1317"></a>
-<span class="sourceLineNo">1318</span><a name="line.1318"></a>
-<span class="sourceLineNo">1319</span>  // Block readers and writers<a name="line.1319"></a>
-<span class="sourceLineNo">1320</span><a name="line.1320"></a>
-<span class="sourceLineNo">1321</span>  /** An interface allowing to iterate {@link HFileBlock}s. */<a name="line.1321"></a>
-<span class="sourceLineNo">1322</span>  interface BlockIterator {<a name="line.1322"></a>
+<span class="sourceLineNo">1259</span>     * @return Returns a copy of block bytes for caching on write<a name="line.1259"></a>
+<span class="sourceLineNo">1260</span>     */<a name="line.1260"></a>
+<span class="sourceLineNo">1261</span>    private ByteBuffer cloneOnDiskBufferWithHeader() {<a name="line.1261"></a>
+<span class="sourceLineNo">1262</span>      expectState(State.BLOCK_READY);<a name="line.1262"></a>
+<span class="sourceLineNo">1263</span>      return ByteBuffer.wrap(onDiskBlockBytesWithHeader.toByteArray());<a name="line.1263"></a>
+<span class="sourceLineNo">1264</span>    }<a name="line.1264"></a>
+<span class="sourceLineNo">1265</span><a name="line.1265"></a>
+<span class="sourceLineNo">1266</span>    private void expectState(State expectedState) {<a name="line.1266"></a>
+<span class="sourceLineNo">1267</span>      if (state != expectedState) {<a name="line.1267"></a>
+<span class="sourceLineNo">1268</span>        throw new IllegalStateException("Expected state: " + expectedState +<a name="line.1268"></a>
+<span class="sourceLineNo">1269</span>            ", actual state: " + state);<a name="line.1269"></a>
+<span class="sourceLineNo">1270</span>      }<a name="line.1270"></a>
+<span class="sourceLineNo">1271</span>    }<a name="line.1271"></a>
+<span class="sourceLineNo">1272</span><a name="line.1272"></a>
+<span class="sourceLineNo">1273</span>    /**<a name="line.1273"></a>
+<span class="sourceLineNo">1274</span>     * Takes the given {@link BlockWritable} instance, creates a new block of<a name="line.1274"></a>
+<span class="sourceLineNo">1275</span>     * its appropriate type, writes the writable into this block, and flushes<a name="line.1275"></a>
+<span class="sourceLineNo">1276</span>     * the block into the output stream. The writer is instructed not to buffer<a name="line.1276"></a>
+<span class="sourceLineNo">1277</span>     * uncompressed bytes for cache-on-write.<a name="line.1277"></a>
+<span class="sourceLineNo">1278</span>     *<a name="line.1278"></a>
+<span class="sourceLineNo">1279</span>     * @param bw the block-writable object to write as a block<a name="line.1279"></a>
+<span class="sourceLineNo">1280</span>     * @param out the file system output stream<a name="line.1280"></a>
+<span class="sourceLineNo">1281</span>     * @throws IOException<a name="line.1281"></a>
+<span class="sourceLineNo">1282</span>     */<a name="line.1282"></a>
+<span class="sourceLineNo">1283</span>    void writeBlock(BlockWritable bw, FSDataOutputStream out)<a name="line.1283"></a>
+<span class="sourceLineNo">1284</span>        throws IOException {<a name="line.1284"></a>
+<span class="sourceLineNo">1285</span>      bw.writeToBlock(startWriting(bw.getBlockType()));<a name="line.1285"></a>
+<span class="sourceLineNo">1286</span>      writeHeaderAndData(out);<a 

<TRUNCATED>

Mime
View raw message