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.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertSame;
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.KeyValue;
29 import org.apache.hadoop.hbase.testclassification.ClientTests;
30 import org.apache.hadoop.hbase.testclassification.SmallTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36
37 @Category({ SmallTests.class, ClientTests.class })
38 public class TestCompleteResultScanResultCache {
39
40 private static byte[] CF = Bytes.toBytes("cf");
41
42 private static byte[] CQ1 = Bytes.toBytes("cq1");
43
44 private static byte[] CQ2 = Bytes.toBytes("cq2");
45
46 private static byte[] CQ3 = Bytes.toBytes("cq3");
47
48 private CompleteScanResultCache resultCache;
49
50 private final LinkedList<Result> cache = new LinkedList<Result>();
51
52 @Before
53 public void setUp() {
54 resultCache = new CompleteScanResultCache(cache);
55 }
56
57 @After
58 public void tearDown() {
59 resultCache.clear();
60 resultCache = null;
61 }
62
63 private static Cell createCell(int key, byte[] cq) {
64 return new KeyValue(Bytes.toBytes(key), CF, cq, Bytes.toBytes(key));
65 }
66
67 @Test
68 public void testNoPartial() throws IOException {
69 cache.clear();
70
71 resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, false);
72 assertEquals(0, cache.size());
73 resultCache.loadResultsToCache(ScanResultCache.EMPTY_RESULT_ARRAY, true);
74 assertEquals(0, cache.size());
75
76 int count = 10;
77 Result[] results = new Result[count];
78 for (int i = 0; i < count; i++) {
79 results[i] = Result.create(Arrays.asList(createCell(i, CQ1)));
80 }
81 resultCache.loadResultsToCache(results, false);
82 results = cache.toArray(new Result[0]);
83 assertEquals(count, results.length);
84 for (int i = 0; i < count; i++) {
85 assertEquals(i, Bytes.toInt(results[i].getRow()));
86 assertEquals(i, Bytes.toInt(results[i].getValue(CF, CQ1)));
87 }
88 }
89
90 @Test
91 public void testCombine1() throws IOException {
92 cache.clear();
93
94 Result previousResult = Result.create(Arrays.asList(createCell(0, CQ1)), null, false, true);
95 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
96 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
97 Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true);
98 resultCache.loadResultsToCache(new Result[] { previousResult, result1 }, false);
99 Result[] results = cache.toArray(new Result[0]);
100 assertEquals(1, results.length);
101 assertSame(previousResult, results[0]);
102
103 cache.clear();
104 resultCache.loadResultsToCache(new Result[] { result2 }, false);
105 assertEquals(0, cache.size());
106 resultCache.loadResultsToCache(new Result[] { result3 }, false);
107 assertEquals(0, cache.size());
108 resultCache.loadResultsToCache(new Result[0], true);
109 assertEquals(0, cache.size());
110
111 resultCache.loadResultsToCache(new Result[0], false);
112 results = cache.toArray(new Result[0]);
113 assertEquals(1, results.length);
114 assertEquals(1, Bytes.toInt(results[0].getRow()));
115 assertEquals(3, results[0].rawCells().length);
116 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
117 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
118 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3)));
119 }
120
121 @Test
122 public void testCombine2() throws IOException {
123 cache.clear();
124
125 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
126 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
127 Result result3 = Result.create(Arrays.asList(createCell(1, CQ3)), null, false, true);
128 Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true);
129 Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ2)), null, false, false);
130
131 resultCache.loadResultsToCache(new Result[] { result1 }, false);
132 assertEquals(0, cache.size());
133 resultCache.loadResultsToCache(new Result[] { result2 }, false);
134 assertEquals(0, cache.size());
135 resultCache.loadResultsToCache(new Result[] { result3 }, false);
136 assertEquals(0, cache.size());
137
138 resultCache.loadResultsToCache(new Result[] { nextResult1 }, false);
139 Result[] results = cache.toArray(new Result[0]);
140 assertEquals(1, results.length);
141 assertEquals(1, Bytes.toInt(results[0].getRow()));
142 assertEquals(3, results[0].rawCells().length);
143 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
144 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
145 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ3)));
146
147 cache.clear();
148 resultCache.loadResultsToCache(new Result[] { nextToNextResult1 }, false);
149 results = cache.toArray(new Result[0]);
150 assertEquals(2, results.length);
151 assertEquals(2, Bytes.toInt(results[0].getRow()));
152 assertEquals(1, results[0].rawCells().length);
153 assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1)));
154 assertEquals(3, Bytes.toInt(results[1].getRow()));
155 assertEquals(1, results[1].rawCells().length);
156 assertEquals(3, Bytes.toInt(results[1].getValue(CF, CQ2)));
157 }
158
159 @Test
160 public void testCombine3() throws IOException {
161 cache.clear();
162
163 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
164 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, true);
165 Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, false);
166 Result nextToNextResult1 = Result.create(Arrays.asList(createCell(3, CQ1)), null, false, true);
167
168 resultCache.loadResultsToCache(new Result[] { result1 }, false);
169 assertEquals(0, cache.size());
170 resultCache.loadResultsToCache(new Result[] { result2 }, false);
171 assertEquals(0, cache.size());
172
173 resultCache.loadResultsToCache(new Result[] { nextResult1, nextToNextResult1 }, false);
174 Result[] results = cache.toArray(new Result[0]);
175 assertEquals(2, results.length);
176 assertEquals(1, Bytes.toInt(results[0].getRow()));
177 assertEquals(2, results[0].rawCells().length);
178 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
179 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
180 assertEquals(2, Bytes.toInt(results[1].getRow()));
181 assertEquals(1, results[1].rawCells().length);
182 assertEquals(2, Bytes.toInt(results[1].getValue(CF, CQ1)));
183
184 cache.clear();
185 resultCache.loadResultsToCache(new Result[0], false);
186 results = cache.toArray(new Result[0]);
187 assertEquals(1, results.length);
188 assertEquals(3, Bytes.toInt(results[0].getRow()));
189 assertEquals(1, results[0].rawCells().length);
190 assertEquals(3, Bytes.toInt(results[0].getValue(CF, CQ1)));
191 }
192
193 @Test
194 public void testCombine4() throws IOException {
195 cache.clear();
196
197 Result result1 = Result.create(Arrays.asList(createCell(1, CQ1)), null, false, true);
198 Result result2 = Result.create(Arrays.asList(createCell(1, CQ2)), null, false, false);
199 Result nextResult1 = Result.create(Arrays.asList(createCell(2, CQ1)), null, false, true);
200 Result nextResult2 = Result.create(Arrays.asList(createCell(2, CQ2)), null, false, false);
201
202 resultCache.loadResultsToCache(new Result[] { result1 }, false);
203 assertEquals(0, cache.size());
204
205 resultCache.loadResultsToCache(new Result[] { result2, nextResult1 }, false);
206 Result[] results = cache.toArray(new Result[0]);
207 assertEquals(1, results.length);
208 assertEquals(1, Bytes.toInt(results[0].getRow()));
209 assertEquals(2, results[0].rawCells().length);
210 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ1)));
211 assertEquals(1, Bytes.toInt(results[0].getValue(CF, CQ2)));
212
213 cache.clear();
214 resultCache.loadResultsToCache(new Result[] { nextResult2 }, false);
215 results = cache.toArray(new Result[0]);
216 assertEquals(1, results.length);
217 assertEquals(2, Bytes.toInt(results[0].getRow()));
218 assertEquals(2, results[0].rawCells().length);
219 assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ1)));
220 assertEquals(2, Bytes.toInt(results[0].getValue(CF, CQ2)));
221 }
222 }