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