View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.io.encoding;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellUtil;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.io.WritableUtils;
30  
31  @InterfaceAudience.Private
32  public class NoneEncoder {
33  
34    private DataOutputStream out;
35    private HFileBlockDefaultEncodingContext encodingCtx;
36  
37    public NoneEncoder(DataOutputStream out,
38        HFileBlockDefaultEncodingContext encodingCtx) {
39      this.out = out;
40      this.encodingCtx = encodingCtx;
41    }
42  
43    public int write(Cell cell) throws IOException {
44      int klength = KeyValueUtil.keyLength(cell);
45      int vlength = cell.getValueLength();
46  
47      out.writeInt(klength);
48      out.writeInt(vlength);
49      CellUtil.writeFlatKey(cell, out);
50      out.write(cell.getValueArray(), cell.getValueOffset(), vlength);
51      int size = klength + vlength + KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE;
52      // Write the additional tag into the stream
53      if (encodingCtx.getHFileContext().isIncludesTags()) {
54        int tagsLength = cell.getTagsLength();
55        out.writeShort(tagsLength);
56        if (tagsLength > 0) {
57          out.write(cell.getTagsArray(), cell.getTagsOffset(), tagsLength);
58        }
59        size += tagsLength + KeyValue.TAGS_LENGTH_SIZE;
60      }
61      if (encodingCtx.getHFileContext().isIncludesMvcc()) {
62        WritableUtils.writeVLong(out, cell.getSequenceId());
63        size += WritableUtils.getVIntSize(cell.getSequenceId());
64      }
65      return size;
66    }
67  
68  }