Skip to content

Commit 96a15bc

Browse files
committed
GEODE-9602: Changes after review
1 parent d32b31c commit 96a15bc

File tree

5 files changed

+62
-75
lines changed

5 files changed

+62
-75
lines changed

geode-core/src/integrationTest/java/org/apache/geode/cache/query/internal/QueryObserverCallbacksTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.mockito.ArgumentMatchers.any;
2020
import static org.mockito.ArgumentMatchers.anyInt;
21+
import static org.mockito.Mockito.never;
2122
import static org.mockito.Mockito.spy;
2223
import static org.mockito.Mockito.times;
2324
import static org.mockito.Mockito.verify;
@@ -251,8 +252,8 @@ public void testBeforeAndAfterIterationEvaluateNoWhere() throws Exception {
251252
"select count(*) from " + SEPARATOR + "portfolio p");
252253

253254
query.execute();
254-
verify(myQueryObserver, times(0)).beforeIterationEvaluation(any(), any());
255-
verify(myQueryObserver, times(0)).afterIterationEvaluation(any());
255+
verify(myQueryObserver, never()).beforeIterationEvaluation(any(), any());
256+
verify(myQueryObserver, never()).afterIterationEvaluation(any());
256257
}
257258

258259
@Test

geode-core/src/integrationTest/java/org/apache/geode/cache/query/internal/QueryTraceJUnitTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ public void testTraceOnLocalRegionWithTracePrefixNoComments() throws Exception {
324324
BeforeQueryExecutionHook hook = (BeforeQueryExecutionHook) DefaultQuery.testHook;
325325
assertThat(hook.getObserver()).isInstanceOf(IndexTrackingQueryObserver.class);
326326

327+
// The query should return all elements in region.
327328
assertEquals(region.size(), results.size());
328329
QueryObserverHolder.reset();
329330
}

geode-core/src/main/java/org/apache/geode/cache/query/internal/QueryObserverHolder.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
package org.apache.geode.cache.query.internal;
1616

17+
import java.util.concurrent.atomic.AtomicReference;
18+
1719
import org.apache.geode.annotations.Immutable;
1820
import org.apache.geode.annotations.internal.MakeNotStatic;
1921

@@ -49,31 +51,30 @@ public class QueryObserverHolder {
4951
* The current observer which will be notified of all query events.
5052
*/
5153
@MakeNotStatic
52-
private static QueryObserver _instance = NO_OBSERVER;
54+
private static final AtomicReference<QueryObserver> _instance =
55+
new AtomicReference<>(NO_OBSERVER);
5356

5457
/**
5558
* Set the given observer to be notified of query events. Returns the current observer.
5659
*/
57-
public static synchronized QueryObserver setInstance(QueryObserver observer) {
60+
public static QueryObserver setInstance(QueryObserver observer) {
5861
Support.assertArg(observer != null, "setInstance expects a non-null argument!");
59-
QueryObserver oldObserver = _instance;
60-
_instance = observer;
61-
return oldObserver;
62+
return _instance.getAndSet(observer);
6263
}
6364

64-
public static synchronized boolean hasObserver() {
65-
return _instance != NO_OBSERVER;
65+
public static boolean hasObserver() {
66+
return _instance.get() != NO_OBSERVER;
6667
}
6768

6869
/** Return the current QueryObserver instance */
69-
public static synchronized QueryObserver getInstance() {
70-
return _instance;
70+
public static QueryObserver getInstance() {
71+
return _instance.get();
7172
}
7273

7374
/**
7475
* Only for test purposes.
7576
*/
76-
public static synchronized void reset() {
77-
_instance = NO_OBSERVER;
77+
public static void reset() {
78+
_instance.set(NO_OBSERVER);
7879
}
7980
}

geode-core/src/main/java/org/apache/geode/cache/query/internal/index/AbstractIndex.java

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,19 +1824,8 @@ void addValuesToCollection(Collection result, CompiledValue iterOp, RuntimeItera
18241824
synchronized (value) {
18251825
for (Object o1 : ((Iterable) value)) {
18261826
boolean ok = true;
1827-
if (reUpdateInProgress) {
1828-
// Compare the value in index with value in RegionEntry.
1829-
ok = verifyEntryAndIndexValue(entry, o1, context);
1830-
}
1831-
try {
1832-
if (ok && runtimeItr != null) {
1833-
runtimeItr.setCurrent(o1);
1834-
observer.beforeIterationEvaluation(iterOp, o1);
1835-
ok = QueryUtils.applyCondition(iterOp, context);
1836-
}
1837-
} finally {
1838-
observer.afterIterationEvaluation(ok);
1839-
}
1827+
ok = applyCondition(iterOp, runtimeItr, context, observer, o1, entry,
1828+
reUpdateInProgress, ok);
18401829
if (ok) {
18411830
applyProjection(projAttrib, context, result, o1, intermediateResults,
18421831
isIntersection);
@@ -1849,19 +1838,8 @@ void addValuesToCollection(Collection result, CompiledValue iterOp, RuntimeItera
18491838
} else {
18501839
for (Object o1 : ((Iterable) value)) {
18511840
boolean ok = true;
1852-
if (reUpdateInProgress) {
1853-
// Compare the value in index with value in RegionEntry.
1854-
ok = verifyEntryAndIndexValue(entry, o1, context);
1855-
}
1856-
try {
1857-
if (ok && runtimeItr != null) {
1858-
runtimeItr.setCurrent(o1);
1859-
observer.beforeIterationEvaluation(iterOp, o1);
1860-
ok = QueryUtils.applyCondition(iterOp, context);
1861-
}
1862-
} finally {
1863-
observer.afterIterationEvaluation(ok);
1864-
}
1841+
ok = applyCondition(iterOp, runtimeItr, context, observer, o1, entry,
1842+
reUpdateInProgress, ok);
18651843
if (ok) {
18661844
applyProjection(projAttrib, context, result, o1, intermediateResults,
18671845
isIntersection);
@@ -1873,19 +1851,8 @@ void addValuesToCollection(Collection result, CompiledValue iterOp, RuntimeItera
18731851
}
18741852
} else {
18751853
boolean ok = true;
1876-
if (reUpdateInProgress) {
1877-
// Compare the value in index with in RegionEntry.
1878-
ok = verifyEntryAndIndexValue(entry, value, context);
1879-
}
1880-
try {
1881-
if (ok && runtimeItr != null) {
1882-
runtimeItr.setCurrent(value);
1883-
observer.beforeIterationEvaluation(iterOp, value);
1884-
ok = QueryUtils.applyCondition(iterOp, context);
1885-
}
1886-
} finally {
1887-
observer.afterIterationEvaluation(ok);
1888-
}
1854+
ok = applyCondition(iterOp, runtimeItr, context, observer, value, entry,
1855+
reUpdateInProgress, ok);
18891856
if (ok) {
18901857
if (context.isCqQueryContext()) {
18911858
result.add(new CqEntry(((RegionEntry) e.getKey()).getKey(), value));
@@ -1899,6 +1866,26 @@ void addValuesToCollection(Collection result, CompiledValue iterOp, RuntimeItera
18991866
}
19001867
}
19011868

1869+
private boolean applyCondition(CompiledValue iterOp, RuntimeIterator runtimeItr,
1870+
ExecutionContext context, QueryObserver observer, Object value, RegionEntry entry,
1871+
boolean reUpdateInProgress, boolean ok) throws FunctionDomainException,
1872+
TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
1873+
if (reUpdateInProgress) {
1874+
// Compare the value in index with in RegionEntry.
1875+
ok = verifyEntryAndIndexValue(entry, value, context);
1876+
}
1877+
try {
1878+
if (ok && runtimeItr != null) {
1879+
runtimeItr.setCurrent(value);
1880+
observer.beforeIterationEvaluation(iterOp, value);
1881+
ok = QueryUtils.applyCondition(iterOp, context);
1882+
}
1883+
} finally {
1884+
observer.afterIterationEvaluation(ok);
1885+
}
1886+
return ok;
1887+
}
1888+
19021889
private boolean verifyLimit(Collection result, int limit, ExecutionContext context) {
19031890
if (limit > 0) {
19041891
if (!context.isDistinct()) {

geode-core/src/main/java/org/apache/geode/cache/query/internal/index/CompactRangeIndex.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -805,18 +805,7 @@ private void addToResultsFromEntries(Object lowerBoundKey, Object upperBoundKey,
805805
value = iterator.next();
806806
if (value != null) {
807807
boolean ok = true;
808-
809-
if (runtimeItr != null) {
810-
runtimeItr.setCurrent(value);
811-
}
812-
try {
813-
if (ok && runtimeItr != null) {
814-
observer.beforeIterationEvaluation(iterOps, value);
815-
ok = QueryUtils.applyCondition(iterOps, context);
816-
}
817-
} finally {
818-
observer.afterIterationEvaluation(ok);
819-
}
808+
ok = applyCondition(iterOps, runtimeItr, context, observer, value, ok);
820809
if (ok) {
821810
applyCqOrProjection(projAttrib, context, result, value, intermediateResults,
822811
isIntersection, indexEntry.getDeserializedRegionKey());
@@ -845,17 +834,7 @@ private void addToResultsFromEntries(Object lowerBoundKey, Object upperBoundKey,
845834

846835
ok = evaluateEntry((IndexInfo) indexInfo, context, null);
847836
}
848-
if (runtimeItr != null) {
849-
runtimeItr.setCurrent(value);
850-
}
851-
try {
852-
if (ok && runtimeItr != null) {
853-
observer.beforeIterationEvaluation(iterOps, value);
854-
ok = QueryUtils.applyCondition(iterOps, context);
855-
}
856-
} finally {
857-
observer.afterIterationEvaluation(ok);
858-
}
837+
ok = applyCondition(iterOps, runtimeItr, context, observer, value, ok);
859838
if (ok) {
860839
if (context != null && context.isCqQueryContext()) {
861840
result.add(new CqEntry(indexEntry.getDeserializedRegionKey(), value));
@@ -879,6 +858,24 @@ private void addToResultsFromEntries(Object lowerBoundKey, Object upperBoundKey,
879858
}
880859
}
881860

861+
private boolean applyCondition(CompiledValue iterOps, RuntimeIterator runtimeItr,
862+
ExecutionContext context, QueryObserver observer, Object value, boolean ok)
863+
throws FunctionDomainException, TypeMismatchException, NameResolutionException,
864+
QueryInvocationTargetException {
865+
if (runtimeItr != null) {
866+
runtimeItr.setCurrent(value);
867+
}
868+
try {
869+
if (ok && runtimeItr != null) {
870+
observer.beforeIterationEvaluation(iterOps, value);
871+
ok = QueryUtils.applyCondition(iterOps, context);
872+
}
873+
} finally {
874+
observer.afterIterationEvaluation(ok);
875+
}
876+
return ok;
877+
}
878+
882879
public List expandValue(ExecutionContext context, Object lowerBoundKey, Object upperBoundKey,
883880
int lowerBoundOperator, int upperBoundOperator, Object value) {
884881
try {

0 commit comments

Comments
 (0)