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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.codec
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
22 import java
.io
.OutputStream
;
24 import org
.apache
.commons
.io
.IOUtils
;
25 import org
.apache
.hadoop
.hbase
.Cell
;
26 import org
.apache
.hadoop
.hbase
.CellBuilderType
;
27 import org
.apache
.hadoop
.hbase
.ExtendedCellBuilder
;
28 import org
.apache
.hadoop
.hbase
.ExtendedCellBuilderFactory
;
29 import org
.apache
.hadoop
.hbase
.HBaseInterfaceAudience
;
30 import org
.apache
.hadoop
.hbase
.io
.ByteBuffInputStream
;
31 import org
.apache
.hadoop
.hbase
.nio
.ByteBuff
;
32 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
33 import org
.apache
.yetus
.audience
.InterfaceAudience
;
36 * Basic Cell codec that just writes out all the individual elements of a Cell. Uses ints
37 * delimiting all lengths. Profligate. Needs tune up.
38 * Note: This will not write tags of a Cell.
40 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience
.CONFIG
)
41 public class CellCodec
implements Codec
{
42 static class CellEncoder
extends BaseEncoder
{
43 CellEncoder(final OutputStream out
) {
48 public void write(Cell cell
) throws IOException
{
51 write(cell
.getRowArray(), cell
.getRowOffset(), cell
.getRowLength());
53 write(cell
.getFamilyArray(), cell
.getFamilyOffset(), cell
.getFamilyLength());
55 write(cell
.getQualifierArray(), cell
.getQualifierOffset(), cell
.getQualifierLength());
57 this.out
.write(Bytes
.toBytes(cell
.getTimestamp()));
59 this.out
.write(cell
.getTypeByte());
61 write(cell
.getValueArray(), cell
.getValueOffset(), cell
.getValueLength());
63 this.out
.write(Bytes
.toBytes(cell
.getSequenceId()));
67 * Write int length followed by array bytes.
73 private void write(final byte [] bytes
, final int offset
, final int length
)
75 // TODO add BB backed os check and do for write. Pass Cell
76 this.out
.write(Bytes
.toBytes(length
));
77 this.out
.write(bytes
, offset
, length
);
81 static class CellDecoder
extends BaseDecoder
{
82 private final ExtendedCellBuilder cellBuilder
= ExtendedCellBuilderFactory
.create(CellBuilderType
.SHALLOW_COPY
);
83 public CellDecoder(final InputStream in
) {
88 protected Cell
parseCell() throws IOException
{
89 byte [] row
= readByteArray(this.in
);
90 byte [] family
= readByteArray(in
);
91 byte [] qualifier
= readByteArray(in
);
92 byte [] longArray
= new byte[Bytes
.SIZEOF_LONG
];
93 IOUtils
.readFully(this.in
, longArray
);
94 long timestamp
= Bytes
.toLong(longArray
);
95 byte type
= (byte) this.in
.read();
96 byte[] value
= readByteArray(in
);
97 // Read memstore version
98 byte[] memstoreTSArray
= new byte[Bytes
.SIZEOF_LONG
];
99 IOUtils
.readFully(this.in
, memstoreTSArray
);
100 long memstoreTS
= Bytes
.toLong(memstoreTSArray
);
101 return cellBuilder
.clear()
104 .setQualifier(qualifier
)
105 .setTimestamp(timestamp
)
108 .setSequenceId(memstoreTS
)
113 * @return Byte array read from the stream.
114 * @throws IOException
116 private byte [] readByteArray(final InputStream in
) throws IOException
{
117 byte [] intArray
= new byte[Bytes
.SIZEOF_INT
];
118 IOUtils
.readFully(in
, intArray
);
119 int length
= Bytes
.toInt(intArray
);
120 byte [] bytes
= new byte [length
];
121 IOUtils
.readFully(in
, bytes
);
127 public Decoder
getDecoder(InputStream is
) {
128 return new CellDecoder(is
);
132 public Decoder
getDecoder(ByteBuff buf
) {
133 return getDecoder(new ByteBuffInputStream(buf
));
137 public Encoder
getEncoder(OutputStream os
) {
138 return new CellEncoder(os
);