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;
26  import org.apache.hadoop.hbase.KeyValue.KVComparator;
27  import org.apache.hadoop.hbase.util.ByteBufferUtils;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * Compress key by storing size of common prefix with previous KeyValue
32   * and storing raw size of rest.
33   *
34   * Format:
35   * 1-5 bytes: compressed key length minus prefix (7-bit encoding)
36   * 1-5 bytes: compressed value length (7-bit encoding)
37   * 1-3 bytes: compressed length of common key prefix
38   * ... bytes: rest of key (including timestamp)
39   * ... bytes: value
40   *
41   * In a worst case compressed KeyValue will be three bytes longer than original.
42   *
43   */
44  @InterfaceAudience.Private
45  public class PrefixKeyDeltaEncoder extends BufferedDataBlockEncoder {
46  
47    private int addKV(int prevKeyOffset, DataOutputStream out,
48        ByteBuffer in, int prevKeyLength) throws IOException {
49      int keyLength = in.getInt();
50      int valueLength = in.getInt();
51  
52      if (prevKeyOffset == -1) {
53        // copy the key, there is no common prefix with none
54        ByteBufferUtils.putCompressedInt(out, keyLength);
55        ByteBufferUtils.putCompressedInt(out, valueLength);
56        ByteBufferUtils.putCompressedInt(out, 0);
57        ByteBufferUtils.moveBufferToStream(out, in, keyLength + valueLength);
58      } else {
59        // find a common prefix and skip it
60        int common = ByteBufferUtils.findCommonPrefix(
61            in, prevKeyOffset + KeyValue.ROW_OFFSET,
62            in.position(),
63            Math.min(prevKeyLength, keyLength));
64  
65        ByteBufferUtils.putCompressedInt(out, keyLength - common);
66        ByteBufferUtils.putCompressedInt(out, valueLength);
67        ByteBufferUtils.putCompressedInt(out, common);
68  
69        ByteBufferUtils.skip(in, common);
70        ByteBufferUtils.moveBufferToStream(out, in, keyLength - common
71            + valueLength);
72      }
73  
74      return keyLength;
75    }
76  
77    @Override
78    public void internalEncodeKeyValues(DataOutputStream writeHere, ByteBuffer in,
79        HFileBlockDefaultEncodingContext encodingCtx) throws IOException {
80      in.rewind();
81      ByteBufferUtils.putInt(writeHere, in.limit());
82      int prevOffset = -1;
83      int offset = 0;
84      int keyLength = 0;
85      while (in.hasRemaining()) {
86        offset = in.position();
87        keyLength = addKV(prevOffset, writeHere, in, keyLength);
88        afterEncodingKeyValue(in, writeHere, encodingCtx);
89        prevOffset = offset;
90      }
91    }
92  
93    @Override
94    protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocateHeaderLength,
95        int skipLastBytes, HFileBlockDefaultDecodingContext decodingCtx) throws IOException {
96      int decompressedSize = source.readInt();
97      ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
98          allocateHeaderLength);
99      buffer.position(allocateHeaderLength);
100     int prevKeyOffset = 0;
101 
102     while (source.available() > skipLastBytes) {
103       prevKeyOffset = decodeKeyValue(source, buffer, prevKeyOffset);
104       afterDecodingKeyValue(source, buffer, decodingCtx);
105     }
106 
107     if (source.available() != skipLastBytes) {
108       throw new IllegalStateException("Read too many bytes.");
109     }
110 
111     buffer.limit(buffer.position());
112     return buffer;
113   }
114 
115   private int decodeKeyValue(DataInputStream source, ByteBuffer buffer,
116       int prevKeyOffset)
117           throws IOException, EncoderBufferTooSmallException {
118     int keyLength = ByteBufferUtils.readCompressedInt(source);
119     int valueLength = ByteBufferUtils.readCompressedInt(source);
120     int commonLength = ByteBufferUtils.readCompressedInt(source);
121     int keyOffset;
122     keyLength += commonLength;
123 
124     ensureSpace(buffer, keyLength + valueLength + KeyValue.ROW_OFFSET);
125 
126     buffer.putInt(keyLength);
127     buffer.putInt(valueLength);
128 
129     // copy the prefix
130     if (commonLength > 0) {
131       keyOffset = buffer.position();
132       ByteBufferUtils.copyFromBufferToBuffer(buffer, buffer, prevKeyOffset,
133           commonLength);
134     } else {
135       keyOffset = buffer.position();
136     }
137 
138     // copy rest of the key and value
139     int len = keyLength - commonLength + valueLength;
140     ByteBufferUtils.copyFromStreamToBuffer(buffer, source, len);
141     return keyOffset;
142   }
143 
144   @Override
145   public ByteBuffer getFirstKeyInBlock(ByteBuffer block) {
146     block.mark();
147     block.position(Bytes.SIZEOF_INT);
148     int keyLength = ByteBufferUtils.readCompressedInt(block);
149     ByteBufferUtils.readCompressedInt(block);
150     int commonLength = ByteBufferUtils.readCompressedInt(block);
151     if (commonLength != 0) {
152       throw new AssertionError("Nonzero common length in the first key in "
153           + "block: " + commonLength);
154     }
155     int pos = block.position();
156     block.reset();
157     return ByteBuffer.wrap(block.array(), pos, keyLength).slice();
158   }
159 
160   @Override
161   public String toString() {
162     return PrefixKeyDeltaEncoder.class.getSimpleName();
163   }
164 
165   @Override
166   public EncodedSeeker createSeeker(KVComparator comparator,
167       final HFileBlockDecodingContext decodingCtx) {
168     return new BufferedEncodedSeeker<SeekerState>(comparator, decodingCtx) {
169       @Override
170       protected void decodeNext() {
171         current.keyLength = ByteBufferUtils.readCompressedInt(currentBuffer);
172         current.valueLength = ByteBufferUtils.readCompressedInt(currentBuffer);
173         current.lastCommonPrefix =
174             ByteBufferUtils.readCompressedInt(currentBuffer);
175         current.keyLength += current.lastCommonPrefix;
176         current.ensureSpaceForKey();
177         currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
178             current.keyLength - current.lastCommonPrefix);
179         current.valueOffset = currentBuffer.position();
180         ByteBufferUtils.skip(currentBuffer, current.valueLength);
181         if (includesTags()) {
182           decodeTags();
183         }
184         if (includesMvcc()) {
185           current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
186         } else {
187           current.memstoreTS = 0;
188         }
189         current.nextKvOffset = currentBuffer.position();
190       }
191 
192       @Override
193       protected void decodeFirst() {
194         ByteBufferUtils.skip(currentBuffer, Bytes.SIZEOF_INT);
195         decodeNext();
196       }
197     };
198   }
199 }