Search in sources :

Example 76 with Struct

use of org.apache.geode.cache.query.Struct in project geode by apache.

the class PDXQueryTestBase method printResults.

public void printResults(SelectResults results, String message) {
    Object r;
    Struct s;
    LogWriterI18n logger = GemFireCacheImpl.getInstance().getLoggerI18n();
    logger.fine(message);
    int row = 0;
    for (Iterator iter = results.iterator(); iter.hasNext(); ) {
        r = iter.next();
        row++;
        if (r instanceof Struct) {
            s = (Struct) r;
            String[] fieldNames = ((Struct) r).getStructType().getFieldNames();
            for (int i = 0; i < fieldNames.length; i++) {
                logger.fine("### Row " + row + "\n" + "Field: " + fieldNames[i] + " > " + s.get(fieldNames[i]).toString());
            }
        } else {
            logger.fine("#### Row " + row + "\n" + r);
        }
    }
}
Also used : Iterator(java.util.Iterator) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) Struct(org.apache.geode.cache.query.Struct)

Example 77 with Struct

use of org.apache.geode.cache.query.Struct in project geode by apache.

the class IndexWithSngleFrmAndMultCondQryJUnitTest method testNoIndexSkipping.

@Test
public void testNoIndexSkipping() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 400; i++) {
        region.put("" + i, new Portfolio(i));
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /pos pf,  positions.values pos where pf.ID > 0 and pf.ID < 250  and pf.status='active' and  pos.secId != null " };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            sr[i][0] = (SelectResults) q.execute();
            if (!observer.isIndexesUsed) {
                CacheUtils.log("NO INDEX USED");
            } else {
                fail("How could index be present when not created!?");
            }
            // CacheUtils.log(Utils.printResult(r));
            resType1 = (StructType) ((SelectResults) sr[i][0]).getCollectionType().getElementType();
            resSize1 = (((SelectResults) sr[i][0]).size());
            CacheUtils.log(resType1);
            strg1 = resType1.getFieldNames();
            set1 = (((SelectResults) sr[i][0]).asSet());
            Iterator iter = set1.iterator();
            while (iter.hasNext()) {
                Struct stc1 = (Struct) iter.next();
                valPf1 = stc1.get(strg1[0]);
                valPos1 = stc1.get(strg1[1]);
                isActive1 = ((Portfolio) stc1.get(strg1[0])).isActive();
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    Index index1 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf");
    Index index2 = (Index) qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "pos.secId", "/pos pf, pf.positions.values pos");
    Index index3 = qs.createIndex("IDIndex", IndexType.FUNCTIONAL, "pf.ID", "/pos pf");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            sr[i][1] = (SelectResults) q.execute();
            if (!observer2.isIndexesUsed) {
                fail("FAILED: Index NOT Used");
            }
            assertEquals(observer2.indexesUsed.size(), 1);
            resType2 = (StructType) ((SelectResults) sr[i][1]).getCollectionType().getElementType();
            resSize2 = (((SelectResults) sr[i][1]).size());
            CacheUtils.log(resType2);
            strg2 = resType2.getFieldNames();
            set2 = (((SelectResults) sr[i][1]).asSet());
            Iterator iter = set2.iterator();
            while (iter.hasNext()) {
                Struct stc2 = (Struct) iter.next();
                valPf2 = stc2.get(strg2[0]);
                valPos2 = stc2.get(strg2[1]);
                isActive2 = ((Portfolio) stc2.get(strg2[0])).isActive();
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    if ((resType1).equals(resType2)) {
        CacheUtils.log("Both Search Results are of the same Type i.e.--> " + resType1);
    } else {
        fail("FAILED:Search result Type is different in both the cases");
    }
    if (resSize1 == resSize2 || resSize1 != 0) {
        CacheUtils.log("Search Results size is Non Zero and equal in both cases i.e.  Size= " + resSize1);
    } else {
        fail("FAILED:Search result size is different in both the cases");
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 78 with Struct

use of org.apache.geode.cache.query.Struct in project geode by apache.

the class IndexWithSngleFrmAndMultCondQryJUnitTest method testNonDistinctOrCondResults.

@Test
public void testNonDistinctOrCondResults() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 10; i++) {
        region.put("" + i, new Portfolio(i));
    }
    for (int i = 10; i < 20; i++) {
        Portfolio p = new Portfolio(i);
        if (i % 2 == 0) {
            p.status = null;
        }
        region.put("" + i, p);
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT * FROM /pos pf,  positions.values pos where pf.ID > 0 OR pf.status='active' OR  pos.secId != 'IBM'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status='active'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status LIKE 'act%'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status IN SET('active', 'inactive')" };
    SelectResults[] sr = new SelectResults[queries.length];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            sr[i] = (SelectResults) q.execute();
            if (observer.isIndexesUsed) {
                fail("How could index be present when not created!?");
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Verify Results
    for (int i = 0; i < sr.length; i++) {
        Set resultSet = sr[i].asSet();
        // Check Element Type
        if (queries[i].contains("values")) {
            assertTrue("Checking Element type for Query: [" + queries[i] + "] results", sr[i].getCollectionType().getElementType().toString().equals("struct<pf:org.apache.geode.cache.query.data.Portfolio,pos:java.lang.Object>"));
            // Check Size of results
            assertEquals("Checking Element type for Query: [" + queries[i] + "] results", 40, resultSet.size());
        } else {
            assertTrue("Checking Element type for Query: [" + queries[i] + "] results", sr[i].getCollectionType().getElementType().toString().equals("org.apache.geode.cache.query.data.Portfolio"));
            // Check Size of results
            assertEquals("Checking Element type for Query: [" + queries[i] + "] results", 20, resultSet.size());
        }
        Iterator itr = resultSet.iterator();
        while (itr.hasNext()) {
            Object obj = itr.next();
            if (sr[i].getCollectionType().getElementType().toString().equals("struct<pf:org.apache.geode.cache.query.data.Portfolio,pos:java.lang.Object>")) {
                Object[] values = ((Struct) obj).getFieldValues();
                Portfolio port = (Portfolio) values[0];
                Position pos = (Position) values[1];
                if (!(port.getID() > 0 || port.status.equals("active") || pos.secId.equals("IBM"))) {
                    fail("Result object" + obj + " failed to satisfy all OR conditions of where clause of query " + queries[i]);
                }
            } else {
                Portfolio port = (Portfolio) obj;
                if (!(port.getID() > 0 || port.status.equals("active"))) {
                    fail("Result object" + port + " failed to satisfy all OR conditions of where clause of query " + queries[i]);
                }
            }
        }
    }
}
Also used : Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 79 with Struct

use of org.apache.geode.cache.query.Struct in project geode by apache.

the class NWayMergeResultsJUnitTest method testDistinctStruct.

@Test
public void testDistinctStruct() throws Exception {
    final int numSortedLists = 40;
    StructTypeImpl structType = new StructTypeImpl(new String[] { "a", "b" }, new ObjectType[] { new ObjectTypeImpl(Integer.TYPE), new ObjectTypeImpl(Integer.TYPE) });
    Collection<List<Struct>> listOfSortedLists = new ArrayList<List<Struct>>();
    for (int i = 0; i < numSortedLists; ++i) {
        listOfSortedLists.add(new ArrayList<Struct>());
    }
    int step = 0;
    for (List<Struct> list : listOfSortedLists) {
        step = step + 1;
        int j = 1000;
        for (int i = -500; i < 500; i = i + step) {
            Struct struct = new StructImpl(structType, new Object[] { Integer.valueOf(i), Integer.valueOf(j - step) });
            list.add(struct);
        }
    }
    SortedSet<Struct> sortedSet = new TreeSet<Struct>(new Comparator<Struct>() {

        @Override
        public int compare(Struct o1, Struct o2) {
            Object[] fields_1 = o1.getFieldValues();
            Object[] fields_2 = o2.getFieldValues();
            int compare = ((Comparable) fields_1[0]).compareTo((Comparable) fields_2[0]);
            if (compare == 0) {
                // second field is descending
                compare = ((Comparable) fields_2[1]).compareTo((Comparable) fields_1[1]);
            }
            return compare;
        }
    });
    int i = 0;
    for (List<Struct> list : listOfSortedLists) {
        for (Struct struct : list) {
            sortedSet.add(struct);
        }
    }
    NWayMergeResults<Struct> mergedResults = createStructFieldMergedResult(listOfSortedLists, true, -1, structType);
    Iterator<Struct> iter = mergedResults.iterator();
    for (Struct elem : sortedSet) {
        assertEquals(elem, iter.next());
    }
    assertFalse(iter.hasNext());
    try {
        iter.next();
        fail("next should have thrown NoSuchElementException");
    } catch (NoSuchElementException nsee) {
    // Ok.
    }
}
Also used : ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) TreeSet(java.util.TreeSet) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 80 with Struct

use of org.apache.geode.cache.query.Struct in project geode by apache.

the class NWayMergeResultsJUnitTest method testNonDistinctStruct.

@Test
public void testNonDistinctStruct() throws Exception {
    final int numSortedLists = 40;
    StructTypeImpl structType = new StructTypeImpl(new String[] { "a", "b" }, new ObjectType[] { new ObjectTypeImpl(Integer.TYPE), new ObjectTypeImpl(Integer.TYPE) });
    Collection<List<Struct>> listOfSortedLists = new ArrayList<List<Struct>>();
    for (int i = 0; i < numSortedLists; ++i) {
        listOfSortedLists.add(new ArrayList<Struct>());
    }
    int step = 0;
    for (List<Struct> list : listOfSortedLists) {
        step = step + 1;
        int j = 1000;
        for (int i = -500; i < 500; i = i + step) {
            Struct struct = new StructImpl(structType, new Object[] { Integer.valueOf(i), Integer.valueOf(j - step) });
            list.add(struct);
        }
    }
    int totalElements = 0;
    for (List<Struct> list : listOfSortedLists) {
        totalElements += list.size();
    }
    Struct[] combinedArray = new Struct[totalElements];
    int i = 0;
    for (List<Struct> list : listOfSortedLists) {
        for (Struct struct : list) {
            combinedArray[i++] = struct;
        }
    }
    Arrays.sort(combinedArray, new Comparator<Struct>() {

        @Override
        public int compare(Struct o1, Struct o2) {
            Object[] fields_1 = o1.getFieldValues();
            Object[] fields_2 = o2.getFieldValues();
            int compare = ((Comparable) fields_1[0]).compareTo((Comparable) fields_2[0]);
            if (compare == 0) {
                // second field is descending
                compare = ((Comparable) fields_2[1]).compareTo((Comparable) fields_1[1]);
            }
            return compare;
        }
    });
    NWayMergeResults<Struct> mergedResults = createStructFieldMergedResult(listOfSortedLists, false, -1, structType);
    Iterator<Struct> iter = mergedResults.iterator();
    for (Struct elem : combinedArray) {
        assertEquals(elem, iter.next());
    }
    assertFalse(iter.hasNext());
    try {
        iter.next();
        fail("next should have thrown NoSuchElementException");
    } catch (NoSuchElementException nsee) {
    // Ok.
    }
}
Also used : ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

Struct (org.apache.geode.cache.query.Struct)110 SelectResults (org.apache.geode.cache.query.SelectResults)80 QueryService (org.apache.geode.cache.query.QueryService)70 Region (org.apache.geode.cache.Region)67 Test (org.junit.Test)66 Query (org.apache.geode.cache.query.Query)57 Iterator (java.util.Iterator)54 Portfolio (org.apache.geode.cache.query.data.Portfolio)46 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)30 ObjectType (org.apache.geode.cache.query.types.ObjectType)30 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)27 StructType (org.apache.geode.cache.query.types.StructType)26 HashSet (java.util.HashSet)19 List (java.util.List)17 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)17 CacheException (org.apache.geode.cache.CacheException)16 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)16 ArrayList (java.util.ArrayList)15 Host (org.apache.geode.test.dunit.Host)14 VM (org.apache.geode.test.dunit.VM)14