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.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
25  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
26  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultDecodingContext;
27  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext;
28  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
29  import org.apache.hadoop.io.WritableUtils;
30  
31  /**
32   * Does not perform any kind of encoding/decoding.
33   */
34  @InterfaceAudience.Private
35  public class NoOpDataBlockEncoder implements HFileDataBlockEncoder {
36  
37    public static final NoOpDataBlockEncoder INSTANCE =
38        new NoOpDataBlockEncoder();
39  
40    /** Cannot be instantiated. Use {@link #INSTANCE} instead. */
41    private NoOpDataBlockEncoder() {
42    }
43  
44    @Override
45    public int encode(KeyValue kv, HFileBlockEncodingContext encodingCtx, DataOutputStream out)
46        throws IOException {
47      int klength = kv.getKeyLength();
48      int vlength = kv.getValueLength();
49  
50      out.writeInt(klength);
51      out.writeInt(vlength);
52      out.write(kv.getBuffer(), kv.getKeyOffset(), klength);
53      out.write(kv.getValueArray(), kv.getValueOffset(), vlength);
54      int encodedKvSize = klength + vlength + KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE;
55      // Write the additional tag into the stream
56      if (encodingCtx.getHFileContext().isIncludesTags()) {
57        short tagsLength = kv.getTagsLength();
58        out.writeShort(tagsLength);
59        if (tagsLength > 0) {
60          out.write(kv.getTagsArray(), kv.getTagsOffset(), tagsLength);
61        }
62        encodedKvSize += tagsLength + KeyValue.TAGS_LENGTH_SIZE;
63      }
64      if (encodingCtx.getHFileContext().isIncludesMvcc()) {
65        WritableUtils.writeVLong(out, kv.getMvccVersion());
66        encodedKvSize += WritableUtils.getVIntSize(kv.getMvccVersion());
67      }
68      return encodedKvSize;
69    }
70  
71    @Override
72    public boolean useEncodedScanner() {
73      return false;
74    }
75  
76    @Override
77    public void saveMetadata(HFile.Writer writer) {
78    }
79  
80    @Override
81    public DataBlockEncoding getDataBlockEncoding() {
82      return DataBlockEncoding.NONE;
83    }
84  
85    @Override
86    public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction) {
87      return DataBlockEncoding.NONE;
88    }
89    
90    @Override
91    public String toString() {
92      return getClass().getSimpleName();
93    }
94  
95    @Override
96    public HFileBlockEncodingContext newDataBlockEncodingContext(
97        byte[] dummyHeader, HFileContext meta) {
98      return new HFileBlockDefaultEncodingContext(null, dummyHeader, meta);
99    }
100 
101   @Override
102   public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext meta) {
103     return new HFileBlockDefaultDecodingContext(meta);
104   }
105 
106   @Override
107   public void startBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out)
108       throws IOException {
109   }
110 
111   @Override
112   public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
113       byte[] uncompressedBytesWithHeader, BlockType blockType) throws IOException {
114     encodingCtx.postEncoding(BlockType.DATA);
115   }
116 }