View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.hfile;
18  
19  import java.io.IOException;
20  import java.nio.ByteBuffer;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
24  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
25  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
26  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * Controls what kind of data block encoding is used. If data block encoding is
31   * not set or the given block is not a data block (encoded or not), methods
32   * should just return the unmodified block.
33   */
34  @InterfaceAudience.Private
35  public interface HFileDataBlockEncoder {
36    /** Type of encoding used for data blocks in HFile. Stored in file info. */
37    byte[] DATA_BLOCK_ENCODING = Bytes.toBytes("DATA_BLOCK_ENCODING");
38    
39    /**
40     * Converts a block from the on-disk format to the in-cache format. Called in
41     * the following cases:
42     * <ul>
43     * <li>After an encoded or unencoded data block is read from disk, but before
44     * it is put into the cache.</li>
45     * <li>To convert brand-new blocks to the in-cache format when doing
46     * cache-on-write.</li>
47     * </ul>
48     * @param block a block in an on-disk format (read from HFile or freshly
49     *          generated).
50     * @return non null block which is coded according to the settings.
51     */
52    HFileBlock diskToCacheFormat(
53      HFileBlock block, boolean isCompaction
54    );
55  
56    /**
57     * Should be called before an encoded or unencoded data block is written to
58     * disk.
59     * @param in KeyValues next to each other
60     * @param encodingResult the encoded result
61     * @param blockType block type
62     * @throws IOException
63     */
64    void beforeWriteToDisk(
65      ByteBuffer in,
66      boolean includesMemstoreTS,
67      HFileBlockEncodingContext encodingResult,
68      BlockType blockType
69    ) throws IOException;
70  
71    /**
72     * Decides whether we should use a scanner over encoded blocks.
73     * @param isCompaction whether we are in a compaction.
74     * @return Whether to use encoded scanner.
75     */
76    boolean useEncodedScanner(boolean isCompaction);
77  
78    /**
79     * Save metadata in HFile which will be written to disk
80     * @param writer writer for a given HFile
81     * @exception IOException on disk problems
82     */
83    void saveMetadata(HFile.Writer writer)
84        throws IOException;
85  
86    /** @return the on-disk data block encoding */
87    DataBlockEncoding getEncodingOnDisk();
88  
89    /** @return the preferred in-cache data block encoding for normal reads */
90    DataBlockEncoding getEncodingInCache();
91  
92    /**
93     * @return the effective in-cache data block encoding, taking into account
94     *         whether we are doing a compaction.
95     */
96    DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction);
97  
98    /**
99     * Create an encoder specific encoding context object for writing. And the
100    * encoding context should also perform compression if compressionAlgorithm is
101    * valid.
102    *
103    * @param compressionAlgorithm compression algorithm
104    * @param headerBytes header bytes
105    * @return a new {@link HFileBlockEncodingContext} object
106    */
107   HFileBlockEncodingContext newOnDiskDataBlockEncodingContext(
108     Algorithm compressionAlgorithm, byte[] headerBytes
109   );
110 
111   /**
112    * create a encoder specific decoding context for reading. And the
113    * decoding context should also do decompression if compressionAlgorithm
114    * is valid.
115    *
116    * @param compressionAlgorithm
117    * @return a new {@link HFileBlockDecodingContext} object
118    */
119   HFileBlockDecodingContext newOnDiskDataBlockDecodingContext(
120     Algorithm compressionAlgorithm
121   );
122 
123 }