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.KVComparator;
26  import org.apache.hadoop.hbase.util.ByteBufferUtils;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * Just copy data, do not do any kind of compression. Use for comparison and
31   * benchmarking.
32   */
33  @InterfaceAudience.Private
34  public class CopyKeyDataBlockEncoder extends BufferedDataBlockEncoder {
35    @Override
36    public void internalEncodeKeyValues(DataOutputStream out,
37        ByteBuffer in, HFileBlockDefaultEncodingContext encodingCtx) throws IOException {
38      in.rewind();
39      ByteBufferUtils.putInt(out, in.limit());
40      ByteBufferUtils.moveBufferToStream(out, in, in.limit());
41    }
42  
43  
44    @Override
45    public ByteBuffer getFirstKeyInBlock(ByteBuffer block) {
46      int keyLength = block.getInt(Bytes.SIZEOF_INT);
47      return ByteBuffer.wrap(block.array(),
48          block.arrayOffset() + 3 * Bytes.SIZEOF_INT, keyLength).slice();
49    }
50  
51  
52    @Override
53    public String toString() {
54      return CopyKeyDataBlockEncoder.class.getSimpleName();
55    }
56  
57    @Override
58    public EncodedSeeker createSeeker(KVComparator comparator,
59        final HFileBlockDecodingContext decodingCtx) {
60      return new BufferedEncodedSeeker<SeekerState>(comparator, decodingCtx) {
61        @Override
62        protected void decodeNext() {
63          current.keyLength = currentBuffer.getInt();
64          current.valueLength = currentBuffer.getInt();
65          current.ensureSpaceForKey();
66          currentBuffer.get(current.keyBuffer, 0, current.keyLength);
67          current.valueOffset = currentBuffer.position();
68          ByteBufferUtils.skip(currentBuffer, current.valueLength);
69          if (includesTags()) {
70            current.tagsLength = currentBuffer.getShort();
71            ByteBufferUtils.skip(currentBuffer, current.tagsLength);
72          }
73          if (includesMvcc()) {
74            current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
75          } else {
76            current.memstoreTS = 0;
77          }
78          current.nextKvOffset = currentBuffer.position();
79        }
80  
81        @Override
82        protected void decodeFirst() {
83          ByteBufferUtils.skip(currentBuffer, Bytes.SIZEOF_INT);
84          current.lastCommonPrefix = 0;
85          decodeNext();
86        }
87      };
88    }
89  
90    @Override
91    protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocateHeaderLength,
92        int skipLastBytes, HFileBlockDefaultDecodingContext decodingCtx) throws IOException {
93      int decompressedSize = source.readInt();
94      ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
95          allocateHeaderLength);
96      buffer.position(allocateHeaderLength);
97      ByteBufferUtils.copyFromStreamToBuffer(buffer, source, decompressedSize);
98  
99      return buffer;
100   }
101 
102 }