You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2015/07/06 20:15:44 UTC
[37/53] [partial] incubator-geode git commit: Initial import of
geode-1.0.0.0-SNAPSHOT-2. All the new sub-project directories (like jvsd)
were not imported. A diff was done to confirm that this commit is exactly the
same as the open directory the snapsho
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/QueryUtils.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/QueryUtils.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/QueryUtils.java
index 7e9aa06..7f584e9 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/QueryUtils.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/QueryUtils.java
@@ -95,6 +95,30 @@ public class QueryUtils {
Assert.assertTrue(sr1.getCollectionType().getElementType().equals(
sr2.getCollectionType().getElementType()));
}
+
+
+ public static SelectResults createResultCollection(ExecutionContext context, ObjectType elementType) {
+ return context.isDistinct() ? new ResultsSet(elementType) :
+ new ResultsBag(elementType, context.getCachePerfStats());
+ }
+
+ public static SelectResults createStructCollection(ExecutionContext context, StructType elementType) {
+ return context.isDistinct() ? new StructSet(elementType) :
+ new StructBag(elementType, context.getCachePerfStats()) ;
+ }
+
+ public static SelectResults createResultCollection(boolean distinct, ObjectType elementType,
+ ExecutionContext context) {
+ return distinct ? new ResultsSet(elementType) :
+ new ResultsBag(elementType, context.getCachePerfStats());
+ }
+
+ public static SelectResults createStructCollection(boolean distinct, StructType elementType,
+ ExecutionContext context) {
+ return distinct ? new StructSet(elementType) :
+ new StructBag(elementType, context.getCachePerfStats()) ;
+ }
+
/**
* Returns an appropriate, empty <code>SelectResults</code>
@@ -186,9 +210,15 @@ public class QueryUtils {
// didn't succeed because large is actually unmodifiable
}
}
- ResultsBag rs = new ResultsBag(small,
- contextOrNull == null ?
- null : contextOrNull.getCachePerfStats());
+
+ SelectResults rs ;
+ if(contextOrNull != null) {
+ rs = contextOrNull.isDistinct() ? new ResultsSet(small) :new ResultsBag(small,
+ contextOrNull.getCachePerfStats());
+ }else {
+ rs =new ResultsBag(small,null);
+ }
+
for (Iterator itr = rs.iterator(); itr.hasNext(); ) {
Object element = itr.next();
int count = large.occurrences(element);
@@ -242,9 +272,15 @@ public class QueryUtils {
// didn't succeed because small is actually unmodifiable
}
}
- ResultsBag rs = new ResultsBag(large, contextOrNull == null ?
- null : contextOrNull.getCachePerfStats());
- for (Iterator itr = small.iterator(); itr.hasNext(); ) {
+ SelectResults rs;
+ if(contextOrNull != null) {
+ rs = contextOrNull.isDistinct() ? new ResultsSet(large) :new ResultsBag(large,
+ contextOrNull.getCachePerfStats());
+ }else {
+ rs =new ResultsBag(large, null);
+ }
+
+ for (Iterator itr = small.iterator(); itr.hasNext(); ) {
Object element = itr.next();
rs.add(element);
}
@@ -324,16 +360,15 @@ public class QueryUtils {
ObjectType type = ((RuntimeIterator) finalList.iterator().next())
.getElementType();
if (type instanceof StructType) {
- returnSet = new StructBag((StructTypeImpl) type, context.getCachePerfStats());
+ returnSet = QueryUtils.createStructCollection(context,(StructTypeImpl) type) ;
}
else {
- return new ResultsBag(type, context.getCachePerfStats());
+ return QueryUtils.createResultCollection(context, type);
}
}
else {
- returnSet = new StructBag(
- (StructTypeImpl) createStructTypeForRuntimeIterators(finalList),
- context.getCachePerfStats());
+ StructType structType = createStructTypeForRuntimeIterators(finalList);
+ returnSet = QueryUtils.createStructCollection(context, structType);
}
ListIterator expnItr = expansionList.listIterator();
// RuntimeIterator levelExpnItr =
@@ -365,7 +400,11 @@ public class QueryUtils {
while (itr.hasNext()) {
values[j++] = ((RuntimeIterator) itr.next()).evaluate(context);
}
- if (select) ((StructBag) returnSet).addFieldValues(values);
+ if (select) {
+
+ ((StructFields) returnSet).addFieldValues(values);
+
+ }
}
else {
if (select)
@@ -589,15 +628,33 @@ public class QueryUtils {
TypeMismatchException, NameResolutionException,
QueryInvocationTargetException {
SelectResults returnSet = null;
+ boolean useLinkedDataStructure = false;
+ boolean nullValuesAtStart = true;
+ Boolean orderByClause = (Boolean)context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
+ if(orderByClause != null && orderByClause.booleanValue()) {
+ List orderByAttrs = (List)context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
+ useLinkedDataStructure = orderByAttrs.size()==1;
+ nullValuesAtStart = !((CompiledSortCriterion)orderByAttrs.get(0)).getCriterion();
+ }
if (finalItrs.size() == 1) {
- returnSet = new ResultsBag(
- ((RuntimeIterator) finalItrs.iterator().next()).getElementType(),
- context.getCachePerfStats());
+ ObjectType resultType = ((RuntimeIterator) finalItrs.iterator().next()).getElementType();
+ if (useLinkedDataStructure) {
+ returnSet = context.isDistinct() ? new LinkedResultSet(resultType):
+ new SortedResultsBag(resultType, nullValuesAtStart);
+ } else {
+ returnSet =QueryUtils.createResultCollection(context, resultType);
+ }
+
}
else {
- returnSet = new StructBag(
- (StructTypeImpl) createStructTypeForRuntimeIterators(finalItrs),
- context.getCachePerfStats());
+ StructTypeImpl resultType = (StructTypeImpl) createStructTypeForRuntimeIterators(finalItrs);
+ if(useLinkedDataStructure) {
+ returnSet = context.isDistinct() ? new LinkedStructSet(resultType)
+ : new SortedResultsBag<Struct>((StructTypeImpl)resultType, nullValuesAtStart);
+ }else {
+ returnSet = QueryUtils.createStructCollection(context, resultType);
+ }
+
}
cutDownAndExpandIndexResults(returnSet, result, indexFieldToItrsMapping,
expansionList, finalItrs, context, checkList, iterOps);
@@ -669,6 +726,11 @@ public class QueryUtils {
select = applyCondition(iterOps, context);
}
if (len > 1) {
+ boolean isOrdered = resultSet.getCollectionType().isOrdered();
+ StructTypeImpl elementType = (StructTypeImpl)resultSet.getCollectionType().getElementType();
+ //TODO:Asif Optimize the LinkedStructSet implementation so that
+ // Object[] can be added rather than Struct
+
Object values[] = new Object[len];
int j = 0;
//creates tuple
@@ -679,7 +741,15 @@ public class QueryUtils {
values[j++] = ((RuntimeIterator) itr.next()).evaluate(context);
}
if (select) {
- ((StructBag) resultSet).addFieldValues(values);
+ if(isOrdered) {
+ //((LinkedStructSet) resultSet).add(new StructImpl(elementType, values));
+ // Can be LinkedStructSet or SortedResultsBag ( containing underlying LinkedHashMap)
+ resultSet.add(new StructImpl(elementType, values));
+ }else {
+
+ ((StructFields) resultSet).addFieldValues(values);
+
+ }
}
}
else {
@@ -1250,9 +1320,8 @@ public class QueryUtils {
}
}
List[] checkList = new List[] { ich1.checkList, ich2.checkList};
- SelectResults returnSet = new StructBag(
- (StructTypeImpl) createStructTypeForRuntimeIterators(finalList),
- context.getCachePerfStats());
+ StructType stype = createStructTypeForRuntimeIterators(finalList);
+ SelectResults returnSet = QueryUtils.createStructCollection(context, stype) ;
RuntimeIterator[][] mappings = new RuntimeIterator[2][];
mappings[0] = ich1.indexFieldToItrsMapping;
mappings[1] = ich2.indexFieldToItrsMapping;
@@ -1357,9 +1426,8 @@ public class QueryUtils {
}
}
// List[] checkList = new List[] { ich1.checkList, ich2.checkList};
- SelectResults returnSet = new StructBag(
- (StructTypeImpl) createStructTypeForRuntimeIterators(finalList),
- context.getCachePerfStats());
+ StructType stype = createStructTypeForRuntimeIterators(finalList);
+ SelectResults returnSet = QueryUtils.createStructCollection(context, stype) ;
//Asif :Obtain the empty resultset for the single usable index
IndexProtocol singleUsblIndex = singleUsableICH.indxInfo._index;
CompiledValue nonUsblIndxPath = nonUsableICH.indxInfo._path;
@@ -1367,15 +1435,12 @@ public class QueryUtils {
// int singleUsblIndexFieldsSize = -1;
SelectResults singlUsblIndxRes = null;
if (singlUsblIndxResType instanceof StructType) {
- singlUsblIndxRes = new StructBag(
- (StructTypeImpl) singlUsblIndxResType,
- context.getCachePerfStats());
+ singlUsblIndxRes = QueryUtils.createStructCollection(context,(StructTypeImpl) singlUsblIndxResType);
// singleUsblIndexFieldsSize = ((StructTypeImpl) singlUsblIndxResType)
// .getFieldNames().length;
}
else {
- singlUsblIndxRes = new ResultsBag(singlUsblIndxResType,
- context.getCachePerfStats());
+ singlUsblIndxRes = QueryUtils.createResultCollection(context, singlUsblIndxResType);
// singleUsblIndexFieldsSize = 1;
}
//Asif iterate over the intermediate structset
@@ -1566,9 +1631,8 @@ public class QueryUtils {
.Assert(
totalFinalList.size() > 1,
" Since we are in relationship index this itself means that we have atleast two RuntimeIterators");
- SelectResults returnSet = new StructBag(
- (StructTypeImpl) createStructTypeForRuntimeIterators(totalFinalList),
- context.getCachePerfStats());
+ StructType stype = createStructTypeForRuntimeIterators(totalFinalList);
+ SelectResults returnSet = QueryUtils.createStructCollection(context, stype) ;
RuntimeIterator[][] mappings = new RuntimeIterator[2][];
mappings[0] = ich1.indexFieldToItrsMapping;
mappings[1] = ich2.indexFieldToItrsMapping;
@@ -1941,28 +2005,30 @@ class IndexCutDownExpansionHelper {
cutDownNeeded = checkList != null && (checkSize = checkList.size()) > 0;
if (cutDownNeeded) {
Boolean orderByClause = (Boolean)context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
- boolean useLinkedSet = false;
+ boolean useLinkedDataStructure = false;
+ boolean nullValuesAtStart = true;
if(orderByClause != null && orderByClause.booleanValue()) {
List orderByAttrs = (List)context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
- useLinkedSet =orderByAttrs.size()==1;
+ useLinkedDataStructure =orderByAttrs.size()==1;
+ nullValuesAtStart = !((CompiledSortCriterion)orderByAttrs.get(0)).getCriterion();
}
if (checkSize > 1) {
checkType = QueryUtils.createStructTypeForRuntimeIterators(checkList);
- if (useLinkedSet) {
- checkSet = new LinkedStructSet((StructTypeImpl) checkType);
+ if (useLinkedDataStructure) {
+ checkSet = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) checkType)
+ : new SortedResultsBag<Struct>((StructTypeImpl)checkType, nullValuesAtStart);
} else {
- checkSet = new StructBag((StructTypeImpl) checkType,
- context.getCachePerfStats());
+ checkSet = QueryUtils.createStructCollection(context, (StructTypeImpl)checkType) ;
}
}
else {
checkType = ((RuntimeIterator) checkList.get(0)).getElementType();
- if (useLinkedSet) {
- checkSet = new LinkedResultSet(checkType);
+ if (useLinkedDataStructure) {
+ checkSet = context.isDistinct() ? new LinkedResultSet(checkType) :
+ new SortedResultsBag(checkType, nullValuesAtStart);
} else {
- checkSet = new ResultsBag(checkType,
- context.getCachePerfStats());
+ checkSet = QueryUtils.createResultCollection(context, checkType) ;
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/RangeJunction.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/RangeJunction.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/RangeJunction.java
index 1b06c80..b570e9e 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/RangeJunction.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/RangeJunction.java
@@ -24,6 +24,7 @@ import com.gemstone.gemfire.cache.query.NameResolutionException;
import com.gemstone.gemfire.cache.query.QueryInvocationTargetException;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.SelectResults;
+import com.gemstone.gemfire.cache.query.Struct;
import com.gemstone.gemfire.cache.query.TypeMismatchException;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
import com.gemstone.gemfire.cache.query.internal.parse.OQLLexerTokenTypes;
@@ -799,10 +800,12 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
int indexFieldsSize = -1;
SelectResults set = null;
Boolean orderByClause = (Boolean)context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
- boolean useLinkedSet = false;
+ boolean useLinkedDataStructure = false;
+ boolean nullValuesAtStart = true;
if(orderByClause != null && orderByClause.booleanValue()) {
List orderByAttrs = (List)context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
- useLinkedSet =orderByAttrs.size()==1;
+ useLinkedDataStructure =orderByAttrs.size()==1;
+ nullValuesAtStart = !((CompiledSortCriterion)orderByAttrs.get(0)).getCriterion();
}
if (resultType instanceof StructType) {
@@ -810,11 +813,11 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
context.getCache().getLogger().fine(
"StructType resultType.class=" + resultType.getClass().getName());
}
- if (useLinkedSet) {
- set = new LinkedStructSet((StructTypeImpl)resultType) ;
+ if (useLinkedDataStructure) {
+ set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl)resultType)
+ : new SortedResultsBag<Struct>((StructTypeImpl)resultType, nullValuesAtStart);
} else {
- set = (SelectResults)new StructBag((StructTypeImpl)resultType,
- context.getCachePerfStats());
+ set = QueryUtils.createStructCollection(context, (StructTypeImpl)resultType) ;
}
indexFieldsSize = ((StructTypeImpl)resultType).getFieldNames().length;
}
@@ -824,10 +827,11 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
"non-StructType resultType.class="
+ resultType.getClass().getName());
}
- if (useLinkedSet) {
- set = new LinkedResultSet(resultType);
+ if (useLinkedDataStructure) {
+ set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType,
+ nullValuesAtStart);
} else {
- set = new ResultsBag(resultType, context.getCachePerfStats());
+ set = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
@@ -991,21 +995,23 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
int indexFieldsSize = -1;
SelectResults set = null;
Boolean orderByClause = (Boolean)context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
- boolean useLinkedSet = false;
+ boolean useLinkedDataStructure = false;
+ boolean nullValuesAtStart = true;
if(orderByClause != null && orderByClause.booleanValue()) {
List orderByAttrs = (List)context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
- useLinkedSet =orderByAttrs.size()==1;
+ useLinkedDataStructure =orderByAttrs.size()==1;
+ nullValuesAtStart = !((CompiledSortCriterion)orderByAttrs.get(0)).getCriterion();
}
if (resultType instanceof StructType) {
if (context.getCache().getLogger().fineEnabled()) {
context.getCache().getLogger().fine(
"StructType resultType.class=" + resultType.getClass().getName());
}
- if (useLinkedSet) {
- set = new LinkedStructSet((StructTypeImpl)resultType);
+ if (useLinkedDataStructure) {
+ set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl)resultType)
+ : new SortedResultsBag<Struct>((StructTypeImpl)resultType, nullValuesAtStart);
} else {
- set = (SelectResults)new StructBag((StructTypeImpl)resultType,
- context.getCachePerfStats());
+ set = QueryUtils.createStructCollection(context, (StructTypeImpl)resultType) ;
}
indexFieldsSize = ((StructTypeImpl)resultType).getFieldNames().length;
}
@@ -1015,10 +1021,11 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
"non-StructType resultType.class="
+ resultType.getClass().getName());
}
- if (useLinkedSet) {
- set = new LinkedResultSet(resultType);
+ if (useLinkedDataStructure) {
+ set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType,
+ nullValuesAtStart);
} else {
- set = new ResultsBag(resultType, context.getCachePerfStats());
+ set = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
@@ -1143,10 +1150,12 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
int indexFieldsSize = -1;
SelectResults set = null;
Boolean orderByClause = (Boolean)context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
- boolean useLinkedSet = false;
+ boolean useLinkedDataStructure = false;
+ boolean nullValuesAtStart = true;
if(orderByClause != null && orderByClause.booleanValue()) {
List orderByAttrs = (List)context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
- useLinkedSet =orderByAttrs.size()==1;
+ useLinkedDataStructure =orderByAttrs.size()==1;
+ nullValuesAtStart = !((CompiledSortCriterion)orderByAttrs.get(0)).getCriterion();
}
if (resultType instanceof StructType) {
@@ -1154,11 +1163,11 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
context.getCache().getLogger().fine(
"StructType resultType.class=" + resultType.getClass().getName());
}
- if(useLinkedSet) {
- set = new LinkedStructSet((StructTypeImpl)resultType);
+ if(useLinkedDataStructure) {
+ set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl)resultType)
+ : new SortedResultsBag<Struct>((StructTypeImpl)resultType, nullValuesAtStart);
}else {
- set = new StructBag((StructTypeImpl)resultType,
- context.getCachePerfStats());
+ set = QueryUtils.createStructCollection(context, (StructTypeImpl)resultType) ;
}
indexFieldsSize = ((StructTypeImpl)resultType).getFieldNames().length;
}
@@ -1168,10 +1177,11 @@ public class RangeJunction extends AbstractGroupOrRangeJunction {
"non-StructType resultType.class="
+ resultType.getClass().getName());
}
- if (useLinkedSet) {
- set = new LinkedResultSet(resultType);
+ if (useLinkedDataStructure) {
+ set = context.isDistinct() ? new LinkedResultSet(resultType):
+ new SortedResultsBag(resultType, nullValuesAtStart);
} else {
- set = new ResultsBag(resultType, context.getCachePerfStats());
+ set = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsBag.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsBag.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsBag.java
index b05fc6a..1f6fb0a 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsBag.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsBag.java
@@ -1,393 +1,138 @@
-/*=========================================================================
- * Copyright (c) 2005-2014 Pivotal Software, Inc. All Rights Reserved.
- * This product is protected by U.S. and international copyright
- * and intellectual property laws. Pivotal products are covered by
- * more patents listed at http://www.pivotal.io/patents.
- *========================================================================
- */
-
package com.gemstone.gemfire.cache.query.internal;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-import java.util.AbstractCollection;
-import java.util.AbstractSet;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
-import java.util.List;
-import java.util.NoSuchElementException;
-import java.util.Set;
import com.gemstone.gemfire.DataSerializer;
-import com.gemstone.gemfire.cache.query.CqResults;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.query.internal.ObjectIntHashMap.Entry;
-import com.gemstone.gemfire.cache.query.internal.types.CollectionTypeImpl;
-import com.gemstone.gemfire.cache.query.types.CollectionType;
import com.gemstone.gemfire.cache.query.types.ObjectType;
-import com.gemstone.gemfire.cache.query.types.StructType;
import com.gemstone.gemfire.internal.DataSerializableFixedID;
import com.gemstone.gemfire.internal.Version;
import com.gemstone.gemfire.internal.cache.CachePerfStats;
-import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
-
-// @todo probably should assert element type when elements added
-// @todo support generics when no longer support Java 1.4
-/**
- * Implementation of SelectResults that allows duplicates and
- * extends IntHashMap. The keys store the elements of the collection and the
- * the values store the number of occurrences as an int.
- * If the elements are Structs, then use a StructBag instead.
- *
- * @author Eric Zoerner
- * @since 5.1
- */
-public class ResultsBag extends AbstractCollection
-implements CqResults, DataSerializableFixedID {
- protected ObjectType elementType;
- protected ObjectIntHashMap map;
- protected int size = 0;
- /** adds support for null elements*/
+public class ResultsBag extends Bag implements
+ DataSerializableFixedID {
- protected int numNulls = 0;
- //Asif: These fields are used for limiting the results based on the limit
- //clause in the query
- private int limit = -1;
- boolean hasLimitIterator = false;
- final Object limitLock = new Object();
+ protected ObjectIntHashMap map;
public ResultsBag() {
this.map = new ObjectIntHashMap();
}
-
+
/**
- * This constructor should only be used by the DataSerializer.
- * Creates a ResultsBag with no fields.
+ * This constructor should only be used by the DataSerializer. Creates a
+ * ResultsBag with no fields.
*/
public ResultsBag(boolean ignored) {
+ super(ignored);
}
-
-
+
/**
- * @param stats the CachePerfStats to track hash collisions. Should
- * be null unless this is used as a query execution-time result set.
+ * @param stats
+ * the CachePerfStats to track hash collisions. Should be null unless
+ * this is used as a query execution-time result set.
*/
- public ResultsBag(CachePerfStats stats) {
+ public ResultsBag(CachePerfStats stats) {
+ super(stats);
this.map = new ObjectIntHashMap();
}
- protected ResultsBag(HashingStrategy strategy,
- CachePerfStats stats) {
+ protected ResultsBag(HashingStrategy strategy, CachePerfStats stats) {
+ super(stats);
this.map = new ObjectIntHashMap(strategy);
}
-
+
/**
- * @param stats the CachePerfStats to track hash collisions. Should
- * be null unless this is used as a query execution-time result set.
+ * @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(); ) {
+ for (Iterator itr = c.iterator(); itr.hasNext();) {
this.add(itr.next());
}
}
- protected ResultsBag(Collection c,
- HashingStrategy strategy,
- CachePerfStats stats) {
+ protected ResultsBag(Collection c, HashingStrategy strategy,
+ CachePerfStats stats) {
this(strategy, stats);
- for (Iterator itr = c.iterator(); itr.hasNext(); ) {
+ for (Iterator itr = c.iterator(); itr.hasNext();) {
this.add(itr.next());
}
}
-
+
/**
- * @param stats the CachePerfStats to track hash collisions. Should
- * be null unless this is used as a query execution-time result set.
+ * @param stats
+ * the CachePerfStats to track hash collisions. Should be null unless
+ * this is used as a query execution-time result set.
*/
ResultsBag(SelectResults sr, CachePerfStats stats) {
- this((Collection)sr, stats);
+ this((Collection) sr, stats);
// grab type info
setElementType(sr.getCollectionType().getElementType());
}
-
+
/**
- * @param stats the CachePerfStats to track hash collisions. Should
- * be null unless this is used as a query execution-time result set.
+ * @param stats
+ * the CachePerfStats to track hash collisions. Should be null unless
+ * this is used as a query execution-time result set.
*/
ResultsBag(ObjectType elementType, CachePerfStats stats) {
this(stats);
setElementType(elementType);
}
-
+
/**
- * @param stats the CachePerfStats to track hash collisions. Should
- * be null unless this is used as a query execution-time result set.
+ * @param stats
+ * the CachePerfStats to track hash collisions. Should be null unless
+ * this is used as a query execution-time result set.
*/
- ResultsBag(ObjectType elementType,
- int initialCapacity,
- CachePerfStats stats) {
+ ResultsBag(ObjectType elementType, int initialCapacity, CachePerfStats stats) {
this(initialCapacity, stats);
setElementType(elementType);
}
-
+
/**
- * @param stats the CachePerfStats to track hash collisions. Should
- * be null unless this is used as a query execution-time result set.
+ * @param stats
+ * the CachePerfStats to track hash collisions. Should be null unless
+ * this is used as a query execution-time result set.
*/
ResultsBag(int initialCapacity, float loadFactor, CachePerfStats stats) {
this.map = new ObjectIntHashMap(initialCapacity, loadFactor);
}
-
- protected ResultsBag(int initialCapacity,
- float loadFactor,
- HashingStrategy strategy,
- CachePerfStats stats) {
+
+ protected ResultsBag(int initialCapacity, float loadFactor,
+ HashingStrategy strategy, CachePerfStats stats) {
+ super(stats);
this.map = new ObjectIntHashMap(initialCapacity, loadFactor, strategy);
}
-
+
ResultsBag(int initialCapacity, CachePerfStats stats) {
+ super(stats);
this.map = new ObjectIntHashMap(initialCapacity);
-
- }
-
- protected ResultsBag(int initialCapacity,
- HashingStrategy strategy,
- CachePerfStats stats) {
- this.map = new ObjectIntHashMap(initialCapacity, strategy);
-
- }
-
- public void setElementType(ObjectType elementType) {
- if (elementType instanceof StructType)
- throw new IllegalArgumentException(LocalizedStrings.ResultsBag_THIS_COLLECTION_DOES_NOT_SUPPORT_STRUCT_ELEMENTS.toLocalizedString());
- this.elementType = elementType;
- }
-
- // @todo Currently does an iteration, could make this more efficient
- // by providing a ListView
- /**
- * Returns this bag as a list.
- */
- public List asList() {
- return new ArrayList(this);
- }
-
- /**
- * Return an unmodifiable Set view of this bag.
- * Does not require an iteration by using a lightweight wrapper.
- */
- public Set asSet() {
- return new SetView();
- }
-
- public CollectionType getCollectionType() {
- return new CollectionTypeImpl(Collection.class, this.elementType);
- }
-
- public boolean isModifiable() {
- return true;
- }
-
- public int occurrences(Object element) {
- if (this.hasLimitIterator) {
- // Asif: If limit iterator then occurrence should be calculated
- // via the limit iterator
- int count = 0;
- boolean encounteredObject = false;
- for (Iterator itr = this.iterator()/* this.base.iterator() */; itr
- .hasNext();) {
- Object v = itr.next();
- if (element == null ? v == null : element.equals(v)) {
- count++;
- encounteredObject = true;
- }
- else if (encounteredObject) {
- // Asif: No possibility of its occurrence again
- break;
- }
- }
- return count;
- }
- else {
- if (element == null) {
- return this.numNulls;
- }
- return this.map.get(element); // returns 0 if not found
- }
- }
- /**
- * Return an iterator over the elements in this collection. Duplicates will
- * show up the number of times it has occurrences.
- */
- @Override
- public Iterator iterator() {
- if (this.hasLimitIterator) {
- // Asif: Return a new LimitIterator in the block so that
- // the current Limit does not get changed by a remove
- // operation of another thread. The current limit is
- // set in the limit iterator. So the setting of the current limit
- // & creation of iterator should be atomic .If any subsequent
- // modifcation in limit occurs, the iterator will itself fail.
- // Similarly a remove called by a iterator should decrease the
- // current limit and underlying itr.remove atomically
- synchronized (this.limitLock) {
- return new LimitResultsBagIterator();
- }
- }
- else {
- return new ResultsBagIterator();
- }
}
- @Override
- public boolean contains(Object element) {
- if (this.hasLimitIterator) {
- return super.contains(element);
- }
- else {
- if (element == null) {
- return this.numNulls > 0;
- }
- return this.map.containsKey(element);
- }
- }
-
- // not thread safe!
- @Override
- public boolean add(Object element) {
- if( this.limit > -1) {
- throw new UnsupportedOperationException("Addition to the SelectResults not allowed as the query result is constrained by LIMIT");
- }
- if (element == null) {
- numNulls++;
- }
- else {
- int count = this.map.get(element); // 0 if not found
- this.map.put(element, count + 1);
- }
- this.size++;
- assert this.size >= 0 : this.size;
- return true;
- }
+ protected ResultsBag(int initialCapacity, HashingStrategy strategy,
+ CachePerfStats stats) {
+ super(stats);
+ this.map = new ObjectIntHashMap(initialCapacity, strategy);
- // Internal usage method
- // Asif :In case of StructBag , we will ensure that it
- // gets an Object [] indicating field values as parameter
- // from the CompiledSelect
- public int addAndGetOccurence(Object element) {
- int occurence;
- if (element == null) {
- numNulls++;
- occurence = numNulls;
- }
- else {
- occurence = this.map.get(element); // 0 if not found
- this.map.put(element, ++occurence);
- }
- this.size++;
- assert this.size >= 0: this.size;
- return occurence;
}
-
- @Override
- public int size() {
- if (this.hasLimitIterator) {
- synchronized (this.limitLock) {
- return this.limit;
- }
- } else {
- return this.size;
- }
- }
-
-
- public int distinctElementsSize() {
- return this.map.size();
- }
- // not thread safe!
- @Override
- public boolean remove(Object element) {
- if(this.hasLimitIterator) {
- return super.remove(element);
- }else {
- if (element == null) {
- if (this.numNulls > 0) {
- this.numNulls--;
- this.size--;
- assert this.size >= 0 : this.size;
- return true;
- }
- else {
- return false;
- }
- }
- int count = this.map.get(element); // 0 if not found
- if (count == 0) {
- return false;
- }
- if (count == 1) {
- this.map.remove(element);
- }
- else {
- this.map.put(element, --count);
- }
- this.size--;
- assert this.size >= 0 : this.size;
- return true;
- }
- }
-
- // not thread safe!
- @Override
- public void clear() {
- this.map.clear();
- this.numNulls = 0;
- this.size = 0;
- if (this.hasLimitIterator) {
- synchronized (this.limitLock) {
- this.limit = 0;
- }
- }
- }
-
- // not thread safe!
- @Override
- public boolean equals(Object o) {
- if (!(o instanceof ResultsBag)) {
- return false;
- }
- ResultsBag otherBag = (ResultsBag)o;
- return this.size == otherBag.size
- && this.elementType.equals(otherBag.elementType)
- && this.map.equals(otherBag.map)
- && this.numNulls == otherBag.numNulls;
- }
-
- @Override // GemStoneAddition
- public int hashCode() {
- return this.map.hashCode();
- }
- public boolean addAll(Collection coll) {
- if(this.limit > -1) {
- throw new UnsupportedOperationException("Addition to the SelectResults not allowed as the query result is constrained by LIMIT");
- }else {
- return super.addAll(coll);
- }
- }
protected ObjectIntHashMap createMapForFromData() {
return new ObjectIntHashMap(this.size);
}
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
- this.elementType = (ObjectType)DataSerializer.readObject(in);
+ this.elementType = (ObjectType) DataSerializer.readObject(in);
this.size = in.readInt();
- assert this.size >= 0: this.size;
+ assert this.size >= 0 : this.size;
this.map = createMapForFromData();
this.readNumNulls(in);
// Asif: The size will be including null so the Map should at max contain
@@ -428,24 +173,6 @@ implements CqResults, DataSerializableFixedID {
numLeft -= occurence;
}
}
-
- /**
- *
- * @param out
- * @throws IOException
- */
- void writeNumNulls(DataOutput out) throws IOException {
- out.writeInt(this.numNulls);
- }
-
- /**
- *
- * @param in
- * @throws IOException
- */
- void readNumNulls(DataInput in) throws IOException {
- this.numNulls = in.readInt();
- }
/**
*/
@@ -453,266 +180,81 @@ implements CqResults, DataSerializableFixedID {
this.map = new ObjectIntHashMap(this.size - this.numNulls);
}
- void applyLimit(int limit) {
- this.limit = limit;
- // Asif : From the code of IntHashMap, it appears that if no data is
- // going to be added , then the rehash does not occur & default code
- // of rehash does not appear to change the order of data . So we can assume
- // that this iterator will be returning data in order.
- // Limit Iterator is needed if the limit happens to be less than the size
- if (this.limit > -1 && this.size > this.limit) {
- this.hasLimitIterator = true;
- }
+ @Override
+ public boolean isModifiable() {
+ return true;
}
-
- protected class ResultsBagIterator implements Iterator {
- final Iterator<Entry> mapIterator = ResultsBag.this.map.entrySet().iterator();
- Entry currentEntry = null;
-
- /**
- * duplicates are numbered from 1 to n;
- * 0 = no current, otherwise goes from 1 to dupLimit,
- * indicating the last dup that was emitted by next()
- */
- int currentDup = 0;
- /**
- * dupLimit is the total number of occurrences;
- * start by emitting the nulls
- */
- int dupLimit = ResultsBag.this.numNulls;
-
- public boolean hasNext() {
- return this.mapIterator.hasNext() || this.currentDup < this.dupLimit;
- }
-
- public Object next() {
- // see if there is another duplicate to emit
- if (this.currentDup < this.dupLimit) {
- this.currentDup++;
- return (this.currentEntry == null) ? null : this.currentEntry.getKey();
- }
- //otherwise, go to next object
- currentEntry = this.mapIterator.next();
- this.dupLimit = (Integer) currentEntry.getValue();
- this.currentDup = 1;
- return currentEntry.getKey();
- }
-
- public void remove() {
- if (this.currentDup == 0) {
- // next has not yet been called
- throw new IllegalStateException(LocalizedStrings.ResultsBag_NEXT_MUST_BE_CALLED_BEFORE_REMOVE.toLocalizedString());
- }
-
- this.dupLimit--;
- assert this.dupLimit >= 0 : this.dupLimit;
- if (this.currentEntry == null) {
- ResultsBag.this.numNulls = this.dupLimit;
- assert ResultsBag.this.numNulls >= 0 : ResultsBag.this.numNulls;
- }
- else {
- if (this.dupLimit > 0) {
- ResultsBag.this.map.put(currentEntry.getKey(), this.dupLimit);
- }
- else {
- this.mapIterator.remove();
- }
- }
- ResultsBag.this.size--;
- this.currentDup--;
- assert ResultsBag.this.size >= 0 : ResultsBag.this.size;
- assert this.currentDup >= 0 : this.currentDup;
- }
- }
-
- /** package visibility so ResultsCollectionWrapper can reference
- * it.
- * This SetView is serialized as a special case by a
- * ResultsCollectionWrapper.
- * Keith: Refactored to add consideration for LIMIT, April 1, 2009
- * @see ResultsCollectionWrapper#toData
- */
- class SetView extends AbstractSet {
- private int localLimit;
+ @Override
+ public Version[] getSerializationVersions() {
+ return null;
+ }
- SetView() {
- localLimit = ResultsBag.this.limit;
- }
+ @Override
+ protected int mapGet(Object element) {
+ return this.map.get(element);
+ }
- public Iterator iterator() {
- if (localLimit > -1) {
- return new LimitSetViewIterator();
- } else {
- return new SetViewIterator();
- }
- }
+ @Override
+ protected boolean mapContainsKey(Object element) {
+ return this.map.containsKey(element);
+ }
- @Override
- public boolean add(Object o) {
- if(contains(o)) {
- return false;
- }
- return ResultsBag.this.add(o);
- }
+ @Override
+ protected void mapPut(Object element, int count) {
+ this.map.put(element, count);
- @Override
- public void clear() {
- ResultsBag.this.clear();
- }
-
- @Override
- public int size() {
- int calculatedSize = ResultsBag.this.map.size() +
- (ResultsBag.this.numNulls > 0 ? 1 : 0);
- if (localLimit > -1) {
- return Math.min(localLimit, calculatedSize);
- }
- return calculatedSize;
- }
-
- @Override
- public boolean contains(Object o) {
- if (o == null) {
- return ResultsBag.this.numNulls > 0;
- }
- return ResultsBag.this.map.containsKey(o);
- }
-
- @Override
- public boolean isEmpty() {
- if(localLimit == 0) {
- return true;
- }
- if (ResultsBag.this.numNulls > 0) {
- return false;
- }
- return ResultsBag.this.map.isEmpty();
- }
+ }
- public class SetViewIterator implements Iterator {
- /** need to emit a null value if true */
- boolean emitNull = ResultsBag.this.numNulls > 0;
- final Iterator it = ResultsBag.this.map.keySet().iterator();
- boolean currentIsNull = false;
-
- public Object next() {
- if (this.emitNull) {
- this.emitNull = false;
- currentIsNull = true;
- return null;
- }
- Object key = it.next();
- currentIsNull = false;
- return key;
- }
-
- public boolean hasNext() {
- if (this.emitNull) {
- return true;
- }
- return it.hasNext();
- }
-
- public void remove() {
- if(currentIsNull) {
- ResultsBag.this.numNulls = 0;
- } else {
- it.remove();
- }
- }
- };
-
- class LimitSetViewIterator extends SetViewIterator {
- private int currPos = 0;
- private Object currentKey;
- @Override
- public Object next() {
- if (this.currPos == ResultsBag.SetView.this.localLimit) {
- throw new NoSuchElementException();
- }
- else {
- currentKey = super.next();
- ++currPos;
- return currentKey;
- }
- }
+ @Override
+ protected int mapSize() {
+ return this.map.size();
+ }
- @Override
- public boolean hasNext() {
- return (this.currPos < ResultsBag.SetView.this.localLimit)
- && super.hasNext();
- }
+ @Override
+ protected int mapRemove(Object element) {
+ return this.map.remove(element);
+ }
+
+ @Override
+ protected void mapClear() {
+ this.map.clear();
- @Override
- public void remove() {
- if (this.currPos == 0) {
- // next has not yet been called
- throw new IllegalStateException("next() must be called before remove()");
- }
- synchronized (ResultsBag.this.limitLock) {
- if(currentIsNull) {
- ResultsBag.this.limit -= ResultsBag.this.numNulls;
- ResultsBag.this.numNulls = 0;
- ResultsBag.SetView.this.localLimit--;
- } else {
- int count = ResultsBag.this.map.remove(currentKey);
- assert count != 0 : "Attempted to remove an element that was not in the map.";
- ResultsBag.this.limit -= count;
- ResultsBag.SetView.this.localLimit--;
- }
- }
- }
- }
}
-
- /**
- * @author Asif
- *
- */
- protected class LimitResultsBagIterator extends ResultsBag.ResultsBagIterator {
- final private int localLimit;
- private int currPos = 0;
+ @Override
+ protected Object getMap() {
+ return this.map;
+ }
- /**
- *guarded by ResultsBag.this.limitLock object
- */
- public LimitResultsBagIterator() {
- localLimit = ResultsBag.this.limit;
- }
+ @Override
+ protected int mapHashCode() {
+ return this.map.hashCode();
+ }
- public boolean hasNext() {
- return this.currPos < this.localLimit;
- }
+ @Override
+ protected boolean mapEmpty() {
+ return this.map.isEmpty();
+ }
- public Object next() {
- if (this.currPos == this.localLimit) {
- throw new NoSuchElementException();
- }
- else {
- Object next = super.next();
- ++currPos;
- return next;
- }
+ @Override
+ protected Iterator mapEntryIterator() {
+ return this.map.entrySet().iterator();
+ }
- }
+ @Override
+ protected Iterator mapKeyIterator() {
+ return this.map.keySet().iterator();
+ }
- public void remove() {
- if (this.currPos == 0) {
- // next has not yet been called
- throw new IllegalStateException("next() must be called before remove()");
- }
- synchronized (ResultsBag.this.limitLock) {
- super.remove();
- --ResultsBag.this.limit;
- }
- }
+ @Override
+ protected Object keyFromEntry(Object entry) {
+ return ((Entry) entry).getKey();
}
@Override
- public Version[] getSerializationVersions() {
- return null;
+ protected Integer valueFromEntry(Object entry) {
+
+ return ((Entry) entry).getValue();
}
-
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsCollectionWrapper.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsCollectionWrapper.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsCollectionWrapper.java
index 5834699..3283974 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsCollectionWrapper.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsCollectionWrapper.java
@@ -524,7 +524,7 @@ public final class ResultsCollectionWrapper
*/
public void toData(DataOutput out) throws IOException {
// special case when wrapping a ResultsBag.SetView
- boolean isBagSetView = this.base instanceof ResultsBag.SetView;
+ boolean isBagSetView = this.base instanceof Bag.SetView;
out.writeBoolean(isBagSetView);
if (isBagSetView) {
InternalDataSerializer.writeSet((Set)this.base, out);
@@ -570,12 +570,13 @@ public final class ResultsCollectionWrapper
* at this time
*/
private Class getBaseClass() {
- if (this.base instanceof SortedSet || this.base instanceof LinkedStructSet)
+ if (this.base instanceof Ordered)
{
- return SortedSet.class;
- } else if (this.base instanceof LinkedResultSet) {
- return LinkedHashSet.class;
- } else if (this.base instanceof Set) {
+ return Ordered.class;
+ }else if (this.base instanceof TreeSet) {
+ return TreeSet.class;
+ }
+ else if (this.base instanceof Set) {
return Set.class;
} else {
return this.base.getClass();
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsSet.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsSet.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsSet.java
index 1e21c6b..600aa1c 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsSet.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/ResultsSet.java
@@ -31,7 +31,8 @@ import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
* @author Eric Zoerner
* @since 4.0
*/
-public final class ResultsSet extends HashSet implements SelectResults, DataSerializableFixedID {
+public final class ResultsSet extends HashSet implements SelectResults,
+ DataSerializableFixedID {
private static final long serialVersionUID = -5423281031630216824L;
private ObjectType elementType;
@@ -51,7 +52,7 @@ public final class ResultsSet extends HashSet implements SelectResults, DataSer
setElementType(sr.getCollectionType().getElementType());
}
- ResultsSet(ObjectType elementType) {
+ public ResultsSet(ObjectType elementType) {
super();
setElementType(elementType);
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultSet.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultSet.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultSet.java
index 9ecdc22..5d4d925 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultSet.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultSet.java
@@ -26,7 +26,7 @@ import com.gemstone.gemfire.internal.Version;
* @author Yogesh Mahajan
* @since 4.0
*/
-public final class SortedResultSet extends TreeSet implements SelectResults,
+public final class SortedResultSet extends TreeSet implements SelectResults, Ordered,
DataSerializableFixedID {
private static final long serialVersionUID = 5184711453750319224L;
@@ -81,7 +81,7 @@ public final class SortedResultSet extends TreeSet implements SelectResults,
}
public CollectionType getCollectionType() {
- return new CollectionTypeImpl(TreeSet.class, this.elementType);
+ return new CollectionTypeImpl(SortedResultSet.class, this.elementType);
}
public boolean isModifiable() {
@@ -117,4 +117,9 @@ public final class SortedResultSet extends TreeSet implements SelectResults,
public Version[] getSerializationVersions() {
return null;
}
+
+ @Override
+ public boolean dataPreordered() {
+ return false;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultsBag.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultsBag.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultsBag.java
new file mode 100644
index 0000000..0dbbb3d
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedResultsBag.java
@@ -0,0 +1,244 @@
+package com.gemstone.gemfire.cache.query.internal;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import com.gemstone.gemfire.cache.query.internal.types.CollectionTypeImpl;
+import com.gemstone.gemfire.cache.query.types.CollectionType;
+import com.gemstone.gemfire.cache.query.types.ObjectType;
+import com.gemstone.gemfire.internal.cache.CachePerfStats;
+
+/**
+ * This results set is used to sort the data allowing duplicates. If the data
+ * being added is already sorted, than the underlying Map is a LinkedHashMap ,
+ * else a TreeMap is created. For index results expecting duplicates, the
+ * constructor used is the one which creates LinkedHashMap
+ * This class is used to store ordered data of Structs too, obtained from index lookup
+ *
+ * @author ashahid
+ *
+ * @param <E>
+ */
+public class SortedResultsBag<E> extends Bag implements Ordered {
+
+ private final Map<E, Integer> sortedMap;
+ private final boolean orderedDataAddition;
+ private final boolean emitNullAtStart;
+
+ /**
+ * Constructor for unordered input
+ *
+ * @param comparator
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(Comparator<E> comparator, boolean nullAtStart) {
+ super();
+ this.sortedMap = new TreeMap<E, Integer>(comparator);
+ this.emitNullAtStart = nullAtStart;
+ this.orderedDataAddition = false;
+ }
+
+ /**
+ * Constructor for unordered input
+ * @param comparator
+ * @param elementType
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(Comparator<E> comparator,
+ ObjectType elementType, boolean nullAtStart) {
+ super();
+ this.sortedMap = new TreeMap<E, Integer>(comparator);
+ this.setElementType(elementType);
+ this.emitNullAtStart = nullAtStart;
+ this.orderedDataAddition = false;
+ }
+
+ /**
+ * Constructor for unordered input
+ *
+ * @param comparator
+ * @param elementType
+ * @param stats
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(Comparator<E> comparator, ObjectType elementType,
+ CachePerfStats stats, boolean nullAtStart) {
+ super(elementType, stats);
+ this.sortedMap = new TreeMap<E, Integer>(comparator);
+ this.emitNullAtStart = nullAtStart;
+ this.orderedDataAddition = false;
+ }
+
+ /**
+ * Constructor for unordered input
+ *
+ * @param comparator
+ * @param stats
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(Comparator<E> comparator, CachePerfStats stats, boolean nullAtStart) {
+ super(stats);
+ this.sortedMap = new TreeMap<E, Integer>(comparator);
+ this.emitNullAtStart = nullAtStart;
+ this.orderedDataAddition = false;
+ }
+
+ /**
+ * Constructor for ordered input. Creates underlying Map as LinkedHashMap
+ *
+ * @param stats
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(CachePerfStats stats, boolean nullAtStart) {
+ super(stats);
+ this.sortedMap = new LinkedHashMap<E, Integer>();
+ this.orderedDataAddition = true;
+ this.emitNullAtStart = nullAtStart;
+ }
+
+ /**
+ * Constructor for ordered input. Creates underlying Map as LinkedHashMap
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(boolean nullAtStart) {
+ super();
+ this.sortedMap = new LinkedHashMap<E, Integer>();
+ this.orderedDataAddition = true;
+ this.emitNullAtStart = nullAtStart;
+ }
+
+ /**
+ * Constructor for ordered input. Creates underlying Map as LinkedHashMap
+ *
+ * @param elementType
+ * @param nullAtStart Indicates that the first order by coumn is asc , so that tuple with null
+ * order by column value need to be emitted at start, else if desc, then emit at last
+ */
+ public SortedResultsBag(ObjectType elementType, boolean nullAtStart) {
+ super();
+ this.sortedMap = new LinkedHashMap<E, Integer>();
+ this.orderedDataAddition = true;
+ this.setElementType(elementType);
+ this.emitNullAtStart = nullAtStart;
+ }
+
+ @Override
+ public boolean isModifiable() {
+ return false;
+ }
+
+ @Override
+ protected int mapGet(Object element) {
+ Integer count = this.sortedMap.get(element);
+ if (count == null) {
+ return 0;
+ } else {
+ return count;
+ }
+ }
+
+ @Override
+ protected boolean mapContainsKey(Object element) {
+ return this.sortedMap.containsKey(element);
+ }
+
+ @Override
+ protected void mapPut(Object element, int count) {
+ this.sortedMap.put((E) element, count);
+ }
+
+ @Override
+ protected int mapSize() {
+ return this.sortedMap.size();
+ }
+
+ @Override
+ protected int mapRemove(Object element) {
+ Integer count = this.sortedMap.remove(element);
+ if (count == null) {
+ return 0;
+ } else {
+ return count;
+ }
+ }
+
+ @Override
+ protected void mapClear() {
+ this.sortedMap.clear();
+ }
+
+ @Override
+ protected Object getMap() {
+ return this.sortedMap;
+ }
+
+ @Override
+ protected int mapHashCode() {
+ return this.sortedMap.hashCode();
+ }
+
+ @Override
+ protected boolean mapEmpty() {
+ return this.sortedMap.isEmpty();
+ }
+
+ @Override
+ protected Iterator mapEntryIterator() {
+ return this.sortedMap.entrySet().iterator();
+ }
+
+ @Override
+ protected Iterator mapKeyIterator() {
+ return this.sortedMap.keySet().iterator();
+ }
+
+ @Override
+ protected Object keyFromEntry(Object entry) {
+ Map.Entry<E, Integer> mapEntry = (Map.Entry<E, Integer>) entry;
+ return mapEntry.getKey();
+ }
+
+ @Override
+ protected Integer valueFromEntry(Object entry) {
+ Map.Entry<E, Integer> mapEntry = (Map.Entry<E, Integer>) entry;
+ return mapEntry.getValue();
+ }
+
+ @Override
+ public CollectionType getCollectionType() {
+ return new CollectionTypeImpl(SortedResultsBag.class, this.elementType);
+ }
+
+ @Override
+ public Comparator comparator() {
+ return this.orderedDataAddition ? null : ((SortedMap) this.sortedMap)
+ .comparator();
+ }
+
+ @Override
+ public void setElementType(ObjectType elementType) {
+ this.elementType = elementType;
+ }
+
+
+ @Override
+ public boolean dataPreordered() {
+ return this.orderedDataAddition;
+ }
+
+ @Override
+ protected boolean nullOutputAtBegining() {
+ return this.emitNullAtStart;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructBag.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructBag.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructBag.java
new file mode 100644
index 0000000..f9bbc1c
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructBag.java
@@ -0,0 +1,353 @@
+package com.gemstone.gemfire.cache.query.internal;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import com.gemstone.gemfire.cache.query.Struct;
+import com.gemstone.gemfire.cache.query.internal.types.CollectionTypeImpl;
+import com.gemstone.gemfire.cache.query.internal.types.StructTypeImpl;
+import com.gemstone.gemfire.cache.query.types.CollectionType;
+import com.gemstone.gemfire.cache.query.types.ObjectType;
+import com.gemstone.gemfire.cache.query.types.StructType;
+import com.gemstone.gemfire.internal.cache.CachePerfStats;
+import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
+
+/**
+ *
+ * @author ashahid
+ *
+ */
+public class SortedStructBag extends SortedResultsBag<Object[]> implements StructFields{
+
+
+ /**
+ * Constructor for unordered input
+ *
+ * @param comparator
+ * @param nullAtStart
+ */
+ public SortedStructBag(Comparator<Object[]> comparator, boolean nullAtStart) {
+ super(comparator, nullAtStart);
+
+ }
+
+ /**
+ * Constructor for unordered input
+ * @param comparator
+ * @param elementType
+ * @param nullAtStart
+ */
+ public SortedStructBag(Comparator<Object[]> comparator, StructType elementType,
+ boolean nullAtStart) {
+ super(comparator, elementType, nullAtStart);
+ }
+
+ /**
+ * Constructor for unordered input
+ *
+ * @param comparator
+ * @param elementType
+ * @param stats
+ * @param nullAtStart
+ */
+ public SortedStructBag(Comparator<Object[]> comparator,
+ ObjectType elementType, CachePerfStats stats, boolean nullAtStart) {
+ super(comparator, elementType, stats, nullAtStart);
+
+ }
+
+ /**
+ * Constructor for unordered input
+ *
+ * @param comparator
+ * @param stats
+ * @param nullAtStart
+ */
+ public SortedStructBag(Comparator<Object[]> comparator, CachePerfStats stats,
+ boolean nullAtStart) {
+ super(comparator, stats, nullAtStart);
+
+ }
+
+ /** Add a Struct */
+ @Override
+ public boolean add(Object obj) {
+ if (!(obj instanceof StructImpl)) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.StructBag_THIS_SET_ONLY_ACCEPTS_STRUCTIMPL
+ .toLocalizedString());
+ }
+ StructImpl s = (StructImpl) obj;
+ if (!this.elementType.equals(s.getStructType())) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.StructBag_OBJ_DOES_NOT_HAVE_THE_SAME_STRUCTTYPE
+ .toLocalizedString(this.elementType,s.getStructType()) );
+ }
+ return addFieldValues(s.getFieldValues());
+ }
+
+ /**
+ * For internal use. Just add the Object[] values for a struct with same type
+ */
+ public boolean addFieldValues(Object[] fieldValues) {
+ return super.add(fieldValues);
+ }
+
+ /** Does this set contain specified struct? */
+ @Override
+ public boolean contains(Object obj) {
+ if (!(obj instanceof Struct)) {
+ return false;
+ }
+ Struct s = (Struct) obj;
+ if (!this.elementType.equals(StructTypeImpl.typeFromStruct(s))) {
+ return false;
+ }
+ return containsFieldValues(s.getFieldValues());
+ }
+
+ public CollectionType getCollectionType() {
+ return new CollectionTypeImpl(SortedStructBag.class, this.elementType);
+ }
+
+ /**
+ * Does this set contain a Struct of the correct type with the specified
+ * values?
+ */
+ 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);
+ }
+ }
+
+ @Override
+ public int occurrences(Object element) {
+ if (!(element instanceof Struct)) {
+ return 0;
+ }
+ Struct s = (Struct) element;
+ if (!this.elementType.equals(StructTypeImpl.typeFromStruct(s))) {
+ return 0;
+ }
+ if (this.hasLimitIterator) {
+ int count = 0;
+ boolean encounteredObject = false;
+ Object[] fields = s.getFieldValues();
+ for (Iterator itr = this.fieldValuesIterator(); itr.hasNext();) {
+ Object[] structFields = (Object[]) itr.next();
+ if (Arrays.equals(fields, structFields)) {
+ count++;
+ encounteredObject = true;
+ } else if (encounteredObject) {
+ // Asif: No possibility of its occurence again
+ break;
+ }
+ }
+ return count;
+ } else {
+ return this.mapGet(s.getFieldValues()); // returns 0 if not found
+ }
+ }
+
+ /** Remove the specified Struct */
+ @Override
+ public boolean remove(Object o) {
+ if (!(o instanceof Struct)) {
+ return false;
+ }
+ Struct s = (Struct) o;
+ if (!this.elementType.equals(StructTypeImpl.typeFromStruct(s))) {
+ return false;
+ }
+ return removeFieldValues(s.getFieldValues());
+ }
+
+ /** Remove the field values from a struct of the correct type */
+ public boolean removeFieldValues(Object[] fieldValues) {
+ if (this.hasLimitIterator) {
+ // Asif : Get the field value Iterator
+ Iterator fieldItr = this.fieldValuesIterator();
+ while (fieldItr.hasNext()) {
+ if (Arrays.equals((Object[]) fieldItr.next(), fieldValues)) {
+ fieldItr.remove();
+ return true;
+ }
+ }
+ return false;
+ } else {
+ return super.remove(fieldValues);
+ }
+ }
+
+ // downcast StructBags to call more efficient methods
+ @Override
+ public boolean addAll(Collection c) {
+ if (c instanceof StructFields) {
+ return addAll((StructFields) c);
+ }
+ return super.addAll(c);
+ }
+
+ @Override
+ public boolean removeAll(Collection c) {
+ if (c instanceof StructFields) {
+ return removeAll((StructFields) c);
+ }
+ return super.removeAll(c);
+ }
+
+ @Override
+ public boolean retainAll(Collection c) {
+ if (c instanceof StructFields) {
+ return retainAll((StructFields) c);
+ }
+ return super.retainAll(c);
+ }
+
+ public boolean addAll(StructFields sb) {
+ boolean modified = false;
+ if (!this.elementType.equals(sb.getCollectionType().getElementType())) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.StructBag_TYPES_DONT_MATCH.toLocalizedString());
+ }
+
+ for (Iterator itr = sb.fieldValuesIterator(); itr.hasNext();) {
+ // Check if query execution on this thread is canceled.
+ QueryMonitor.isQueryExecutionCanceled();
+
+ Object[] vals = (Object[]) itr.next();
+ if (super.add(vals)) {
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+ public boolean removeAll(StructFields ss) {
+ boolean modified = false;
+ if (!this.elementType.equals(ss.getCollectionType().getElementType())) {
+ return false; // nothing // modified
+ }
+ for (Iterator itr = ss.fieldValuesIterator(); itr.hasNext();) {
+ Object[] vals = (Object[]) itr.next();
+ if (this.removeFieldValues(vals)) {
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+ public boolean retainAll(StructFields ss) {
+ if (!this.elementType.equals(ss.getCollectionType().getElementType())) {
+ if (isEmpty()) {
+ return false; // nothing modified
+ } else {
+ clear();
+ return true; // nothing retained in receiver collection
+ }
+ }
+ boolean changed = false;
+ int size = size();
+ Iterator it;
+ it = fieldValuesIterator();
+ while (size-- > 0) {
+ Object[] vals = (Object[]) it.next();
+ if (!ss.containsFieldValues(vals)) {
+ it.remove();
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+ /**
+ * Return an iterator over the elements in this collection. Duplicates will
+ * show up the number of times it has occurrances.
+ */
+ @Override
+ public Iterator iterator() {
+ return new StructBagIterator(fieldValuesIterator());
+ }
+
+ /** Returns an iterator over the fieldValues Object[] instances */
+ public Iterator fieldValuesIterator() {
+ return super.iterator();
+ }
+
+ // note: this method is dangerous in that it could result in undefined
+ // behavior if the new struct type is not compatible with the data.
+ // For now just trust that the application knows what it is doing if it
+ // is overriding the element type in a set of structs
+ @Override
+ public void setElementType(ObjectType elementType) {
+ if (!(elementType instanceof StructTypeImpl)) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.StructBag_ELEMENT_TYPE_MUST_BE_STRUCT
+ .toLocalizedString());
+ }
+ this.elementType = elementType;
+ }
+
+ @Override
+ public Set asSet() {
+ return new HashSet(this);
+ }
+
+ /**
+ * Getter for property modifiable.
+ *
+ * @return Value of property modifiable.
+ */
+ @Override
+ public boolean isModifiable() {
+ return false;
+ }
+
+ /**
+ * Iterator wrapper to construct Structs on demand.
+ */
+ private class StructBagIterator extends BagIterator {
+
+ private final Iterator itr;
+
+ /**
+ * @param itr
+ * iterator over the Object[] instances of fieldValues
+ */
+ StructBagIterator(Iterator itr) {
+ this.itr = itr;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return this.itr.hasNext();
+ }
+
+ @Override
+ public Object next() {
+ return new StructImpl((StructTypeImpl) SortedStructBag.this.elementType,
+ (Object[]) this.itr.next());
+ }
+
+ @Override
+ public void remove() {
+ this.itr.remove();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructSet.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructSet.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructSet.java
index f26de49..1530a7e 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructSet.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/SortedStructSet.java
@@ -28,310 +28,349 @@ import com.gemstone.gemfire.internal.Version;
* @author Yogesh Mahajan
* @since 4.0
*/
-public final class SortedStructSet extends TreeSet implements SelectResults, DataSerializableFixedID {
- private static final long serialVersionUID = -1687142950781718159L;
-
- protected StructTypeImpl structType;
- /**
- * Holds value of property modifiable.
- */
- private boolean modifiable = true;
-
- /** Creates a new instance of StructSet */
- public SortedStructSet() {};
-
- /** Creates a new instance of StructSet */
- private SortedStructSet(Comparator c){
- super(c);
- }
-
- /** Creates a new instance of StructSet */
- public SortedStructSet(Comparator c ,StructTypeImpl structType) {
- this(c);
- if (structType == null) { throw new IllegalArgumentException(LocalizedStrings.SortedStructSet_STRUCTTYPE_MUST_NOT_BE_NULL.toLocalizedString()); }
- this.structType = structType;
- }
-
- /** Creates a new instance of StructSet */
- public SortedStructSet(StructTypeImpl structType) {
- if (structType == null) { throw new IllegalArgumentException(LocalizedStrings.SortedStructSet_STRUCTTYPE_MUST_NOT_BE_NULL.toLocalizedString()); }
- this.structType = structType;
- }
-
-
- @Override
- public boolean equals(Object other) {
- if (!(other instanceof SortedStructSet)) {
- return false;
+public final class SortedStructSet extends TreeSet implements SelectResults,
+ DataSerializableFixedID, Ordered, StructFields {
+ private static final long serialVersionUID = -1687142950781718159L;
+
+ protected StructTypeImpl structType;
+ /**
+ * Holds value of property modifiable.
+ */
+ private boolean modifiable = true;
+
+ /** Creates a new instance of StructSet */
+ public SortedStructSet() {
+ };
+
+ /** Creates a new instance of StructSet */
+ private SortedStructSet(Comparator c) {
+ super(c);
+ }
+
+ /** Creates a new instance of StructSet */
+ public SortedStructSet(Comparator c, StructTypeImpl structType) {
+ this(c);
+ if (structType == null) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.SortedStructSet_STRUCTTYPE_MUST_NOT_BE_NULL
+ .toLocalizedString());
+ }
+ this.structType = structType;
+ }
+
+ /** Creates a new instance of StructSet */
+ public SortedStructSet(StructTypeImpl structType) {
+ if (structType == null) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.SortedStructSet_STRUCTTYPE_MUST_NOT_BE_NULL
+ .toLocalizedString());
+ }
+ this.structType = structType;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof SortedStructSet)) {
+ return false;
+ }
+ if (!this.structType.equals(((SortedStructSet) other).structType)) {
+ return false;
+ }
+ return super.equals(other);
+ }
+
+ /** Add a Struct */
+ @Override
+ public boolean add(Object obj) {
+ if (!(obj instanceof StructImpl)) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.SortedStructSet_THIS_SET_ONLY_ACCEPTS_STRUCTIMPL
+ .toLocalizedString());
+ }
+ StructImpl s = (StructImpl) obj;
+ if (!s.getStructType().equals(this.structType)) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.SortedStructSet_OBJ_DOES_NOT_HAVE_THE_SAME_STRUCTTYPE
+ .toLocalizedString());
+ }
+ // return addFieldValues(s.getFieldValues());
+ return this.addFieldValues(s.getFieldValues());
+ }
+
+ /**
+ * For internal use. Just add the Object[] values for a struct with same type
+ */
+ public boolean addFieldValues(Object[] fieldValues) {
+ return super.add(fieldValues);
+ }
+
+ /**
+ * For internal use. Just add the Object[] values for a struct with same type
+ *
+ * public boolean addFieldValues(Object[] fieldValues) { //return
+ * super.add(fieldValues); StructImpl s = new StructImpl(this.structType,
+ * fieldValues); return super.add(s); }
+ */
+
+ /** Does this set contain specified struct? */
+ @Override
+ public boolean contains(Object obj) {
+ if (!(obj instanceof Struct)) {
+ return false;
+ }
+ Struct s = (Struct) obj;
+ if (!this.structType.equals(StructTypeImpl.typeFromStruct(s))) {
+ return false;
+ }
+ return containsFieldValues(s.getFieldValues());
+ }
+
+ /**
+ * Does this set contain a Struct of the correct type with the specified
+ * values?
+ */
+ public boolean containsFieldValues(Object[] fieldValues) {
+ return super.contains(fieldValues);
+ }
+
+ /**
+ * Does this set contain a Struct of the correct type with the specified
+ * values?
+ *
+ * public boolean containsFieldValues(Object[] fieldValues) { return
+ * super.contains(fieldValues); }
+ */
+
+ /** Remove the specified Struct */
+ @Override
+ public boolean remove(Object o) {
+ if (!(o instanceof Struct)) {
+ return false;
+ }
+ Struct s = (Struct) o;
+ if (!this.structType.equals(StructTypeImpl.typeFromStruct(s))) {
+ return false;
+ }
+ return removeFieldValues(s.getFieldValues());
+ }
+
+ /** Remove the field values from a struct of the correct type */
+ public boolean removeFieldValues(Object[] fieldValues) {
+ return super.remove(fieldValues);
+
+ }
+
+ // downcast StructSets to call more efficient methods
+ @Override
+ public boolean addAll(Collection c) {
+ if (c instanceof StructSet) {
+ return addAll((StructSet) c);
+ }
+ return super.addAll(c);
+ }
+
+ @Override
+ public boolean removeAll(Collection c) {
+ if (c instanceof StructSet) {
+ return removeAll((StructSet) c);
+ }
+ return super.removeAll(c);
+ }
+
+ @Override
+ public boolean retainAll(Collection c) {
+ if (c instanceof StructSet) {
+ return retainAll((StructSet) c);
+ }
+ return super.retainAll(c);
+ }
+
+ public boolean addAll(StructSet ss) {
+ boolean modified = false;
+ if (!this.structType.equals(ss.structType)) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.SortedStructSet_TYPES_DONT_MATCH.toLocalizedString());
+ }
+ for (Iterator itr = ss.fieldValuesIterator(); itr.hasNext();) {
+ if (this.addFieldValues((Object[]) itr.next())) {
+ modified = true;
}
- if (!this.structType.equals(((SortedStructSet)other).structType)) {
- return false;
+ }
+ return modified;
+ }
+
+ public boolean removeAll(StructSet ss) {
+ boolean modified = false;
+ if (!this.structType.equals(ss.structType)) {
+ return false; // nothing
+ // modified
+ }
+ for (Iterator itr = ss.fieldValuesIterator(); itr.hasNext();) {
+ Object[] fieldValues = (Object[]) itr.next();
+ if (this.removeFieldValues(fieldValues)) {
+ modified = true;
}
- return super.equals(other);
}
-
-
- /** Add a Struct */
- @Override
- public boolean add(Object obj) {
- if (!(obj instanceof StructImpl)) { throw new IllegalArgumentException(LocalizedStrings.SortedStructSet_THIS_SET_ONLY_ACCEPTS_STRUCTIMPL.toLocalizedString()); }
- StructImpl s = (StructImpl) obj;
- if (!s.getStructType().equals(this.structType)) { throw new IllegalArgumentException(LocalizedStrings.SortedStructSet_OBJ_DOES_NOT_HAVE_THE_SAME_STRUCTTYPE.toLocalizedString()); }
- //return addFieldValues(s.getFieldValues());
- return this.addFieldValues(s.getFieldValues());
- }
- /**
- * For internal use. Just add the Object[] values for a struct with same type
- */
- public boolean addFieldValues(Object[] fieldValues) {
- return super.add(fieldValues);
- }
- /**
- * For internal use. Just add the Object[] values for a struct with same type
-
- public boolean addFieldValues(Object[] fieldValues) {
- //return super.add(fieldValues);
- StructImpl s = new StructImpl(this.structType, fieldValues);
- return super.add(s);
- }*/
-
-
-
- /** Does this set contain specified struct? */
- @Override
- public boolean contains(Object obj) {
- if (!(obj instanceof Struct)) { return false; }
- Struct s = (Struct) obj;
- if (!this.structType.equals(StructTypeImpl.typeFromStruct(s))) { return false; }
- return containsFieldValues(s.getFieldValues());
- }
-
- /**
- * Does this set contain a Struct of the correct type with the specified
- * values?
- */
- public boolean containsFieldValues(Object[] fieldValues) {
- return super.contains(fieldValues);
- }
-
- /**
- * Does this set contain a Struct of the correct type with the specified
- * values?
-
- public boolean containsFieldValues(Object[] fieldValues) {
- return super.contains(fieldValues);
- }*/
-
-
-
- /** Remove the specified Struct */
- @Override
- public boolean remove(Object o) {
- if (!(o instanceof Struct)) { return false; }
- Struct s = (Struct) o;
- if (!this.structType.equals(StructTypeImpl.typeFromStruct(s))) { return false; }
- return removeFieldValues(s.getFieldValues());
- }
-
- /** Remove the field values from a struct of the correct type */
- public boolean removeFieldValues(Object[] fieldValues) {
- return super.remove(fieldValues);
-
- }
-
-
- // downcast StructSets to call more efficient methods
- @Override
- public boolean addAll(Collection c) {
- if (c instanceof StructSet) { return addAll((StructSet) c); }
- return super.addAll(c);
- }
-
- @Override
- public boolean removeAll(Collection c) {
- if (c instanceof StructSet) { return removeAll((StructSet) c); }
- return super.removeAll(c);
- }
-
- @Override
- public boolean retainAll(Collection c) {
- if (c instanceof StructSet) { return retainAll((StructSet) c); }
- return super.retainAll(c);
- }
-
- public boolean addAll(StructSet ss) {
- boolean modified = false;
- if (!this.structType.equals(ss.structType)) { throw new IllegalArgumentException(LocalizedStrings.SortedStructSet_TYPES_DONT_MATCH.toLocalizedString()); }
- for (Iterator itr = ss.fieldValuesIterator(); itr.hasNext();) {
- if (this.addFieldValues((Object[])itr.next())) {
- modified = true;
- }
- }
- return modified;
- }
-
- public boolean removeAll(StructSet ss) {
- boolean modified = false;
- if (!this.structType.equals(ss.structType)) { return false; // nothing
- // modified
- }
- for (Iterator itr = ss.fieldValuesIterator(); itr.hasNext();) {
- Object[] fieldValues = (Object[]) itr.next();
- if (this.removeFieldValues(fieldValues)) {
- modified = true;
- }
- }
- return modified;
- }
-
- public boolean retainAll(StructSet ss) {
- if (!this.structType.equals(ss.structType)) {
- if (isEmpty()) {
- return false; // nothing modified
- }
- else {
- clear();
- return true; // nothing retained in receiver collection
- }
- }
- boolean changed = false;
- int size = size();
- Iterator it;
- it = fieldValuesIterator();
- while (size-- > 0) {
- Object[] val = (Object[]) it.next();
- //if (!ss.containsFieldValues(vals)) {
- if (!ss.containsFieldValues(val)) {
- it.remove();
- changed = true;
- }
- }
- return changed;
- }
-
- /** Returns an Iterator over the Structs in this set */
- @Override
- public Iterator iterator() {
- return new StructIterator(fieldValuesIterator());
- }
- /** Returns an iterator over the fieldValues Object[] instances */
- public Iterator fieldValuesIterator() {
- return super.iterator();
- }
-
- public CollectionType getCollectionType() {
- return new CollectionTypeImpl(TreeSet.class, this.structType);
- }
-
- // note: this method is dangerous in that it could result in undefined
- // behavior if the new struct type is not compatible with the data.
- // For now just trust that the application knows what it is doing if it
- // is overriding the element type in a set of structs
- public void setElementType(ObjectType elementType) {
- if (!(elementType instanceof StructTypeImpl)) { throw new IllegalArgumentException(LocalizedStrings.SortedStructSet_ELEMENT_TYPE_MUST_BE_STRUCT.toLocalizedString()); }
- this.structType = (StructTypeImpl) elementType;
- }
-
- public List asList() {
- return new ArrayList(this);
- }
-
- public Set asSet() {
- return this;
- }
-
- /**
- * Getter for property modifiable.
- *
- * @return Value of property modifiable.
- */
- public boolean isModifiable() {
- return this.modifiable;
- }
-
- public int occurrences(Object element) {
- return contains(element) ? 1 : 0;
+ return modified;
+ }
+
+ public boolean retainAll(StructSet ss) {
+ if (!this.structType.equals(ss.structType)) {
+ if (isEmpty()) {
+ return false; // nothing modified
+ } else {
+ clear();
+ return true; // nothing retained in receiver collection
+ }
+ }
+ boolean changed = false;
+ int size = size();
+ Iterator it;
+ it = fieldValuesIterator();
+ while (size-- > 0) {
+ Object[] val = (Object[]) it.next();
+ // if (!ss.containsFieldValues(vals)) {
+ if (!ss.containsFieldValues(val)) {
+ it.remove();
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+ /** Returns an Iterator over the Structs in this set */
+ @Override
+ public Iterator iterator() {
+ return new StructIterator(fieldValuesIterator());
+ }
+
+ /** Returns an iterator over the fieldValues Object[] instances */
+ public Iterator fieldValuesIterator() {
+ return super.iterator();
+ }
+
+ public CollectionType getCollectionType() {
+ return new CollectionTypeImpl(SortedStructSet.class, this.structType);
+ }
+
+ // note: this method is dangerous in that it could result in undefined
+ // behavior if the new struct type is not compatible with the data.
+ // For now just trust that the application knows what it is doing if it
+ // is overriding the element type in a set of structs
+ public void setElementType(ObjectType elementType) {
+ if (!(elementType instanceof StructTypeImpl)) {
+ throw new IllegalArgumentException(
+ LocalizedStrings.SortedStructSet_ELEMENT_TYPE_MUST_BE_STRUCT
+ .toLocalizedString());
+ }
+ this.structType = (StructTypeImpl) elementType;
+ }
+
+ public List asList() {
+ return new ArrayList(this);
+ }
+
+ public Set asSet() {
+ return this;
+ }
+
+ /**
+ * Getter for property modifiable.
+ *
+ * @return Value of property modifiable.
+ */
+ public boolean isModifiable() {
+ return this.modifiable;
+ }
+
+ public int occurrences(Object element) {
+ return contains(element) ? 1 : 0;
+ }
+
+ /**
+ * Setter for property modifiable.
+ *
+ * @param modifiable
+ * New value of property modifiable.
+ */
+ public void setModifiable(boolean modifiable) {
+ this.modifiable = modifiable;
+ }
+
+ @Override
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append("[");
+ Iterator i = iterator();
+ boolean hasNext = i.hasNext();
+ while (hasNext) {
+ Object o = i.next();
+ buf.append(o == this ? "(this Collection)" : String.valueOf(o));
+ hasNext = i.hasNext();
+ if (hasNext)
+ buf.append(",\n ");
}
+ buf.append("]");
+ return buf.toString();
+ }
- /**
- * Setter for property modifiable.
- *
- * @param modifiable New value of property modifiable.
- */
- public void setModifiable(boolean modifiable) {
- this.modifiable = modifiable;
- }
-
- @Override
- public String toString() {
- StringBuffer buf = new StringBuffer();
- buf.append("[");
- Iterator i = iterator();
- boolean hasNext = i.hasNext();
- while (hasNext) {
- Object o = i.next();
- buf.append(o == this ? "(this Collection)" : String.valueOf(o));
- hasNext = i.hasNext();
- if (hasNext) buf.append(",\n ");
- }
- buf.append("]");
- return buf.toString();
- }
-
- /**
- * Iterator wrapper to construct Structs on demand.
- */
- private class StructIterator implements Iterator {
-
- private final Iterator itr;
-
- StructIterator(Iterator itr) {
- this.itr = itr;
- }
-
- public boolean hasNext() {
- return this.itr.hasNext();
- }
-
- public Object next() {
- return new StructImpl((StructTypeImpl)SortedStructSet.this.structType, (Object[]) this.itr.next());
- }
-
-
- public void remove() {
- this.itr.remove();
- }
- }
-
- public int getDSFID() {
- return SORTED_STRUCT_SET;
+ /**
+ * Iterator wrapper to construct Structs on demand.
+ */
+ private class StructIterator implements Iterator {
+
+ private final Iterator itr;
+
+ StructIterator(Iterator itr) {
+ this.itr = itr;
}
- public void fromData(DataInput in) throws IOException, ClassNotFoundException
- {
- this.modifiable = in.readBoolean();
- int size = in.readInt();
- this.structType = (StructTypeImpl)DataSerializer.readObject(in);
- for (int j = size; j > 0; j--) {
- Object[] fieldValues = DataSerializer.readObject(in);
- this.addFieldValues( fieldValues);
- }
+ public boolean hasNext() {
+ return this.itr.hasNext();
}
- public void toData(DataOutput out) throws IOException
- {
- // how do we serialize the comparator?
- out.writeBoolean(this.modifiable);
- out.writeInt(this.size());
- DataSerializer.writeObject(this.structType, out);
- for (Iterator i = this.fieldValuesIterator(); i.hasNext();) {
- Object[] fieldValues = (Object[])i.next();
- DataSerializer.writeObjectArray(fieldValues, out);
- }
+ public Object next() {
+ return new StructImpl((StructTypeImpl) SortedStructSet.this.structType,
+ (Object[]) this.itr.next());
}
- @Override
- public Version[] getSerializationVersions() {
- return null;
+ public void remove() {
+ this.itr.remove();
}
+ }
+
+ public int getDSFID() {
+ return SORTED_STRUCT_SET;
+ }
+
+ public void fromData(DataInput in) throws IOException, ClassNotFoundException {
+ this.modifiable = in.readBoolean();
+ int size = in.readInt();
+ this.structType = (StructTypeImpl) DataSerializer.readObject(in);
+ for (int j = size; j > 0; j--) {
+ Object[] fieldValues = DataSerializer.readObject(in);
+ this.addFieldValues(fieldValues);
+ }
+ }
+
+ public void toData(DataOutput out) throws IOException {
+ // how do we serialize the comparator?
+ out.writeBoolean(this.modifiable);
+ out.writeInt(this.size());
+ DataSerializer.writeObject(this.structType, out);
+ for (Iterator i = this.fieldValuesIterator(); i.hasNext();) {
+ Object[] fieldValues = (Object[]) i.next();
+ DataSerializer.writeObjectArray(fieldValues, out);
+ }
+ }
+
+ @Override
+ public Version[] getSerializationVersions() {
+ return null;
+ }
+
+ @Override
+ public boolean dataPreordered() {
+ return false;
+ }
}