Protect against memory leaks with dropped search results
This commit is contained in:
parent
a909aaaf52
commit
a0eb80b130
@ -1,21 +1,83 @@
|
||||
package it.cavallium.dbengine.client;
|
||||
|
||||
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
|
||||
import it.cavallium.dbengine.database.LLSearchResultShard;
|
||||
import java.util.Objects;
|
||||
import org.warp.commonutils.log.Logger;
|
||||
import org.warp.commonutils.log.LoggerFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public record SearchResult<T, U>(Flux<SearchResultItem<T, U>> results, TotalHitsCount totalHitsCount, Mono<Void> release) {
|
||||
public final class SearchResult<T, U> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SearchResult.class);
|
||||
|
||||
private volatile boolean releaseCalled;
|
||||
|
||||
private final Flux<SearchResultItem<T, U>> results;
|
||||
private final TotalHitsCount totalHitsCount;
|
||||
private final Mono<Void> release;
|
||||
|
||||
public SearchResult(Flux<SearchResultItem<T, U>> results, TotalHitsCount totalHitsCount, Mono<Void> release) {
|
||||
this.results = results;
|
||||
this.totalHitsCount = totalHitsCount;
|
||||
this.release = Mono.fromRunnable(() -> {
|
||||
if (releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has been called twice!");
|
||||
}
|
||||
releaseCalled = true;
|
||||
}).then(release);
|
||||
}
|
||||
|
||||
public static <T, U> SearchResult<T, U> empty() {
|
||||
return new SearchResult<>(Flux.empty(), TotalHitsCount.of(0, true), Mono.empty());
|
||||
}
|
||||
|
||||
public Flux<SearchResultItem<T, U>> resultsThenRelease() {
|
||||
return Flux
|
||||
.usingWhen(
|
||||
Mono.just(true),
|
||||
_unused -> results,
|
||||
_unused -> release
|
||||
);
|
||||
return Flux.usingWhen(Mono.just(true), _unused -> results, _unused -> release);
|
||||
}
|
||||
|
||||
public Flux<SearchResultItem<T, U>> results() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public TotalHitsCount totalHitsCount() {
|
||||
return totalHitsCount;
|
||||
}
|
||||
|
||||
public Mono<Void> release() {
|
||||
return release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
if (obj == null || obj.getClass() != this.getClass())
|
||||
return false;
|
||||
var that = (SearchResult) obj;
|
||||
return Objects.equals(this.results, that.results) && Objects.equals(this.totalHitsCount, that.totalHitsCount)
|
||||
&& Objects.equals(this.release, that.release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(results, totalHitsCount, release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SearchResult[" + "results=" + results + ", " + "totalHitsCount=" + totalHitsCount + ", " + "release="
|
||||
+ release + ']';
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (!releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has not been called before class finalization!");
|
||||
}
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,36 @@
|
||||
package it.cavallium.dbengine.client;
|
||||
|
||||
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
|
||||
import it.cavallium.dbengine.database.LLSearchResultShard;
|
||||
import it.cavallium.dbengine.database.collections.ValueGetter;
|
||||
import java.util.Objects;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.warp.commonutils.log.Logger;
|
||||
import org.warp.commonutils.log.LoggerFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public record SearchResultKeys<T>(Flux<SearchResultKey<T>> results, TotalHitsCount totalHitsCount, Mono<Void> release) {
|
||||
public final class SearchResultKeys<T> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SearchResultKeys.class);
|
||||
|
||||
private volatile boolean releaseCalled;
|
||||
|
||||
private final Flux<SearchResultKey<T>> results;
|
||||
private final TotalHitsCount totalHitsCount;
|
||||
private final Mono<Void> release;
|
||||
|
||||
public SearchResultKeys(Flux<SearchResultKey<T>> results, TotalHitsCount totalHitsCount, Mono<Void> release) {
|
||||
this.results = results;
|
||||
this.totalHitsCount = totalHitsCount;
|
||||
this.release = Mono.fromRunnable(() -> {
|
||||
if (releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has been called twice!");
|
||||
}
|
||||
releaseCalled = true;
|
||||
}).then(release);
|
||||
}
|
||||
|
||||
public static <T, U> SearchResultKeys<T> empty() {
|
||||
return new SearchResultKeys<>(Flux.empty(), TotalHitsCount.of(0, true), Mono.empty());
|
||||
@ -21,11 +44,50 @@ public record SearchResultKeys<T>(Flux<SearchResultKey<T>> results, TotalHitsCou
|
||||
}
|
||||
|
||||
public Flux<SearchResultKey<T>> resultsThenRelease() {
|
||||
return Flux
|
||||
.usingWhen(
|
||||
Mono.just(true),
|
||||
_unused -> results,
|
||||
_unused -> release
|
||||
);
|
||||
return Flux.usingWhen(Mono.just(true), _unused -> results, _unused -> release);
|
||||
}
|
||||
|
||||
public Flux<SearchResultKey<T>> results() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public TotalHitsCount totalHitsCount() {
|
||||
return totalHitsCount;
|
||||
}
|
||||
|
||||
public Mono<Void> release() {
|
||||
return release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
if (obj == null || obj.getClass() != this.getClass())
|
||||
return false;
|
||||
var that = (SearchResultKeys) obj;
|
||||
return Objects.equals(this.results, that.results) && Objects.equals(this.totalHitsCount, that.totalHitsCount)
|
||||
&& Objects.equals(this.release, that.release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(results, totalHitsCount, release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SearchResultKeys[" + "results=" + results + ", " + "totalHitsCount=" + totalHitsCount + ", " + "release="
|
||||
+ release + ']';
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (!releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has not been called before class finalization!");
|
||||
}
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,75 @@
|
||||
package it.cavallium.dbengine.database;
|
||||
|
||||
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
|
||||
import it.cavallium.dbengine.lucene.searcher.LuceneSearchResult;
|
||||
import java.util.Objects;
|
||||
import org.warp.commonutils.log.Logger;
|
||||
import org.warp.commonutils.log.LoggerFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public record LLSearchResultShard (Flux<LLKeyScore> results, TotalHitsCount totalHitsCount, Mono<Void> release) {}
|
||||
public final class LLSearchResultShard {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LLSearchResultShard.class);
|
||||
|
||||
private volatile boolean releaseCalled;
|
||||
|
||||
private final Flux<LLKeyScore> results;
|
||||
private final TotalHitsCount totalHitsCount;
|
||||
private final Mono<Void> release;
|
||||
|
||||
public LLSearchResultShard(Flux<LLKeyScore> results, TotalHitsCount totalHitsCount, Mono<Void> release) {
|
||||
this.results = results;
|
||||
this.totalHitsCount = totalHitsCount;
|
||||
this.release = Mono.fromRunnable(() -> {
|
||||
if (releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has been called twice!");
|
||||
}
|
||||
releaseCalled = true;
|
||||
}).then(release);
|
||||
}
|
||||
|
||||
public Flux<LLKeyScore> results() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public TotalHitsCount totalHitsCount() {
|
||||
return totalHitsCount;
|
||||
}
|
||||
|
||||
public Mono<Void> release() {
|
||||
return release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
if (obj == null || obj.getClass() != this.getClass())
|
||||
return false;
|
||||
var that = (LLSearchResultShard) obj;
|
||||
return Objects.equals(this.results, that.results) && Objects.equals(this.totalHitsCount, that.totalHitsCount)
|
||||
&& Objects.equals(this.release, that.release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(results, totalHitsCount, release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LLSearchResultShard[" + "results=" + results + ", " + "totalHitsCount=" + totalHitsCount + ", " + "release="
|
||||
+ release + ']';
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (!releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has not been called before class finalization!");
|
||||
}
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,9 +12,10 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
public final class LuceneSearchResult {
|
||||
|
||||
protected static final Logger logger = LoggerFactory.getLogger(LuceneSearchResult.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(LuceneSearchResult.class);
|
||||
|
||||
private volatile boolean releaseCalled;
|
||||
|
||||
private final TotalHitsCount totalHitsCount;
|
||||
private final Flux<LLKeyScore> results;
|
||||
private final Mono<Void> release;
|
||||
@ -30,15 +31,6 @@ public final class LuceneSearchResult {
|
||||
}).then(release);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (!releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has not been called before class finalization!");
|
||||
}
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
public TotalHitsCount totalHitsCount() {
|
||||
return totalHitsCount;
|
||||
}
|
||||
@ -71,4 +63,13 @@ public final class LuceneSearchResult {
|
||||
return "LuceneSearchResult[" + "totalHitsCount=" + totalHitsCount + ", " + "results=" + results + ']';
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (!releaseCalled) {
|
||||
logger.warn("LuceneSearchResult::release has not been called before class finalization!");
|
||||
}
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user