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

135 lines
4.9 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;
2021-11-09 00:05:26 +01:00
import io.net5.buffer.api.Send;
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-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-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 {
@Override
public Mono<LuceneSearchResult> collectMulti(Mono<Send<LLIndexSearchers>> indexSearchersMono,
LocalQueryParams queryParams,
String keyFieldName,
2022-01-28 21:12:10 +01:00
GlobalQueryRewrite transformer) {
Mono<LocalQueryParams> queryParamsMono;
if (transformer == GlobalQueryRewrite.NO_REWRITE) {
queryParamsMono = Mono.just(queryParams);
} else {
queryParamsMono = indexSearchersMono
.publishOn(uninterruptibleScheduler(Schedulers.boundedElastic()))
.handle((indexSearchers, sink) -> {
try {
sink.next(transformer.rewrite(indexSearchers.receive(), queryParams));
} catch (IOException ex) {
sink.error(ex);
}
});
}
2021-11-09 00:05:26 +01:00
2022-01-28 21:12:10 +01:00
return queryParamsMono.flatMap(queryParams2 -> LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> {
var localQueryParams = getLocalQueryParams(queryParams2);
return Mono.fromRunnable(() -> {
LLUtils.ensureBlocking();
if (queryParams2.isSorted() && queryParams2.limitLong() > 0) {
throw new UnsupportedOperationException(
2022-02-11 15:27:12 +01:00
"Sorted queries are not supported by SimpleUnsortedUnscoredLuceneMultiSearcher");
2022-01-28 21:12:10 +01:00
}
if (queryParams2.needsScores() && queryParams2.limitLong() > 0) {
throw new UnsupportedOperationException(
2022-02-11 15:27:12 +01:00
"Scored queries are not supported by SimpleUnsortedUnscoredLuceneMultiSearcher");
2022-01-28 21:12:10 +01:00
}
}).thenMany(Flux.fromIterable(indexSearchers.shards())).flatMap(searcher -> {
var llSearcher = Mono.fromCallable(() -> new LLIndexSearcher(searcher, false, null).send());
return this.collect(llSearcher, 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-01-28 21:12:10 +01: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-01-28 21:12:10 +01:00
return new LuceneSearchResult(totalHitsCount, mergedFluxes, () -> {
for (LuceneSearchResult luceneSearchResult : resultsToDrop) {
luceneSearchResult.close();
}
indexSearchers.close();
});
});
}, false));
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
public Mono<LuceneSearchResult> collect(Mono<Send<LLIndexSearcher>> indexSearcherMono,
LocalQueryParams queryParams,
2022-02-26 03:28:20 +01:00
@Nullable String keyFieldName,
2022-01-28 21:12:10 +01:00
GlobalQueryRewrite transformer) {
2021-11-09 00:05:26 +01:00
return Mono
.usingWhen(
indexSearcherMono,
indexSearcher -> {
Mono<LocalQueryParams> queryParamsMono;
2022-01-28 21:12:10 +01:00
if (transformer == GlobalQueryRewrite.NO_REWRITE) {
2021-11-09 00:05:26 +01:00
queryParamsMono = Mono.just(queryParams);
} else {
2022-01-28 21:12:10 +01:00
queryParamsMono = Mono
.fromCallable(() -> transformer.rewrite(LLIndexSearchers.unsharded(indexSearcher), queryParams))
.subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic()));
2021-11-09 00:05:26 +01:00
}
2021-12-12 23:40:30 +01:00
return queryParamsMono
.flatMap(queryParams2 -> Mono.fromCallable(() -> {
try (var is = indexSearcher.receive()) {
LLUtils.ensureBlocking();
2021-12-17 16:24:18 +01:00
return (long) is.getIndexSearcher().count(queryParams2.query());
2021-12-12 23:40:30 +01:00
}
}).subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic())))
.publishOn(Schedulers.parallel())
2022-02-25 15:46:32 +01:00
.transform(TimeoutUtil.timeoutMono(queryParams.timeout()));
2021-11-09 00:05:26 +01:00
},
is -> Mono.empty()
)
2022-01-26 14:22:54 +01:00
.map(count -> new LuceneSearchResult(TotalHitsCount.of(count, true), Flux.empty(), null));
2021-11-09 00:05:26 +01:00
}
@Override
public String getName() {
return "count";
}
}