@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)); }
@Override public boolean addAll(Collection c) { if (c instanceof StructFields) { return addAll((StructFields) c); } return super.addAll(c); }
/** * Does this set contain a Struct of the correct type with the specified values? */ @Override public boolean containsFieldValues(Object[] fieldValues) { // Asif: The fieldValues can never be null . If the Struc contained // null , then the the getFieldValues would have returned // a zero size Object array. So we need not bother about null here if (this.hasLimitIterator) { Iterator fieldItr = this.fieldValuesIterator(); while (fieldItr.hasNext()) { if (Arrays.equals((Object[]) fieldItr.next(), fieldValues)) { return true; } } return false; } else { return super.contains(fieldValues); } }
@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 testNulls() { ResultsBag bag = new ResultsBag(); assertTrue(bag.isEmpty()); bag.add(null); assertTrue(!bag.isEmpty()); assertEquals(1, bag.size()); assertEquals(1, bag.occurrences(null)); bag.add(new Integer(1)); assertEquals(2, bag.size()); bag.add(new Integer(2)); assertEquals(3, bag.size()); bag.add(new Integer(2)); assertEquals(4, bag.size()); bag.add(null); assertEquals(5, bag.size()); assertEquals(2, bag.occurrences(null)); 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)) { String s = bag.toString(); assertTrue("toString didn't contain 'null': '" + s + "'", s.indexOf("null") > 0); assertTrue(bag.remove(null));
private static ResultsBag readResultsBag(DataInput in) throws IOException, ClassNotFoundException { ResultsBag o = new ResultsBag(true); InternalDataSerializer.invokeFromData(o, in); return o; }
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())); bag.applyLimit(15); assertEquals(12, bag.size()); bag.remove(wrap(null, bag.getCollectionType().getElementType())); assertEquals(11, bag.size()); assertEquals(1, bag.occurrences(wrap(null, bag.getCollectionType().getElementType()))); bag.remove(wrap("three", bag.getCollectionType().getElementType())); assertEquals(10, bag.size()); assertEquals(2, bag.occurrences(wrap("three", bag.getCollectionType().getElementType()))); bag.add(wrap(null, bag.getCollectionType().getElementType())); bag.add(wrap(null, bag.getCollectionType().getElementType())); 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(3), bag.getCollectionType().getElementType()));
@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")); }
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(); asList.add(wrap(new Integer(13), bag.getCollectionType().getElementType())); bag.retainAll(asList); assertEquals(4, bag.size()); assertEquals(5, asList.size()); int occurrence = bag.occurrences(asList.get(0)); bag.retainAll(asList); assertEquals((4 - occurrence), bag.size()); Iterator itr = bag.iterator(); for (int i = 0; i < (4 - occurrence); ++i) { itr.next();
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(); asList.add(wrap(new Integer(13), bag.getCollectionType().getElementType())); assertEquals(4, bag.size()); assertEquals(5, asList.size()); int occurrence = bag.occurrences(asList.get(0)); bag.removeAll(asList); assertEquals(occurrence, bag.size()); Iterator itr = bag.iterator(); for (int i = 0; i < occurrence; ++i) { itr.next();
toBag.add(wrap(null, toBag.getCollectionType().getElementType())); toBag.add(wrap(null, toBag.getCollectionType().getElementType())); toBag.add(wrap(null, toBag.getCollectionType().getElementType())); toBag.add(wrap("one", toBag.getCollectionType().getElementType())); toBag.add(wrap("two", toBag.getCollectionType().getElementType())); toBag.add(wrap("two", toBag.getCollectionType().getElementType())); toBag.add(wrap("three", toBag.getCollectionType().getElementType())); toBag.add(wrap("three", toBag.getCollectionType().getElementType())); toBag.add(wrap("three", toBag.getCollectionType().getElementType())); toBag.add(wrap("four", toBag.getCollectionType().getElementType())); toBag.add(wrap("four", toBag.getCollectionType().getElementType())); toBag.add(wrap("four", toBag.getCollectionType().getElementType())); toBag.add(wrap("four", toBag.getCollectionType().getElementType())); toBag.applyLimit(9); assertEquals(9, toBag.size()); ByteArrayOutputStream baos = new ByteArrayOutputStream(10240); DataOutputStream dos = new DataOutputStream(baos); toBag.toData(dos); byte[] data = baos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(data); fromBag.fromData(dis); assertEquals(toBag.size(), fromBag.size()); assertEquals(toBag.occurrences(wrap(null, toBag.getCollectionType().getElementType())), fromBag.occurrences(wrap(null, fromBag.getCollectionType().getElementType()))); assertEquals(toBag.occurrences(wrap("one", toBag.getCollectionType().getElementType())), fromBag.occurrences(wrap("one", fromBag.getCollectionType().getElementType()))); assertEquals(toBag.occurrences(wrap("two", toBag.getCollectionType().getElementType())), fromBag.occurrences(wrap("two", fromBag.getCollectionType().getElementType())));
@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)); }
@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; }
@Around("execution(* org.springframework.data.gemfire.GemfireOperations.find(..)) " + "|| execution(* org.springframework.data.gemfire.GemfireOperations.findUnique(..)) " + "|| execution(* org.springframework.data.gemfire.GemfireOperations.query(..))") public Object templateQuery(ProceedingJoinPoint pjp) { GemfireTemplate template = (GemfireTemplate) pjp.getTarget(); boolean jsonRegion = isIncludedJsonRegion(template.getRegion()); Object returnValue = null; try { if (jsonRegion) { returnValue = pjp.proceed(); if (returnValue instanceof SelectResults && this.convertReturnedCollections) { ResultsBag resultsBag = new ResultsBag(); for (Object obj : (SelectResults<?>) returnValue) { resultsBag.add(convertToJson(obj)); } returnValue = resultsBag; } else { returnValue = convertToJson(returnValue); } } else { returnValue = pjp.proceed(); } } catch (Throwable cause) { handleThrowable(cause); } return returnValue; }
@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); }
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(); assertEquals(4, bag.size()); int occurrence = bag.occurrences(asList.get(0)); asList.remove(toRemove); assertTrue(bag.containsAll(asList)); asList.add(wrap(new Integer(13), bag.getCollectionType().getElementType())); assertFalse(bag.containsAll(asList));
@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(); assertEquals(6, bag.size()); for (int i = 1; i < 17; ++i) { Integer intg = new Integer(i); assertTrue(temp.contains(wrap(intg, bag.getCollectionType().getElementType())) == bag .contains(wrap(intg, bag.getCollectionType().getElementType()))); assertTrue(temp.contains(wrap(null, bag.getCollectionType().getElementType())) == bag
@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()); }
public DummyQRegion(Region region) { super(region, false); Class constraint = region.getAttributes().getValueConstraint(); if (constraint != null) valueType = TypeUtils.getObjectType(constraint); constraint = region.getAttributes().getKeyConstraint(); if (constraint != null) keyType = TypeUtils.getObjectType(constraint); values = new ResultsBag(((HasCachePerfStats) region.getCache()).getCachePerfStats()); values.setElementType(valueType); keys = new ResultsSet(); keys.setElementType(keyType); entries = new ResultsSet(); // gets key and value types from region entries.setElementType(TypeUtils.getRegionEntryType(region)); }
/** * For internal use. Just add the Object[] values for a struct with same type */ @Override public boolean addFieldValues(Object[] fieldValues) { return super.add(fieldValues); }