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.encoding.DataBlockEncoding;
24  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
25  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  /**
29   * Controls what kind of data block encoding is used. If data block encoding is
30   * not set or the given block is not a data block (encoded or not), methods
31   * should just return the unmodified block.
32   */
33  @InterfaceAudience.Private
34  public interface HFileDataBlockEncoder {
35    /** Type of encoding used for data blocks in HFile. Stored in file info. */
36    byte[] DATA_BLOCK_ENCODING = Bytes.toBytes("DATA_BLOCK_ENCODING");
37  
38    /**
39     * Should be called before an encoded or unencoded data block is written to
40     * disk.
41     * @param in KeyValues next to each other
42     * @param encodingResult the encoded result
43     * @param blockType block type
44     * @throws IOException
45     */
46    void beforeWriteToDisk(
47      ByteBuffer in,
48      HFileBlockEncodingContext encodingResult,
49      BlockType blockType
50    ) throws IOException;
51  
52    /**
53     * Decides whether we should use a scanner over encoded blocks.
54     * @return Whether to use encoded scanner.
55     */
56    boolean useEncodedScanner();
57  
58    /**
59     * Save metadata in HFile which will be written to disk
60     * @param writer writer for a given HFile
61     * @exception IOException on disk problems
62     */
63    void saveMetadata(HFile.Writer writer)
64        throws IOException;
65  
66    /** @return the data block encoding */
67    DataBlockEncoding getDataBlockEncoding();
68  
69    /**
70     * Create an encoder specific encoding context object for writing. And the
71     * encoding context should also perform compression if compressionAlgorithm is
72     * valid.
73     *
74     * @param headerBytes header bytes
75     * @param fileContext HFile meta data
76     * @return a new {@link HFileBlockEncodingContext} object
77     */
78    HFileBlockEncodingContext newDataBlockEncodingContext(byte[] headerBytes,
79        HFileContext fileContext);
80  
81    /**
82     * create a encoder specific decoding context for reading. And the
83     * decoding context should also do decompression if compressionAlgorithm
84     * is valid.
85     *
86     * @param fileContext - HFile meta data
87     * @return a new {@link HFileBlockDecodingContext} object
88     */
89    HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext fileContext);
90  }