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

107 lines
4.2 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 io.net5.buffer.api.internal.ResourceSupport;
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;
2021-11-21 12:31:23 +01:00
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 AdaptiveLocalSearcher implements LocalSearcher {
2021-12-12 16:41:49 +01:00
private static final OfficialSearcher officialSearcher = new OfficialSearcher();
private static final LocalSearcher scoredPaged = new PagedLocalSearcher();
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;
2021-12-12 23:40:30 +01:00
public AdaptiveLocalSearcher(LLTempLMDBEnv env, boolean useLMDB, int maxInMemoryResultEntries) {
2021-12-23 02:13:51 +01:00
sortedByScoreFull = useLMDB ? new SortedByScoreFullMultiSearcher(env) : null;
2021-12-12 16:41:49 +01:00
sortedScoredFull = useLMDB ? new SortedScoredFullMultiSearcher(env) : null;
2021-12-12 23:40:30 +01:00
this.maxInMemoryResultEntries = maxInMemoryResultEntries;
2021-11-21 12:31:23 +01:00
}
@Override
2021-11-08 11:17:52 +01:00
public Mono<LuceneSearchResult> collect(Mono<Send<LLIndexSearcher>> indexSearcher,
2021-07-06 01:30:37 +02:00
LocalQueryParams queryParams,
2021-09-20 12:51:27 +02:00
String keyFieldName,
LLSearchTransformer transformer) {
Mono<Send<LLIndexSearchers>> indexSearchersMono = indexSearcher
.map(LLIndexSearchers::unsharded)
.map(ResourceSupport::send);
if (transformer == LLSearchTransformer.NO_TRANSFORMATION) {
return transformedCollect(indexSearcher, queryParams, keyFieldName, transformer);
} else {
return LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> transformer
.transform(Mono.fromCallable(() -> new TransformerInput(indexSearchers, queryParams)))
.flatMap(queryParams2 -> this
.transformedCollect(indexSearcher, queryParams2, keyFieldName, LLSearchTransformer.NO_TRANSFORMATION)),
true);
}
}
@Override
public String getName() {
return "adaptivelocal";
}
2021-11-21 12:31:23 +01:00
// Remember to change also AdaptiveMultiSearcher
2021-11-08 11:17:52 +01:00
public Mono<LuceneSearchResult> transformedCollect(Mono<Send<LLIndexSearcher>> indexSearcher,
LocalQueryParams queryParams,
String keyFieldName,
LLSearchTransformer 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
2021-10-15 22:03:53 +02:00
if (queryParams.limitLong() == 0) {
2021-09-20 12:51:27 +02:00
return countSearcher.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-12-23 02:13:51 +01:00
} else if (queryParams.isSorted()) {
2021-11-21 12:31:23 +01:00
if (realLimit <= maxAllowedInMemoryLimit) {
return scoredPaged.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-11-21 12:31:23 +01:00
} else {
2021-12-23 02:13:51 +01:00
if (queryParams.isSortedByScore()) {
2021-12-12 23:40:30 +01:00
if (queryParams.limitLong() < maxInMemoryResultEntries) {
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) {
return sortedByScoreFull.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-12-12 16:41:49 +01:00
} else {
return scoredPaged.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-12-12 16:41:49 +01:00
}
2021-11-21 12:31:23 +01:00
} else {
2021-12-12 23:40:30 +01:00
if (queryParams.limitLong() < maxInMemoryResultEntries) {
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) {
return sortedScoredFull.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-12-12 16:41:49 +01:00
} else {
return scoredPaged.collect(indexSearcher, 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
return unsortedUnscoredContinuous.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-07-06 01:30:37 +02:00
}
}
}