CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/disk/LLIndexSearcher.java

82 lines
2.0 KiB
Java
Raw Normal View History

2021-09-18 18:34:21 +02:00
package it.cavallium.dbengine.database.disk;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Drop;
import io.netty5.buffer.api.Owned;
import io.netty5.buffer.api.internal.ResourceSupport;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-09-18 18:34:21 +02:00
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
2021-10-17 17:15:57 +02:00
public class LLIndexSearcher extends ResourceSupport<LLIndexSearcher, LLIndexSearcher> {
2021-09-18 18:34:21 +02:00
private static final Logger logger = LogManager.getLogger(LLIndexSearcher.class);
2021-10-01 19:17:33 +02:00
private static final Drop<LLIndexSearcher> DROP = new Drop<>() {
@Override
public void drop(LLIndexSearcher obj) {
try {
if (obj.onClose != null) {
obj.onClose.run();
}
} catch (Throwable ex) {
logger.error("Failed to close onClose", ex);
}
}
@Override
public Drop<LLIndexSearcher> fork() {
return this;
}
@Override
public void attach(LLIndexSearcher obj) {
}
};
2021-09-18 18:34:21 +02:00
private IndexSearcher indexSearcher;
2021-09-24 02:44:12 +02:00
private final boolean decRef;
2021-09-18 18:34:21 +02:00
2021-10-01 19:17:33 +02:00
private Runnable onClose;
public LLIndexSearcher(IndexSearcher indexSearcher, boolean decRef, Runnable onClose) {
super(DROP);
2021-09-18 18:34:21 +02:00
this.indexSearcher = indexSearcher;
2021-09-24 02:44:12 +02:00
this.decRef = decRef;
2021-10-01 19:17:33 +02:00
this.onClose = onClose;
2021-09-18 18:34:21 +02:00
}
public IndexReader getIndexReader() {
if (!isOwned()) {
2021-09-22 11:03:39 +02:00
throw attachTrace(new IllegalStateException("LLIndexSearcher must be owned to be used"));
2021-09-18 18:34:21 +02:00
}
return indexSearcher.getIndexReader();
}
public IndexSearcher getIndexSearcher() {
if (!isOwned()) {
2021-09-22 11:03:39 +02:00
throw attachTrace(new IllegalStateException("LLIndexSearcher must be owned to be used"));
2021-09-18 18:34:21 +02:00
}
return indexSearcher;
}
@Override
protected RuntimeException createResourceClosedException() {
return new IllegalStateException("Closed");
}
@Override
protected Owned<LLIndexSearcher> prepareSend() {
var indexSearcher = this.indexSearcher;
2021-10-01 19:17:33 +02:00
var onClose = this.onClose;
return drop -> new LLIndexSearcher(indexSearcher, decRef, onClose);
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.indexSearcher = null;
2021-10-01 19:17:33 +02:00
this.onClose = null;
2021-09-18 18:34:21 +02:00
}
2021-09-24 02:44:12 +02:00
2021-09-18 18:34:21 +02:00
}