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  package org.apache.hadoop.hbase.codec;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.nio.ByteBuffer;
24  
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  import org.apache.hadoop.hbase.NoTagsKeyValue;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.util.ByteBufferUtils;
31  
32  /**
33   * Codec that does KeyValue version 1 serialization.
34   * 
35   * <p>Encodes Cell as serialized in KeyValue with total length prefix.
36   * This is how KVs were serialized in Puts, Deletes and Results pre-0.96.  Its what would
37   * happen if you called the Writable#write KeyValue implementation.  This encoder will fail
38   * if the passed Cell is not an old-school pre-0.96 KeyValue.  Does not copy bytes writing.
39   * It just writes them direct to the passed stream.
40   *
41   * <p>If you wrote two KeyValues to this encoder, it would look like this in the stream:
42   * <pre>
43   * length-of-KeyValue1 // A java int with the length of KeyValue1 backing array
44   * KeyValue1 backing array filled with a KeyValue serialized in its particular format
45   * length-of-KeyValue2
46   * KeyValue2 backing array
47   * </pre>
48   */
49  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
50  public class KeyValueCodec implements Codec {
51    public static class KeyValueEncoder extends BaseEncoder {
52      public KeyValueEncoder(final OutputStream out) {
53        super(out);
54      }
55  
56      @Override
57      public void write(Cell cell) throws IOException {
58        checkFlushed();
59        // Do not write tags over RPC
60        KeyValueUtil.oswrite(cell, out, false);
61      }
62    }
63  
64    public static class KeyValueDecoder extends BaseDecoder {
65      public KeyValueDecoder(final InputStream in) {
66        super(in);
67      }
68  
69      protected Cell parseCell() throws IOException {
70        // No tags here
71        return KeyValueUtil.iscreate(in, false);
72      }
73    }
74  
75    public static class ByteBufferedKeyValueDecoder implements Codec.Decoder {
76  
77      protected final ByteBuffer buf;
78      protected Cell current = null;
79  
80      public ByteBufferedKeyValueDecoder(ByteBuffer buf) {
81        this.buf = buf;
82      }
83  
84      @Override
85      public boolean advance() throws IOException {
86        if (this.buf.remaining() <= 0) {
87          return false;
88        }
89        int len = ByteBufferUtils.toInt(buf);
90        assert buf.hasArray();
91        this.current = createCell(buf.array(), buf.arrayOffset() + buf.position(), len);
92        buf.position(buf.position() + len);
93        return true;
94      }
95  
96      @Override
97      public Cell current() {
98        return this.current;
99      }
100 
101     protected Cell createCell(byte[] buf, int offset, int len) {
102       return new NoTagsKeyValue(buf, offset, len);
103     }
104   }
105 
106   /**
107    * Implementation depends on {@link InputStream#available()}
108    */
109   @Override
110   public Decoder getDecoder(final InputStream is) {
111     return new KeyValueDecoder(is);
112   }
113 
114   @Override
115   public Decoder getDecoder(ByteBuffer buf) {
116     return new ByteBufferedKeyValueDecoder(buf);
117   }
118 
119   @Override
120   public Encoder getEncoder(OutputStream os) {
121     return new KeyValueEncoder(os);
122   }
123 }