Code cleanup
This commit is contained in:
parent
d2e7c56f06
commit
ea2065302a
@ -16,18 +16,26 @@ import reactor.core.publisher.Mono;
|
|||||||
import reactor.util.function.Tuples;
|
import reactor.util.function.Tuples;
|
||||||
|
|
||||||
public final class Hits<T> extends SimpleResource {
|
public final class Hits<T> extends SimpleResource {
|
||||||
|
|
||||||
|
private static final Hits<?> EMPTY_HITS = new Hits<>(Flux.empty(), TotalHitsCount.of(0, true), null, false);
|
||||||
private Flux<T> results;
|
private Flux<T> results;
|
||||||
private TotalHitsCount totalHitsCount;
|
private TotalHitsCount totalHitsCount;
|
||||||
private Runnable onClose;
|
private Runnable onClose;
|
||||||
|
|
||||||
public Hits(Flux<T> results, TotalHitsCount totalHitsCount, Runnable onClose) {
|
public Hits(Flux<T> results, TotalHitsCount totalHitsCount, Runnable onClose) {
|
||||||
|
this(results, totalHitsCount, onClose, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Hits(Flux<T> results, TotalHitsCount totalHitsCount, Runnable onClose, boolean canClose) {
|
||||||
|
super(canClose);
|
||||||
this.results = results;
|
this.results = results;
|
||||||
this.totalHitsCount = totalHitsCount;
|
this.totalHitsCount = totalHitsCount;
|
||||||
this.onClose = onClose;
|
this.onClose = onClose;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> Hits<T> empty() {
|
public static <T> Hits<T> empty() {
|
||||||
return new Hits<>(Flux.empty(), TotalHitsCount.of(0, true), null);
|
return (Hits<T>) EMPTY_HITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <K, V> Hits<LazyHitEntry<K, V>> withValuesLazy(Hits<LazyHitKey<K>> hits,
|
public static <K, V> Hits<LazyHitEntry<K, V>> withValuesLazy(Hits<LazyHitKey<K>> hits,
|
||||||
|
@ -124,7 +124,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
|
|||||||
.collectList()
|
.collectList()
|
||||||
.flatMap(shards -> mergeResults(queryParams, shards))
|
.flatMap(shards -> mergeResults(queryParams, shards))
|
||||||
.map(this::mapResults)
|
.map(this::mapResults)
|
||||||
.single()
|
.defaultIfEmpty(Hits.empty())
|
||||||
.doOnDiscard(LLSearchResultShard.class, SimpleResource::close)
|
.doOnDiscard(LLSearchResultShard.class, SimpleResource::close)
|
||||||
.doOnDiscard(Hits.class, SimpleResource::close);
|
.doOnDiscard(Hits.class, SimpleResource::close);
|
||||||
}
|
}
|
||||||
@ -153,9 +153,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
|
|||||||
.query(query)
|
.query(query)
|
||||||
.timeout(Duration.ofSeconds(30))
|
.timeout(Duration.ofSeconds(30))
|
||||||
.limit(0)
|
.limit(0)
|
||||||
.build()), searchResultKeys -> {
|
.build()), searchResultKeys -> Mono.just(searchResultKeys.totalHitsCount()), LLUtils::finalizeResource);
|
||||||
return Mono.just(searchResultKeys.totalHitsCount());
|
|
||||||
}, LLUtils::finalizeResource);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -6,20 +6,29 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
public abstract class SimpleResource implements SafeCloseable {
|
public abstract class SimpleResource implements SafeCloseable {
|
||||||
|
|
||||||
private final AtomicBoolean closed = new AtomicBoolean();
|
private final AtomicBoolean closed = new AtomicBoolean();
|
||||||
|
private final boolean canClose;
|
||||||
|
|
||||||
|
public SimpleResource() {
|
||||||
|
canClose = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected SimpleResource(boolean canClose) {
|
||||||
|
this.canClose = canClose;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void close() {
|
public final void close() {
|
||||||
if (closed.compareAndSet(false, true)) {
|
if (canClose && closed.compareAndSet(false, true)) {
|
||||||
onClose();
|
onClose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isClosed() {
|
private boolean isClosed() {
|
||||||
return closed.get();
|
return canClose && closed.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ensureOpen() {
|
protected void ensureOpen() {
|
||||||
if (closed.get()) {
|
if (canClose && closed.get()) {
|
||||||
throw new IllegalStateException("Resource is closed");
|
throw new IllegalStateException("Resource is closed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user