1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.hbase.testclassification.SmallTests;
25 import org.apache.hadoop.hbase.Tag;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
29
30 @Category(SmallTests.class)
31 public class TestByteRangeWithKVSerialization {
32
33 static void writeCell(PositionedByteRange pbr, KeyValue kv) throws Exception {
34 pbr.putInt(kv.getKeyLength());
35 pbr.putInt(kv.getValueLength());
36 pbr.put(kv.getBuffer(), kv.getKeyOffset(), kv.getKeyLength());
37 pbr.put(kv.getBuffer(), kv.getValueOffset(), kv.getValueLength());
38 int tagsLen = kv.getTagsLength();
39 pbr.put((byte) (tagsLen >> 8 & 0xff));
40 pbr.put((byte) (tagsLen & 0xff));
41 pbr.put(kv.getTagsArray(), kv.getTagsOffset(), tagsLen);
42 pbr.putVLong(kv.getMvccVersion());
43 }
44
45 static KeyValue readCell(PositionedByteRange pbr) throws Exception {
46 int kvStartPos = pbr.getPosition();
47 int keyLen = pbr.getInt();
48 int valLen = pbr.getInt();
49 pbr.setPosition(pbr.getPosition() + keyLen + valLen);
50 int tagsLen = ((pbr.get() & 0xff) << 8) ^ (pbr.get() & 0xff);
51 pbr.setPosition(pbr.getPosition() + tagsLen);
52 long mvcc = pbr.getVLong();
53 KeyValue kv = new KeyValue(pbr.getBytes(), kvStartPos,
54 (int) KeyValue.getKeyValueDataStructureSize(keyLen, valLen, tagsLen));
55 kv.setSequenceId(mvcc);
56 return kv;
57 }
58
59 @Test
60 public void testWritingAndReadingCells() throws Exception {
61 final byte[] FAMILY = Bytes.toBytes("f1");
62 final byte[] QUALIFIER = Bytes.toBytes("q1");
63 final byte[] VALUE = Bytes.toBytes("v");
64 int kvCount = 1000000;
65 List<KeyValue> kvs = new ArrayList<KeyValue>(kvCount);
66 int totalSize = 0;
67 Tag[] tags = new Tag[] { new Tag((byte) 1, "tag1") };
68 for (int i = 0; i < kvCount; i++) {
69 KeyValue kv = new KeyValue(Bytes.toBytes(i), FAMILY, QUALIFIER, i, VALUE, tags);
70 kv.setSequenceId(i);
71 kvs.add(kv);
72 totalSize += kv.getLength() + Bytes.SIZEOF_LONG;
73 }
74 PositionedByteRange pbr = new SimplePositionedMutableByteRange(totalSize);
75 for (KeyValue kv : kvs) {
76 writeCell(pbr, kv);
77 }
78
79 PositionedByteRange pbr1 = new SimplePositionedMutableByteRange(pbr.getBytes(), 0,
80 pbr.getPosition());
81 for (int i = 0; i < kvCount; i++) {
82 KeyValue kv = readCell(pbr1);
83 KeyValue kv1 = kvs.get(i);
84 Assert.assertTrue(kv.equals(kv1));
85 Assert.assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(),
86 kv1.getValueArray(), kv1.getValueOffset(), kv1.getValueLength()));
87 Assert.assertTrue(Bytes.equals(kv.getTagsArray(), kv.getTagsOffset(),
88 kv.getTagsLength(), kv1.getTagsArray(), kv1.getTagsOffset(),
89 kv1.getTagsLength()));
90 Assert.assertEquals(kv1.getMvccVersion(), kv.getMvccVersion());
91 }
92 }
93 }