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