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