CavalliumDBEngine/src/main/java/it/cavallium/dbengine/lucene/searcher/AdaptiveMultiSearcher.java

103 lines
4.3 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.lucene.searcher;
2021-09-18 18:34:21 +02:00
import io.net5.buffer.api.Send;
import it.cavallium.dbengine.database.LLUtils;
2021-09-20 12:51:27 +02:00
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
import it.cavallium.dbengine.database.disk.LLTempLMDBEnv;
import it.cavallium.dbengine.lucene.searcher.LLSearchTransformer.TransformerInput;
2021-12-12 16:41:49 +01:00
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Mono;
public class AdaptiveMultiSearcher implements MultiSearcher {
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-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-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-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;
}
@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) {
if (transformer == LLSearchTransformer.NO_TRANSFORMATION) {
return transformedCollectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
} else {
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,
LocalQueryParams queryParams,
String keyFieldName,
LLSearchTransformer transformer) {
// offset + limit
2021-10-15 22:03:53 +02:00
long realLimit = queryParams.offsetLong() + queryParams.limitLong();
long maxAllowedInMemoryLimit
2021-12-12 23:40:30 +01:00
= Math.max(maxInMemoryResultEntries, (long) queryParams.pageLimits().getPageLimit(0));
return LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> {
2021-10-15 22:03:53 +02:00
if (queryParams.limitLong() == 0) {
return count.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
} else if (queryParams.isSorted() || queryParams.needsScores()) {
if (realLimit <= maxAllowedInMemoryLimit) {
2021-10-15 22:03:53 +02:00
return scoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
} 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 {
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 {
return scoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
2021-12-12 16:41:49 +01:00
}
2021-10-15 22:03:53 +02:00
}
}
} else {
// Run large/unbounded searches using the continuous multi searcher
return unsortedUnscoredContinuous.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
}
}, true);
}
@Override
public String getName() {
return "adaptive local";
}
}