2021-10-28 23:48:25 +02:00
|
|
|
package it.cavallium.dbengine.client;
|
|
|
|
|
|
|
|
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
|
2022-06-30 15:06:10 +02:00
|
|
|
import it.cavallium.dbengine.database.DiscardingCloseable;
|
2022-07-23 15:03:59 +02:00
|
|
|
import it.cavallium.dbengine.database.SafeCloseable;
|
2021-10-28 23:48:25 +02:00
|
|
|
import it.cavallium.dbengine.database.collections.ValueGetter;
|
|
|
|
import it.cavallium.dbengine.database.collections.ValueTransformer;
|
2022-07-23 14:25:59 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneCloseable;
|
2022-06-14 18:05:26 +02:00
|
|
|
import it.cavallium.dbengine.utils.SimpleResource;
|
2021-11-08 18:52:52 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Optional;
|
2021-10-28 23:48:25 +02:00
|
|
|
import java.util.function.Function;
|
2022-07-23 15:03:59 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-10-28 23:48:25 +02:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2022-07-23 14:25:59 +02:00
|
|
|
public class Hits<T> extends SimpleResource implements DiscardingCloseable {
|
2022-06-20 23:31:42 +02:00
|
|
|
|
2022-07-23 15:03:59 +02:00
|
|
|
private static final Logger LOG = LogManager.getLogger(Hits.class);
|
2022-07-23 14:25:59 +02:00
|
|
|
private static final Hits<?> EMPTY_HITS = new Hits<>(Flux.empty(), TotalHitsCount.of(0, true), false);
|
|
|
|
private final Flux<T> results;
|
|
|
|
private final TotalHitsCount totalHitsCount;
|
2021-10-28 23:48:25 +02:00
|
|
|
|
2022-07-23 14:25:59 +02:00
|
|
|
public Hits(Flux<T> results, TotalHitsCount totalHitsCount) {
|
|
|
|
this(results, totalHitsCount, true);
|
2022-06-20 23:31:42 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 14:25:59 +02:00
|
|
|
private Hits(Flux<T> results, TotalHitsCount totalHitsCount, boolean canClose) {
|
|
|
|
super(canClose);
|
2021-10-28 23:48:25 +02:00
|
|
|
this.results = results;
|
|
|
|
this.totalHitsCount = totalHitsCount;
|
|
|
|
}
|
|
|
|
|
2022-06-20 23:31:42 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2021-10-28 23:48:25 +02:00
|
|
|
public static <T> Hits<T> empty() {
|
2022-06-20 23:31:42 +02:00
|
|
|
return (Hits<T>) EMPTY_HITS;
|
2021-10-28 23:48:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 11:17:52 +01:00
|
|
|
public static <T, U> Function<Hits<HitKey<T>>, Hits<LazyHitEntry<T, U>>> generateMapper(
|
2021-10-28 23:48:25 +02:00
|
|
|
ValueGetter<T, U> valueGetter) {
|
2021-11-08 11:17:52 +01:00
|
|
|
return result -> {
|
2021-10-28 23:48:25 +02:00
|
|
|
var hitsToTransform = result.results()
|
|
|
|
.map(hit -> new LazyHitEntry<>(Mono.just(hit.key()), valueGetter.get(hit.key()), hit.score()));
|
2022-07-23 22:13:17 +02:00
|
|
|
return Hits.withResource(hitsToTransform, result.totalHitsCount(), result);
|
2021-10-28 23:48:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:17:52 +01:00
|
|
|
public static <T, U> Function<Hits<HitKey<T>>, Hits<LazyHitEntry<T, U>>> generateMapper(
|
2021-10-28 23:48:25 +02:00
|
|
|
ValueTransformer<T, U> valueTransformer) {
|
2021-11-08 11:17:52 +01:00
|
|
|
return result -> {
|
|
|
|
try {
|
|
|
|
var sharedHitsFlux = result.results().publish().refCount(3);
|
|
|
|
var scoresFlux = sharedHitsFlux.map(HitKey::score);
|
|
|
|
var keysFlux = sharedHitsFlux.map(HitKey::key);
|
|
|
|
|
|
|
|
var valuesFlux = valueTransformer.transform(keysFlux);
|
|
|
|
|
|
|
|
var transformedFlux = Flux.zip((Object[] data) -> {
|
|
|
|
//noinspection unchecked
|
|
|
|
var keyMono = Mono.just((T) data[0]);
|
|
|
|
//noinspection unchecked
|
2021-11-08 18:52:52 +01:00
|
|
|
var val = (Entry<T, Optional<U>>) data[1];
|
|
|
|
var valMono = Mono.justOrEmpty(val.getValue());
|
2021-11-08 11:17:52 +01:00
|
|
|
var score = (Float) data[2];
|
|
|
|
return new LazyHitEntry<>(keyMono, valMono, score);
|
|
|
|
}, keysFlux, valuesFlux, scoresFlux);
|
|
|
|
|
2022-07-23 22:13:17 +02:00
|
|
|
return Hits.withResource(transformedFlux, result.totalHitsCount(), result);
|
2021-11-08 11:17:52 +01:00
|
|
|
} catch (Throwable t) {
|
|
|
|
result.close();
|
|
|
|
throw t;
|
|
|
|
}
|
2021-10-28 23:48:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:03:59 +02:00
|
|
|
public static <T> Hits<T> withResource(Flux<T> hits, TotalHitsCount count, SafeCloseable resource) {
|
|
|
|
if (resource instanceof LuceneCloseable luceneCloseable) {
|
|
|
|
return new LuceneHits<>(hits, count, luceneCloseable);
|
|
|
|
} else {
|
|
|
|
return new CloseableHits<>(hits, count, resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 23:48:25 +02:00
|
|
|
public Flux<T> results() {
|
2022-06-14 18:05:26 +02:00
|
|
|
ensureOpen();
|
2021-10-28 23:48:25 +02:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TotalHitsCount totalHitsCount() {
|
2022-06-14 18:05:26 +02:00
|
|
|
ensureOpen();
|
2021-10-28 23:48:25 +02:00
|
|
|
return totalHitsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Hits[" + "results=" + results + ", " + "totalHitsCount=" + totalHitsCount + ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-14 18:05:26 +02:00
|
|
|
protected void onClose() {
|
2022-07-23 14:25:59 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:03:59 +02:00
|
|
|
public static final class LuceneHits<U> extends Hits<U> implements LuceneCloseable {
|
|
|
|
|
|
|
|
private final LuceneCloseable resource;
|
|
|
|
|
|
|
|
public LuceneHits(Flux<U> hits, TotalHitsCount count, LuceneCloseable resource) {
|
|
|
|
super(hits, count);
|
|
|
|
this.resource = resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onClose() {
|
|
|
|
try {
|
|
|
|
resource.close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("Failed to close resource", ex);
|
|
|
|
}
|
|
|
|
super.onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final class CloseableHits<U> extends Hits<U> {
|
|
|
|
|
|
|
|
private final SafeCloseable resource;
|
|
|
|
|
|
|
|
public CloseableHits(Flux<U> hits, TotalHitsCount count, SafeCloseable resource) {
|
|
|
|
super(hits, count);
|
|
|
|
this.resource = resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onClose() {
|
|
|
|
try {
|
|
|
|
resource.close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("Failed to close resource", ex);
|
|
|
|
}
|
|
|
|
super.onClose();
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 23:48:25 +02:00
|
|
|
}
|