1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.commons.io.IOUtils;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.classification.InterfaceStability;
27 import org.apache.hadoop.hbase.Cell;
28 import org.apache.hadoop.hbase.CellUtil;
29 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30 import org.apache.hadoop.hbase.util.Bytes;
31
32
33
34
35
36
37 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
38 @InterfaceStability.Unstable
39 public class CellCodecWithTags implements Codec {
40 static class CellEncoder extends BaseEncoder {
41 CellEncoder(final OutputStream out) {
42 super(out);
43 }
44
45 @Override
46 public void write(Cell cell) throws IOException {
47 checkFlushed();
48
49 write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
50
51 write(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
52
53 write(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
54
55 this.out.write(Bytes.toBytes(cell.getTimestamp()));
56
57 this.out.write(cell.getTypeByte());
58
59 write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
60
61 write(cell.getTagsArray(), cell.getTagsOffset(), cell.getTagsLengthUnsigned());
62
63 this.out.write(Bytes.toBytes(cell.getMvccVersion()));
64 }
65
66
67
68
69
70
71
72
73
74 private void write(final byte[] bytes, final int offset, final int length) throws IOException {
75 this.out.write(Bytes.toBytes(length));
76 this.out.write(bytes, offset, length);
77 }
78 }
79
80 static class CellDecoder extends BaseDecoder {
81 public CellDecoder(final InputStream in) {
82 super(in);
83 }
84
85 protected Cell parseCell() throws IOException {
86 byte[] row = readByteArray(this.in);
87 byte[] family = readByteArray(in);
88 byte[] qualifier = readByteArray(in);
89 byte[] longArray = new byte[Bytes.SIZEOF_LONG];
90 IOUtils.readFully(this.in, longArray);
91 long timestamp = Bytes.toLong(longArray);
92 byte type = (byte) this.in.read();
93 byte[] value = readByteArray(in);
94 byte[] tags = readByteArray(in);
95
96 byte[] memstoreTSArray = new byte[Bytes.SIZEOF_LONG];
97 IOUtils.readFully(this.in, memstoreTSArray);
98 long memstoreTS = Bytes.toLong(memstoreTSArray);
99 return CellUtil.createCell(row, family, qualifier, timestamp, type, value, tags, memstoreTS);
100 }
101
102
103
104
105
106 private byte[] readByteArray(final InputStream in) throws IOException {
107 byte[] intArray = new byte[Bytes.SIZEOF_INT];
108 IOUtils.readFully(in, intArray);
109 int length = Bytes.toInt(intArray);
110 byte[] bytes = new byte[length];
111 IOUtils.readFully(in, bytes);
112 return bytes;
113 }
114 }
115
116 @Override
117 public Decoder getDecoder(InputStream is) {
118 return new CellDecoder(is);
119 }
120
121 @Override
122 public Encoder getEncoder(OutputStream os) {
123 return new CellEncoder(os);
124 }
125 }