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.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  
29  /**
30   * Codec that does KeyValue version 1 serialization with serializing tags also.
31   *
32   * <p>
33   * Encodes by casting Cell to KeyValue and writing out the backing array with a length prefix. This
34   * is how KVs were serialized in Puts, Deletes and Results pre-0.96. Its what would happen if you
35   * called the Writable#write KeyValue implementation. This encoder will fail if the passed Cell is
36   * not an old-school pre-0.96 KeyValue. Does not copy bytes writing. It just writes them direct to
37   * the passed stream.
38   *
39   * <p>
40   * If you wrote two KeyValues to this encoder, it would look like this in the stream:
41   *
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   * Note: The only difference of this with KeyValueCodec is the latter ignores tags in KeyValues.
50   * <b>Use this Codec only at server side.</b>
51   */
52  @InterfaceAudience.Private
53  public class KeyValueCodecWithTags implements Codec {
54    public static class KeyValueEncoder extends BaseEncoder {
55      public KeyValueEncoder(final OutputStream out) {
56        super(out);
57      }
58  
59      @Override
60      public void write(Cell cell) throws IOException {
61        checkFlushed();
62        // This is crass and will not work when KV changes. Also if passed a non-kv Cell, it will
63        // make expensive copy.
64        // Write tags
65        KeyValue.oswrite((KeyValue) KeyValueUtil.ensureKeyValue(cell), this.out, true);
66      }
67    }
68  
69    public static class KeyValueDecoder extends BaseDecoder {
70      public KeyValueDecoder(final InputStream in) {
71        super(in);
72      }
73  
74      protected Cell parseCell() throws IOException {
75        return KeyValue.iscreate(in);
76      }
77    }
78  
79    /**
80     * Implementation depends on {@link InputStream#available()}
81     */
82    @Override
83    public Decoder getDecoder(final InputStream is) {
84      return new KeyValueDecoder(is);
85    }
86  
87    @Override
88    public Encoder getEncoder(OutputStream os) {
89      return new KeyValueEncoder(os);
90    }
91  }