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