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