CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/LLSearchResultShard.java

95 lines
2.6 KiB
Java
Raw Normal View History

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;
import java.util.Objects;
import org.warp.commonutils.log.Logger;
import org.warp.commonutils.log.LoggerFactory;
import reactor.core.publisher.Flux;
2021-09-18 18:34:21 +02:00
public final class LLSearchResultShard extends ResourceSupport<LLSearchResultShard, LLSearchResultShard> {
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-09-18 18:34:21 +02:00
public LLSearchResultShard(Flux<LLKeyScore> results, TotalHitsCount totalHitsCount, Drop<LLSearchResultShard> drop) {
super(new LLSearchResultShard.CloseOnDrop(drop));
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"));
}
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"));
}
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);
}
@Override
public int hashCode() {
2021-09-18 18:34:21 +02:00
return Objects.hash(results, totalHitsCount);
}
@Override
public String toString() {
2021-09-18 18:34:21 +02:00
return "LLSearchResultShard[" + "results=" + results + ", " + "totalHitsCount=" + totalHitsCount + ']';
}
@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-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
}
}
}