Search in sources :

Example 36 with RegionEntry

use of org.apache.geode.internal.cache.RegionEntry in project geode by apache.

the class CompactMapRangeIndex method removeMapping.

protected void removeMapping(RegionEntry entry, int opCode) throws IMQException {
    // BEFORE_UPDATE_OP
    if (opCode == BEFORE_UPDATE_OP) {
        return;
    }
    // Object values = this.entryToMapKeysMap.remove(entry);
    Map mapKeyToIndexKey = this.entryToMapKeyIndexKeyMap.remove(entry);
    // contain any key which matches to index expression keys.
    if (mapKeyToIndexKey == null) {
        return;
    }
    Iterator<Map.Entry<?, ?>> mapKeyIterator = mapKeyToIndexKey.entrySet().iterator();
    while (mapKeyIterator.hasNext()) {
        Map.Entry<?, ?> mapEntry = mapKeyIterator.next();
        Object mapKey = mapEntry.getKey();
        Object indexKey = mapEntry.getValue();
        CompactRangeIndex ri = (CompactRangeIndex) this.mapKeyToValueIndex.get(mapKey);
        long start = System.nanoTime();
        this.internalIndexStats.incUpdatesInProgress(1);
        ri.removeMapping(indexKey, entry);
        this.internalIndexStats.incUpdatesInProgress(-1);
        long end = System.nanoTime() - start;
        this.internalIndexStats.incUpdateTime(end);
        this.internalIndexStats.incNumUpdates();
    }
}
Also used : RegionEntry(org.apache.geode.internal.cache.RegionEntry) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with RegionEntry

use of org.apache.geode.internal.cache.RegionEntry in project geode by apache.

the class CompactMapRangeIndex method removeOldMappings.

private void removeOldMappings(Collection presentKeys, RegionEntry entry) throws IMQException {
    Map oldKeysAndValuesForEntry = entryToMapKeyIndexKeyMap.get(entry);
    if (oldKeysAndValuesForEntry == null) {
        oldKeysAndValuesForEntry = Collections.EMPTY_MAP;
    }
    Set<Entry> removedKeyValueEntries = oldKeysAndValuesForEntry != null ? oldKeysAndValuesForEntry.entrySet() : Collections.EMPTY_SET;
    Iterator<Entry> iterator = removedKeyValueEntries.iterator();
    while (iterator.hasNext()) {
        Entry keyValue = iterator.next();
        Object indexKey = keyValue.getKey() == null ? IndexManager.NULL : keyValue.getKey();
        if (!presentKeys.contains(indexKey)) {
            CompactRangeIndex rg = (CompactRangeIndex) this.mapKeyToValueIndex.get(keyValue.getKey());
            rg.removeMapping(keyValue.getValue(), entry);
            iterator.remove();
        }
    }
}
Also used : RegionEntry(org.apache.geode.internal.cache.RegionEntry) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 38 with RegionEntry

use of org.apache.geode.internal.cache.RegionEntry in project geode by apache.

the class AbstractIndexMaintenanceIntegrationTest method whenRemovingRegionEntryFromIndexIfEntryDestroyedIsThrownCorrectlyRemoveFromIndexAndNotThrowException.

@Test
public void whenRemovingRegionEntryFromIndexIfEntryDestroyedIsThrownCorrectlyRemoveFromIndexAndNotThrowException() throws Exception {
    CacheUtils.startCache();
    Cache cache = CacheUtils.getCache();
    LocalRegion region = (LocalRegion) cache.createRegionFactory(RegionShortcut.REPLICATE).create("portfolios");
    QueryService qs = cache.getQueryService();
    AbstractIndex statusIndex = createIndex(qs, "statusIndex", "value.status", "/portfolios.entrySet()");
    PortfolioPdx p = new PortfolioPdx(1);
    region.put("KEY-1", p);
    RegionEntry entry = region.getRegionEntry("KEY-1");
    region.destroy("KEY-1");
    statusIndex.removeIndexMapping(entry, IndexProtocol.OTHER_OP);
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) RegionEntry(org.apache.geode.internal.cache.RegionEntry) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 39 with RegionEntry

use of org.apache.geode.internal.cache.RegionEntry in project geode by apache.

the class IndexManager method populateIndexes.

/**
   * populates all the indexes in the region
   */
public void populateIndexes(Collection<Index> indexSet) throws MultiIndexCreationException {
    waitBeforeUpdate();
    if (region.getCache().getLogger().infoEnabled()) {
        region.getCache().getLogger().info("Populating indexes for region " + region.getName());
    }
    boolean throwException = false;
    HashMap<String, Exception> exceptionsMap = new HashMap<String, Exception>();
    boolean oldReadSerialized = DefaultQuery.getPdxReadSerialized();
    DefaultQuery.setPdxReadSerialized(true);
    try {
        Iterator entryIter = ((LocalRegion) region).getBestIterator(true);
        while (entryIter.hasNext()) {
            RegionEntry entry = (RegionEntry) entryIter.next();
            if (entry == null || entry.isInvalidOrRemoved()) {
                continue;
            }
            // Fault in the value once before index update so that every index
            // update does not have
            // to read the value from disk every time.
            entry.getValue((LocalRegion) this.region);
            Iterator<Index> indexSetIterator = indexSet.iterator();
            while (indexSetIterator.hasNext()) {
                AbstractIndex index = (AbstractIndex) indexSetIterator.next();
                if (!index.isPopulated() && index.getType() != IndexType.PRIMARY_KEY) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Adding to index :{}{} value :{}", index.getName(), this.region.getFullPath(), entry.getKey());
                    }
                    long start = ((AbstractIndex) index).updateIndexUpdateStats();
                    try {
                        index.addIndexMapping(entry);
                    } catch (IMQException e) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Adding to index failed for: {}, {}", index.getName(), e.getMessage(), e);
                        }
                        exceptionsMap.put(index.indexName, e);
                        indexSetIterator.remove();
                        throwException = true;
                    }
                    ((AbstractIndex) index).updateIndexUpdateStats(start);
                }
            }
        }
        setPopulateFlagForIndexes(indexSet);
        if (throwException) {
            throw new MultiIndexCreationException(exceptionsMap);
        }
    } finally {
        DefaultQuery.setPdxReadSerialized(oldReadSerialized);
        notifyAfterUpdate();
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AmbiguousNameException(org.apache.geode.cache.query.AmbiguousNameException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) ExecutionException(java.util.concurrent.ExecutionException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Iterator(java.util.Iterator) RegionEntry(org.apache.geode.internal.cache.RegionEntry) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException)

Example 40 with RegionEntry

use of org.apache.geode.internal.cache.RegionEntry in project geode by apache.

the class IndexMaintenanceJUnitTest method testMapIndexRecreationForAllKeys.

// @Test
// public void testCompactMapIndexRecreationForAllKeys() throws Exception
// {
//
// QueryService qs;
// qs = CacheUtils.getQueryService();
// LocalRegion testRgn = (LocalRegion)CacheUtils.createRegion("testRgn", null);
// int ID = 1;
// // Add some test data now
// // Add 5 main objects. 1 will contain key1, 2 will contain key1 & key2
// // and so on
// for (; ID <= 5; ++ID) {
// MapKeyIndexData mkid = new MapKeyIndexData(ID);
// for (int j = 1; j <= ID; ++j) {
// mkid.maap.put("key" + j, "val" + j);
// }
// testRgn.put(ID, mkid);
// }
// --ID;
// Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "objs.maap[*]",
// "/testRgn objs");
// assertIndexDetailsEquals(i1.getCanonicalizedIndexedExpression(), "index_iter1.maap[*]");
// assertTrue(i1 instanceof CompactMapRangeIndex);
// CompactMapRangeIndex mri = (CompactMapRangeIndex)i1;
// // Test index maintenance
// // addition of new Portfolio object
// Map<Object, CompactRangeIndex> indxMap = mri.getRangeIndexHolderForTesting();
// assertIndexDetailsEquals(indxMap.size(), ID);
// for (int j = 1; j <= ID; ++j) {
// assertTrue(indxMap.containsKey("key" + j));
// CompactRangeIndex rng = indxMap.get("key" + j);
// Iterator itr = rng.valueToEntriesMap.values().iterator();
// assertIndexDetailsEquals(rng.valueToEntriesMap.size(), 1);
// assertTrue(rng.valueToEntriesMap.containsKey("val" + j));
// Set<Integer> expectedElements = new HashSet<Integer>();
// for (int k = j; k <= ID; ++k) {
// expectedElements.add(k);
// }
// Object mapValue = rng.valueToEntriesMap.get("val" + j);
// int size = 0;
// if (mapValue instanceof RegionEntry) {
// size = 1;
// }
// else if (mapValue instanceof ConcurrentHashSet) {
// size = ((ConcurrentHashSet)mapValue).size();
// }
// assertIndexDetailsEquals(expectedElements.size(), size);
// for (Integer elem : expectedElements) {
// RegionEntry re = testRgn.basicGetEntry(elem);
// if (mapValue instanceof RegionEntry) {
// assertTrue(mapValue.equals(re));
// }
// else if (mapValue instanceof ConcurrentHashSet) {
// assertTrue(((ConcurrentHashSet)mapValue).contains(re));
// }
// else {
// fail("no map value");
// }
// }
// }
// IndexManager im = testRgn.getIndexManager();
// im.rerunIndexCreationQuery();
// ID =5;
// i1 =im.getIndex("Index1");
// assertIndexDetailsEquals(i1.getCanonicalizedIndexedExpression(), "index_iter1.maap[*]");
// assertTrue(i1 instanceof CompactMapRangeIndex);
// mri = (CompactMapRangeIndex)i1;
// // Test index maintenance
// // addition of new Portfolio object
// indxMap = mri.getRangeIndexHolderForTesting();
// assertIndexDetailsEquals(indxMap.size(), ID);
// for (int j = 1; j <= ID; ++j) {
// assertTrue(indxMap.containsKey("key" + j));
// CompactRangeIndex rng = indxMap.get("key" + j);
// Iterator itr = rng.valueToEntriesMap.values().iterator();
// assertIndexDetailsEquals(rng.valueToEntriesMap.size(), 1);
// assertTrue(rng.valueToEntriesMap.containsKey("val" + j));
// Set<Integer> expectedElements = new HashSet<Integer>();
// for (int k = j; k <= ID; ++k) {
// expectedElements.add(k);
// }
// Object mapValue = rng.valueToEntriesMap.get("val" + j);
// int size = 0;
// if (mapValue instanceof RegionEntry) {
// size = 1;
// }
// else if (mapValue instanceof ConcurrentHashSet) {
// size = ((ConcurrentHashSet)mapValue).size();
// }
// assertIndexDetailsEquals(expectedElements.size(), size);
// for (Integer elem : expectedElements) {
// RegionEntry re = testRgn.basicGetEntry(elem);
// if (mapValue instanceof RegionEntry) {
// assertTrue(mapValue.equals(re));
// }
// else if (mapValue instanceof ConcurrentHashSet) {
// assertTrue(((ConcurrentHashSet)mapValue).contains(re));
// }
// else {
// fail("no map value");
// }
// }
// }
// }
@Test
public void testMapIndexRecreationForAllKeys() throws Exception {
    IndexManager.TEST_RANGEINDEX_ONLY = true;
    QueryService qs;
    qs = CacheUtils.getQueryService();
    LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
    int ID = 1;
    // and so on
    for (; ID <= 5; ++ID) {
        MapKeyIndexData mkid = new MapKeyIndexData(ID);
        for (int j = 1; j <= ID; ++j) {
            mkid.maap.put("key" + j, "val" + j);
        }
        testRgn.put(ID, mkid);
    }
    --ID;
    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "objs.maap[*]", "/testRgn objs");
    assertEquals(i1.getCanonicalizedIndexedExpression(), "index_iter1.maap[*]");
    assertTrue(i1 instanceof MapRangeIndex);
    MapRangeIndex mri = (MapRangeIndex) i1;
    // Test index maintenance
    // addition of new Portfolio object
    Map<Object, AbstractIndex> indxMap = mri.getRangeIndexHolderForTesting();
    assertEquals(indxMap.size(), ID);
    for (int j = 1; j <= ID; ++j) {
        assertTrue(indxMap.containsKey("key" + j));
        RangeIndex rng = (RangeIndex) indxMap.get("key" + j);
        Iterator itr = rng.valueToEntriesMap.values().iterator();
        assertEquals(rng.valueToEntriesMap.size(), 1);
        assertTrue(rng.valueToEntriesMap.containsKey("val" + j));
        Set<Integer> expectedElements = new HashSet<Integer>();
        for (int k = j; k <= ID; ++k) {
            expectedElements.add(k);
        }
        while (itr.hasNext()) {
            RegionEntryToValuesMap entryMap = (RegionEntryToValuesMap) itr.next();
            assertEquals(ID + 1 - j, entryMap.getNumEntries());
            for (Integer elem : expectedElements) {
                RegionEntry re = testRgn.basicGetEntry(elem);
                assertTrue(entryMap.containsEntry(re));
            }
        }
    }
    IndexManager im = testRgn.getIndexManager();
    im.rerunIndexCreationQuery();
    ID = 5;
    i1 = im.getIndex("Index1");
    assertEquals(i1.getCanonicalizedIndexedExpression(), "index_iter1.maap[*]");
    assertTrue(i1 instanceof MapRangeIndex);
    mri = (MapRangeIndex) i1;
    // Test index maintenance
    // addition of new Portfolio object
    indxMap = mri.getRangeIndexHolderForTesting();
    assertEquals(indxMap.size(), ID);
    for (int j = 1; j <= ID; ++j) {
        assertTrue(indxMap.containsKey("key" + j));
        RangeIndex rng = (RangeIndex) indxMap.get("key" + j);
        Iterator itr = rng.valueToEntriesMap.values().iterator();
        assertEquals(rng.valueToEntriesMap.size(), 1);
        assertTrue(rng.valueToEntriesMap.containsKey("val" + j));
        Set<Integer> expectedElements = new HashSet<Integer>();
        for (int k = j; k <= ID; ++k) {
            expectedElements.add(k);
        }
        while (itr.hasNext()) {
            RegionEntryToValuesMap entryMap = (RegionEntryToValuesMap) itr.next();
            assertEquals(ID + 1 - j, entryMap.getNumEntries());
            for (Integer elem : expectedElements) {
                RegionEntry re = testRgn.basicGetEntry(elem);
                assertTrue(entryMap.containsEntry(re));
            }
        }
    }
}
Also used : Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) RegionEntryToValuesMap(org.apache.geode.cache.query.internal.index.AbstractIndex.RegionEntryToValuesMap) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) RegionEntry(org.apache.geode.internal.cache.RegionEntry) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

RegionEntry (org.apache.geode.internal.cache.RegionEntry)56 Test (org.junit.Test)32 LocalRegion (org.apache.geode.internal.cache.LocalRegion)20 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 Iterator (java.util.Iterator)13 Region (org.apache.geode.cache.Region)13 VM (org.apache.geode.test.dunit.VM)13 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)11 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)10 Host (org.apache.geode.test.dunit.Host)9 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)9 Map (java.util.Map)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8 CacheException (org.apache.geode.cache.CacheException)7 EntrySnapshot (org.apache.geode.internal.cache.EntrySnapshot)7 CloseableIterator (org.apache.geode.internal.cache.persistence.query.CloseableIterator)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)5 Entry (java.util.Map.Entry)5 Cache (org.apache.geode.cache.Cache)5