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