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  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26  import org.apache.hadoop.hbase.KeyValueUtil;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  
29  /**
30   * Codec that does KeyValue version 1 serialization.
31   * 
32   * <p>Encodes Cell as serialized in KeyValue with total length prefix.
33   * This is how KVs were serialized in Puts, Deletes and Results pre-0.96.  Its what would
34   * happen if you called the Writable#write KeyValue implementation.  This encoder will fail
35   * if the passed Cell is not an old-school pre-0.96 KeyValue.  Does not copy bytes writing.
36   * It just writes them direct to the passed stream.
37   *
38   * <p>If you wrote two KeyValues to this encoder, it would look like this in the stream:
39   * <pre>
40   * length-of-KeyValue1 // A java int with the length of KeyValue1 backing array
41   * KeyValue1 backing array filled with a KeyValue serialized in its particular format
42   * length-of-KeyValue2
43   * KeyValue2 backing array
44   * </pre>
45   */
46  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
47  public class KeyValueCodec implements Codec {
48    public static class KeyValueEncoder extends BaseEncoder {
49      public KeyValueEncoder(final OutputStream out) {
50        super(out);
51      }
52  
53      @Override
54      public void write(Cell cell) throws IOException {
55        checkFlushed();
56        // Do not write tags over RPC
57        KeyValueUtil.oswrite(cell, out, false);
58      }
59    }
60  
61    public static class KeyValueDecoder extends BaseDecoder {
62      public KeyValueDecoder(final InputStream in) {
63        super(in);
64      }
65  
66      protected Cell parseCell() throws IOException {
67        // No tags here
68        return KeyValueUtil.iscreate(in, false);
69      }
70    }
71  
72    /**
73     * Implementation depends on {@link InputStream#available()}
74     */
75    @Override
76    public Decoder getDecoder(final InputStream is) {
77      return new KeyValueDecoder(is);
78    }
79  
80    @Override
81    public Encoder getEncoder(OutputStream os) {
82      return new KeyValueEncoder(os);
83    }
84  }