2021-07-06 00:30:14 +02:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
2022-01-28 21:12:10 +01:00
|
|
|
import static it.cavallium.dbengine.client.UninterruptibleScheduler.uninterruptibleScheduler;
|
2022-06-14 17:46:49 +02:00
|
|
|
import static it.cavallium.dbengine.database.LLUtils.singleOrClose;
|
2022-01-28 21:12:10 +01:00
|
|
|
import static it.cavallium.dbengine.lucene.searcher.GlobalQueryRewrite.NO_REWRITE;
|
|
|
|
|
2022-06-14 17:46:49 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-09-18 18:34:21 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearcher;
|
2021-09-20 12:51:27 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
|
2022-07-02 11:44:13 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers.UnshardedIndexSearchers;
|
2022-04-06 02:41:32 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLTempHugePqEnv;
|
2022-07-02 11:44:13 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
2021-12-12 16:41:49 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-07-06 00:30:14 +02:00
|
|
|
import reactor.core.publisher.Mono;
|
2022-01-28 21:12:10 +01:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2021-10-13 12:25:32 +02:00
|
|
|
public class AdaptiveLocalSearcher implements LocalSearcher {
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2022-06-15 13:09:45 +02:00
|
|
|
static final boolean FORCE_HUGE_PQ
|
|
|
|
= Boolean.parseBoolean(System.getProperty("it.cavallium.hugepq.force", "false"));
|
|
|
|
|
2022-03-15 12:36:33 +01:00
|
|
|
private static final StandardSearcher standardSearcher = new StandardSearcher();
|
2021-12-12 16:41:49 +01:00
|
|
|
|
2021-12-12 18:52:50 +01:00
|
|
|
private static final LocalSearcher scoredPaged = new PagedLocalSearcher();
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2021-11-09 00:05:26 +01:00
|
|
|
private static final LocalSearcher countSearcher = new CountMultiSearcher();
|
2021-07-06 01:30:37 +02:00
|
|
|
|
2021-12-23 02:13:51 +01:00
|
|
|
private static final MultiSearcher unsortedUnscoredContinuous = new UnsortedStreamingMultiSearcher();
|
2021-11-21 12:31:23 +01:00
|
|
|
|
2021-12-12 23:40:30 +01:00
|
|
|
/**
|
|
|
|
* Use in-memory collectors if the expected results count is lower or equal than this limit
|
|
|
|
*/
|
|
|
|
private final int maxInMemoryResultEntries;
|
|
|
|
|
2021-12-12 16:41:49 +01:00
|
|
|
@Nullable
|
2021-12-23 02:13:51 +01:00
|
|
|
private final SortedByScoreFullMultiSearcher sortedByScoreFull;
|
2021-11-21 12:31:23 +01:00
|
|
|
|
2021-12-12 16:41:49 +01:00
|
|
|
@Nullable
|
2021-11-21 12:31:23 +01:00
|
|
|
private final SortedScoredFullMultiSearcher sortedScoredFull;
|
|
|
|
|
2022-04-06 02:41:32 +02:00
|
|
|
public AdaptiveLocalSearcher(LLTempHugePqEnv env, boolean useHugePq, int maxInMemoryResultEntries) {
|
2022-06-15 13:09:45 +02:00
|
|
|
sortedByScoreFull = (FORCE_HUGE_PQ || useHugePq) ? new SortedByScoreFullMultiSearcher(env) : null;
|
|
|
|
sortedScoredFull = (FORCE_HUGE_PQ || useHugePq) ? new SortedScoredFullMultiSearcher(env) : null;
|
2021-12-12 23:40:30 +01:00
|
|
|
this.maxInMemoryResultEntries = maxInMemoryResultEntries;
|
2021-11-21 12:31:23 +01:00
|
|
|
}
|
|
|
|
|
2021-07-06 00:30:14 +02:00
|
|
|
@Override
|
2022-06-14 13:10:38 +02:00
|
|
|
public Mono<LuceneSearchResult> collect(Mono<LLIndexSearcher> indexSearcherMono,
|
2021-07-06 01:30:37 +02:00
|
|
|
LocalQueryParams queryParams,
|
2022-02-26 03:28:20 +01:00
|
|
|
@Nullable String keyFieldName,
|
2022-01-28 21:12:10 +01:00
|
|
|
GlobalQueryRewrite transformer) {
|
2022-07-02 11:44:13 +02:00
|
|
|
if (transformer != NO_REWRITE) {
|
|
|
|
return LuceneUtils.rewrite(this, indexSearcherMono, queryParams, keyFieldName, transformer);
|
|
|
|
}
|
|
|
|
return transformedCollect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-10-08 11:50:28 +02:00
|
|
|
}
|
2021-10-13 00:23:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "adaptivelocal";
|
|
|
|
}
|
|
|
|
|
2021-11-21 12:31:23 +01:00
|
|
|
// Remember to change also AdaptiveMultiSearcher
|
2022-07-02 11:44:13 +02:00
|
|
|
public Mono<LuceneSearchResult> transformedCollect(Mono<LLIndexSearcher> indexSearcherMono,
|
2021-10-08 11:50:28 +02:00
|
|
|
LocalQueryParams queryParams,
|
|
|
|
String keyFieldName,
|
2022-01-28 21:12:10 +01:00
|
|
|
GlobalQueryRewrite transformer) {
|
2021-11-21 12:31:23 +01:00
|
|
|
// offset + limit
|
|
|
|
long realLimit = queryParams.offsetLong() + queryParams.limitLong();
|
|
|
|
long maxAllowedInMemoryLimit
|
2021-12-12 23:40:30 +01:00
|
|
|
= Math.max(maxInMemoryResultEntries, (long) queryParams.pageLimits().getPageLimit(0));
|
2021-11-21 12:31:23 +01:00
|
|
|
|
2022-06-15 13:09:45 +02:00
|
|
|
if (!FORCE_HUGE_PQ && queryParams.limitLong() == 0) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return countSearcher.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2022-06-15 13:09:45 +02:00
|
|
|
} else if (!FORCE_HUGE_PQ && realLimit <= maxInMemoryResultEntries) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return standardSearcher.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2022-08-12 02:08:15 +02:00
|
|
|
} else if (queryParams.isSorted()) {
|
2022-06-15 13:09:45 +02:00
|
|
|
if (!FORCE_HUGE_PQ && realLimit <= maxAllowedInMemoryLimit) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return scoredPaged.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-11-21 12:31:23 +01:00
|
|
|
} else {
|
2021-12-23 02:13:51 +01:00
|
|
|
if (queryParams.isSortedByScore()) {
|
2022-06-15 13:09:45 +02:00
|
|
|
if (!FORCE_HUGE_PQ && queryParams.limitLong() < maxInMemoryResultEntries) {
|
2021-12-12 23:40:30 +01:00
|
|
|
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
|
2021-11-21 12:31:23 +01:00
|
|
|
}
|
2021-12-23 02:13:51 +01:00
|
|
|
if (sortedByScoreFull != null) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return sortedByScoreFull.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-12-12 16:41:49 +01:00
|
|
|
} else {
|
2022-07-02 11:44:13 +02:00
|
|
|
return scoredPaged.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-12-12 16:41:49 +01:00
|
|
|
}
|
2021-11-21 12:31:23 +01:00
|
|
|
} else {
|
2022-06-15 13:09:45 +02:00
|
|
|
if (!FORCE_HUGE_PQ && queryParams.limitLong() < maxInMemoryResultEntries) {
|
2021-12-12 23:40:30 +01:00
|
|
|
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
|
2021-11-21 12:31:23 +01:00
|
|
|
}
|
2021-12-23 02:13:51 +01:00
|
|
|
if (sortedScoredFull != null) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return sortedScoredFull.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-12-12 16:41:49 +01:00
|
|
|
} else {
|
2022-07-02 11:44:13 +02:00
|
|
|
return scoredPaged.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-12-12 16:41:49 +01:00
|
|
|
}
|
2021-11-21 12:31:23 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-06 01:30:37 +02:00
|
|
|
} else {
|
2021-11-21 12:31:23 +01:00
|
|
|
// Run large/unbounded searches using the continuous multi searcher
|
2022-07-02 11:44:13 +02:00
|
|
|
return unsortedUnscoredContinuous.collect(indexSearcherMono, queryParams, keyFieldName, transformer);
|
2021-07-06 01:30:37 +02:00
|
|
|
}
|
2021-07-06 00:30:14 +02:00
|
|
|
}
|
|
|
|
}
|