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;
|
|
|
|
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;
|
|
|
|
import org.warp.commonutils.log.Logger;
|
|
|
|
import org.warp.commonutils.log.LoggerFactory;
|
2021-03-27 03:35:27 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
public final class LLSearchResultShard extends ResourceSupport<LLSearchResultShard, LLSearchResultShard> {
|
2021-08-24 11:53:19 +02:00
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LLSearchResultShard.class);
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
private Flux<LLKeyScore> results;
|
|
|
|
private TotalHitsCount totalHitsCount;
|
2021-08-24 11:53:19 +02:00
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
public LLSearchResultShard(Flux<LLKeyScore> results, TotalHitsCount totalHitsCount, Drop<LLSearchResultShard> drop) {
|
|
|
|
super(new LLSearchResultShard.CloseOnDrop(drop));
|
2021-08-24 11:53:19 +02:00
|
|
|
this.results = results;
|
|
|
|
this.totalHitsCount = totalHitsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
makeInaccessible();
|
|
|
|
return drop -> new LLSearchResultShard(results, totalHitsCount, drop);
|
2021-08-24 11:53:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
private void makeInaccessible() {
|
|
|
|
this.results = null;
|
|
|
|
this.totalHitsCount = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class CloseOnDrop implements Drop<LLSearchResultShard> {
|
|
|
|
|
|
|
|
private final Drop<LLSearchResultShard> delegate;
|
|
|
|
|
|
|
|
public CloseOnDrop(Drop<LLSearchResultShard> drop) {
|
|
|
|
this.delegate = drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void drop(LLSearchResultShard obj) {
|
2021-09-21 02:01:02 +02:00
|
|
|
try {
|
|
|
|
delegate.drop(obj);
|
|
|
|
} finally {
|
|
|
|
obj.makeInaccessible();
|
|
|
|
}
|
2021-09-18 18:34:21 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-24 11:53:19 +02:00
|
|
|
}
|