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