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     * Should be called before an encoded or unencoded data block is written to
41     * disk.
42     * @param in KeyValues next to each other
43     * @param encodingResult the encoded result
44     * @param blockType block type
45     * @throws IOException
46     */
47    void beforeWriteToDisk(
48      ByteBuffer in,
49      boolean includesMemstoreTS,
50      HFileBlockEncodingContext encodingResult,
51      BlockType blockType
52    ) throws IOException;
53  
54    /**
55     * Decides whether we should use a scanner over encoded blocks.
56     * @return Whether to use encoded scanner.
57     */
58    boolean useEncodedScanner();
59  
60    /**
61     * Save metadata in HFile which will be written to disk
62     * @param writer writer for a given HFile
63     * @exception IOException on disk problems
64     */
65    void saveMetadata(HFile.Writer writer)
66        throws IOException;
67  
68    /** @return the data block encoding */
69    DataBlockEncoding getDataBlockEncoding();
70  
71    /**
72     * Create an encoder specific encoding context object for writing. And the
73     * encoding context should also perform compression if compressionAlgorithm is
74     * valid.
75     *
76     * @param compressionAlgorithm compression algorithm
77     * @param headerBytes header bytes
78     * @return a new {@link HFileBlockEncodingContext} object
79     */
80    HFileBlockEncodingContext newDataBlockEncodingContext(
81      Algorithm compressionAlgorithm, byte[] headerBytes
82    );
83  
84    /**
85     * create a encoder specific decoding context for reading. And the
86     * decoding context should also do decompression if compressionAlgorithm
87     * is valid.
88     *
89     * @param compressionAlgorithm
90     * @return a new {@link HFileBlockDecodingContext} object
91     */
92    HFileBlockDecodingContext newDataBlockDecodingContext(
93      Algorithm compressionAlgorithm
94    );
95  }