diff --git a/src/main/java/it/cavallium/dbengine/client/Hits.java b/src/main/java/it/cavallium/dbengine/client/Hits.java index 33b9272..03be35a 100644 --- a/src/main/java/it/cavallium/dbengine/client/Hits.java +++ b/src/main/java/it/cavallium/dbengine/client/Hits.java @@ -1,10 +1,8 @@ package it.cavallium.dbengine.client; -import io.netty5.buffer.api.Drop; -import io.netty5.buffer.api.Owned; import it.cavallium.dbengine.client.query.current.data.TotalHitsCount; -import io.netty5.buffer.api.internal.ResourceSupport; import it.cavallium.dbengine.database.DiscardingCloseable; +import it.cavallium.dbengine.database.SafeCloseable; import it.cavallium.dbengine.database.collections.ValueGetter; import it.cavallium.dbengine.database.collections.ValueTransformer; import it.cavallium.dbengine.lucene.LuceneCloseable; @@ -12,12 +10,14 @@ import it.cavallium.dbengine.utils.SimpleResource; import java.util.Map.Entry; import java.util.Optional; import java.util.function.Function; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.util.function.Tuples; public class Hits extends SimpleResource implements DiscardingCloseable { + private static final Logger LOG = LogManager.getLogger(Hits.class); private static final Hits EMPTY_HITS = new Hits<>(Flux.empty(), TotalHitsCount.of(0, true), false); private final Flux results; private final TotalHitsCount totalHitsCount; @@ -74,6 +74,14 @@ public class Hits extends SimpleResource implements DiscardingCloseable { }; } + public static Hits withResource(Flux hits, TotalHitsCount count, SafeCloseable resource) { + if (resource instanceof LuceneCloseable luceneCloseable) { + return new LuceneHits<>(hits, count, luceneCloseable); + } else { + return new CloseableHits<>(hits, count, resource); + } + } + public Flux results() { ensureOpen(); return results; @@ -117,4 +125,44 @@ public class Hits extends SimpleResource implements DiscardingCloseable { super(hits, count, parent); } } + + public static final class LuceneHits extends Hits implements LuceneCloseable { + + private final LuceneCloseable resource; + + public LuceneHits(Flux hits, TotalHitsCount count, LuceneCloseable resource) { + super(hits, count); + this.resource = resource; + } + + @Override + protected void onClose() { + try { + resource.close(); + } catch (Throwable ex) { + LOG.error("Failed to close resource", ex); + } + super.onClose(); + } + } + + public static final class CloseableHits extends Hits { + + private final SafeCloseable resource; + + public CloseableHits(Flux hits, TotalHitsCount count, SafeCloseable resource) { + super(hits, count); + this.resource = resource; + } + + @Override + protected void onClose() { + try { + resource.close(); + } catch (Throwable ex) { + LOG.error("Failed to close resource", ex); + } + super.onClose(); + } + } } diff --git a/src/main/java/it/cavallium/dbengine/client/LuceneIndexImpl.java b/src/main/java/it/cavallium/dbengine/client/LuceneIndexImpl.java index b90c203..bc81bd9 100644 --- a/src/main/java/it/cavallium/dbengine/client/LuceneIndexImpl.java +++ b/src/main/java/it/cavallium/dbengine/client/LuceneIndexImpl.java @@ -1,5 +1,7 @@ package it.cavallium.dbengine.client; +import it.cavallium.dbengine.client.Hits.CloseableHits; +import it.cavallium.dbengine.client.Hits.LuceneHits; import it.cavallium.dbengine.client.query.ClientQueryParams; import it.cavallium.dbengine.client.query.current.data.Query; import it.cavallium.dbengine.client.query.current.data.TotalHitsCount; @@ -17,7 +19,6 @@ import it.cavallium.dbengine.lucene.LuceneCloseable; import it.cavallium.dbengine.lucene.LuceneUtils; import it.cavallium.dbengine.lucene.collector.Buckets; import it.cavallium.dbengine.lucene.searcher.BucketParams; -import it.cavallium.dbengine.utils.SimpleResource; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.time.Duration; import java.util.List; @@ -247,44 +248,4 @@ public class LuceneIndexImpl implements LuceneIndex { } } - private static final class LuceneHits extends Hits implements LuceneCloseable { - - private final LuceneCloseable resource; - - public LuceneHits(Flux hits, TotalHitsCount count, LuceneCloseable resource) { - super(hits, count); - this.resource = resource; - } - - @Override - protected void onClose() { - try { - resource.close(); - } catch (Throwable ex) { - LOG.error("Failed to close resource", ex); - } - super.onClose(); - } - } - - private static final class CloseableHits extends Hits { - - private final SafeCloseable resource; - - public CloseableHits(Flux hits, TotalHitsCount count, SafeCloseable resource) { - super(hits, count); - this.resource = resource; - } - - @Override - protected void onClose() { - try { - resource.close(); - } catch (Throwable ex) { - LOG.error("Failed to close resource", ex); - } - super.onClose(); - } - } - }