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 java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.NavigableMap;
25 import java.util.TreeMap;
26
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31
32 @Category(SmallTests.class)
33 public class TestCellUtil {
34
35
36
37 private class TestCellScannable implements CellScannable {
38 private final int cellsCount;
39 TestCellScannable(final int cellsCount) {
40 this.cellsCount = cellsCount;
41 }
42 @Override
43 public CellScanner cellScanner() {
44 return new TestCellScanner(this.cellsCount);
45 }
46 };
47
48
49
50
51 private class TestCellScanner implements CellScanner {
52 private int count = 0;
53 private Cell current = null;
54 private final int cellsCount;
55
56 TestCellScanner(final int cellsCount) {
57 this.cellsCount = cellsCount;
58 }
59
60 @Override
61 public Cell current() {
62 return this.current;
63 }
64
65 @Override
66 public boolean advance() throws IOException {
67 if (this.count < cellsCount) {
68 this.current = new TestCell(this.count);
69 this.count++;
70 return true;
71 }
72 return false;
73 }
74 }
75
76
77
78
79 private class TestCell implements Cell {
80 private final byte [] row;
81
82 TestCell(final int i) {
83 this.row = Bytes.toBytes(i);
84 }
85
86 @Override
87 public byte[] getRowArray() {
88 return this.row;
89 }
90
91 @Override
92 public int getRowOffset() {
93 return 0;
94 }
95
96 @Override
97 public short getRowLength() {
98 return (short)this.row.length;
99 }
100
101 @Override
102 public byte[] getFamilyArray() {
103
104 return null;
105 }
106
107 @Override
108 public int getFamilyOffset() {
109
110 return 0;
111 }
112
113 @Override
114 public byte getFamilyLength() {
115
116 return 0;
117 }
118
119 @Override
120 public byte[] getQualifierArray() {
121
122 return null;
123 }
124
125 @Override
126 public int getQualifierOffset() {
127
128 return 0;
129 }
130
131 @Override
132 public int getQualifierLength() {
133
134 return 0;
135 }
136
137 @Override
138 public long getTimestamp() {
139
140 return 0;
141 }
142
143 @Override
144 public byte getTypeByte() {
145
146 return 0;
147 }
148
149 @Override
150 public long getMvccVersion() {
151
152 return 0;
153 }
154
155 @Override
156 public byte[] getValueArray() {
157
158 return null;
159 }
160
161 @Override
162 public int getValueOffset() {
163
164 return 0;
165 }
166
167 @Override
168 public int getValueLength() {
169
170 return 0;
171 }
172
173 @Override
174 public byte[] getTagsArray() {
175
176 return null;
177 }
178
179 @Override
180 public int getTagsOffset() {
181
182 return 0;
183 }
184
185 @Override
186 public short getTagsLength() {
187
188 return 0;
189 }
190
191 @Override
192 public int getTagsLengthUnsigned() {
193
194 return 0;
195 }
196
197 @Override
198 public byte[] getValue() {
199
200 return null;
201 }
202
203 @Override
204 public byte[] getFamily() {
205
206 return null;
207 }
208
209 @Override
210 public byte[] getQualifier() {
211
212 return null;
213 }
214
215 @Override
216 public byte[] getRow() {
217
218 return null;
219 }
220 };
221
222
223
224
225
226 @Test
227 public void testCreateCellScannerOverflow() throws IOException {
228 consume(doCreateCellScanner(1, 1), 1 * 1);
229 consume(doCreateCellScanner(3, 0), 3 * 0);
230 consume(doCreateCellScanner(3, 3), 3 * 3);
231 consume(doCreateCellScanner(0, 1), 0 * 1);
232
233 final int hundredK = 100000;
234 consume(doCreateCellScanner(hundredK, 0), hundredK * 0);
235 consume(doCreateCellArray(1), 1);
236 consume(doCreateCellArray(0), 0);
237 consume(doCreateCellArray(3), 3);
238 List<CellScannable> cells = new ArrayList<CellScannable>(hundredK);
239 for (int i = 0; i < hundredK; i++) {
240 cells.add(new TestCellScannable(1));
241 }
242 consume(CellUtil.createCellScanner(cells), hundredK * 1);
243 NavigableMap<byte [], List<Cell>> m = new TreeMap<byte [], List<Cell>>(Bytes.BYTES_COMPARATOR);
244 List<Cell> cellArray = new ArrayList<Cell>(hundredK);
245 for (int i = 0; i < hundredK; i++) cellArray.add(new TestCell(i));
246 m.put(new byte [] {'f'}, cellArray);
247 consume(CellUtil.createCellScanner(m), hundredK * 1);
248 }
249
250 private CellScanner doCreateCellArray(final int itemsPerList) {
251 Cell [] cells = new Cell [itemsPerList];
252 for (int i = 0; i < itemsPerList; i++) {
253 cells[i] = new TestCell(i);
254 }
255 return CellUtil.createCellScanner(cells);
256 }
257
258 private CellScanner doCreateCellScanner(final int listsCount, final int itemsPerList)
259 throws IOException {
260 List<CellScannable> cells = new ArrayList<CellScannable>(listsCount);
261 for (int i = 0; i < listsCount; i++) {
262 CellScannable cs = new CellScannable() {
263 @Override
264 public CellScanner cellScanner() {
265 return new TestCellScanner(itemsPerList);
266 }
267 };
268 cells.add(cs);
269 }
270 return CellUtil.createCellScanner(cells);
271 }
272
273 private void consume(final CellScanner scanner, final int expected) throws IOException {
274 int count = 0;
275 while (scanner.advance()) count++;
276 Assert.assertEquals(expected, count);
277 }
278
279 @Test
280 public void testOverlappingKeys() {
281 byte[] empty = HConstants.EMPTY_BYTE_ARRAY;
282 byte[] a = Bytes.toBytes("a");
283 byte[] b = Bytes.toBytes("b");
284 byte[] c = Bytes.toBytes("c");
285 byte[] d = Bytes.toBytes("d");
286
287
288 Assert.assertTrue(CellUtil.overlappingKeys(a, b, a, b));
289 Assert.assertTrue(CellUtil.overlappingKeys(a, c, a, b));
290 Assert.assertTrue(CellUtil.overlappingKeys(a, b, a, c));
291 Assert.assertTrue(CellUtil.overlappingKeys(b, c, a, c));
292 Assert.assertTrue(CellUtil.overlappingKeys(a, c, b, c));
293 Assert.assertTrue(CellUtil.overlappingKeys(a, d, b, c));
294 Assert.assertTrue(CellUtil.overlappingKeys(b, c, a, d));
295
296 Assert.assertTrue(CellUtil.overlappingKeys(empty, b, a, b));
297 Assert.assertTrue(CellUtil.overlappingKeys(empty, b, a, c));
298
299 Assert.assertTrue(CellUtil.overlappingKeys(a, b, empty, b));
300 Assert.assertTrue(CellUtil.overlappingKeys(a, b, empty, c));
301
302 Assert.assertTrue(CellUtil.overlappingKeys(a, empty, a, b));
303 Assert.assertTrue(CellUtil.overlappingKeys(a, empty, a, c));
304
305 Assert.assertTrue(CellUtil.overlappingKeys(a, b, empty, empty));
306 Assert.assertTrue(CellUtil.overlappingKeys(empty, empty, a, b));
307
308
309 Assert.assertFalse(CellUtil.overlappingKeys(a, b, c, d));
310 Assert.assertFalse(CellUtil.overlappingKeys(c, d, a, b));
311
312 Assert.assertFalse(CellUtil.overlappingKeys(b, c, c, d));
313 Assert.assertFalse(CellUtil.overlappingKeys(b, c, c, empty));
314 Assert.assertFalse(CellUtil.overlappingKeys(b, c, d, empty));
315 Assert.assertFalse(CellUtil.overlappingKeys(c, d, b, c));
316 Assert.assertFalse(CellUtil.overlappingKeys(c, empty, b, c));
317 Assert.assertFalse(CellUtil.overlappingKeys(d, empty, b, c));
318
319 Assert.assertFalse(CellUtil.overlappingKeys(b, c, a, b));
320 Assert.assertFalse(CellUtil.overlappingKeys(b, c, empty, b));
321 Assert.assertFalse(CellUtil.overlappingKeys(b, c, empty, a));
322 Assert.assertFalse(CellUtil.overlappingKeys(a,b, b, c));
323 Assert.assertFalse(CellUtil.overlappingKeys(empty, b, b, c));
324 Assert.assertFalse(CellUtil.overlappingKeys(empty, a, b, c));
325 }
326 }