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

55 lines
1.5 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;
2022-06-14 13:10:38 +02:00
import it.cavallium.dbengine.database.SafeCloseable;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
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;
2022-06-14 13:10:38 +02:00
public abstract class LLIndexSearcher implements Closeable {
2021-09-18 18:34:21 +02:00
2022-06-14 13:10:38 +02:00
protected static final Logger LOG = LogManager.getLogger(LLIndexSearcher.class);
2021-10-01 19:17:33 +02:00
2022-06-14 13:10:38 +02:00
protected final IndexSearcher indexSearcher;
2022-06-14 17:46:49 +02:00
private final AtomicBoolean closed;
2021-10-01 19:17:33 +02:00
2022-06-14 13:10:38 +02:00
public LLIndexSearcher(IndexSearcher indexSearcher) {
2021-09-18 18:34:21 +02:00
this.indexSearcher = indexSearcher;
2022-06-14 17:46:49 +02:00
this.closed = new AtomicBoolean();
}
public LLIndexSearcher(IndexSearcher indexSearcher, AtomicBoolean closed) {
this.indexSearcher = indexSearcher;
this.closed = closed;
2021-09-18 18:34:21 +02:00
}
public IndexReader getIndexReader() {
2022-06-14 13:10:38 +02:00
if (closed.get()) throw new IllegalStateException("Closed");
2021-09-18 18:34:21 +02:00
return indexSearcher.getIndexReader();
}
public IndexSearcher getIndexSearcher() {
2022-06-14 13:10:38 +02:00
if (closed.get()) throw new IllegalStateException("Closed");
2021-09-18 18:34:21 +02:00
return indexSearcher;
}
2022-06-14 17:46:49 +02:00
public AtomicBoolean getClosed() {
return closed;
}
2021-09-18 18:34:21 +02:00
@Override
2022-06-14 13:10:38 +02:00
public final void close() throws IOException {
if (closed.compareAndSet(false, true)) {
onClose();
}
2021-09-18 18:34:21 +02:00
}
2021-09-24 02:44:12 +02:00
2022-06-14 13:10:38 +02:00
protected abstract void onClose() throws IOException;
2021-09-18 18:34:21 +02:00
}