public void removeIndexes(Region region) {

    if (pool != null) {
      throw new UnsupportedOperationException(
          "Index Operation is not supported on the Server Region.");
    }

    // removing indexes on paritioned region will reguire sending message and
    // remvoing all the local indexes on the local bucket regions.
    if (region instanceof PartitionedRegion) {
      try {
        // not remotely orignated
        ((PartitionedRegion) region).removeIndexes(false);
      } catch (ForceReattemptException ex) {
        // will have to throw a proper exception relating to remove index.
        logger.info(
            LocalizedMessage.create(
                LocalizedStrings.DefaultQueryService_EXCEPTION_REMOVING_INDEX___0),
            ex);
      }
    }
    IndexManager indexManager = IndexUtils.getIndexManager(region, false);
    if (indexManager == null) return;

    indexManager.removeIndexes();
  }
  public void removeIndex(Index index) {

    if (pool != null) {
      throw new UnsupportedOperationException(
          "Index Operation is not supported on the Server Region.");
    }

    Region region = index.getRegion();
    if (region instanceof PartitionedRegion) {
      try {
        ((PartitionedRegion) region).removeIndex(index, false);
      } catch (ForceReattemptException ex) {
        logger.info(
            LocalizedMessage.create(
                LocalizedStrings.DefaultQueryService_EXCEPTION_REMOVING_INDEX___0),
            ex);
      }
      return;
    }
    // get write lock for indexes in replicated region
    // for PR lock will be taken in PartitionRegion.removeIndex
    ((AbstractIndex) index).acquireIndexWriteLockForRemove();
    try {
      IndexManager indexManager = ((LocalRegion) index.getRegion()).getIndexManager();
      indexManager.removeIndex(index);
    } finally {
      ((AbstractIndex) index).releaseIndexWriteLockForRemove();
    }
  }
  private boolean createDefinedIndexesForReplicatedRegion(
      HashSet<Index> indexes,
      Region region,
      Set<IndexCreationData> icds,
      HashMap<String, Exception> exceptionsMap) {
    boolean throwException = false;
    for (IndexCreationData icd : icds) {
      try {
        // First step is creating all the defined indexes. Do this only if
        // the region is not PR. For PR creation and population is done in
        // the PartitionedRegion#createDefinedIndexes
        indexes.add(
            createIndex(
                icd.getIndexName(),
                icd.getIndexType(),
                icd.getIndexExpression(),
                icd.getIndexFromClause(),
                icd.getIndexImportString(),
                false,
                region));
      } catch (Exception ex) {
        // If an index creation fails, add the exception to the map and
        // continue creating rest of the indexes.The failed indexes will
        // be removed from the IndexManager#indexes map by the createIndex
        // method so that those indexes will not be populated in the next
        // step.
        if (logger.isDebugEnabled()) {
          logger.debug("Index creation failed, {}, {}", icd.getIndexName(), ex.getMessage(), ex);
        }
        exceptionsMap.put(icd.getIndexName(), ex);
        throwException = true;
      }
    }
    if (IndexManager.testHook != null) {
      IndexManager.testHook.hook(13);
    }
    // Second step is iterating over REs and populating all the created
    // indexes
    IndexManager indexManager = IndexUtils.getIndexManager(region, false);
    if (indexManager == null) {
      for (IndexCreationData icd : icds) {
        exceptionsMap.put(
            icd.getIndexName(),
            new IndexCreationException("Index Creation Failed due to region destroy"));
      }
      return true;
    }

    if (indexes.size() > 0) {
      try {
        indexManager.populateIndexes(indexes);
      } catch (MultiIndexCreationException ex) {
        exceptionsMap.putAll(ex.getExceptionsMap());
        throwException = true;
      }
    }
    return throwException;
  }
  public Collection getIndexes(Region region, IndexType indexType) {

    if (pool != null) {
      throw new UnsupportedOperationException(
          "Index Operation is not supported on the Server Region.");
    }

    IndexManager indexManager = IndexUtils.getIndexManager(region, false);
    if (indexManager == null) return null;
    return indexManager.getIndexes(indexType);
  }
  public Collection getIndexes(Region region) {

    if (pool != null) {
      throw new UnsupportedOperationException(
          "Index Operation is not supported on the Server Region.");
    }

    if (region instanceof PartitionedRegion) {
      return ((PartitionedRegion) region).getIndexes();
    }
    IndexManager indexManager = IndexUtils.getIndexManager(region, false);
    if (indexManager == null) return null;
    return indexManager.getIndexes();
  }
 /**
  * Asif : Gets an exact match index ( match level 0)
  *
  * @param regionPath String containing the region name
  * @param definitions An array of String objects containing canonicalized definitions of
  *     RuntimeIterators. A Canonicalized definition of a RuntimeIterator is the canonicalized
  *     expression obtainded from its underlying collection expression.
  * @param indexType IndexType object which can be either of type RangeIndex or PrimaryKey Index
  * @param indexedExpression CompiledValue containing the path expression on which index needs to
  *     be created
  * @param context ExecutionContext
  * @return IndexData object
  * @throws NameResolutionException
  * @throws TypeMismatchException
  * @throws AmbiguousNameException
  */
 public IndexData getIndex(
     String regionPath,
     String[] definitions,
     IndexType indexType,
     CompiledValue indexedExpression,
     ExecutionContext context)
     throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
   Region region = cache.getRegion(regionPath);
   if (region == null) {
     return null;
   }
   IndexManager indexManager = IndexUtils.getIndexManager(region, true);
   IndexData indexData = indexManager.getIndex(indexType, definitions, indexedExpression, context);
   return indexData;
 }
  public Index getIndex(Region region, String indexName) {

    if (pool != null) {
      throw new UnsupportedOperationException(
          "Index Operation is not supported on the Server Region.");
    }

    // A Partition Region does not have an IndexManager, but it's buckets have.
    if (region instanceof PartitionedRegion) {
      return (Index) ((PartitionedRegion) region).getIndex().get(indexName);
    } else {
      IndexManager indexManager = IndexUtils.getIndexManager(region, false);
      if (indexManager == null) return null;
      return indexManager.getIndex(indexName);
    }
  }
 /**
  * Asif: Gets a best match index which is available. An index with match level equal to 0 is the
  * best index to use as it implies that the query from clause iterators belonging to the region
  * exactly match the index from clause iterators ( the difference in the relative positions of the
  * iterators do not matter). A match level less than 0 means that number of iteratots in the index
  * resultset is more than that present in the query from clause and hence index resultset will
  * need a cutdown. A match level greater than 0 means that there definitely is atleast one
  * iterator in the query from clause which is more than the index from clause iterators & hence
  * definitely expansion of index results will be needed. Pls note that a match level greater than
  * 0 does not imply that index from clause does not have an extra iterator in it , too. Hence a
  * match level greater than 0 will definitely mean expansion of index results but may also require
  * a cut down of results . The order of preference is match level 0 , less than 0 and lastly
  * greater than 0
  *
  * @param regionPath String containing the region name
  * @param definitions An array of String objects containing canonicalized definitions of
  *     RuntimeIterators. A Canonicalized definition of a RuntimeIterator is the canonicalized
  *     expression obtainded from its underlying collection expression.
  * @param indexType IndexType object which can be either of type RangeIndex or PrimaryKey Index
  * @param indexedExpression CompiledValue representing the path expression on which index needs to
  *     be created
  * @param context ExecutionContext object
  * @return IndexData object
  * @throws NameResolutionException
  * @throws TypeMismatchException
  * @throws AmbiguousNameException
  */
 public IndexData getBestMatchIndex(
     String regionPath,
     String definitions[],
     IndexType indexType,
     CompiledValue indexedExpression,
     ExecutionContext context)
     throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
   Region region = cache.getRegion(regionPath);
   if (region == null) {
     return null;
   }
   // return getBestMatchIndex(region, indexType, definitions,
   // indexedExpression);
   IndexManager indexManager = IndexUtils.getIndexManager(region, false);
   if (indexManager == null) {
     return null;
   }
   return indexManager.getBestMatchIndex(indexType, definitions, indexedExpression, context);
 }
  /**
   * This test is for a fix for #47475 We added a way to determine whether to reevaluate an entry
   * for query execution The method is to keep track of the delta and current time in a single long
   * value The value is then used by the query to determine if a region entry needs to be
   * reevaluated based on subtracting the value with the query execution time. This provides a delta
   * + some false positive time If the delta + last modified time of the region entry is > query
   * start time, we can assume that it needs to be reevaluated
   */
  @Test
  public void testSafeQueryTime() {
    IndexManager.resetIndexBufferTime();
    // fake entry update at LMT of 0 and actual time of 10
    // safe query time set in index manager is going to be 20
    assertTrue(IndexManager.setIndexBufferTime(0, 10));

    // fake query start at actual time of 9, 10, 11 and using the fake LMT of 0
    assertTrue(IndexManager.needsRecalculation(9, 0));
    assertTrue(IndexManager.needsRecalculation(10, 0));
    assertFalse(IndexManager.needsRecalculation(11, 0));

    assertFalse(
        IndexManager.needsRecalculation(9, -3)); // old enough updates shouldn't trigger a recalc
    assertTrue(
        IndexManager.needsRecalculation(
            9, -2)); // older updates but falls within the delta (false positive)

    assertTrue(IndexManager.needsRecalculation(10, 5));
    // This should eval to true only because of false positives.
    assertTrue(IndexManager.needsRecalculation(11, 5));

    // Now let's assume a new update has occurred, this update delta and time combo still is not
    // larger
    assertFalse(IndexManager.setIndexBufferTime(0, 9));
    assertFalse(IndexManager.setIndexBufferTime(1, 10));

    // Now let's assume a new update has occured where the time is larger (enough to roll off the
    // large delta)
    // but the delta is smaller
    assertTrue(IndexManager.setIndexBufferTime(30, 30));

    // Now that we have a small delta, let's see if a query that was "stuck" would reevaluate
    // appropriately
    assertTrue(IndexManager.needsRecalculation(9, 0));
  }
  // Let's test for negative delta's or a system that is slower than others in the  cluster
  @Test
  public void testSafeQueryTimeForASlowNode() {
    IndexManager.resetIndexBufferTime();
    // fake entry update at LMT of 0 and actual time of 10
    // safe query time set in index manager is going to be -10
    assertTrue(IndexManager.setIndexBufferTime(210, 200));

    assertFalse(IndexManager.needsRecalculation(200, 190));
    assertFalse(IndexManager.needsRecalculation(200, 200));
    assertTrue(IndexManager.needsRecalculation(200, 210));

    assertTrue(IndexManager.needsRecalculation(200, 220));
    assertTrue(IndexManager.needsRecalculation(200, 221));

    // now lets say an entry updates with no delta
    assertTrue(IndexManager.setIndexBufferTime(210, 210));

    assertTrue(IndexManager.needsRecalculation(200, 190));
    assertTrue(IndexManager.needsRecalculation(200, 200));
    assertTrue(IndexManager.needsRecalculation(200, 210));

    assertTrue(IndexManager.needsRecalculation(200, 220));
    assertTrue(IndexManager.needsRecalculation(200, 221));

    assertTrue(IndexManager.needsRecalculation(210, 211));
    assertFalse(IndexManager.needsRecalculation(212, 210));
  }
  public Index createIndex(
      String indexName,
      IndexType indexType,
      String indexedExpression,
      String fromClause,
      String imports,
      boolean loadEntries,
      Region region)
      throws IndexNameConflictException, IndexExistsException, RegionNotFoundException {

    if (pool != null) {
      throw new UnsupportedOperationException(
          "Index creation on the server is not supported from the client.");
    }
    PartitionedIndex parIndex = null;
    if (region == null) {
      region = getRegionFromPath(imports, fromClause);
    }
    RegionAttributes ra = region.getAttributes();

    // Asif: If the evistion action is Overflow to disk then do not allow index creation
    // It is Ok to have index creation if it is persist only mode as data will always
    // exist in memory
    // if(ra.getEvictionAttributes().getAction().isOverflowToDisk() ) {
    //  throw new
    // UnsupportedOperationException(LocalizedStrings.DefaultQueryService_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_REGIONS_WHICH_OVERFLOW_TO_DISK_THE_REGION_INVOLVED_IS_0.toLocalizedString(regionPath));
    // }
    // if its a pr the create index on all of the local buckets.
    if (((LocalRegion) region).heapThresholdReached.get()
        && !InternalResourceManager.isLowMemoryExceptionDisabled()) {
      LocalRegion lr = (LocalRegion) region;
      throw new LowMemoryException(
          LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_INDEX.toLocalizedString(region.getName()),
          lr.getHeapThresholdReachedMembers());
    }
    if (region instanceof PartitionedRegion) {
      try {
        parIndex =
            (PartitionedIndex)
                ((PartitionedRegion) region)
                    .createIndex(
                        false,
                        indexType,
                        indexName,
                        indexedExpression,
                        fromClause,
                        imports,
                        loadEntries);
      } catch (ForceReattemptException ex) {
        region
            .getCache()
            .getLoggerI18n()
            .info(
                LocalizedStrings
                    .DefaultQueryService_EXCEPTION_WHILE_CREATING_INDEX_ON_PR_DEFAULT_QUERY_PROCESSOR,
                ex);
      } catch (IndexCreationException exx) {
        region
            .getCache()
            .getLoggerI18n()
            .info(
                LocalizedStrings
                    .DefaultQueryService_EXCEPTION_WHILE_CREATING_INDEX_ON_PR_DEFAULT_QUERY_PROCESSOR,
                exx);
      }
      return parIndex;

    } else {

      IndexManager indexManager = IndexUtils.getIndexManager(region, true);
      Index index =
          indexManager.createIndex(
              indexName,
              indexType,
              indexedExpression,
              fromClause,
              imports,
              null,
              null,
              loadEntries);

      return index;
    }
  }