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