Search in sources :

Example 1 with PrimaryKeyIndex

use of org.apache.geode.cache.query.internal.index.PrimaryKeyIndex in project geode by apache.

the class MultiIndexCreationJUnitTest method testBasicMultiIndexCreationDifferentTypes.

@Test
public void testBasicMultiIndexCreationDifferentTypes() throws Exception {
    Region r = CacheUtils.getRegion(regionName);
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("statusIndex", "status", r.getFullPath());
    qs.defineHashIndex("IDIndex", "ID", r.getFullPath());
    qs.defineKeyIndex("keyIDIndex", "ID", r.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(r, "statusIndex");
    assertTrue(ind instanceof CompactRangeIndex);
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "IDIndex");
    assertTrue(ind instanceof HashIndex);
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "keyIDIndex");
    assertTrue(ind instanceof PrimaryKeyIndex);
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        assertEquals(5, sr.size());
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with PrimaryKeyIndex

use of org.apache.geode.cache.query.internal.index.PrimaryKeyIndex in project geode by apache.

the class PdxStringQueryJUnitTest method testQueriesWithPrimaryKeyIndex.

@Test
public void testQueriesWithPrimaryKeyIndex() throws Exception {
    Index index = qs.createKeyIndex("index3", "secId", "/exampleRegion");
    assertTrue(index instanceof PrimaryKeyIndex);
    putPdxInstances();
    executeQueriesValidateResults(INDEX_TYPE_PRIMARYKEY);
    r.clear();
    putHeterogeneousObjects();
    executeQueriesValidateResults(INDEX_TYPE_PRIMARYKEY);
    qs.removeIndex(index);
    r.clear();
}
Also used : CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 3 with PrimaryKeyIndex

use of org.apache.geode.cache.query.internal.index.PrimaryKeyIndex in project geode by apache.

the class CacheXmlGenerator method generate.

/**
   * Generates XML for an index
   */
private void generate(Index index) throws SAXException {
    if (index == null) {
        return;
    }
    AttributesImpl atts = new AttributesImpl();
    if (index instanceof IndexCreationData) {
        IndexCreationData indexData = (IndexCreationData) index;
        atts.addAttribute("", "", NAME, "", indexData.getIndexName());
        String indexType = indexData.getIndexType();
        if (indexType.equals("KEY")) {
            atts.addAttribute("", "", KEY_INDEX, "", "true");
        } else {
            // convert the indexType to the xml indexType
            if (indexType.equals("HASH")) {
                indexType = HASH_INDEX_TYPE;
            } else {
                indexType = RANGE_INDEX_TYPE;
            }
            atts.addAttribute("", "", KEY_INDEX, "", "false");
            atts.addAttribute("", "", INDEX_TYPE, "", "" + indexType);
        }
        atts.addAttribute("", "", FROM_CLAUSE, "", indexData.getIndexFromClause());
        atts.addAttribute("", "", EXPRESSION, "", indexData.getIndexExpression());
    } else {
        atts.addAttribute("", "", NAME, "", index.getName());
        if (index instanceof PrimaryKeyIndex) {
            atts.addAttribute("", "", KEY_INDEX, "", "true");
        } else {
            atts.addAttribute("", "", KEY_INDEX, "", "false");
            String indexType = "range";
            if (index instanceof HashIndex) {
                indexType = "hash";
            }
            atts.addAttribute("", "", INDEX_TYPE, "", "" + indexType);
        }
        atts.addAttribute("", "", FROM_CLAUSE, "", index.getFromClause());
        atts.addAttribute("", "", EXPRESSION, "", index.getIndexedExpression());
    }
    handler.startElement("", INDEX, INDEX, atts);
    handler.endElement("", INDEX, INDEX);
}
Also used : DiskWriteAttributesImpl(org.apache.geode.internal.cache.DiskWriteAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) AttributesImpl(org.xml.sax.helpers.AttributesImpl) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex)

Aggregations

PrimaryKeyIndex (org.apache.geode.cache.query.internal.index.PrimaryKeyIndex)3 CompactRangeIndex (org.apache.geode.cache.query.internal.index.CompactRangeIndex)2 HashIndex (org.apache.geode.cache.query.internal.index.HashIndex)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 Region (org.apache.geode.cache.Region)1 Index (org.apache.geode.cache.query.Index)1 QueryService (org.apache.geode.cache.query.QueryService)1 SelectResults (org.apache.geode.cache.query.SelectResults)1 Portfolio (org.apache.geode.cache.query.data.Portfolio)1 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)1 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)1 RangeIndex (org.apache.geode.cache.query.internal.index.RangeIndex)1 DiskWriteAttributesImpl (org.apache.geode.internal.cache.DiskWriteAttributesImpl)1 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1