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

137 lines
5.1 KiB
Java
Raw Normal View History

2021-11-09 00:05:26 +01:00
package it.cavallium.dbengine.lucene.searcher;
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-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Send;
2021-11-09 00:05:26 +01:00
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
import it.cavallium.dbengine.database.LLKeyScore;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.disk.LLIndexSearcher;
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
2022-06-14 18:05:26 +02:00
import it.cavallium.dbengine.utils.SimpleResource;
2022-01-28 21:12:10 +01:00
import java.io.IOException;
2021-11-09 00:05:26 +01:00
import java.util.ArrayList;
import java.util.List;
2022-06-14 13:10:38 +02:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2022-02-26 03:28:20 +01:00
import org.jetbrains.annotations.Nullable;
2021-11-09 00:05:26 +01:00
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
public class CountMultiSearcher implements MultiSearcher {
2022-06-14 13:10:38 +02:00
protected static final Logger LOG = LogManager.getLogger(CountMultiSearcher.class);
2021-11-09 00:05:26 +01:00
@Override
2022-06-14 13:10:38 +02:00
public Mono<LuceneSearchResult> collectMulti(Mono<LLIndexSearchers> indexSearchersMono,
2021-11-09 00:05:26 +01:00
LocalQueryParams queryParams,
String keyFieldName,
2022-01-28 21:12:10 +01:00
GlobalQueryRewrite transformer) {
2022-06-14 17:46:49 +02:00
return singleOrClose(indexSearchersMono, indexSearchers -> {
2022-06-14 13:10:38 +02:00
Mono<LocalQueryParams> queryParamsMono;
if (transformer == GlobalQueryRewrite.NO_REWRITE) {
queryParamsMono = Mono.just(queryParams);
} else {
queryParamsMono = Mono
.fromCallable(() -> transformer.rewrite(indexSearchers, queryParams))
.subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic()));
}
2021-11-09 00:05:26 +01:00
2022-06-14 13:10:38 +02:00
return queryParamsMono.flatMap(queryParams2 -> {
var localQueryParams = getLocalQueryParams(queryParams2);
return Mono
.fromRunnable(() -> {
if (queryParams2.isSorted() && queryParams2.limitLong() > 0) {
throw new UnsupportedOperationException(
"Sorted queries are not supported by SimpleUnsortedUnscoredLuceneMultiSearcher");
}
if (queryParams2.needsScores() && queryParams2.limitLong() > 0) {
throw new UnsupportedOperationException(
"Scored queries are not supported by SimpleUnsortedUnscoredLuceneMultiSearcher");
}
})
.thenMany(Flux.fromIterable(indexSearchers.llShards()))
.flatMap(searcher -> this.collect(Mono.just(searcher), localQueryParams, keyFieldName, transformer))
.collectList()
.map(results -> {
List<LuceneSearchResult> resultsToDrop = new ArrayList<>(results.size());
List<Flux<LLKeyScore>> resultsFluxes = new ArrayList<>(results.size());
boolean exactTotalHitsCount = true;
long totalHitsCountValue = 0;
for (LuceneSearchResult result : results) {
resultsToDrop.add(result);
resultsFluxes.add(result.results());
exactTotalHitsCount &= result.totalHitsCount().exact();
totalHitsCountValue += result.totalHitsCount().value();
}
2021-11-09 00:05:26 +01:00
2022-06-14 13:10:38 +02:00
var totalHitsCount = new TotalHitsCount(totalHitsCountValue, exactTotalHitsCount);
Flux<LLKeyScore> mergedFluxes = Flux
.merge(resultsFluxes)
.skip(queryParams2.offsetLong())
.take(queryParams2.limitLong(), true);
2021-11-09 00:05:26 +01:00
2022-06-14 13:10:38 +02:00
return new LuceneSearchResult(totalHitsCount, mergedFluxes, () -> {
2022-06-14 18:05:26 +02:00
resultsToDrop.forEach(SimpleResource::close);
2022-06-14 13:10:38 +02:00
try {
indexSearchers.close();
} catch (IOException e) {
LOG.error("Can't close index searchers", e);
}
});
});
2022-01-28 21:12:10 +01:00
});
2022-06-14 13:10:38 +02:00
});
2021-11-09 00:05:26 +01:00
}
private LocalQueryParams getLocalQueryParams(LocalQueryParams queryParams) {
return new LocalQueryParams(queryParams.query(),
0L,
queryParams.offsetLong() + queryParams.limitLong(),
queryParams.pageLimits(),
queryParams.sort(),
2021-12-12 23:40:30 +01:00
queryParams.computePreciseHitsCount(),
queryParams.timeout()
2021-11-09 00:05:26 +01:00
);
}
@Override
2022-06-14 13:10:38 +02:00
public Mono<LuceneSearchResult> collect(Mono<LLIndexSearcher> indexSearcherMono,
2021-11-09 00:05:26 +01: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-06-14 17:46:49 +02:00
return singleOrClose(indexSearcherMono, indexSearcher -> {
2022-06-14 13:10:38 +02:00
Mono<LocalQueryParams> queryParamsMono;
if (transformer == GlobalQueryRewrite.NO_REWRITE) {
queryParamsMono = Mono.just(queryParams);
} else {
queryParamsMono = Mono
.fromCallable(() -> transformer.rewrite(LLIndexSearchers.unsharded(indexSearcher), queryParams))
.subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic()));
}
2021-12-17 16:24:18 +01:00
2022-06-14 13:10:38 +02:00
return queryParamsMono
.flatMap(queryParams2 -> Mono.fromCallable(() -> {
LLUtils.ensureBlocking();
return (long) indexSearcher.getIndexSearcher().count(queryParams2.query());
}).subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic())))
.publishOn(Schedulers.parallel())
.transform(TimeoutUtil.timeoutMono(queryParams.timeout()))
.map(count -> new LuceneSearchResult(TotalHitsCount.of(count, true), Flux.empty(), () -> {
try {
indexSearcher.close();
} catch (IOException e) {
LOG.error("Can't close index searchers", e);
}
}));
});
2021-11-09 00:05:26 +01:00
}
@Override
public String getName() {
return "count";
}
}