LLKeyScore is now a regular record

This commit is contained in:
Andrea Cavalli 2021-08-24 11:06:25 +02:00
parent c5552d2827
commit a909aaaf52
3 changed files with 14 additions and 34 deletions

View File

@ -86,7 +86,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
private Mono<SearchResultKeys<T>> transformLuceneResultWithTransformer(LLSearchResultShard llSearchResult) {
return Mono.just(new SearchResultKeys<>(llSearchResult.results()
.map(signal -> new SearchResultKey<>(signal.key().map(indicizer::getKey), signal.score())),
.map(signal -> new SearchResultKey<>(Mono.fromCallable(signal::key).map(indicizer::getKey), signal.score())),
llSearchResult.totalHitsCount(),
llSearchResult.release()
));
@ -95,7 +95,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
private Mono<SearchResult<T, U>> transformLuceneResultWithValues(LLSearchResultShard llSearchResult,
ValueGetter<T, U> valueGetter) {
return Mono.fromCallable(() -> new SearchResult<>(llSearchResult.results().map(signal -> {
var key = signal.key().map(indicizer::getKey);
var key = Mono.fromCallable(signal::key).map(indicizer::getKey);
return new SearchResultItem<>(key, key.flatMap(valueGetter::get), signal.score());
}), llSearchResult.totalHitsCount(), llSearchResult.release()));
}
@ -104,7 +104,11 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
ValueTransformer<T, U> valueTransformer) {
var scoresWithKeysFlux = llSearchResult
.results()
.flatMapSequential(signal -> signal.key().map(indicizer::getKey).map(key -> Tuples.of(signal.score(), key)));
.flatMapSequential(signal -> Mono
.fromCallable(signal::key)
.map(indicizer::getKey)
.map(key -> Tuples.of(signal.score(), key))
);
var resultItemsFlux = valueTransformer
.transform(scoresWithKeysFlux)
.filter(tuple3 -> tuple3.getT3().isPresent())

View File

@ -2,32 +2,7 @@ package it.cavallium.dbengine.database;
import java.util.Objects;
import java.util.StringJoiner;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Mono;
public record LLKeyScore(int docId, float score, Mono<String> key) {
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LLKeyScore that = (LLKeyScore) o;
return docId == that.docId && Float.compare(that.score, score) == 0;
}
@Override
public int hashCode() {
return Objects.hash(docId, score);
}
@Override
public String toString() {
return new StringJoiner(", ", LLKeyScore.class.getSimpleName() + "[", "]")
.add("docId=" + docId)
.add("score=" + score)
.toString();
}
}
public record LLKeyScore(int docId, float score, @Nullable String key) {}

View File

@ -370,7 +370,7 @@ public class LuceneUtils {
.map(hit -> {
var result = mapHitBlocking(hit, indexSearchers, keyFieldName);
// The "else" value is an errored key score, to filter out next
return Objects.requireNonNullElseGet(result, () -> new LLKeyScore(-1, -1, Mono.empty()));
return Objects.requireNonNullElseGet(result, () -> new LLKeyScore(-1, -1, null));
})
.sequential()
// Filter out the errored key scores
@ -389,12 +389,13 @@ public class LuceneUtils {
var indexSearcher = indexSearchers.shard(shardIndex);
try {
String collectedDoc = keyOfTopDoc(shardDocId, indexSearcher.getIndexReader(), keyFieldName);
return new LLKeyScore(shardDocId, score, Mono.just(collectedDoc));
return new LLKeyScore(shardDocId, score, collectedDoc);
} catch (NoSuchElementException ex) {
logger.debug("Error: document " + shardDocId + " key is not present!");
logger.debug("Error: document {} key is not present!", shardDocId);
return null;
} catch (Exception ex) {
return new LLKeyScore(shardDocId, score, Mono.error(ex));
logger.error("Failed to read document {}", shardDocId, ex);
return new LLKeyScore(shardDocId, score, null);
}
}