1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.math.BigDecimal;
27 import java.nio.ByteBuffer;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.NavigableMap;
31 import java.util.TreeMap;
32
33 import org.apache.hadoop.hbase.KeyValue.Type;
34 import org.apache.hadoop.hbase.testclassification.MiscTests;
35 import org.apache.hadoop.hbase.testclassification.SmallTests;
36 import org.apache.hadoop.hbase.util.ByteBufferUtils;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.Assert;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41
42 @Category({MiscTests.class, SmallTests.class})
43 public class TestCellUtil {
44
45
46
47 private class TestCellScannable implements CellScannable {
48 private final int cellsCount;
49 TestCellScannable(final int cellsCount) {
50 this.cellsCount = cellsCount;
51 }
52 @Override
53 public CellScanner cellScanner() {
54 return new TestCellScanner(this.cellsCount);
55 }
56 };
57
58
59
60
61 private class TestCellScanner implements CellScanner {
62 private int count = 0;
63 private Cell current = null;
64 private final int cellsCount;
65
66 TestCellScanner(final int cellsCount) {
67 this.cellsCount = cellsCount;
68 }
69
70 @Override
71 public Cell current() {
72 return this.current;
73 }
74
75 @Override
76 public boolean advance() throws IOException {
77 if (this.count < cellsCount) {
78 this.current = new TestCell(this.count);
79 this.count++;
80 return true;
81 }
82 return false;
83 }
84 }
85
86
87
88
89 private class TestCell implements Cell {
90 private final byte [] row;
91
92 TestCell(final int i) {
93 this.row = Bytes.toBytes(i);
94 }
95
96 @Override
97 public byte[] getRowArray() {
98 return this.row;
99 }
100
101 @Override
102 public int getRowOffset() {
103 return 0;
104 }
105
106 @Override
107 public short getRowLength() {
108 return (short)this.row.length;
109 }
110
111 @Override
112 public byte[] getFamilyArray() {
113
114 return null;
115 }
116
117 @Override
118 public int getFamilyOffset() {
119
120 return 0;
121 }
122
123 @Override
124 public byte getFamilyLength() {
125
126 return 0;
127 }
128
129 @Override
130 public byte[] getQualifierArray() {
131
132 return null;
133 }
134
135 @Override
136 public int getQualifierOffset() {
137
138 return 0;
139 }
140
141 @Override
142 public int getQualifierLength() {
143
144 return 0;
145 }
146
147 @Override
148 public long getTimestamp() {
149
150 return 0;
151 }
152
153 @Override
154 public byte getTypeByte() {
155
156 return 0;
157 }
158
159 @Override
160 public byte[] getValueArray() {
161
162 return null;
163 }
164
165 @Override
166 public int getValueOffset() {
167
168 return 0;
169 }
170
171 @Override
172 public int getValueLength() {
173
174 return 0;
175 }
176
177 @Override
178 public byte[] getTagsArray() {
179
180 return null;
181 }
182
183 @Override
184 public int getTagsOffset() {
185
186 return 0;
187 }
188
189 @Override
190 public long getSequenceId() {
191
192 return 0;
193 }
194
195 @Override
196 public int getTagsLength() {
197
198 return 0;
199 }
200 };
201
202
203
204
205
206 @Test
207 public void testCreateCellScannerOverflow() throws IOException {
208 consume(doCreateCellScanner(1, 1), 1 * 1);
209 consume(doCreateCellScanner(3, 0), 3 * 0);
210 consume(doCreateCellScanner(3, 3), 3 * 3);
211 consume(doCreateCellScanner(0, 1), 0 * 1);
212
213 final int hundredK = 100000;
214 consume(doCreateCellScanner(hundredK, 0), hundredK * 0);
215 consume(doCreateCellArray(1), 1);
216 consume(doCreateCellArray(0), 0);
217 consume(doCreateCellArray(3), 3);
218 List<CellScannable> cells = new ArrayList<CellScannable>(hundredK);
219 for (int i = 0; i < hundredK; i++) {
220 cells.add(new TestCellScannable(1));
221 }
222 consume(CellUtil.createCellScanner(cells), hundredK * 1);
223 NavigableMap<byte [], List<Cell>> m = new TreeMap<byte [], List<Cell>>(Bytes.BYTES_COMPARATOR);
224 List<Cell> cellArray = new ArrayList<Cell>(hundredK);
225 for (int i = 0; i < hundredK; i++) cellArray.add(new TestCell(i));
226 m.put(new byte [] {'f'}, cellArray);
227 consume(CellUtil.createCellScanner(m), hundredK * 1);
228 }
229
230 private CellScanner doCreateCellArray(final int itemsPerList) {
231 Cell [] cells = new Cell [itemsPerList];
232 for (int i = 0; i < itemsPerList; i++) {
233 cells[i] = new TestCell(i);
234 }
235 return CellUtil.createCellScanner(cells);
236 }
237
238 private CellScanner doCreateCellScanner(final int listsCount, final int itemsPerList)
239 throws IOException {
240 List<CellScannable> cells = new ArrayList<CellScannable>(listsCount);
241 for (int i = 0; i < listsCount; i++) {
242 CellScannable cs = new CellScannable() {
243 @Override
244 public CellScanner cellScanner() {
245 return new TestCellScanner(itemsPerList);
246 }
247 };
248 cells.add(cs);
249 }
250 return CellUtil.createCellScanner(cells);
251 }
252
253 private void consume(final CellScanner scanner, final int expected) throws IOException {
254 int count = 0;
255 while (scanner.advance()) count++;
256 Assert.assertEquals(expected, count);
257 }
258
259 @Test
260 public void testOverlappingKeys() {
261 byte[] empty = HConstants.EMPTY_BYTE_ARRAY;
262 byte[] a = Bytes.toBytes("a");
263 byte[] b = Bytes.toBytes("b");
264 byte[] c = Bytes.toBytes("c");
265 byte[] d = Bytes.toBytes("d");
266
267
268 Assert.assertTrue(CellUtil.overlappingKeys(a, b, a, b));
269 Assert.assertTrue(CellUtil.overlappingKeys(a, c, a, b));
270 Assert.assertTrue(CellUtil.overlappingKeys(a, b, a, c));
271 Assert.assertTrue(CellUtil.overlappingKeys(b, c, a, c));
272 Assert.assertTrue(CellUtil.overlappingKeys(a, c, b, c));
273 Assert.assertTrue(CellUtil.overlappingKeys(a, d, b, c));
274 Assert.assertTrue(CellUtil.overlappingKeys(b, c, a, d));
275
276 Assert.assertTrue(CellUtil.overlappingKeys(empty, b, a, b));
277 Assert.assertTrue(CellUtil.overlappingKeys(empty, b, a, c));
278
279 Assert.assertTrue(CellUtil.overlappingKeys(a, b, empty, b));
280 Assert.assertTrue(CellUtil.overlappingKeys(a, b, empty, c));
281
282 Assert.assertTrue(CellUtil.overlappingKeys(a, empty, a, b));
283 Assert.assertTrue(CellUtil.overlappingKeys(a, empty, a, c));
284
285 Assert.assertTrue(CellUtil.overlappingKeys(a, b, empty, empty));
286 Assert.assertTrue(CellUtil.overlappingKeys(empty, empty, a, b));
287
288
289 Assert.assertFalse(CellUtil.overlappingKeys(a, b, c, d));
290 Assert.assertFalse(CellUtil.overlappingKeys(c, d, a, b));
291
292 Assert.assertFalse(CellUtil.overlappingKeys(b, c, c, d));
293 Assert.assertFalse(CellUtil.overlappingKeys(b, c, c, empty));
294 Assert.assertFalse(CellUtil.overlappingKeys(b, c, d, empty));
295 Assert.assertFalse(CellUtil.overlappingKeys(c, d, b, c));
296 Assert.assertFalse(CellUtil.overlappingKeys(c, empty, b, c));
297 Assert.assertFalse(CellUtil.overlappingKeys(d, empty, b, c));
298
299 Assert.assertFalse(CellUtil.overlappingKeys(b, c, a, b));
300 Assert.assertFalse(CellUtil.overlappingKeys(b, c, empty, b));
301 Assert.assertFalse(CellUtil.overlappingKeys(b, c, empty, a));
302 Assert.assertFalse(CellUtil.overlappingKeys(a,b, b, c));
303 Assert.assertFalse(CellUtil.overlappingKeys(empty, b, b, c));
304 Assert.assertFalse(CellUtil.overlappingKeys(empty, a, b, c));
305 }
306
307 @Test
308 public void testFindCommonPrefixInFlatKey() {
309
310 KeyValue kv1 = new KeyValue("r1".getBytes(), "f1".getBytes(), "q1".getBytes(), null);
311 Assert.assertEquals(kv1.getKeyLength(),
312 CellUtil.findCommonPrefixInFlatKey(kv1, kv1, true, true));
313 Assert.assertEquals(kv1.getKeyLength(),
314 CellUtil.findCommonPrefixInFlatKey(kv1, kv1, false, true));
315 Assert.assertEquals(kv1.getKeyLength() - KeyValue.TIMESTAMP_TYPE_SIZE,
316 CellUtil.findCommonPrefixInFlatKey(kv1, kv1, true, false));
317
318 KeyValue kv2 = new KeyValue("r12".getBytes(), "f1".getBytes(), "q1".getBytes(), null);
319 Assert.assertEquals(1, CellUtil.findCommonPrefixInFlatKey(kv1, kv2, true, true));
320
321 KeyValue kv3 = new KeyValue("r14".getBytes(), "f1".getBytes(), "q1".getBytes(), null);
322 Assert.assertEquals(KeyValue.ROW_LENGTH_SIZE + "r1".getBytes().length,
323 CellUtil.findCommonPrefixInFlatKey(kv2, kv3, true, true));
324
325 KeyValue kv4 = new KeyValue("r14".getBytes(), "f2".getBytes(), "q1".getBytes(), null);
326 Assert.assertEquals(KeyValue.ROW_LENGTH_SIZE + kv3.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE
327 + "f".getBytes().length, CellUtil.findCommonPrefixInFlatKey(kv3, kv4, false, true));
328
329 KeyValue kv5 = new KeyValue("r14".getBytes(), "f2".getBytes(), "q123".getBytes(), null);
330 Assert.assertEquals(KeyValue.ROW_LENGTH_SIZE + kv3.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE
331 + kv4.getFamilyLength() + kv4.getQualifierLength(),
332 CellUtil.findCommonPrefixInFlatKey(kv4, kv5, true, true));
333
334 KeyValue kv6 = new KeyValue("rk".getBytes(), 1234L);
335 KeyValue kv7 = new KeyValue("rk".getBytes(), 1235L);
336
337 Assert.assertEquals(KeyValue.ROW_LENGTH_SIZE + kv6.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE
338 + kv6.getFamilyLength() + kv6.getQualifierLength() + 7,
339 CellUtil.findCommonPrefixInFlatKey(kv6, kv7, true, true));
340
341 KeyValue kv8 = new KeyValue("rk".getBytes(), 1234L, Type.Delete);
342 Assert.assertEquals(KeyValue.ROW_LENGTH_SIZE + kv6.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE
343 + kv6.getFamilyLength() + kv6.getQualifierLength() + KeyValue.TIMESTAMP_SIZE,
344 CellUtil.findCommonPrefixInFlatKey(kv6, kv8, true, true));
345
346 Assert.assertEquals(KeyValue.ROW_LENGTH_SIZE + kv6.getRowLength() + KeyValue.FAMILY_LENGTH_SIZE
347 + kv6.getFamilyLength() + kv6.getQualifierLength(),
348 CellUtil.findCommonPrefixInFlatKey(kv6, kv8, true, false));
349 }
350
351
352
353
354 @Test
355 public void testToString() {
356 byte [] row = Bytes.toBytes("row");
357 long ts = 123l;
358
359 KeyValue kv = new KeyValue(row, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
360 ts, KeyValue.Type.Minimum, HConstants.EMPTY_BYTE_ARRAY);
361 Cell cell = CellUtil.createCell(row, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
362 ts, KeyValue.Type.Minimum.getCode(), HConstants.EMPTY_BYTE_ARRAY);
363 String cellToString = CellUtil.getCellKeyAsString(cell);
364 assertEquals(kv.toString(), cellToString);
365
366 byte [] f = new byte [] {'f'};
367 byte [] q = new byte [] {'q'};
368 kv = new KeyValue(row, f, q, ts, KeyValue.Type.Minimum, HConstants.EMPTY_BYTE_ARRAY);
369 cell = CellUtil.createCell(row, f, q, ts, KeyValue.Type.Minimum.getCode(),
370 HConstants.EMPTY_BYTE_ARRAY);
371 cellToString = CellUtil.getCellKeyAsString(cell);
372 assertEquals(kv.toString(), cellToString);
373
374 }
375
376 @Test
377 public void testToString1() {
378 String row = "test.row";
379 String family = "test.family";
380 String qualifier = "test.qualifier";
381 long timestamp = 42;
382 Type type = Type.Put;
383 String value = "test.value";
384 long seqId = 1042;
385
386 Cell cell = CellUtil.createCell(Bytes.toBytes(row), Bytes.toBytes(family),
387 Bytes.toBytes(qualifier), timestamp, type.getCode(), Bytes.toBytes(value), seqId);
388
389 String nonVerbose = CellUtil.toString(cell, false);
390 String verbose = CellUtil.toString(cell, true);
391
392 System.out.println("nonVerbose=" + nonVerbose);
393 System.out.println("verbose=" + verbose);
394
395 Assert.assertEquals(
396 String.format("%s/%s:%s/%d/%s/vlen=%s/seqid=%s",
397 row, family, qualifier, timestamp, type.toString(),
398 Bytes.toBytes(value).length, seqId),
399 nonVerbose);
400
401 Assert.assertEquals(
402 String.format("%s/%s:%s/%d/%s/vlen=%s/seqid=%s/%s",
403 row, family, qualifier, timestamp, type.toString(), Bytes.toBytes(value).length,
404 seqId, value),
405 verbose);
406
407
408 }
409
410 @Test
411 public void testCloneCellFieldsFromByteBufferedCell() {
412 byte[] r = Bytes.toBytes("row1");
413 byte[] f = Bytes.toBytes("cf1");
414 byte[] q = Bytes.toBytes("qual1");
415 byte[] v = Bytes.toBytes("val1");
416 byte[] tags = Bytes.toBytes("tag1");
417 KeyValue kv = new KeyValue(r, f, q, 0, q.length, 1234L, Type.Put, v, 0, v.length, tags);
418 ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer());
419 Cell bbCell = new ByteBufferedCellImpl(buffer, 0, buffer.remaining());
420 byte[] rDest = CellUtil.cloneRow(bbCell);
421 assertTrue(Bytes.equals(r, rDest));
422 byte[] fDest = CellUtil.cloneFamily(bbCell);
423 assertTrue(Bytes.equals(f, fDest));
424 byte[] qDest = CellUtil.cloneQualifier(bbCell);
425 assertTrue(Bytes.equals(q, qDest));
426 byte[] vDest = CellUtil.cloneValue(bbCell);
427 assertTrue(Bytes.equals(v, vDest));
428 byte[] tDest = new byte[tags.length];
429 CellUtil.copyTagTo(bbCell, tDest, 0);
430 assertTrue(Bytes.equals(tags, tDest));
431 }
432
433 @Test
434 public void testMatchingCellFieldsFromByteBufferedCell() {
435 byte[] r = Bytes.toBytes("row1");
436 byte[] f = Bytes.toBytes("cf1");
437 byte[] q1 = Bytes.toBytes("qual1");
438 byte[] q2 = Bytes.toBytes("qual2");
439 byte[] v = Bytes.toBytes("val1");
440 byte[] tags = Bytes.toBytes("tag1");
441 KeyValue kv = new KeyValue(r, f, q1, 0, q1.length, 1234L, Type.Put, v, 0, v.length, tags);
442 ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer());
443 Cell bbCell1 = new ByteBufferedCellImpl(buffer, 0, buffer.remaining());
444 kv = new KeyValue(r, f, q2, 0, q2.length, 1234L, Type.Put, v, 0, v.length, tags);
445 buffer = ByteBuffer.wrap(kv.getBuffer());
446 Cell bbCell2 = new ByteBufferedCellImpl(buffer, 0, buffer.remaining());
447 assertTrue(CellUtil.matchingRows(bbCell1, bbCell2));
448 assertTrue(CellUtil.matchingRows(kv, bbCell2));
449 assertTrue(CellUtil.matchingRow(bbCell1, r));
450 assertTrue(CellUtil.matchingFamily(bbCell1, bbCell2));
451 assertTrue(CellUtil.matchingFamily(kv, bbCell2));
452 assertTrue(CellUtil.matchingFamily(bbCell1, f));
453 assertFalse(CellUtil.matchingQualifier(bbCell1, bbCell2));
454 assertTrue(CellUtil.matchingQualifier(kv, bbCell2));
455 assertTrue(CellUtil.matchingQualifier(bbCell1, q1));
456 assertTrue(CellUtil.matchingQualifier(bbCell2, q2));
457 assertTrue(CellUtil.matchingValue(bbCell1, bbCell2));
458 assertTrue(CellUtil.matchingValue(kv, bbCell2));
459 assertTrue(CellUtil.matchingValue(bbCell1, v));
460 assertFalse(CellUtil.matchingColumn(bbCell1, bbCell2));
461 assertTrue(CellUtil.matchingColumn(kv, bbCell2));
462 assertTrue(CellUtil.matchingColumn(bbCell1, f, q1));
463 assertTrue(CellUtil.matchingColumn(bbCell2, f, q2));
464 }
465
466 @Test
467 public void testCellFieldsAsPrimitiveTypesFromByteBufferedCell() {
468 int ri = 123;
469 byte[] r = Bytes.toBytes(ri);
470 byte[] f = Bytes.toBytes("cf1");
471 byte[] q = Bytes.toBytes("qual1");
472 long vl = 10981L;
473 byte[] v = Bytes.toBytes(vl);
474 KeyValue kv = new KeyValue(r, f, q, v);
475 ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer());
476 Cell bbCell = new ByteBufferedCellImpl(buffer, 0, buffer.remaining());
477 assertEquals(ri, CellUtil.getRowAsInt(bbCell));
478 assertEquals(vl, CellUtil.getValueAsLong(bbCell));
479 double vd = 3005.5;
480 v = Bytes.toBytes(vd);
481 kv = new KeyValue(r, f, q, v);
482 buffer = ByteBuffer.wrap(kv.getBuffer());
483 bbCell = new ByteBufferedCellImpl(buffer, 0, buffer.remaining());
484 assertEquals(vd, CellUtil.getValueAsDouble(bbCell), 0.0);
485 BigDecimal bd = new BigDecimal(9999);
486 v = Bytes.toBytes(bd);
487 kv = new KeyValue(r, f, q, v);
488 buffer = ByteBuffer.wrap(kv.getBuffer());
489 bbCell = new ByteBufferedCellImpl(buffer, 0, buffer.remaining());
490 assertEquals(bd, CellUtil.getValueAsBigDecimal(bbCell));
491 }
492
493
494 public static class ByteBufferedCellImpl extends ByteBufferedCell {
495
496 private final ByteBuffer buffer;
497 private final int offset, length;
498
499 public ByteBufferedCellImpl(ByteBuffer buffer, int offset, int length) {
500 this.buffer = buffer;
501 this.offset = offset;
502 this.length = length;
503 }
504
505 @Override
506 public byte[] getRowArray() {
507 return CellUtil.cloneRow(this);
508 }
509
510 @Override
511 public int getRowOffset() {
512 return 0;
513 }
514
515 @Override
516 public short getRowLength() {
517 return ByteBufferUtils.toShort(this.buffer, this.offset + KeyValue.ROW_OFFSET);
518 }
519
520 @Override
521 public byte[] getFamilyArray() {
522 return CellUtil.cloneFamily(this);
523 }
524
525 @Override
526 public int getFamilyOffset() {
527 return 0;
528 }
529
530 @Override
531 public byte getFamilyLength() {
532 return this.buffer.get(getFamilyPosition() - 1);
533 }
534
535 @Override
536 public byte[] getQualifierArray() {
537 return CellUtil.cloneQualifier(this);
538 }
539
540 @Override
541 public int getQualifierOffset() {
542 return 0;
543 }
544
545 @Override
546 public int getQualifierLength() {
547 return getKeyLength()
548 - (int) KeyValue.getKeyDataStructureSize(getRowLength(), getFamilyLength(), 0);
549 }
550
551 private int getKeyLength() {
552 return ByteBufferUtils.toInt(this.buffer, this.offset);
553 }
554
555 @Override
556 public long getTimestamp() {
557 int tsOffset = this.offset + KeyValue.ROW_OFFSET + getKeyLength()
558 - KeyValue.TIMESTAMP_TYPE_SIZE;
559 return ByteBufferUtils.toLong(buffer, tsOffset);
560 }
561
562 @Override
563 public byte getTypeByte() {
564 return KeyValue.Type.Put.getCode();
565 }
566
567 @Override
568 public long getSequenceId() {
569 return 0;
570 }
571
572 @Override
573 public byte[] getValueArray() {
574 return CellUtil.cloneValue(this);
575 }
576
577 @Override
578 public int getValueOffset() {
579 return 0;
580 }
581
582 @Override
583 public int getValueLength() {
584 return ByteBufferUtils.toInt(this.buffer, this.offset + KeyValue.KEY_LENGTH_SIZE);
585 }
586
587 @Override
588 public byte[] getTagsArray() {
589 byte[] tDest = new byte[getTagsLength()];
590 CellUtil.copyTagTo(this, tDest, 0);
591 return tDest;
592 }
593
594 @Override
595 public int getTagsOffset() {
596 return 0;
597 }
598
599 @Override
600 public int getTagsLength() {
601 int tagsLen = this.length
602 - (getKeyLength() + getValueLength() + KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE);
603 if (tagsLen > 0) {
604 tagsLen -= KeyValue.TAGS_LENGTH_SIZE;
605 }
606 return tagsLen;
607 }
608
609 @Override
610 public ByteBuffer getRowByteBuffer() {
611 return this.buffer;
612 }
613
614 @Override
615 public int getRowPosition() {
616 return this.offset + KeyValue.ROW_KEY_OFFSET;
617 }
618
619 @Override
620 public ByteBuffer getFamilyByteBuffer() {
621 return this.buffer;
622 }
623
624 @Override
625 public int getFamilyPosition() {
626 return this.offset + KeyValue.ROW_KEY_OFFSET + getRowLength() + Bytes.SIZEOF_BYTE;
627 }
628
629 @Override
630 public ByteBuffer getQualifierByteBuffer() {
631 return this.buffer;
632 }
633
634 @Override
635 public int getQualifierPosition() {
636 return getFamilyPosition() + getFamilyLength();
637 }
638
639 @Override
640 public ByteBuffer getValueByteBuffer() {
641 return this.buffer;
642 }
643
644 @Override
645 public int getValuePosition() {
646 return this.offset + KeyValue.ROW_OFFSET + getKeyLength();
647 }
648
649 @Override
650 public ByteBuffer getTagsByteBuffer() {
651 return this.buffer;
652 }
653
654 @Override
655 public int getTagsPosition() {
656 int tagsLen = getTagsLength();
657 if (tagsLen == 0) {
658 return this.offset + this.length;
659 }
660 return this.offset + this.length - tagsLen;
661 }
662 }
663 }