001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.codec;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.nio.ByteBuffer;
024import org.apache.hadoop.hbase.ByteBufferKeyValue;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.HBaseInterfaceAudience;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.KeyValueUtil;
029import org.apache.hadoop.hbase.nio.ByteBuff;
030import org.apache.hadoop.hbase.util.ByteBufferUtils;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * Codec that does KeyValue version 1 serialization with serializing tags also.
035 * <p>
036 * Encodes Cell as serialized in KeyValue with total length prefix. This is how KVs were serialized
037 * in Puts, Deletes and Results pre-0.96. Its what would happen if you called the Writable#write
038 * KeyValue implementation. This encoder will fail if the passed Cell is not an old-school pre-0.96
039 * KeyValue. Does not copy bytes writing. It just writes them direct to the passed stream.
040 * <p>
041 * If you wrote two KeyValues to this encoder, it would look like this in the stream:
042 *
043 * <pre>
044 * length-of-KeyValue1 // A java int with the length of KeyValue1 backing array
045 * KeyValue1 backing array filled with a KeyValue serialized in its particular format
046 * length-of-KeyValue2
047 * KeyValue2 backing array
048 * </pre>
049 *
050 * Note: The only difference of this with KeyValueCodec is the latter ignores tags in Cells. <b>Use
051 * this Codec only at server side.</b>
052 */
053@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
054public class KeyValueCodecWithTags implements Codec {
055  public static class KeyValueEncoder extends BaseEncoder {
056    public KeyValueEncoder(final OutputStream out) {
057      super(out);
058    }
059
060    @Override
061    public void write(Cell cell) throws IOException {
062      checkFlushed();
063      // Write tags
064      ByteBufferUtils.putInt(this.out, KeyValueUtil.getSerializedSize(cell, true));
065      KeyValueUtil.oswrite(cell, out, true);
066    }
067  }
068
069  public static class KeyValueDecoder extends BaseDecoder {
070    public KeyValueDecoder(final InputStream in) {
071      super(in);
072    }
073
074    @Override
075    protected Cell parseCell() throws IOException {
076      // create KeyValue with tags
077      return KeyValueUtil.createKeyValueFromInputStream(in, true);
078    }
079  }
080
081  public static class ByteBuffKeyValueDecoder extends KeyValueCodec.ByteBuffKeyValueDecoder {
082
083    public ByteBuffKeyValueDecoder(ByteBuff buf) {
084      super(buf);
085    }
086
087    @Override
088    protected Cell createCell(byte[] buf, int offset, int len) {
089      return new KeyValue(buf, offset, len);
090    }
091
092    @Override
093    protected Cell createCell(ByteBuffer bb, int pos, int len) {
094      return new ByteBufferKeyValue(bb, pos, len);
095    }
096  }
097
098  /**
099   * Implementation depends on {@link InputStream#available()}
100   */
101  @Override
102  public Decoder getDecoder(final InputStream is) {
103    return new KeyValueDecoder(is);
104  }
105
106  @Override
107  public Encoder getEncoder(OutputStream os) {
108    return new KeyValueEncoder(os);
109  }
110
111  @Override
112  public Decoder getDecoder(ByteBuff buf) {
113    return new ByteBuffKeyValueDecoder(buf);
114  }
115}