@Test public void testRetainAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); EncodedColumnQualiferCellsList list2 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list2); // retainAll won't be modifying the list1 since they both have the same elements equality wise assertFalse(list1.retainAll(list2)); list2.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(12))); assertTrue(list1.retainAll(list2)); assertEquals(list1.size(), list2.size()); for (Cell c : list1) { assertTrue(list2.contains(c)); } }
@Test public void testSetNull() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); try { list.add(null); fail("Adding null elements to the list is not allowed"); } catch (NullPointerException expected) { } }
@Override public Cell getValue(int index) { return index == 0 ? cells.getFirstCell() : cells.get(index); }
@Override public Cell get(int index) { rangeCheck(index); int numNonNullElementsFound = 0; for (int i = firstNonNullElementIdx; i < array.length; i++) { if (array[i] != null) { numNonNullElementsFound++; if (numNonNullElementsFound == index + 1) { return array[i]; } } } throw new IllegalStateException("There was no element present in the list at index " + index + " even though number of elements in the list are " + size()); }
ListIterator<Cell> listItr = this.listIterator(); while (listItr.hasNext()) { Cell cellInThis = listItr.next(); cellInThis.getQualifierOffset(), cellInThis.getQualifierLength()); try { Cell cellInParam = list.getCellForColumnQualifier(qualifier); if (cellInParam != null && cellInParam.equals(cellInThis)) { continue;
@Test public void testFailFastListIterator() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); ListIterator<Cell> itr = list.listIterator(); itr.next(); list.add(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(0))); try { itr.next(); fail("ConcurrentModificationException should have been thrown as the list was modified without using iterator"); } catch (ConcurrentModificationException expected) { } list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); itr = list.listIterator(); itr.next(); itr.next(); itr.remove(); itr.next(); list.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(0))); try { itr.next(); fail("ConcurrentModificationException should have been thrown as the list was modified without using iterator"); } catch (ConcurrentModificationException expected) { } }
@Test public void testListIterator() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; int i = 0; populateListAndArray(list, cells); ListIterator<Cell> itr = list.listIterator(); assertTrue(itr.hasNext()); assertEquals(7, list.size()); itr = list.listIterator(); i = 0; int numRemoved = 0; assertTrue(list.isEmpty());
@Test public void testClear() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); list.clear(); assertTrue(list.isEmpty()); assertEquals(0, list.size()); }
@Test public void testContainsAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); EncodedColumnQualiferCellsList list2 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list2); assertTrue(list1.containsAll(list2)); list2.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(11))); assertTrue(list1.containsAll(list2)); assertFalse(list2.containsAll(list1)); list2.add(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(13))); assertFalse(list1.containsAll(list2)); assertFalse(list2.containsAll(list1)); List<Cell> arrayList = new ArrayList<>(); populateList(arrayList); assertTrue(list1.containsAll(arrayList)); }
@Test public void testSize() { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); assertEquals(0, list.size()); populateList(list); assertEquals(7, list.size()); int originalSize = list.size(); Iterator itr = list.iterator(); while (itr.hasNext()) { itr.next(); itr.remove(); assertEquals(--originalSize, list.size()); } }
@Test public void testIsEmpty() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); assertTrue(list.isEmpty()); populateList(list); assertFalse(list.isEmpty()); Iterator itr = list.iterator(); while (itr.hasNext()) { itr.next(); itr.remove(); if (itr.hasNext()) { assertFalse(list.isEmpty()); } } assertTrue(list.isEmpty()); }
@Test public void testRemove() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); assertTrue(list.remove(cells[0])); assertEquals(6, list.size()); assertTrue(list.remove(cells[6])); assertEquals(5, list.size()); assertTrue(list.remove(cells[3])); assertEquals(4, list.size()); assertFalse(list.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(13)))); assertEquals(4, list.size()); }
@Override public Tuple next() throws SQLException { // XXX: No access here to the region instance to enclose this with startRegionOperation / // stopRegionOperation synchronized (scanner) { try { // TODO: size List<Cell> results = useQualifierAsIndex ? new EncodedColumnQualiferCellsList(minMaxQualifiers.getFirst(), minMaxQualifiers.getSecond(), encodingScheme) : new ArrayList<Cell>(); // Results are potentially returned even when the return value of s.next is false // since this is an indication of whether or not there are more values after the // ones returned boolean hasMore = scanner.nextRaw(results); if (!hasMore && results.isEmpty()) { return null; } // We instantiate a new tuple because in all cases currently we hang on to it // (i.e. to compute and hold onto the TopN). Tuple tuple = useQualifierAsIndex ? new PositionBasedMultiKeyValueTuple() : new MultiKeyValueTuple(); tuple.setKeyValues(results); return tuple; } catch (IOException e) { throw ServerUtil.parseServerException(e); } } }
@Test public void testFailFastIterator() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); int i = 0; Iterator<Cell> itr = list.iterator(); while (itr.hasNext()) { i++; try { itr.next(); list.add(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(0))); if (i == 2) { fail("ConcurrentModificationException should have been thrown as the list is being modified while being iterated through"); } } catch (ConcurrentModificationException expected) { assertEquals("Exception should have been thrown when getting the second element", 2, i); break; } } }
@Test public void testRemoveAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); ArrayList<Cell> list2 = new ArrayList<>(); populateList(list2); assertTrue(list1.removeAll(list2)); assertTrue(list1.isEmpty()); assertFalse(list2.isEmpty()); }
@Override public int size() { return cells.size(); }
@Override public int lastIndexOf(Object o) { if (o == null || isEmpty()) { return -1; } int lastIndex = numNonNullElements; for (int i = array.length - 1; i >= 0; i--) { if (array[i] != null) { lastIndex--; } if (o.equals(array[i])) { return lastIndex; } } return -1; }
@Test public void testListIteratorSet() { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] array = new Cell[7]; populateListAndArray(list, array); ListIterator<Cell> itr = list.listIterator(); itr = list.listIterator(); i = 0;
@Test public void testGetIndex() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); for (int i = 0; i < cells.length; i++) { assertEquals(cells[i], list.get(i)); } }
@Test public void testIndexOf() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); for (int i = 0; i < cells.length; i++) { assertEquals(i, list.indexOf(cells[i])); } }