View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * CellScannable used in test. Returns a {@link TestCellScanner}
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     * CellScanner used in test.
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     * Cell used in test. Has row only.
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       // TODO Auto-generated method stub
104       return null;
105     }
106 
107     @Override
108     public int getFamilyOffset() {
109       // TODO Auto-generated method stub
110       return 0;
111     }
112 
113     @Override
114     public byte getFamilyLength() {
115       // TODO Auto-generated method stub
116       return 0;
117     }
118 
119     @Override
120     public byte[] getQualifierArray() {
121       // TODO Auto-generated method stub
122       return null;
123     }
124 
125     @Override
126     public int getQualifierOffset() {
127       // TODO Auto-generated method stub
128       return 0;
129     }
130 
131     @Override
132     public int getQualifierLength() {
133       // TODO Auto-generated method stub
134       return 0;
135     }
136 
137     @Override
138     public long getTimestamp() {
139       // TODO Auto-generated method stub
140       return 0;
141     }
142 
143     @Override
144     public byte getTypeByte() {
145       // TODO Auto-generated method stub
146       return 0;
147     }
148 
149     @Override
150     public long getMvccVersion() {
151       // TODO Auto-generated method stub
152       return 0;
153     }
154 
155     @Override
156     public byte[] getValueArray() {
157       // TODO Auto-generated method stub
158       return null;
159     }
160 
161     @Override
162     public int getValueOffset() {
163       // TODO Auto-generated method stub
164       return 0;
165     }
166 
167     @Override
168     public int getValueLength() {
169       // TODO Auto-generated method stub
170       return 0;
171     }
172 
173     @Override
174     public byte[] getTagsArray() {
175       // TODO Auto-generated method stub
176       return null;
177     }
178 
179     @Override
180     public int getTagsOffset() {
181       // TODO Auto-generated method stub
182       return 0;
183     }
184 
185     @Override
186     public short getTagsLength() {
187       // TODO Auto-generated method stub
188       return 0;
189     }
190 
191     @Override
192     public int getTagsLengthUnsigned() {
193       // TODO Auto-generated method stub
194       return 0;
195     }
196 
197     @Override
198     public byte[] getValue() {
199       // TODO Auto-generated method stub
200       return null;
201     }
202 
203     @Override
204     public byte[] getFamily() {
205       // TODO Auto-generated method stub
206       return null;
207     }
208 
209     @Override
210     public byte[] getQualifier() {
211       // TODO Auto-generated method stub
212       return null;
213     }
214 
215     @Override
216     public byte[] getRow() {
217       // TODO Auto-generated method stub
218       return null;
219     }
220   };
221 
222   /**
223    * Was overflowing if 100k or so lists of cellscanners to return.
224    * @throws IOException
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     // Do big number. See HBASE-11813 for why.
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     // overlaps
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     // non overlaps
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 }