Use in-memory database if the limit is lower than 8192

This commit is contained in:
Andrea Cavalli 2021-10-22 01:26:22 +02:00
parent a77af5253d
commit 90aa7a2522
4 changed files with 16 additions and 2 deletions

View File

@ -53,12 +53,14 @@ public class AdaptiveMultiSearcher implements MultiSearcher {
LLSearchTransformer transformer) {
// offset + limit
long realLimit = queryParams.offsetLong() + queryParams.limitLong();
long maxAllowedInMemoryLimit
= Math.max(MultiSearcher.MAX_IN_MEMORY_SIZE, (long) queryParams.pageLimits().getPageLimit(0));
return LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> {
if (queryParams.limitLong() == 0) {
return count.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
} else if (queryParams.isSorted() || queryParams.needsScores()) {
if (realLimit <= (long) queryParams.pageLimits().getPageLimit(0)) {
if (realLimit <= maxAllowedInMemoryLimit) {
return scoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
} else {
if ((queryParams.isSorted() && !queryParams.isSortedByScore())) {
@ -67,7 +69,7 @@ public class AdaptiveMultiSearcher implements MultiSearcher {
return unsortedScoredFull.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
}
}
} else if (realLimit <= (long) queryParams.pageLimits().getPageLimit(0)) {
} else if (realLimit <= maxAllowedInMemoryLimit) {
// Run single-page searches using the paged multi searcher
return unsortedUnscoredPaged.collectMulti(indexSearchersMono, queryParams, keyFieldName, transformer);
} else {

View File

@ -7,6 +7,11 @@ import reactor.core.publisher.Mono;
public interface MultiSearcher extends LocalSearcher {
/**
* Use in-memory collectors if the expected results count is lower or equal than this limit
*/
int MAX_IN_MEMORY_SIZE = 8192;
/**
* @param indexSearchersMono Lucene index searcher
* @param queryParams the query parameters

View File

@ -16,6 +16,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.util.ServiceLoader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopFieldCollector;
import org.warp.commonutils.log.Logger;
import org.warp.commonutils.log.LoggerFactory;
import reactor.core.publisher.Flux;
@ -64,6 +65,9 @@ public class SortedScoredFullMultiSearcher implements MultiSearcher {
.fromCallable(() -> {
LLUtils.ensureBlocking();
var totalHitsThreshold = queryParams.getTotalHitsThresholdLong();
if (queryParams.limitLong() < MAX_IN_MEMORY_SIZE) {
throw new UnsupportedOperationException("Allowed limit is " + MAX_IN_MEMORY_SIZE + " or greater");
}
return LMDBFullFieldDocCollector.createSharedManager(env, queryParams.sort(), queryParams.limitInt(),
totalHitsThreshold);
})

View File

@ -69,6 +69,9 @@ public class UnsortedScoredFullMultiSearcher implements MultiSearcher {
return Mono
.fromCallable(() -> {
LLUtils.ensureBlocking();
if (queryParams.limitLong() < MAX_IN_MEMORY_SIZE) {
throw new UnsupportedOperationException("Allowed limit is " + MAX_IN_MEMORY_SIZE + " or greater");
}
var totalHitsThreshold = queryParams.getTotalHitsThresholdLong();
return LMDBFullScoreDocCollector.createSharedManager(env, queryParams.limitLong(), totalHitsThreshold);
})