package it.cavallium.dbengine.client; import io.net5.buffer.api.Drop; import io.net5.buffer.api.Owned; import it.cavallium.dbengine.client.query.current.data.TotalHitsCount; import io.net5.buffer.api.internal.ResourceSupport; import it.cavallium.dbengine.database.collections.ValueGetter; import org.warp.commonutils.log.Logger; import org.warp.commonutils.log.LoggerFactory; import reactor.core.publisher.Flux; @SuppressWarnings("unused") public final class SearchResultKeys extends ResourceSupport, SearchResultKeys> { private static final Logger logger = LoggerFactory.getLogger(SearchResultKeys.class); private static final Drop> DROP = new Drop<>() { @Override public void drop(SearchResultKeys obj) { if (obj.onClose != null) { obj.onClose.run(); } } @Override public Drop> fork() { return this; } @Override public void attach(SearchResultKeys obj) { } }; private Flux> results; private TotalHitsCount totalHitsCount; private Runnable onClose; @SuppressWarnings({"unchecked", "rawtypes"}) public SearchResultKeys(Flux> results, TotalHitsCount totalHitsCount, Runnable onClose) { super((Drop>) (Drop) DROP); this.results = results; this.totalHitsCount = totalHitsCount; this.onClose = onClose; } public static SearchResultKeys empty() { return new SearchResultKeys<>(Flux.empty(), TotalHitsCount.of(0, true), null); } public SearchResult withValues(ValueGetter valuesGetter) { return new SearchResult<>(results.map(item -> new SearchResultItem<>(item.key(), item.key().flatMap(valuesGetter::get), item.score() )), totalHitsCount, this::close); } public Flux> results() { return results; } public TotalHitsCount totalHitsCount() { return totalHitsCount; } @Override public String toString() { return "SearchResultKeys[" + "results=" + results + ", " + "totalHitsCount=" + totalHitsCount + ']'; } @Override protected RuntimeException createResourceClosedException() { return new IllegalStateException("Closed"); } @Override protected Owned> prepareSend() { var results = this.results; var totalHitsCount = this.totalHitsCount; var onClose = this.onClose; makeInaccessible(); return drop -> { var instance = new SearchResultKeys<>(results, totalHitsCount, onClose); drop.attach(instance); return instance; }; } protected void makeInaccessible() { this.results = null; this.totalHitsCount = null; this.onClose = null; } }