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

126 lines
3.4 KiB
Java
Raw Normal View History

2021-09-19 19:59:37 +02:00
package it.cavallium.dbengine.database.disk;
2022-06-30 15:06:10 +02:00
import it.cavallium.dbengine.database.DiscardingCloseable;
2022-06-14 21:58:26 +02:00
import it.cavallium.dbengine.lucene.searcher.ShardIndexSearcher;
2022-06-30 13:54:55 +02:00
import it.cavallium.dbengine.utils.SimpleResource;
2021-09-19 19:59:37 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2021-09-22 11:03:39 +02:00
import org.apache.lucene.search.IndexSearcher;
2021-09-19 19:59:37 +02:00
2022-06-30 15:06:10 +02:00
public interface LLIndexSearchers extends DiscardingCloseable {
2021-09-19 19:59:37 +02:00
2022-06-14 13:10:38 +02:00
static LLIndexSearchers of(List<LLIndexSearcher> indexSearchers) {
return new ShardedIndexSearchers(indexSearchers);
2021-09-19 19:59:37 +02:00
}
2022-06-14 13:10:38 +02:00
static UnshardedIndexSearchers unsharded(LLIndexSearcher indexSearcher) {
return new UnshardedIndexSearchers(indexSearcher);
2021-09-19 19:59:37 +02:00
}
2021-09-22 11:03:39 +02:00
List<IndexSearcher> shards();
2021-09-19 19:59:37 +02:00
2022-06-14 13:10:38 +02:00
List<LLIndexSearcher> llShards();
2021-09-19 19:59:37 +02:00
2022-06-14 13:10:38 +02:00
IndexSearcher shard(int shardIndex);
2021-10-01 19:17:33 +02:00
2022-06-14 13:10:38 +02:00
LLIndexSearcher llShard(int shardIndex);
2021-10-01 19:17:33 +02:00
2022-06-30 13:54:55 +02:00
class UnshardedIndexSearchers extends SimpleResource implements LLIndexSearchers {
2021-10-01 19:17:33 +02:00
2022-06-14 13:10:38 +02:00
private final LLIndexSearcher indexSearcher;
2021-09-19 19:59:37 +02:00
2022-06-14 13:10:38 +02:00
public UnshardedIndexSearchers(LLIndexSearcher indexSearcher) {
this.indexSearcher = indexSearcher;
2021-09-19 19:59:37 +02:00
}
@Override
2021-09-22 11:03:39 +02:00
public List<IndexSearcher> shards() {
return List.of(indexSearcher.getIndexSearcher());
2021-09-19 19:59:37 +02:00
}
2022-06-14 13:10:38 +02:00
@Override
public List<LLIndexSearcher> llShards() {
return Collections.singletonList(indexSearcher);
}
2021-09-19 19:59:37 +02:00
@Override
2021-09-22 11:03:39 +02:00
public IndexSearcher shard(int shardIndex) {
2021-09-19 19:59:37 +02:00
if (shardIndex != -1) {
throw new IndexOutOfBoundsException("Shard index " + shardIndex + " is invalid, this is a unsharded index");
}
2021-09-22 11:03:39 +02:00
return indexSearcher.getIndexSearcher();
2021-09-19 19:59:37 +02:00
}
2022-06-14 13:10:38 +02:00
@Override
public LLIndexSearcher llShard(int shardIndex) {
if (shardIndex != -1) {
throw new IndexOutOfBoundsException("Shard index " + shardIndex + " is invalid, this is a unsharded index");
}
return indexSearcher;
}
2021-09-22 11:03:39 +02:00
public IndexSearcher shard() {
2021-09-20 18:20:59 +02:00
return this.shard(-1);
2021-09-19 19:59:37 +02:00
}
2022-06-14 13:10:38 +02:00
public LLIndexSearcher llShard() {
return this.llShard(-1);
2021-09-19 19:59:37 +02:00
}
@Override
2022-06-30 13:54:55 +02:00
protected void onClose() {
2022-06-14 13:10:38 +02:00
indexSearcher.close();
2021-09-19 19:59:37 +02:00
}
2021-10-01 19:17:33 +02:00
}
2021-09-19 19:59:37 +02:00
2022-06-30 13:54:55 +02:00
class ShardedIndexSearchers extends SimpleResource implements LLIndexSearchers {
2021-09-19 19:59:37 +02:00
2022-06-14 13:10:38 +02:00
private final List<LLIndexSearcher> indexSearchers;
private final List<IndexSearcher> indexSearchersVals;
2021-09-19 19:59:37 +02:00
2022-06-14 13:10:38 +02:00
public ShardedIndexSearchers(List<LLIndexSearcher> indexSearchers) {
2022-06-14 21:58:26 +02:00
List<IndexSearcher> shardedIndexSearchersVals = new ArrayList<>(indexSearchers.size());
2022-06-14 13:10:38 +02:00
for (LLIndexSearcher indexSearcher : indexSearchers) {
2022-06-14 21:58:26 +02:00
shardedIndexSearchersVals.add(indexSearcher.getIndexSearcher());
2021-09-19 19:59:37 +02:00
}
2022-06-14 21:58:26 +02:00
shardedIndexSearchersVals = ShardIndexSearcher.create(shardedIndexSearchersVals);
this.indexSearchers = indexSearchers;
2022-06-14 21:58:26 +02:00
this.indexSearchersVals = shardedIndexSearchersVals;
2021-09-19 19:59:37 +02:00
}
@Override
2021-09-22 11:03:39 +02:00
public List<IndexSearcher> shards() {
return Collections.unmodifiableList(indexSearchersVals);
2021-09-19 19:59:37 +02:00
}
2022-06-14 13:10:38 +02:00
@Override
public List<LLIndexSearcher> llShards() {
return Collections.unmodifiableList(indexSearchers);
}
2021-09-19 19:59:37 +02:00
@Override
2021-09-22 11:03:39 +02:00
public IndexSearcher shard(int shardIndex) {
2021-09-19 19:59:37 +02:00
if (shardIndex < 0) {
throw new IndexOutOfBoundsException("Shard index " + shardIndex + " is invalid");
}
2021-09-22 11:03:39 +02:00
return indexSearchersVals.get(shardIndex);
2021-09-19 19:59:37 +02:00
}
@Override
2022-06-14 13:10:38 +02:00
public LLIndexSearcher llShard(int shardIndex) {
if (shardIndex < 0) {
throw new IndexOutOfBoundsException("Shard index " + shardIndex + " is invalid");
2021-09-22 11:03:39 +02:00
}
2022-06-14 13:10:38 +02:00
return indexSearchers.get(shardIndex);
}
2021-09-19 19:59:37 +02:00
@Override
2022-06-30 13:54:55 +02:00
protected void onClose() {
2022-06-14 13:10:38 +02:00
for (LLIndexSearcher indexSearcher : indexSearchers) {
indexSearcher.close();
2021-09-19 19:59:37 +02:00
}
}
}
}