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();
}
}
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();
}
}
}
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);
}
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();
}
}
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));
}
}
}
}
Aggregations