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