2021-07-06 00:30:14 +02:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
import io.net5.buffer.api.Send;
|
2021-10-08 11:50:28 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-09-20 12:51:27 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
|
2021-10-16 14:35:04 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLTempLMDBEnv;
|
2021-10-08 11:50:28 +02:00
|
|
|
import it.cavallium.dbengine.lucene.searcher.LLSearchTransformer.TransformerInput;
|
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;
|
|
|
|
|
2021-10-16 14:35:04 +02:00
|
|
|
public class AdaptiveMultiSearcher implements MultiSearcher {
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2021-12-12 16:41:49 +01:00
|
|
|
private static final OfficialSearcher officialSearcher = new OfficialSearcher();
|
|
|
|
|
2021-11-09 00:05:26 +01:00
|
|
|
private static final MultiSearcher count = new CountMultiSearcher();
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2021-10-15 22:03:53 +02:00
|
|
|
private static final MultiSearcher scoredPaged = new ScoredPagedMultiSearcher();
|
2021-07-27 19:34:51 +02:00
|
|
|
|
2021-11-09 00:05:26 +01:00
|
|
|
private static final MultiSearcher unsortedUnscoredContinuous = new UnsortedUnscoredStreamingMultiSearcher();
|
2021-09-25 13:07:52 +02: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-10-14 15:55:58 +02:00
|
|
|
private final UnsortedScoredFullMultiSearcher unsortedScoredFull;
|
2021-10-13 00:23:56 +02:00
|
|
|
|
2021-12-12 16:41:49 +01:00
|
|
|
@Nullable
|
2021-10-15 22:03:53 +02:00
|
|
|
private final SortedScoredFullMultiSearcher sortedScoredFull;
|
|
|
|
|
2021-12-12 23:40:30 +01:00
|
|
|
public AdaptiveMultiSearcher(LLTempLMDBEnv env, boolean useLMDB, int maxInMemoryResultEntries) {
|
2021-12-12 16:41:49 +01:00
|
|
|
unsortedScoredFull = useLMDB ? new UnsortedScoredFullMultiSearcher(env) : null;
|
|
|
|
sortedScoredFull = useLMDB ? new SortedScoredFullMultiSearcher(env) : null;
|
2021-12-12 23:40:30 +01:00
|
|
|
this.maxInMemoryResultEntries = maxInMemoryResultEntries;
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 00:30:14 +02:00
|
|
|
@Override
|
2021-11-08 11:17:52 +01:00
|
|
|
public Mono<LuceneSearchResult> collectMulti(Mono<Send<LLIndexSearchers>> indexSearchersMono,
|
2021-09-19 19:59:37 +02:00
|
|
|
LocalQueryParams queryParams,
|
2021-09-20 12:51:27 +02:00
|
|
|
String keyFieldName,
|
|
|
|
LLSearchTransformer transformer) {
|
2021-10-08 11:50:28 +02:00
|
|
|
if (transformer == LLSearchTransformer.NO_TRANSFORMATION) {
|
|
|
|
return transformedCollectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
2021-07-06 00:30:14 +02:00
|
|
|
} else {
|
2021-10-08 11:50:28 +02:00
|
|
|
return LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> transformer
|
|
|
|
.transform(Mono.fromCallable(() -> new TransformerInput(indexSearchers, queryParams)))
|
|
|
|
.flatMap(queryParams2 -> this
|
|
|
|
.transformedCollectMulti(indexSearchersMono, queryParams2, keyFieldName, LLSearchTransformer.NO_TRANSFORMATION)),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 12:31:23 +01:00
|
|
|
// Remember to change also AdaptiveLocalSearcher
|
2021-11-08 11:17:52 +01:00
|
|
|
public Mono<LuceneSearchResult> transformedCollectMulti(Mono<Send<LLIndexSearchers>> indexSearchersMono,
|
2021-10-08 11:50:28 +02:00
|
|
|
LocalQueryParams queryParams,
|
|
|
|
String keyFieldName,
|
|
|
|
LLSearchTransformer transformer) {
|
|
|
|
// offset + limit
|
2021-10-15 22:03:53 +02:00
|
|
|
long realLimit = queryParams.offsetLong() + queryParams.limitLong();
|
2021-10-22 01:26:22 +02:00
|
|
|
long maxAllowedInMemoryLimit
|
2021-12-12 23:40:30 +01:00
|
|
|
= Math.max(maxInMemoryResultEntries, (long) queryParams.pageLimits().getPageLimit(0));
|
2021-10-08 11:50:28 +02:00
|
|
|
|
|
|
|
return LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> {
|
2021-10-15 22:03:53 +02:00
|
|
|
if (queryParams.limitLong() == 0) {
|
2021-10-08 11:50:28 +02:00
|
|
|
return count.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
2021-10-14 00:49:21 +02:00
|
|
|
} else if (queryParams.isSorted() || queryParams.needsScores()) {
|
2021-10-22 01:26:22 +02:00
|
|
|
if (realLimit <= maxAllowedInMemoryLimit) {
|
2021-10-15 22:03:53 +02:00
|
|
|
return scoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
2021-10-13 00:23:56 +02:00
|
|
|
} else {
|
2021-10-15 22:03:53 +02:00
|
|
|
if ((queryParams.isSorted() && !queryParams.isSortedByScore())) {
|
2021-12-12 23:40:30 +01:00
|
|
|
if (queryParams.limitLong() < maxInMemoryResultEntries) {
|
|
|
|
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
|
2021-10-25 00:42:48 +02:00
|
|
|
}
|
2021-12-12 16:41:49 +01:00
|
|
|
if (sortedScoredFull != null) {
|
|
|
|
return sortedScoredFull.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
|
|
|
} else {
|
2021-12-12 18:52:50 +01:00
|
|
|
return scoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
2021-12-12 16:41:49 +01:00
|
|
|
}
|
2021-10-15 22:03:53 +02:00
|
|
|
} else {
|
2021-12-12 23:40:30 +01:00
|
|
|
if (queryParams.limitLong() < maxInMemoryResultEntries) {
|
|
|
|
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
|
2021-10-25 00:42:48 +02:00
|
|
|
}
|
2021-12-12 16:41:49 +01:00
|
|
|
if (unsortedScoredFull != null) {
|
|
|
|
return unsortedScoredFull.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
|
|
|
} else {
|
2021-12-12 18:52:50 +01:00
|
|
|
return scoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
2021-12-12 16:41:49 +01:00
|
|
|
}
|
2021-10-15 22:03:53 +02:00
|
|
|
}
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
2021-09-25 13:07:52 +02:00
|
|
|
} else {
|
|
|
|
// Run large/unbounded searches using the continuous multi searcher
|
2021-10-08 11:50:28 +02:00
|
|
|
return unsortedUnscoredContinuous.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
|
2021-09-25 13:07:52 +02:00
|
|
|
}
|
2021-10-08 11:50:28 +02:00
|
|
|
}, true);
|
2021-07-06 00:30:14 +02:00
|
|
|
}
|
2021-10-13 00:23:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2021-10-13 12:25:32 +02:00
|
|
|
return "adaptive local";
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
2021-07-06 00:30:14 +02:00
|
|
|
}
|