Search in sources :

Example 1 with StructImpl

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);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) StructImpl(org.apache.geode.cache.query.internal.StructImpl) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with StructImpl

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;
}
Also used : StructImpl(org.apache.geode.cache.query.internal.StructImpl) PdxInstance(org.apache.geode.pdx.PdxInstance) CacheException(org.apache.geode.cache.CacheException) PdxString(org.apache.geode.pdx.internal.PdxString) VMCachedDeserializable(org.apache.geode.internal.cache.VMCachedDeserializable) CacheException(org.apache.geode.cache.CacheException)

Example 3 with StructImpl

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;
}
Also used : StructImpl(org.apache.geode.cache.query.internal.StructImpl) PdxInstance(org.apache.geode.pdx.PdxInstance) InternalCache(org.apache.geode.internal.cache.InternalCache)

Example 4 with StructImpl

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);
                }
            }
        }
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) StructImpl(org.apache.geode.cache.query.internal.StructImpl) StructFields(org.apache.geode.cache.query.internal.StructFields)

Example 5 with 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);
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) StructImpl(org.apache.geode.cache.query.internal.StructImpl) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

StructImpl (org.apache.geode.cache.query.internal.StructImpl)17 Test (org.junit.Test)10 SelectResults (org.apache.geode.cache.query.SelectResults)8 Region (org.apache.geode.cache.Region)7 QueryService (org.apache.geode.cache.query.QueryService)7 ClientCache (org.apache.geode.cache.client.ClientCache)6 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)6 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)6 PositionPdx (org.apache.geode.cache.query.data.PositionPdx)6 Host (org.apache.geode.test.dunit.Host)6 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)6 VM (org.apache.geode.test.dunit.VM)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 Struct (org.apache.geode.cache.query.Struct)5 PdxInstance (org.apache.geode.pdx.PdxInstance)5 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)4 StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)4 ArrayList (java.util.ArrayList)3 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)3 Collection (java.util.Collection)2