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.util.ByteBufferUtils;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.io.RawComparator;
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, boolean includesMemstoreTS) throws IOException {
38      in.rewind();
39      ByteBufferUtils.putInt(out, in.limit());
40      ByteBufferUtils.moveBufferToStream(out, in, in.limit());
41    }
42  
43    @Override
44    public ByteBuffer decodeKeyValues(DataInputStream source,
45        int preserveHeaderLength, int skipLastBytes, boolean includesMemstoreTS)
46        throws IOException {
47      int decompressedSize = source.readInt();
48      ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
49          preserveHeaderLength);
50      buffer.position(preserveHeaderLength);
51      ByteBufferUtils.copyFromStreamToBuffer(buffer, source, decompressedSize);
52  
53      return buffer;
54    }
55  
56    @Override
57    public ByteBuffer getFirstKeyInBlock(ByteBuffer block) {
58      int keyLength = block.getInt(Bytes.SIZEOF_INT);
59      return ByteBuffer.wrap(block.array(),
60          block.arrayOffset() + 3 * Bytes.SIZEOF_INT, keyLength).slice();
61    }
62  
63  
64    @Override
65    public String toString() {
66      return CopyKeyDataBlockEncoder.class.getSimpleName();
67    }
68  
69    @Override
70    public EncodedSeeker createSeeker(RawComparator<byte[]> comparator,
71        final boolean includesMemstoreTS) {
72      return new BufferedEncodedSeeker<SeekerState>(comparator) {
73        @Override
74        protected void decodeNext() {
75          current.keyLength = currentBuffer.getInt();
76          current.valueLength = currentBuffer.getInt();
77          current.ensureSpaceForKey();
78          currentBuffer.get(current.keyBuffer, 0, current.keyLength);
79          current.valueOffset = currentBuffer.position();
80          ByteBufferUtils.skip(currentBuffer, current.valueLength);
81          if (includesMemstoreTS) {
82            current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
83          } else {
84            current.memstoreTS = 0;
85          }
86          current.nextKvOffset = currentBuffer.position();
87        }
88  
89        @Override
90        protected void decodeFirst() {
91          ByteBufferUtils.skip(currentBuffer, Bytes.SIZEOF_INT);
92          current.lastCommonPrefix = 0;
93          decodeNext();
94        }
95      };
96    }
97  
98  }