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.hbase.io.encoding.DataBlockEncoding;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.apache.hadoop.hbase.util.Pair;
25  
26  /**
27   * Controls what kind of data block encoding is used. If data block encoding is
28   * not set or the given block is not a data block (encoded or not), methods
29   * should just return the unmodified block.
30   */
31  public interface HFileDataBlockEncoder {
32    /** Type of encoding used for data blocks in HFile. Stored in file info. */
33    public static final byte[] DATA_BLOCK_ENCODING = Bytes.toBytes("DATA_BLOCK_ENCODING");
34    
35    /**
36     * Converts a block from the on-disk format to the in-cache format. Called in
37     * the following cases:
38     * <ul>
39     * <li>After an encoded or unencoded data block is read from disk, but before
40     * it is put into the cache.</li>
41     * <li>To convert brand-new blocks to the in-cache format when doing
42     * cache-on-write.</li>
43     * </ul>
44     * @param block a block in an on-disk format (read from HFile or freshly
45     *          generated).
46     * @return non null block which is coded according to the settings.
47     */
48    public HFileBlock diskToCacheFormat(HFileBlock block,
49        boolean isCompaction);
50  
51    /**
52     * Should be called before an encoded or unencoded data block is written to
53     * disk.
54     * @param in KeyValues next to each other
55     * @param dummyHeader A dummy header to be written as a placeholder
56     * @return a non-null on-heap buffer containing the contents of the
57     *         HFileBlock with unfilled header and block type
58     */
59    public Pair<ByteBuffer, BlockType> beforeWriteToDisk(
60        ByteBuffer in, boolean includesMemstoreTS, byte[] dummyHeader);
61  
62    /**
63     * Decides whether we should use a scanner over encoded blocks.
64     * @param isCompaction whether we are in a compaction.
65     * @return Whether to use encoded scanner.
66     */
67    public boolean useEncodedScanner(boolean isCompaction);
68  
69    /**
70     * Save metadata in HFile which will be written to disk
71     * @param writer writer for a given HFile
72     * @exception IOException on disk problems
73     */
74    public void saveMetadata(HFile.Writer writer) throws IOException;
75  
76    /** @return the on-disk data block encoding */
77    public DataBlockEncoding getEncodingOnDisk();
78  
79    /** @return the preferred in-cache data block encoding for normal reads */
80    public DataBlockEncoding getEncodingInCache();
81  
82    /**
83     * @return the effective in-cache data block encoding, taking into account
84     *         whether we are doing a compaction.
85     */
86    public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction);
87  
88  }