PrimaryKeyIndex extends AbstractIndex { @Override void lockedQuery(Object key, int operator, Collection results, Set keysToRemove, ExecutionContext context) throws TypeMismatchException { assert keysToRemove == null; int limit = -1; if (key instanceof PdxString) { key = key.toString(); } Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX); if (applyLimit != null && applyLimit) { limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT); } QueryObserver observer = QueryObserverHolder.getInstance(); if (limit != -1 && results.size() == limit) { observer.limitAppliedAtIndexLevel(this, limit, results); return; } switch (operator) { case OQLLexerTokenTypes.TOK_EQ: { if (key != null && key != QueryService.UNDEFINED) { Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false); if (entry != null) { Object value = entry.getValue(); if (value != null) { addResultToResults(context, results, key, value); } } } break; } case OQLLexerTokenTypes.TOK_NE_ALT: case OQLLexerTokenTypes.TOK_NE: { Set values = (Set) getRegion().values(); if (limit != -1) { ++limit; } Iterator iter = values.iterator(); while (iter.hasNext()) { QueryMonitor.throwExceptionIfQueryOnCurrentThreadIsCanceled(); addResultToResults(context, results, key, iter.next()); if (limit != -1 && results.size() == limit) { observer.limitAppliedAtIndexLevel(this, limit, results); return; } } boolean removeOneRow = limit != -1; if (key != null && key != QueryService.UNDEFINED) { Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false); if (entry != null) { if (entry.getValue() != null) { results.remove(entry.getValue()); removeOneRow = false; } } } if (removeOneRow) { Iterator itr = results.iterator(); if (itr.hasNext()) { itr.next(); itr.remove(); } } break; } default: { throw new IllegalArgumentException( "Invalid Operator"); } } numUses++; } PrimaryKeyIndex(InternalCache cache, String indexName, Region region, String fromClause,
      String indexedExpression, String projectionAttributes, String origFromClause,
      String origIndxExpr, String[] defintions, IndexStatistics indexStatistics); @Override IndexType getType(); @Override ObjectType getResultSetType(); @Override boolean clear(); @Override int getSizeEstimate(Object key, int op, int matchLevel); @Override boolean isEmpty();  }
@Test public void lockQueryWithoutProjectionWithMatchingResultForCqShouldReturnCorrectCqEvent() throws Exception { String value = "value"; when(context.isCqQueryContext()).thenReturn(true); when(context.getQuery()).thenReturn(mock(DefaultQuery.class)); Region.Entry entry = mock(Region.Entry.class); when(entry.getValue()).thenReturn(value); when(region.accessEntry(eq("key"), anyBoolean())).thenReturn(entry); List results = new LinkedList(); index.lockedQuery("key", OQLLexerTokenTypes.TOK_EQ, results, null, context); assertTrue(results.get(0) instanceof CqEntry); }
@Test public void lockQueryWithoutProjectionWithMatchingResultShouldReturnCorrectValue() throws Exception { String value = "value"; when(context.getQuery()).thenReturn(mock(DefaultQuery.class)); Region.Entry entry = mock(Region.Entry.class); when(entry.getValue()).thenReturn(value); when(region.accessEntry(eq("key"), anyBoolean())).thenReturn(entry); List results = new LinkedList(); index.lockedQuery("key", OQLLexerTokenTypes.TOK_EQ, results, null, context); assertEquals(value, results.get(0)); }