-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LUCENE-8939: Introduce Shared Count Early Termination In Parallel Sea…
…rch (#823) This commit introduces a strategy to early terminate for sorted collections during parallel search when requested number of hits have been collected but the total hits threshold has not yet been reached.
- Loading branch information
Showing
7 changed files
with
331 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
lucene/core/src/java/org/apache/lucene/search/HitsThresholdChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.lucene.search; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Used for defining custom algorithms to allow searches to early terminate | ||
*/ | ||
abstract class HitsThresholdChecker { | ||
/** | ||
* Implementation of HitsThresholdChecker which allows global hit counting | ||
*/ | ||
private static class GlobalHitsThresholdChecker extends HitsThresholdChecker { | ||
private final int totalHitsThreshold; | ||
private final AtomicLong globalHitCount; | ||
|
||
public GlobalHitsThresholdChecker(int totalHitsThreshold) { | ||
|
||
if (totalHitsThreshold < 0) { | ||
throw new IllegalArgumentException("totalHitsThreshold must be >= 0, got " + totalHitsThreshold); | ||
} | ||
|
||
this.totalHitsThreshold = totalHitsThreshold; | ||
this.globalHitCount = new AtomicLong(); | ||
} | ||
|
||
@Override | ||
public void incrementHitCount() { | ||
globalHitCount.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public boolean isThresholdReached(){ | ||
return globalHitCount.getAcquire() > totalHitsThreshold; | ||
} | ||
|
||
@Override | ||
public ScoreMode scoreMode() { | ||
return totalHitsThreshold == Integer.MAX_VALUE ? ScoreMode.COMPLETE : ScoreMode.TOP_SCORES; | ||
} | ||
|
||
@Override | ||
public int getHitsThreshold() { | ||
return totalHitsThreshold; | ||
} | ||
} | ||
|
||
/** | ||
* Default implementation of HitsThresholdChecker to be used for single threaded execution | ||
*/ | ||
private static class LocalHitsThresholdChecker extends HitsThresholdChecker { | ||
private final int totalHitsThreshold; | ||
private int hitCount; | ||
|
||
public LocalHitsThresholdChecker(int totalHitsThreshold) { | ||
|
||
if (totalHitsThreshold < 0) { | ||
throw new IllegalArgumentException("totalHitsThreshold must be >= 0, got " + totalHitsThreshold); | ||
} | ||
|
||
this.totalHitsThreshold = totalHitsThreshold; | ||
} | ||
|
||
@Override | ||
public void incrementHitCount() { | ||
++hitCount; | ||
} | ||
|
||
@Override | ||
public boolean isThresholdReached() { | ||
return hitCount > totalHitsThreshold; | ||
} | ||
|
||
@Override | ||
public ScoreMode scoreMode() { | ||
return totalHitsThreshold == Integer.MAX_VALUE ? ScoreMode.COMPLETE : ScoreMode.TOP_SCORES; | ||
} | ||
|
||
@Override | ||
public int getHitsThreshold() { | ||
return totalHitsThreshold; | ||
} | ||
} | ||
|
||
/* | ||
* Returns a threshold checker that is useful for single threaded searches | ||
*/ | ||
public static HitsThresholdChecker create(final int totalHitsThreshold) { | ||
return new LocalHitsThresholdChecker(totalHitsThreshold); | ||
} | ||
|
||
/* | ||
* Returns a threshold checker that is based on a shared counter | ||
*/ | ||
public static HitsThresholdChecker createShared(final int totalHitsThreshold) { | ||
return new GlobalHitsThresholdChecker(totalHitsThreshold); | ||
} | ||
|
||
public abstract void incrementHitCount(); | ||
public abstract ScoreMode scoreMode(); | ||
public abstract int getHitsThreshold(); | ||
public abstract boolean isThresholdReached(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.