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

109 lines
2.8 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.lucene.searcher;
2021-09-18 18:34:21 +02:00
import io.net5.buffer.api.Drop;
import io.net5.buffer.api.Owned;
2021-08-04 01:12:39 +02:00
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
import it.cavallium.dbengine.database.LLKeyScore;
2021-10-17 17:15:57 +02:00
import io.net5.buffer.api.internal.ResourceSupport;
2021-07-10 20:52:01 +02:00
import java.util.Objects;
import org.warp.commonutils.log.Logger;
import org.warp.commonutils.log.LoggerFactory;
import reactor.core.publisher.Flux;
2021-07-10 20:52:01 +02:00
2021-10-17 17:15:57 +02:00
public final class LuceneSearchResult extends ResourceSupport<LuceneSearchResult, LuceneSearchResult> {
2021-07-10 20:52:01 +02:00
private static final Logger logger = LoggerFactory.getLogger(LuceneSearchResult.class);
2021-07-10 20:52:01 +02:00
2021-10-01 19:17:33 +02:00
private static final Drop<LuceneSearchResult> DROP = new Drop<>() {
@Override
public void drop(LuceneSearchResult obj) {
try {
if (obj.onClose != null) {
obj.onClose.run();
}
} catch (Throwable ex) {
logger.error("Failed to close onClose", ex);
}
}
@Override
public Drop<LuceneSearchResult> fork() {
return this;
}
@Override
public void attach(LuceneSearchResult obj) {
}
};
2021-09-18 18:34:21 +02:00
private TotalHitsCount totalHitsCount;
private Flux<LLKeyScore> results;
2021-10-01 19:17:33 +02:00
private Runnable onClose;
2021-10-01 19:17:33 +02:00
public LuceneSearchResult(TotalHitsCount totalHitsCount, Flux<LLKeyScore> results, Runnable onClose) {
super(DROP);
2021-07-10 20:52:01 +02:00
this.totalHitsCount = totalHitsCount;
this.results = results;
2021-10-01 19:17:33 +02:00
this.onClose = onClose;
2021-07-10 20:52:01 +02:00
}
2021-08-04 01:12:39 +02:00
public TotalHitsCount totalHitsCount() {
2021-09-18 18:34:21 +02:00
if (!isOwned()) {
throw attachTrace(new IllegalStateException("LuceneSearchResult must be owned to be used"));
}
2021-07-10 20:52:01 +02:00
return totalHitsCount;
}
public Flux<LLKeyScore> results() {
2021-09-18 18:34:21 +02:00
if (!isOwned()) {
throw attachTrace(new IllegalStateException("LuceneSearchResult must be owned to be used"));
}
2021-07-10 20:52:01 +02:00
return results;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (LuceneSearchResult) obj;
return this.totalHitsCount == that.totalHitsCount && Objects.equals(this.results, that.results);
}
@Override
public int hashCode() {
return Objects.hash(totalHitsCount, results);
}
@Override
public String toString() {
return "LuceneSearchResult[" + "totalHitsCount=" + totalHitsCount + ", " + "results=" + results + ']';
}
@Override
2021-09-18 18:34:21 +02:00
protected RuntimeException createResourceClosedException() {
return new IllegalStateException("Closed");
}
@Override
protected Owned<LuceneSearchResult> prepareSend() {
var totalHitsCount = this.totalHitsCount;
var results = this.results;
2021-10-01 19:17:33 +02:00
var onClose = this.onClose;
return drop -> {
var instance = new LuceneSearchResult(totalHitsCount, results, onClose);
drop.attach(instance);
return instance;
};
2021-09-18 18:34:21 +02:00
}
2021-09-23 15:34:56 +02:00
protected void makeInaccessible() {
2021-09-18 18:34:21 +02:00
this.totalHitsCount = null;
this.results = null;
2021-10-01 19:17:33 +02:00
this.onClose = null;
2021-09-18 18:34:21 +02:00
}
}