CavalliumDBEngine/src/main/java/it/cavallium/dbengine/client/LuceneIndexImpl.java

168 lines
4.4 KiB
Java
Raw Normal View History

2021-04-03 19:09:06 +02:00
package it.cavallium.dbengine.client;
2021-09-19 19:59:37 +02:00
import io.net5.buffer.api.Send;
2021-04-03 19:09:06 +02:00
import it.cavallium.dbengine.client.query.ClientQueryParams;
import it.cavallium.dbengine.client.query.current.data.Query;
2021-08-04 01:12:39 +02:00
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
2021-04-03 19:09:06 +02:00
import it.cavallium.dbengine.database.LLLuceneIndex;
import it.cavallium.dbengine.database.LLSearchResultShard;
2021-04-03 19:09:06 +02:00
import it.cavallium.dbengine.database.LLSnapshot;
import it.cavallium.dbengine.database.LLTerm;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
2021-06-07 16:21:12 +02:00
import org.jetbrains.annotations.NotNull;
2021-04-03 19:09:06 +02:00
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
private final LLLuceneIndex luceneIndex;
private final Indicizer<T,U> indicizer;
public LuceneIndexImpl(LLLuceneIndex luceneIndex, Indicizer<T, U> indicizer) {
this.luceneIndex = luceneIndex;
this.indicizer = indicizer;
}
private LLSnapshot resolveSnapshot(CompositeSnapshot snapshot) {
if (snapshot == null) {
return null;
} else {
return snapshot.getSnapshot(luceneIndex);
}
}
@Override
public Mono<Void> addDocument(T key, U value) {
return indicizer
2021-11-07 18:00:11 +01:00
.toDocument(key, value)
.flatMap(doc -> luceneIndex.addDocument(indicizer.toIndex(key), doc));
2021-04-03 19:09:06 +02:00
}
@Override
public Mono<Void> addDocuments(Flux<Entry<T, U>> entries) {
return luceneIndex
.addDocuments(entries
.flatMap(entry -> indicizer
2021-11-07 18:00:11 +01:00
.toDocument(entry.getKey(), entry.getValue())
.map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc)))
);
2021-04-03 19:09:06 +02:00
}
@Override
public Mono<Void> deleteDocument(T key) {
LLTerm id = indicizer.toIndex(key);
return luceneIndex.deleteDocument(id);
2021-04-03 19:09:06 +02:00
}
@Override
2021-06-07 16:21:12 +02:00
public Mono<Void> updateDocument(T key, @NotNull U value) {
2021-04-03 19:09:06 +02:00
return indicizer
2021-11-07 17:46:40 +01:00
.toIndexRequest(key, value)
.flatMap(doc -> luceneIndex.update(indicizer.toIndex(key), doc));
2021-04-03 19:09:06 +02:00
}
@Override
public Mono<Void> updateDocuments(Flux<Entry<T, U>> entries) {
return luceneIndex
.updateDocuments(entries
.flatMap(entry -> indicizer
2021-11-07 18:00:11 +01:00
.toDocument(entry.getKey(), entry.getValue())
.map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc)))
.collectMap(Entry::getKey, Entry::getValue)
);
2021-04-03 19:09:06 +02:00
}
@Override
public Mono<Void> deleteAll() {
return luceneIndex.deleteAll();
2021-04-03 19:09:06 +02:00
}
@Override
2021-11-08 11:17:52 +01:00
public Mono<Hits<HitKey<T>>> moreLikeThis(ClientQueryParams queryParams,
2021-04-03 19:09:06 +02:00
T key,
U mltDocumentValue) {
Flux<Tuple2<String, Set<String>>> mltDocumentFields
= indicizer.getMoreLikeThisDocumentFields(key, mltDocumentValue);
return luceneIndex
2021-05-21 00:19:40 +02:00
.moreLikeThis(resolveSnapshot(queryParams.snapshot()),
queryParams.toQueryParams(),
2021-04-03 19:09:06 +02:00
indicizer.getKeyFieldName(),
mltDocumentFields
)
2021-10-28 23:48:25 +02:00
.map(this::mapResults)
.single();
2021-07-17 11:52:08 +02:00
}
@Override
2021-11-08 11:17:52 +01:00
public Mono<Hits<HitKey<T>>> search(ClientQueryParams queryParams) {
2021-04-03 19:09:06 +02:00
return luceneIndex
2021-05-21 00:19:40 +02:00
.search(resolveSnapshot(queryParams.snapshot()),
queryParams.toQueryParams(),
2021-04-03 19:09:06 +02:00
indicizer.getKeyFieldName()
)
2021-10-28 23:48:25 +02:00
.map(this::mapResults)
.single();
2021-04-03 19:09:06 +02:00
}
2021-11-08 11:17:52 +01:00
private Hits<HitKey<T>> mapResults(LLSearchResultShard llSearchResult) {
2021-10-28 23:48:25 +02:00
var scoresWithKeysFlux = llSearchResult
.results()
.map(hit -> new HitKey<>(indicizer.getKey(hit.key()), hit.score()));
2021-04-03 19:09:06 +02:00
2021-11-08 11:17:52 +01:00
return new Hits<>(scoresWithKeysFlux, llSearchResult.totalHitsCount(), llSearchResult::close);
2021-07-17 11:52:08 +02:00
}
2021-04-03 19:09:06 +02:00
@Override
2021-08-04 01:12:39 +02:00
public Mono<TotalHitsCount> count(@Nullable CompositeSnapshot snapshot, Query query) {
2021-07-17 11:52:08 +02:00
return this
2021-11-07 18:00:11 +01:00
.search(ClientQueryParams.builder().snapshot(snapshot).query(query).limit(0).build())
.single()
2021-11-08 11:17:52 +01:00
.map(searchResultKeys -> {
try (searchResultKeys) {
2021-09-19 19:59:37 +02:00
return searchResultKeys.totalHitsCount();
}
});
2021-04-03 19:09:06 +02:00
}
@Override
public boolean isLowMemoryMode() {
return luceneIndex.isLowMemoryMode();
}
@Override
public Mono<Void> close() {
return luceneIndex.close();
2021-04-03 19:09:06 +02:00
}
/**
* Flush writes to disk
*/
@Override
public Mono<Void> flush() {
return luceneIndex.flush();
2021-04-03 19:09:06 +02:00
}
/**
* Refresh index searcher
*/
@Override
2021-07-18 19:37:24 +02:00
public Mono<Void> refresh(boolean force) {
return luceneIndex.refresh(force);
2021-04-03 19:09:06 +02:00
}
@Override
public Mono<LLSnapshot> takeSnapshot() {
return luceneIndex.takeSnapshot();
2021-04-03 19:09:06 +02:00
}
@Override
public Mono<Void> releaseSnapshot(LLSnapshot snapshot) {
return luceneIndex.releaseSnapshot(snapshot);
2021-04-03 19:09:06 +02:00
}
}