Search in sources :

Example 26 with RegionEntry

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

the class AbstractIndex method populateListForEquiJoin.

/**
   * This will populate resultSet from both type of indexes, {@link CompactRangeIndex} and
   * {@link RangeIndex}.
   */
void populateListForEquiJoin(List list, Object outerEntries, Object innerEntries, ExecutionContext context, Object key) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    Assert.assertTrue(outerEntries != null && innerEntries != null, "OuterEntries or InnerEntries must not be null");
    Object[][] values = new Object[2][];
    Iterator itr = null;
    int j = 0;
    while (j < 2) {
        boolean isRangeIndex = false;
        if (j == 0) {
            if (outerEntries instanceof RegionEntryToValuesMap) {
                itr = ((RegionEntryToValuesMap) outerEntries).map.entrySet().iterator();
                isRangeIndex = true;
            } else if (outerEntries instanceof CloseableIterator) {
                itr = (Iterator) outerEntries;
            }
        } else {
            if (innerEntries instanceof RegionEntryToValuesMap) {
                itr = ((RegionEntryToValuesMap) innerEntries).map.entrySet().iterator();
                isRangeIndex = true;
            } else if (innerEntries instanceof CloseableIterator) {
                itr = (Iterator) innerEntries;
            }
        }
        // extract the values from the RegionEntries
        List dummy = new ArrayList();
        RegionEntry re = null;
        IndexStoreEntry ie = null;
        Object val = null;
        Object entryVal = null;
        IndexInfo[] indexInfo = (IndexInfo[]) context.cacheGet(CompiledValue.INDEX_INFO);
        IndexInfo indInfo = indexInfo[j];
        while (itr.hasNext()) {
            if (isRangeIndex) {
                Map.Entry entry = (Map.Entry) itr.next();
                val = entry.getValue();
                if (val instanceof Collection) {
                    entryVal = ((Iterable) val).iterator().next();
                } else {
                    entryVal = val;
                }
                re = (RegionEntry) entry.getKey();
            } else {
                ie = (IndexStoreEntry) itr.next();
            }
            // Bug#41010: We need to verify if Inner and Outer Entries
            // are consistent with index key values.
            boolean ok = true;
            if (isRangeIndex) {
                if (re.isUpdateInProgress()) {
                    ok = ((RangeIndex) indInfo._getIndex()).verifyEntryAndIndexValue(re, entryVal, context);
                }
            } else if (ie.isUpdateInProgress()) {
                ok = ((CompactRangeIndex) indInfo._getIndex()).verifyInnerAndOuterEntryValues(ie, context, indInfo, key);
            }
            if (ok) {
                if (isRangeIndex) {
                    if (val instanceof Collection) {
                        dummy.addAll((Collection) val);
                    } else {
                        dummy.add(val);
                    }
                } else {
                    if (IndexManager.IS_TEST_EXPANSION) {
                        dummy.addAll(((CompactRangeIndex) indInfo._getIndex()).expandValue(context, key, null, OQLLexerTokenTypes.TOK_EQ, -1, ie.getDeserializedValue()));
                    } else {
                        dummy.add(ie.getDeserializedValue());
                    }
                }
            }
        }
        Object[] newValues = new Object[dummy.size()];
        dummy.toArray(newValues);
        values[j++] = newValues;
    }
    list.add(values);
}
Also used : Entry(java.util.Map.Entry) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) ArrayList(java.util.ArrayList) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) IndexInfo(org.apache.geode.cache.query.internal.IndexInfo) Entry(java.util.Map.Entry) CqEntry(org.apache.geode.cache.query.internal.CqEntry) RegionEntry(org.apache.geode.internal.cache.RegionEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) RegionEntry(org.apache.geode.internal.cache.RegionEntry) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with RegionEntry

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

the class MemoryIndexStore method printAll.

public String printAll() {
    StringBuffer sb = new StringBuffer();
    Iterator iterator = this.valueToEntriesMap.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry mapEntry = (Map.Entry) iterator.next();
        sb.append("Key: " + mapEntry.getKey());
        Object value = mapEntry.getValue();
        if (value instanceof Collection) {
            Iterator entriesIterator = ((Collection) value).iterator();
            while (entriesIterator.hasNext()) {
                sb.append(" Value:" + getTargetObject((RegionEntry) entriesIterator.next()));
            }
        } else {
            sb.append(" Value:" + getTargetObject((RegionEntry) value));
        }
        sb.append("\n");
    }
    return sb.toString();
}
Also used : Entry(java.util.Map.Entry) RegionEntry(org.apache.geode.internal.cache.RegionEntry) Entry(java.util.Map.Entry) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) Collection(java.util.Collection) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 28 with RegionEntry

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

the class MemoryIndexStore method updateMapping.

@Override
public void updateMapping(Object indexKey, Object oldKey, RegionEntry re, Object oldValue) throws IMQException {
    try {
        if (DefaultQuery.testHook != null) {
            DefaultQuery.testHook.doTestHook(3);
        }
        // Check if reverse-map is present.
        if (IndexManager.isObjectModificationInplace()) {
            // If reverse map get the old index key from reverse map.
            if (this.entryToValuesMap.containsKey(re)) {
                oldKey = this.entryToValuesMap.get(re);
            }
        } else {
            // forward map.
            if (oldValue != null && oldValue == getTargetObjectInVM(re)) {
                oldKey = getOldKey(indexKey, re);
            }
        }
        // No need to update the map if new and old index key are same.
        if (oldKey != null && oldKey.equals(TypeUtils.indexKeyFor(indexKey))) {
            return;
        }
        boolean retry = false;
        indexKey = TypeUtils.indexKeyFor(indexKey);
        if (indexKey.equals(QueryService.UNDEFINED)) {
            Object targetObject = getTargetObjectForUpdate(re);
            if (Token.isInvalidOrRemoved(targetObject)) {
                if (oldKey != null) {
                    basicRemoveMapping(oldKey, re, false);
                }
                return;
            }
        }
        do {
            retry = false;
            Object regionEntries = this.valueToEntriesMap.putIfAbsent(indexKey, re);
            if (regionEntries == TRANSITIONING_TOKEN) {
                retry = true;
                continue;
            } else if (regionEntries == null) {
                internalIndexStats.incNumKeys(1);
                numIndexKeys.incrementAndGet();
            } else if (regionEntries instanceof RegionEntry) {
                IndexElemArray elemArray = new IndexElemArray();
                if (DefaultQuery.testHook != null) {
                    DefaultQuery.testHook.doTestHook("BEGIN_TRANSITION_FROM_REGION_ENTRY_TO_ELEMARRAY");
                }
                elemArray.add(regionEntries);
                elemArray.add(re);
                if (!this.valueToEntriesMap.replace(indexKey, regionEntries, elemArray)) {
                    retry = true;
                }
                if (DefaultQuery.testHook != null) {
                    DefaultQuery.testHook.doTestHook("TRANSITIONED_FROM_REGION_ENTRY_TO_ELEMARRAY");
                }
                if (DefaultQuery.testHook != null) {
                    DefaultQuery.testHook.doTestHook("COMPLETE_TRANSITION_FROM_REGION_ENTRY_TO_ELEMARRAY");
                }
            } else if (regionEntries instanceof IndexConcurrentHashSet) {
                // basicRemoveMapping();
                synchronized (regionEntries) {
                    ((IndexConcurrentHashSet) regionEntries).add(re);
                }
                if (regionEntries != this.valueToEntriesMap.get(indexKey)) {
                    retry = true;
                }
            } else {
                IndexElemArray elemArray = (IndexElemArray) regionEntries;
                synchronized (elemArray) {
                    if (elemArray.size() >= IndexManager.INDEX_ELEMARRAY_THRESHOLD) {
                        IndexConcurrentHashSet set = new IndexConcurrentHashSet(IndexManager.INDEX_ELEMARRAY_THRESHOLD + 20, 0.75f, 1);
                        if (DefaultQuery.testHook != null) {
                            DefaultQuery.testHook.doTestHook("BEGIN_TRANSITION_FROM_ELEMARRAY_TO_CONCURRENT_HASH_SET");
                        }
                        // retry?
                        if (!this.valueToEntriesMap.replace(indexKey, regionEntries, TRANSITIONING_TOKEN)) {
                            retry = true;
                        } else {
                            if (DefaultQuery.testHook != null) {
                                DefaultQuery.testHook.doTestHook("TRANSITIONED_FROM_ELEMARRAY_TO_TOKEN");
                            }
                            set.add(re);
                            set.addAll(elemArray);
                            if (!this.valueToEntriesMap.replace(indexKey, TRANSITIONING_TOKEN, set)) {
                                // This should never happen. If we see this in the log, then something is wrong
                                // with the TRANSITIONING TOKEN and synchronization of changing collection types
                                // we should then just go from RE to CHS and completely remove the Elem Array.
                                region.getCache().getLogger().warning("Unable to transition from index elem to concurrent hash set.  Index needs to be recreated");
                                throw new IndexMaintenanceException("Unable to transition from index elem to concurrent hash set.  Index needs to be recreated");
                            }
                            if (DefaultQuery.testHook != null) {
                                DefaultQuery.testHook.doTestHook("COMPLETE_TRANSITION_FROM_ELEMARRAY_TO_CONCURRENT_HASH_SET");
                            }
                        }
                    } else {
                        elemArray.add(re);
                        if (regionEntries != this.valueToEntriesMap.get(indexKey)) {
                            retry = true;
                        }
                    }
                }
            }
            // Add to reverse Map with the new value.
            if (!retry) {
                // oldKey is not null only for an update
                if (oldKey != null) {
                    basicRemoveMapping(oldKey, re, false);
                }
                if (IndexManager.isObjectModificationInplace()) {
                    this.entryToValuesMap.put(re, indexKey);
                }
            }
        } while (retry);
    } catch (TypeMismatchException ex) {
        throw new IMQException("Could not add object of type " + indexKey.getClass().getName(), ex);
    }
    internalIndexStats.incNumValues(1);
}
Also used : TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) RegionEntry(org.apache.geode.internal.cache.RegionEntry) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException)

Example 29 with RegionEntry

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

the class HashIndex method dump.

public String dump() {
    StringBuilder sb = new StringBuilder(toString()).append(" {").append(getLineSeparator());
    sb.append(" -----------------------------------------------").append(getLineSeparator());
    for (Object anEntriesSet : this.entriesSet) {
        Entry indexEntry = (Entry) anEntriesSet;
        sb.append(" Key = ").append(indexEntry.getKey()).append(getLineSeparator());
        sb.append(" Value Type = ").append(' ').append(indexEntry.getValue().getClass().getName()).append(getLineSeparator());
        if (indexEntry.getValue() instanceof Collection) {
            sb.append(" Value Size = ").append(' ').append(((Collection) indexEntry.getValue()).size()).append(getLineSeparator());
        } else if (indexEntry.getValue() instanceof RegionEntry) {
            sb.append(" Value Size = ").append(" " + 1).append(getLineSeparator());
        } else {
            throw new AssertionError("value instance of " + indexEntry.getValue().getClass().getName());
        }
        Collection entrySet = regionEntryCollection(indexEntry.getValue());
        for (Object anEntrySet : entrySet) {
            RegionEntry e = (RegionEntry) anEntrySet;
            Object value = getTargetObject(e);
            sb.append("  RegionEntry.key = ").append(e.getKey());
            sb.append("  Value.type = ").append(value.getClass().getName());
            if (value instanceof Collection) {
                sb.append("  Value.size = ").append(((Collection) value).size());
            }
            sb.append(getLineSeparator());
        }
        sb.append(" -----------------------------------------------").append(getLineSeparator());
    }
    sb.append("}// Index ").append(getName()).append(" end");
    return sb.toString();
}
Also used : Entry(java.util.Map.Entry) CqEntry(org.apache.geode.cache.query.internal.CqEntry) RegionEntry(org.apache.geode.internal.cache.RegionEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) Collection(java.util.Collection) RegionEntry(org.apache.geode.internal.cache.RegionEntry) StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 30 with RegionEntry

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

the class HashIndex method addToResultsFromEntries.

/**
   * @param entriesIter is Iterable<RegionEntry>
   */
private void addToResultsFromEntries(Iterator entriesIter, Collection result, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, Set keysToRemove, boolean applyOrderBy, boolean asc, long iteratorCreationTime) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    QueryObserver observer = QueryObserverHolder.getInstance();
    if (result == null || (limit != -1 && result != null && result.size() == limit)) {
        return;
    }
    List orderedKeys = null;
    List orderedResults = null;
    if (applyOrderBy) {
        orderedKeys = new ArrayList();
        orderedResults = new ArrayList();
    }
    int i = 0;
    while (entriesIter.hasNext()) {
        // Check if query execution on this thread is canceled.
        QueryMonitor.isQueryExecutionCanceled();
        if (IndexManager.testHook != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("IndexManager TestHook is set in addToResultsFromEntries.");
            }
            IndexManager.testHook.hook(11);
        }
        Object obj = entriesIter.next();
        Object key = null;
        if (obj != null && obj != HashIndexSet.REMOVED) {
            RegionEntry re = (RegionEntry) obj;
            if (applyOrderBy) {
                key = ((HashIndex.IMQEvaluator) evaluator).evaluateKey(obj);
                orderedKeys.add(new Object[] { key, i++ });
                addValueToResultSet(re, orderedResults, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit, observer, iteratorCreationTime);
            } else {
                addValueToResultSet(re, result, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit, observer, iteratorCreationTime);
            }
        }
    }
    if (applyOrderBy) {
        /*
       * For orderby queries, 1. Store the keys in a list along with the order. 2. Store the results
       * in another temp list. 3. Sort the keys. The order will also get sorted. 4. Fetch the result
       * objects from the temp list according to the sorted orders from the sorted list and add to
       * the result collection.
       */
        Collections.sort(orderedKeys, comparator);
        if (!asc) {
            Collections.reverse(orderedKeys);
        }
        Object[] temp = orderedResults.toArray();
        List tempResults = new ArrayList(temp.length);
        for (Object o : orderedKeys) {
            int index = (Integer) ((Object[]) o)[1];
            tempResults.add(temp[index]);
        }
        result.addAll(tempResults);
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) ArrayList(java.util.ArrayList) RegionEntry(org.apache.geode.internal.cache.RegionEntry) List(java.util.List) ArrayList(java.util.ArrayList) StoredObject(org.apache.geode.internal.offheap.StoredObject)

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