forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rough POC for ordered QueryCollectorContexts
- Loading branch information
Jay Deng
committed
May 20, 2024
1 parent
f30e0e0
commit 42d8918
Showing
3 changed files
with
108 additions
and
1 deletion.
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
74 changes: 74 additions & 0 deletions
74
server/src/main/java/org/opensearch/search/query/QueryContextProvider.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,74 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.query; | ||
|
||
import org.apache.lucene.search.Collector; | ||
import org.apache.lucene.search.CollectorManager; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.PriorityQueue; | ||
|
||
public class QueryContextProvider { | ||
private final PriorityQueue<OrderedQueryContext> contextQueue = new PriorityQueue<>(new Comparator<OrderedQueryContext>() { | ||
@Override | ||
public int compare(OrderedQueryContext o1, OrderedQueryContext o2) { | ||
return o1.getPriority() - o2.getPriority(); | ||
} | ||
}); | ||
|
||
public void addQueryContext(QueryCollectorContext collectorContext, int priority) { | ||
contextQueue.offer(new OrderedQueryContext(collectorContext, priority)); | ||
} | ||
|
||
public QueryCollectorContext getComposedContext() { | ||
return new QueryCollectorContext("provider") { | ||
@Override | ||
Collector create(Collector in) throws IOException { | ||
OrderedQueryContext octx = contextQueue.poll(); | ||
Collector collector = null; | ||
while (octx != null) { | ||
collector = octx.getContext().create(collector); | ||
octx = contextQueue.poll(); | ||
} | ||
return collector; | ||
} | ||
|
||
@Override | ||
CollectorManager<?, ReduceableSearchResult> createManager(CollectorManager<?, ReduceableSearchResult> in) throws IOException { | ||
OrderedQueryContext octx = contextQueue.poll(); | ||
CollectorManager<?, ReduceableSearchResult> manager = null; | ||
while (octx != null) { | ||
manager = octx.getContext().createManager(manager); | ||
octx = contextQueue.poll(); | ||
} | ||
return manager; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
class OrderedQueryContext { | ||
private final int priority; | ||
private final QueryCollectorContext context; | ||
|
||
public OrderedQueryContext(QueryCollectorContext context, int priority) { | ||
this.priority = priority; | ||
this.context = context; | ||
} | ||
|
||
public QueryCollectorContext getContext() { | ||
return this.context; | ||
} | ||
|
||
public int getPriority() { | ||
return priority; | ||
} | ||
} |
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