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.encoding;
18  
19  import java.io.DataInputStream;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellComparator;
26  import org.apache.hadoop.hbase.CellUtil;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.KeyValueUtil;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.nio.ByteBuff;
31  import org.apache.hadoop.hbase.util.ByteBufferUtils;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.io.WritableUtils;
34  
35  /**
36   * Just copy data, do not do any kind of compression. Use for comparison and
37   * benchmarking.
38   */
39  @InterfaceAudience.Private
40  public class CopyKeyDataBlockEncoder extends BufferedDataBlockEncoder {
41  
42    @Override
43    public int internalEncode(Cell cell, HFileBlockDefaultEncodingContext encodingContext,
44        DataOutputStream out) throws IOException {
45      int klength = KeyValueUtil.keyLength(cell);
46      int vlength = cell.getValueLength();
47  
48      out.writeInt(klength);
49      out.writeInt(vlength);
50      CellUtil.writeFlatKey(cell, out);
51      out.write(cell.getValueArray(), cell.getValueOffset(), vlength);
52      int size = klength + vlength + KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE;
53      // Write the additional tag into the stream
54      if (encodingContext.getHFileContext().isIncludesTags()) {
55        int tagsLength = cell.getTagsLength();
56        out.writeShort(tagsLength);
57        if (tagsLength > 0) {
58          out.write(cell.getTagsArray(), cell.getTagsOffset(), tagsLength);
59        }
60        size += tagsLength + KeyValue.TAGS_LENGTH_SIZE;
61      }
62      if (encodingContext.getHFileContext().isIncludesMvcc()) {
63        WritableUtils.writeVLong(out, cell.getSequenceId());
64        size += WritableUtils.getVIntSize(cell.getSequenceId());
65      }
66      return size;
67    }
68  
69    @Override
70    public Cell getFirstKeyCellInBlock(ByteBuff block) {
71      int keyLength = block.getIntAfterPosition(Bytes.SIZEOF_INT);
72      int pos = 3 * Bytes.SIZEOF_INT;
73      ByteBuffer key = block.asSubByteBuffer(pos + keyLength).duplicate();
74      return createFirstKeyCell(key, keyLength);
75    }
76  
77    @Override
78    public String toString() {
79      return CopyKeyDataBlockEncoder.class.getSimpleName();
80    }
81  
82    @Override
83    public EncodedSeeker createSeeker(CellComparator comparator,
84        final HFileBlockDecodingContext decodingCtx) {
85      return new BufferedEncodedSeeker<SeekerState>(comparator, decodingCtx) {
86        @Override
87        protected void decodeNext() {
88          current.keyLength = currentBuffer.getInt();
89          current.valueLength = currentBuffer.getInt();
90          current.ensureSpaceForKey();
91          currentBuffer.get(current.keyBuffer, 0, current.keyLength);
92          current.valueOffset = currentBuffer.position();
93          currentBuffer.skip(current.valueLength);
94          if (includesTags()) {
95            // Read short as unsigned, high byte first
96            current.tagsLength = ((currentBuffer.get() & 0xff) << 8) ^ (currentBuffer.get() & 0xff);
97            currentBuffer.skip(current.tagsLength);
98          }
99          if (includesMvcc()) {
100           current.memstoreTS = ByteBuff.readVLong(currentBuffer);
101         } else {
102           current.memstoreTS = 0;
103         }
104         current.nextKvOffset = currentBuffer.position();
105       }
106 
107       @Override
108       protected void decodeFirst() {
109         currentBuffer.skip(Bytes.SIZEOF_INT);
110         current.lastCommonPrefix = 0;
111         decodeNext();
112       }
113     };
114   }
115 
116   @Override
117   protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocateHeaderLength,
118       int skipLastBytes, HFileBlockDefaultDecodingContext decodingCtx) throws IOException {
119     int decompressedSize = source.readInt();
120     ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
121         allocateHeaderLength);
122     buffer.position(allocateHeaderLength);
123     ByteBufferUtils.copyFromStreamToBuffer(buffer, source, decompressedSize);
124 
125     return buffer;
126   }
127 }