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.HFileBlockDefaultDecodingContext;
26  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext;
27  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
28  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
29  
30  /**
31   * Does not perform any kind of encoding/decoding.
32   */
33  @InterfaceAudience.Private
34  public class NoOpDataBlockEncoder implements HFileDataBlockEncoder {
35  
36    public static final NoOpDataBlockEncoder INSTANCE =
37        new NoOpDataBlockEncoder();
38  
39    /** Cannot be instantiated. Use {@link #INSTANCE} instead. */
40    private NoOpDataBlockEncoder() {
41    }
42  
43    @Override
44    public HFileBlock diskToCacheFormat(HFileBlock block, boolean isCompaction) {
45      if (block.getBlockType() == BlockType.ENCODED_DATA) {
46        throw new IllegalStateException("Unexpected encoded block");
47      }
48      return block;
49    }
50  
51    @Override
52    public void beforeWriteToDisk(ByteBuffer in,
53        boolean includesMemstoreTS,
54        HFileBlockEncodingContext encodeCtx, BlockType blockType)
55        throws IOException {
56      if (!(encodeCtx.getClass().getName().equals(
57          HFileBlockDefaultEncodingContext.class.getName()))) {
58        throw new IOException (this.getClass().getName() + " only accepts " +
59            HFileBlockDefaultEncodingContext.class.getName() + ".");
60      }
61  
62      HFileBlockDefaultEncodingContext defaultContext =
63          (HFileBlockDefaultEncodingContext) encodeCtx;
64      defaultContext.compressAfterEncoding(in.array(), blockType);
65    }
66  
67    @Override
68    public boolean useEncodedScanner(boolean isCompaction) {
69      return false;
70    }
71  
72    @Override
73    public void saveMetadata(HFile.Writer writer) {
74    }
75  
76    @Override
77    public DataBlockEncoding getEncodingOnDisk() {
78      return DataBlockEncoding.NONE;
79    }
80  
81    @Override
82    public DataBlockEncoding getEncodingInCache() {
83      return DataBlockEncoding.NONE;
84    }
85  
86    @Override
87    public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction) {
88      return DataBlockEncoding.NONE;
89    }
90  
91    @Override
92    public String toString() {
93      return getClass().getSimpleName();
94    }
95  
96    @Override
97    public HFileBlockEncodingContext newOnDiskDataBlockEncodingContext(
98        Algorithm compressionAlgorithm, byte[] dummyHeader) {
99      return new HFileBlockDefaultEncodingContext(compressionAlgorithm,
100         null, dummyHeader);
101   }
102 
103   @Override
104   public HFileBlockDecodingContext newOnDiskDataBlockDecodingContext(
105       Algorithm compressionAlgorithm) {
106     return new HFileBlockDefaultDecodingContext(compressionAlgorithm);
107   }
108 
109 }