View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.codec.prefixtree;
20  
21  import java.io.DataInputStream;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.CellComparator;
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.CellComparator.MetaCellComparator;
31  import org.apache.hadoop.hbase.KeyValueUtil;
32  import org.apache.hadoop.hbase.codec.prefixtree.decode.DecoderFactory;
33  import org.apache.hadoop.hbase.codec.prefixtree.decode.PrefixTreeArraySearcher;
34  import org.apache.hadoop.hbase.codec.prefixtree.encode.EncoderFactory;
35  import org.apache.hadoop.hbase.codec.prefixtree.encode.PrefixTreeEncoder;
36  import org.apache.hadoop.hbase.codec.prefixtree.scanner.CellSearcher;
37  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoder;
38  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
39  import org.apache.hadoop.hbase.io.encoding.EncodingState;
40  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
41  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultDecodingContext;
42  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext;
43  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
44  import org.apache.hadoop.hbase.io.hfile.BlockType;
45  import org.apache.hadoop.hbase.io.hfile.HFileContext;
46  import org.apache.hadoop.hbase.nio.ByteBuff;
47  import org.apache.hadoop.hbase.util.ByteBufferUtils;
48  import org.apache.hadoop.io.WritableUtils;
49  
50  /**
51   * <p>
52   * This class is created via reflection in DataBlockEncoding enum. Update the enum if class name or
53   * package changes.
54   * </p>
55   * PrefixTreeDataBlockEncoder implementation of DataBlockEncoder. This is the primary entry point
56   * for PrefixTree encoding and decoding. Encoding is delegated to instances of
57   * {@link PrefixTreeEncoder}, and decoding is delegated to instances of
58   * {@link org.apache.hadoop.hbase.codec.prefixtree.scanner.CellSearcher}.
59   * Encoder and decoder instances are
60   * created and recycled by static PtEncoderFactory and PtDecoderFactory.
61   */
62  @InterfaceAudience.Private
63  public class PrefixTreeCodec implements DataBlockEncoder {
64  
65    /**
66     * no-arg constructor for reflection
67     */
68    public PrefixTreeCodec() {
69    }
70  
71    @Override
72    public ByteBuffer decodeKeyValues(DataInputStream source, HFileBlockDecodingContext decodingCtx)
73        throws IOException {
74      return decodeKeyValues(source, 0, 0, decodingCtx);
75    }
76  
77  
78    /**
79     * I don't think this method is called during normal HBase operation, so efficiency is not
80     * important.
81     */
82    public ByteBuffer decodeKeyValues(DataInputStream source, int allocateHeaderLength,
83        int skipLastBytes, HFileBlockDecodingContext decodingCtx) throws IOException {
84      ByteBuffer sourceAsBuffer = ByteBufferUtils.drainInputStreamToBuffer(source);// waste
85      sourceAsBuffer.mark();
86      PrefixTreeBlockMeta blockMeta = new PrefixTreeBlockMeta(sourceAsBuffer);
87      sourceAsBuffer.rewind();
88      int numV1BytesWithHeader = allocateHeaderLength + blockMeta.getNumKeyValueBytes();
89      byte[] keyValueBytesWithHeader = new byte[numV1BytesWithHeader];
90      ByteBuffer result = ByteBuffer.wrap(keyValueBytesWithHeader);
91      result.rewind();
92      CellSearcher searcher = null;
93      try {
94        boolean includesMvcc = decodingCtx.getHFileContext().isIncludesMvcc();
95        searcher = DecoderFactory.checkOut(sourceAsBuffer, includesMvcc);
96        while (searcher.advance()) {
97          KeyValue currentCell = KeyValueUtil.copyToNewKeyValue(searcher.current());
98          // needs to be modified for DirectByteBuffers. no existing methods to
99          // write VLongs to byte[]
100         int offset = result.arrayOffset() + result.position();
101         System.arraycopy(currentCell.getBuffer(), currentCell.getOffset(), result.array(), offset,
102             currentCell.getLength());
103         int keyValueLength = KeyValueUtil.length(currentCell);
104         ByteBufferUtils.skip(result, keyValueLength);
105         offset += keyValueLength;
106         if (includesMvcc) {
107           ByteBufferUtils.writeVLong(result, currentCell.getSequenceId());
108         }
109       }
110       result.position(result.limit());//make it appear as if we were appending
111       return result;
112     } finally {
113       DecoderFactory.checkIn(searcher);
114     }
115   }
116 
117 
118   @Override
119   public Cell getFirstKeyCellInBlock(ByteBuff block) {
120     block.rewind();
121     PrefixTreeArraySearcher searcher = null;
122     try {
123       // should i includeMemstoreTS (second argument)?  i think PrefixKeyDeltaEncoder is, so i will
124       // TODO : Change to work with BBs
125       searcher = DecoderFactory.checkOut(block.asSubByteBuffer(block.limit() - block.position()),
126           true);
127       if (!searcher.positionAtFirstCell()) {
128         return null;
129       }
130       return searcher.current();
131     } finally {
132       DecoderFactory.checkIn(searcher);
133     }
134   }
135 
136   @Override
137   public HFileBlockEncodingContext newDataBlockEncodingContext(
138       DataBlockEncoding encoding, byte[] header, HFileContext meta) {
139     if(DataBlockEncoding.PREFIX_TREE != encoding){
140       //i'm not sure why encoding is in the interface.  Each encoder implementation should probably
141       //know it's encoding type
142       throw new IllegalArgumentException("only DataBlockEncoding.PREFIX_TREE supported");
143     }
144     return new HFileBlockDefaultEncodingContext(encoding, header, meta);
145   }
146 
147   @Override
148   public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext meta) {
149     return new HFileBlockDefaultDecodingContext(meta);
150   }
151 
152   /**
153    * Is this the correct handling of an illegal comparator?  How to prevent that from getting all
154    * the way to this point.
155    */
156   @Override
157   public EncodedSeeker createSeeker(CellComparator comparator,
158       HFileBlockDecodingContext decodingCtx) {
159     if (comparator instanceof MetaCellComparator) {
160       throw new IllegalArgumentException(
161           "DataBlockEncoding.PREFIX_TREE not compatible with hbase:meta " + "table");
162     }
163 
164     return new PrefixTreeSeeker(decodingCtx.getHFileContext().isIncludesMvcc());
165   }
166 
167   @Override
168   public int encode(Cell cell, HFileBlockEncodingContext encodingCtx, DataOutputStream out)
169       throws IOException {
170     PrefixTreeEncodingState state = (PrefixTreeEncodingState) encodingCtx.getEncodingState();
171     PrefixTreeEncoder builder = state.builder;
172     builder.write(cell);
173     int size = KeyValueUtil.length(cell);
174     if (encodingCtx.getHFileContext().isIncludesMvcc()) {
175       size += WritableUtils.getVIntSize(cell.getSequenceId());
176     }
177     return size;
178   }
179 
180   private static class PrefixTreeEncodingState extends EncodingState {
181     PrefixTreeEncoder builder = null;
182   }
183 
184   @Override
185   public void startBlockEncoding(HFileBlockEncodingContext blkEncodingCtx, DataOutputStream out)
186       throws IOException {
187     if (blkEncodingCtx.getClass() != HFileBlockDefaultEncodingContext.class) {
188       throw new IOException(this.getClass().getName() + " only accepts "
189           + HFileBlockDefaultEncodingContext.class.getName() + " as the " + "encoding context.");
190     }
191 
192     HFileBlockDefaultEncodingContext encodingCtx = 
193         (HFileBlockDefaultEncodingContext) blkEncodingCtx;
194     encodingCtx.prepareEncoding(out);
195 
196     PrefixTreeEncoder builder = EncoderFactory.checkOut(out, encodingCtx.getHFileContext()
197         .isIncludesMvcc());
198     PrefixTreeEncodingState state = new PrefixTreeEncodingState();
199     state.builder = builder;
200     blkEncodingCtx.setEncodingState(state);
201   }
202 
203   @Override
204   public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
205       byte[] uncompressedBytesWithHeader) throws IOException {
206     PrefixTreeEncodingState state = (PrefixTreeEncodingState) encodingCtx.getEncodingState();
207     PrefixTreeEncoder builder = state.builder;
208     builder.flush();
209     EncoderFactory.checkIn(builder);
210     // do i need to check this, or will it always be DataBlockEncoding.PREFIX_TREE?
211     if (encodingCtx.getDataBlockEncoding() != DataBlockEncoding.NONE) {
212       encodingCtx.postEncoding(BlockType.ENCODED_DATA);
213     } else {
214       encodingCtx.postEncoding(BlockType.DATA);
215     }
216   }
217 }