/** * For internal use. Just add the Object[] values for a struct with same type */ @Override public boolean addFieldValues(Object[] fieldValues) { return super.add(fieldValues); }
@Test public void testAddAndGetOccurrence() { ResultsBag bag = getBagObject(String.class); bag = getBagObject(String.class); ObjectType elementType = bag.getCollectionType().getElementType(); assertEquals(1, bag.addAndGetOccurence(elementType instanceof StructType ? ((Struct) wrap("one", elementType)).getFieldValues() : wrap("one", elementType))); bag.add(wrap("two", elementType)); assertEquals(2, bag.addAndGetOccurence( elementType instanceof StructType ? ((Struct) wrap("two", elementType)).getFieldValues() : wrap("two", bag.getCollectionType().getElementType()))); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); assertEquals(3, bag.addAndGetOccurence(elementType instanceof StructType ? ((Struct) wrap("three", elementType)).getFieldValues() : wrap("three", elementType))); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); assertEquals(3, bag.addAndGetOccurence(elementType instanceof StructType ? ((Struct) wrap(null, elementType)).getFieldValues() : wrap(null, elementType))); }
/** * @param stats the CachePerfStats to track hash collisions. Should be null unless this is used as * a query execution-time result set. */ ResultsBag(Collection c, CachePerfStats stats) { this(stats); for (Iterator itr = c.iterator(); itr.hasNext();) { this.add(itr.next()); } }
protected ResultsBag(Collection c, HashingStrategy strategy, CachePerfStats stats) { this(strategy, stats); for (Iterator itr = c.iterator(); itr.hasNext();) { this.add(itr.next()); } }
public boolean addAll(StructFields sb) { boolean modified = false; if (!this.elementType.equals(sb.getCollectionType().getElementType())) { throw new IllegalArgumentException( "types do not match"); } for (Iterator itr = sb.fieldValuesIterator(); itr.hasNext();) { // Check if query execution on this thread is canceled. QueryMonitor.throwExceptionIfQueryOnCurrentThreadIsCanceled(); Object[] vals = (Object[]) itr.next(); if (super.add(vals)) { modified = true; } } return modified; }
@Test public void testIterationNullRemoval() { ResultsBag bag = new ResultsBag(); bag.add(null); bag.add(null); bag.add(new Integer(1)); bag.add(new Integer(2)); bag.add(new Integer(2)); assertEquals(5, bag.size()); for (Iterator itr = bag.iterator(); itr.hasNext();) { Object n = itr.next(); if (n == null) { itr.remove(); } } assertEquals(3, bag.size()); assertEquals(0, bag.occurrences(null)); }
@Test public void testIteration() { ResultsBag bag = new ResultsBag(); bag.add(new Integer(1)); bag.add(new Integer(2)); bag.add(new Integer(2)); int numOnes = 0; int numTwos = 0; Integer one = new Integer(1); Integer two = new Integer(2); for (Iterator itr = bag.iterator(); itr.hasNext();) { Object n = itr.next(); if (one.equals(n)) { numOnes++; } else if (two.equals(n)) { numTwos++; } else { fail(n + " did not equal 1 or 2"); } } assertEquals(1, numOnes); assertEquals(2, numTwos); }
@Test public void testIterationRemoval() { ResultsBag bag = new ResultsBag(); bag.add(new Integer(1)); bag.add(new Integer(2)); bag.add(new Integer(2)); bag.add(new Integer(3)); bag.add(new Integer(3)); bag.add(new Integer(4)); assertEquals(6, bag.size()); Iterator itr = bag.iterator(); for (int i = 0; i < 3; i++) { itr.next(); itr.remove(); } assertEquals(3, bag.size()); for (int i = 0; i < 3; i++) { itr.next(); itr.remove(); } assertTrue(bag.isEmpty()); assertEquals(0, bag.size()); }
@Test public void testSerializingSetViewWithNulls() throws Exception { ResultsBag bag = new ResultsBag(); bag.add(new Integer(4)); bag.add(new Integer(2)); bag.add(new Integer(42)); bag.add(null); bag.add(null); bag.add(null); assertEquals(6, bag.size()); assertEquals(1, bag.occurrences(new Integer(4))); assertEquals(3, bag.occurrences(null)); Set set = bag.asSet(); assertEquals(4, set.size()); assertTrue(set.contains(new Integer(4))); assertTrue(set.contains(null)); ResultsCollectionWrapper w = new ResultsCollectionWrapper(new ObjectTypeImpl(Integer.class), set); HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(w, hdos); DataInputStream in = new DataInputStream(hdos.getInputStream()); SelectResults setCopy = (SelectResults) DataSerializer.readObject(in); assertEquals(4, setCopy.size()); assertTrue(setCopy.contains(new Integer(4))); assertTrue(setCopy.contains(null)); }
@Test public void testAddExceptionIfLimitApplied() { ResultsBag bag = getBagObject(String.class); bag.add(wrap("one", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.applyLimit(6); try { bag.add(wrap("four", bag.getCollectionType().getElementType())); fail("Addition to bag with limit applied should have failed"); } catch (UnsupportedOperationException soe) { // Expected exception } }
ResultsBag bag = getBagObject(Integer.class); bag.add(wrap(new Integer(1), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(4), bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.applyLimit(4); List asList = bag.asList();
@Test public void testAsListAndAsSetMethod() { ResultsBag bag = getBagObject(String.class); bag.add(wrap("one", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); assertEquals(4, bag.size()); bag.applyLimit(2); List list = bag.asList(); assertEquals(2, list.size()); Set set = bag.asSet(); assertEquals(2, set.size()); }
@Test public void testContains() { ResultsBag bag = getBagObject(Integer.class); bag.add(wrap(new Integer(1), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(4), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(5), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(6), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(7), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(8), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(9), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(10), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(11), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(12), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(13), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(14), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(15), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(16), bag.getCollectionType().getElementType())); bag.applyLimit(6); List temp = bag.asList();
ResultsBag bag = getBagObject(Integer.class); bag.add(wrap(new Integer(1), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(4), bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.applyLimit(4); List asList = bag.asList();
ResultsBag bag = getBagObject(Integer.class); bag.add(wrap(new Integer(1), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(2), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(3), bag.getCollectionType().getElementType())); bag.add(wrap(new Integer(4), bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.applyLimit(4); List asList = bag.asList();
@Test public void testIteratorType() { ResultsBag bag = getBagObject(String.class); bag.add(wrap("one", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); assertTrue(bag.iterator() instanceof Bag.BagIterator); bag.applyLimit(6); assertTrue(bag.iterator() instanceof Bag.BagIterator); bag = getBagObject(String.class); bag.add(wrap("one", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.applyLimit(6); if (!(bag instanceof StructBag)) { assertTrue(bag.iterator() instanceof Bag.LimitBagIterator); } }
@Test public void testSizeWithLimitApplied() { ResultsBag bag = getBagObject(String.class); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); assertEquals(5, bag.size()); // Limit less than actual size bag.applyLimit(3); assertEquals(3, bag.size()); bag = getBagObject(String.class); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.applyLimit(7); assertEquals(2, bag.size()); }
@Test public void testOccurrence() { ResultsBag bag = getBagObject(String.class); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap("one", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("two", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("three", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); bag.add(wrap("four", bag.getCollectionType().getElementType())); assertEquals(12, bag.size()); bag.applyLimit(6); int total = bag.occurrences(wrap(null, bag.getCollectionType().getElementType())); total += bag.occurrences(wrap("one", bag.getCollectionType().getElementType())); total += bag.occurrences(wrap("two", bag.getCollectionType().getElementType())); total += bag.occurrences(wrap("three", bag.getCollectionType().getElementType())); total += bag.occurrences(wrap("four", bag.getCollectionType().getElementType())); assertEquals(6, total); }
@Test public void testDuplicates() { ResultsBag bag = new ResultsBag(); bag.add("one"); bag.add("two"); assertEquals(2, bag.size()); bag.add("two"); assertEquals(3, bag.size()); assertEquals(1, bag.occurrences("one")); assertEquals(2, bag.occurrences("two")); assertTrue(bag.remove("two")); assertEquals(1, bag.occurrences("two")); assertTrue(bag.remove("one")); assertEquals(0, bag.occurrences("one")); assertTrue(!bag.remove("one")); assertEquals(0, bag.occurrences("one")); }
@Override public SelectResults getValues() { if (values == null) { values = new ResultsBag(((HasCachePerfStats) getRegion().getCache()).getCachePerfStats()); values.setElementType(valueType); } values.clear(); Object val = this.entry.getValueOffHeapOrDiskWithoutFaultIn((LocalRegion) getRegion()); if (val instanceof StoredObject) { @Retained @Released StoredObject ohval = (StoredObject) val; try { val = ohval.getDeserializedValue(getRegion(), this.entry); } finally { ohval.release(); } } else if (val instanceof CachedDeserializable) { val = ((CachedDeserializable) val).getDeserializedValue(getRegion(), this.entry); } values.add(val); return values; }