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

103 lines
2.7 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;
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;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import reactor.core.publisher.Flux;
2021-10-17 17:15:57 +02:00
public final class LLSearchResultShard extends ResourceSupport<LLSearchResultShard, LLSearchResultShard> {
private static final Logger logger = LogManager.getLogger(LLSearchResultShard.class);
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-10-01 19:17:33 +02:00
public LLSearchResultShard(Flux<LLKeyScore> results, TotalHitsCount totalHitsCount, Runnable onClose) {
super(DROP);
this.results = results;
this.totalHitsCount = totalHitsCount;
2021-10-01 19:17:33 +02:00
this.onClose = onClose;
}
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;
2021-10-01 19:17:33 +02:00
var onClose = this.onClose;
return drop -> new LLSearchResultShard(results, totalHitsCount, onClose);
}
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
}
}