use of org.apache.geode.cache.query.internal.StructImpl in project geode by apache.
the class NestedQueryJUnitTest method testNestedQueriesResultsAsStructSet.
@Test
public void testNestedQueriesResultsAsStructSet() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
String[] queries = { "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios ptf, positions pos)" + " WHERE pos.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios AS ptf, positions AS pos)" + " WHERE pos.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM ptf IN /Portfolios, pos IN positions)" + " WHERE pos.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT pos AS myPos FROM /Portfolios ptf, positions pos)" + " WHERE myPos.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios ptf, positions pos) p" + " WHERE p.pos.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios ptf, positions pos) p" + " WHERE pos.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios, positions) p" + " WHERE p.positions.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios, positions)" + " WHERE positions.value.secId = 'IBM'", "SELECT DISTINCT * FROM" + " (SELECT DISTINCT * FROM /Portfolios ptf, positions pos) p" + " WHERE p.get('pos').value.secId = 'IBM'" };
SelectResults[][] r = new SelectResults[queries.length][2];
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
// DebuggerSupport.waitForJavaDebugger(CacheUtils.getLogger());
r[i][0] = (SelectResults) q.execute();
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
resType1 = (r[i][0]).getCollectionType().getElementType();
resSize1 = ((r[i][0]).size());
set1 = ((r[i][0]).asSet());
// Iterator iter=set1.iterator();
} catch (Exception e) {
throw new AssertionError(e);
}
}
// Create an Index on status and execute the same query again.
qs = CacheUtils.getQueryService();
qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "b.secId", "/Portfolios pf, pf.positions.values b");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i][1] = (SelectResults) q.execute();
QueryObserverImpl observer2 = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer2);
resType2 = (r[i][1]).getCollectionType().getElementType();
resSize2 = ((r[i][1]).size());
set2 = ((r[i][1]).asSet());
} catch (Exception e) {
throw new AssertionError(q.getQueryString(), e);
}
}
for (int j = 0; j < queries.length; j++) {
if (((r[j][0]).getCollectionType().getElementType()).equals((r[j][1]).getCollectionType().getElementType())) {
CacheUtils.log("Both Search Results are of the same Type i.e.--> " + (r[j][0]).getCollectionType().getElementType());
} else {
fail("FAILED:Search result Type is different in both the cases");
}
if ((r[j][0]).size() == (r[j][1]).size()) {
CacheUtils.log("Both Search Results are of Same Size i.e. Size= " + (r[j][1]).size());
} else {
fail("FAILED:Search result Type is different in both the cases");
}
}
boolean pass = true;
itert1 = set1.iterator();
while (itert1.hasNext()) {
StructImpl p1 = (StructImpl) itert1.next();
itert2 = set2.iterator();
boolean found = false;
while (itert2.hasNext()) {
StructImpl p2 = (StructImpl) itert2.next();
if ((p1).equals(p2)) {
found = true;
}
}
if (!found)
pass = false;
}
if (!pass)
fail("Test failed");
CacheUtils.compareResultsOfWithAndWithoutIndex(r, this);
}
use of org.apache.geode.cache.query.internal.StructImpl in project geode by apache.
the class PDXUtils method convertPDX.
public static Object convertPDX(Object obj, boolean isStruct, boolean getDomainObjectForPdx, boolean getDeserializedObject, boolean localResults, boolean[] objectChangedMarker, boolean isDistinct) {
objectChangedMarker[0] = false;
if (isStruct) {
StructImpl simpl = (StructImpl) obj;
if (getDomainObjectForPdx) {
try {
if (simpl.isHasPdx()) {
obj = simpl.getPdxFieldValues();
objectChangedMarker[0] = true;
} else {
obj = simpl.getFieldValues();
}
} catch (Exception ex) {
throw new CacheException("Unable to retrieve domain object from PdxInstance while building the ResultSet. " + ex.getMessage()) {
};
}
} else {
Object[] values = simpl.getFieldValues();
if (getDeserializedObject) {
for (int i = 0; i < values.length; i++) {
if (values[i] instanceof VMCachedDeserializable) {
values[i] = ((VMCachedDeserializable) values[i]).getDeserializedForReading();
}
}
}
/* This is to convert PdxString to String */
if (simpl.isHasPdx() && isDistinct && localResults) {
for (int i = 0; i < values.length; i++) {
if (values[i] instanceof PdxString) {
values[i] = ((PdxString) values[i]).toString();
}
}
}
obj = values;
}
} else {
if (getDomainObjectForPdx) {
if (obj instanceof PdxInstance) {
try {
obj = ((PdxInstance) obj).getObject();
objectChangedMarker[0] = true;
} catch (Exception ex) {
throw new CacheException("Unable to retrieve domain object from PdxInstance while building the ResultSet. " + ex.getMessage()) {
};
}
} else if (obj instanceof PdxString) {
obj = ((PdxString) obj).toString();
}
} else if (isDistinct && localResults && obj instanceof PdxString) {
/* This is to convert PdxString to String */
obj = ((PdxString) obj).toString();
}
if (getDeserializedObject && obj instanceof VMCachedDeserializable) {
obj = ((VMCachedDeserializable) obj).getDeserializedForReading();
objectChangedMarker[0] = true;
}
}
return obj;
}
use of org.apache.geode.cache.query.internal.StructImpl in project geode by apache.
the class AbstractIndex method verifyAndGetPdxDomainObject.
// package-private to avoid synthetic accessor
Object verifyAndGetPdxDomainObject(Object value) {
if (value instanceof StructImpl) {
// Doing hasPdx check first, since its cheaper.
if (((StructImpl) value).isHasPdx() && !((InternalCache) this.region.getCache()).getPdxReadSerializedByAnyGemFireServices()) {
// Set the pdx values for the struct object.
StructImpl v = (StructImpl) value;
Object[] fieldValues = v.getPdxFieldValues();
return new StructImpl((StructTypeImpl) v.getStructType(), fieldValues);
}
} else if (value instanceof PdxInstance && !((InternalCache) this.region.getCache()).getPdxReadSerializedByAnyGemFireServices()) {
return ((PdxInstance) value).getObject();
}
return value;
}
use of org.apache.geode.cache.query.internal.StructImpl in project geode by apache.
the class AbstractIndex method addToStructsWithUnionOrIntersection.
private void addToStructsWithUnionOrIntersection(Collection results, SelectResults intermediateResults, boolean isIntersection, Object[] values) {
for (int i = 0; i < values.length; i++) {
values[i] = verifyAndGetPdxDomainObject(values[i]);
}
if (intermediateResults == null) {
if (results instanceof StructFields) {
((StructFields) results).addFieldValues(values);
} else {
// The results could be LinkedStructSet or SortedResultsBag or StructSet
SelectResults selectResults = (SelectResults) results;
StructImpl structImpl = new StructImpl((StructTypeImpl) selectResults.getCollectionType().getElementType(), values);
selectResults.add(structImpl);
}
} else {
if (isIntersection) {
if (results instanceof StructFields) {
int occurrences = intermediateResults.occurrences(values);
if (occurrences > 0) {
((StructFields) results).addFieldValues(values);
((StructFields) intermediateResults).removeFieldValues(values);
}
} else {
// could be LinkedStructSet or SortedResultsBag
SelectResults selectResults = (SelectResults) results;
StructImpl structImpl = new StructImpl((StructTypeImpl) selectResults.getCollectionType().getElementType(), values);
if (intermediateResults.remove(structImpl)) {
selectResults.add(structImpl);
}
}
} else {
if (results instanceof StructFields) {
((StructFields) results).addFieldValues(values);
} else {
// could be LinkedStructSet or SortedResultsBag
SelectResults selectResults = (SelectResults) results;
StructImpl structImpl = new StructImpl((StructTypeImpl) selectResults.getCollectionType().getElementType(), values);
if (intermediateResults.remove(structImpl)) {
selectResults.add(structImpl);
}
}
}
}
}
use of org.apache.geode.cache.query.internal.StructImpl in project geode by apache.
the class JavaSerializationJUnitTest method testStructImplSerialization.
@Test
public void testStructImplSerialization() throws Exception {
String[] fieldNames = { "col1", "col2" };
ObjectType[] fieldTypes = { new ObjectTypeImpl(Integer.class), new ObjectTypeImpl(String.class) };
StructTypeImpl type = new StructTypeImpl(fieldNames, fieldTypes);
Object[] values = { new Integer(123), new String("456") };
StructImpl si = new StructImpl(type, values);
verifyJavaSerialization(si);
}
Aggregations