HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / codec / CellCodec.java
blob8a1b9af88fbfb53d974f369edcb47b28d89c4aab
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
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;
35 /**
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) {
44 super(out);
47 @Override
48 public void write(Cell cell) throws IOException {
49 checkFlushed();
50 // Row
51 write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
52 // Column family
53 write(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
54 // Qualifier
55 write(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
56 // Version
57 this.out.write(Bytes.toBytes(cell.getTimestamp()));
58 // Type
59 this.out.write(cell.getTypeByte());
60 // Value
61 write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
62 // MvccVersion
63 this.out.write(Bytes.toBytes(cell.getSequenceId()));
66 /**
67 * Write int length followed by array bytes.
68 * @param bytes
69 * @param offset
70 * @param length
71 * @throws IOException
73 private void write(final byte [] bytes, final int offset, final int length)
74 throws IOException {
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) {
84 super(in);
87 @Override
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()
102 .setRow(row)
103 .setFamily(family)
104 .setQualifier(qualifier)
105 .setTimestamp(timestamp)
106 .setType(type)
107 .setValue(value)
108 .setSequenceId(memstoreTS)
109 .build();
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);
122 return bytes;
126 @Override
127 public Decoder getDecoder(InputStream is) {
128 return new CellDecoder(is);
131 @Override
132 public Decoder getDecoder(ByteBuff buf) {
133 return getDecoder(new ByteBuffInputStream(buf));
136 @Override
137 public Encoder getEncoder(OutputStream os) {
138 return new CellEncoder(os);