1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.apache.hadoop.hbase.client.TestBatchScanResultCache.createCells;
21 import static org.junit.Assert.assertEquals;
22
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.LinkedList;
26
27 import org.apache.hadoop.hbase.Cell;
28 import org.apache.hadoop.hbase.testclassification.ClientTests;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36 @Category({ SmallTests.class, ClientTests.class })
37 public class TestAllowPartialScanResultCache {
38
39 private static byte[] CF = Bytes.toBytes("cf");
40
41 private AllowPartialScanResultCache resultCache;
42 private static final LinkedList<Result> cache = new LinkedList<Result>();
43
44 @Before
45 public void setUp() {
46 resultCache = new AllowPartialScanResultCache(cache);
47 }
48
49 @After
50 public void tearDown() {
51 resultCache.clear();
52 resultCache = null;
53 }
54
55 @Test
56 public void test() throws IOException {
57 resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, false);
58 assertEquals(0, cache.size());
59 resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, true);
60 assertEquals(0, cache.size());
61
62 Cell[] cells1 = createCells(CF, 1, 10);
63 Cell[] cells2 = createCells(CF, 2, 10);
64
65 resultCache.loadResultsToCache(
66 new Result[] { Result.create(Arrays.copyOf(cells1, 5), null, false, true) }, false);
67 Result[] results1 = cache.toArray(new Result[0]);
68 assertEquals(1, results1.length);
69 assertEquals(1, Bytes.toInt(results1[0].getRow()));
70 assertEquals(5, results1[0].rawCells().length);
71 for (int i = 0; i < 5; i++) {
72 assertEquals(1, Bytes.toInt(results1[0].getValue(CF, Bytes.toBytes("cq" + i))));
73 }
74
75 cache.clear();
76 resultCache.loadResultsToCache(
77 new Result[] { Result.create(Arrays.copyOfRange(cells1, 1, 10), null, false, true) }, false);
78 Result[] results2 = cache.toArray(new Result[0]);
79 assertEquals(1, results2.length);
80 assertEquals(1, Bytes.toInt(results2[0].getRow()));
81 assertEquals(5, results2[0].rawCells().length);
82 for (int i = 5; i < 10; i++) {
83 assertEquals(1, Bytes.toInt(results2[0].getValue(CF, Bytes.toBytes("cq" + i))));
84 }
85
86 cache.clear();
87 resultCache.loadResultsToCache(
88 new Result[] { Result.create(cells1), Result.create(cells2) }, false);
89 Result[] results3 = cache.toArray(new Result[0]);
90 assertEquals(1, results3.length);
91 assertEquals(2, Bytes.toInt(results3[0].getRow()));
92 assertEquals(10, results3[0].rawCells().length);
93 for (int i = 0; i < 10; i++) {
94 assertEquals(2, Bytes.toInt(results3[0].getValue(CF, Bytes.toBytes("cq" + i))));
95 }
96 }
97 }