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-10-21 11:55:59 +02:00
|
|
|
import it.cavallium.dbengine.client.IndexAction.Add;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.AddMulti;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.Close;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.Delete;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.DeleteAll;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.Flush;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.Refresh;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.ReleaseSnapshot;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.TakeSnapshot;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.Update;
|
|
|
|
import it.cavallium.dbengine.client.IndexAction.UpdateMulti;
|
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;
|
2021-07-06 00:30:14 +02:00
|
|
|
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;
|
2021-07-17 11:52:08 +02:00
|
|
|
import it.cavallium.dbengine.database.collections.ValueTransformer;
|
2021-10-21 11:55:59 +02:00
|
|
|
import java.lang.ref.Cleaner;
|
2021-04-03 19:09:06 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Set;
|
2021-10-21 11:55:59 +02:00
|
|
|
import java.util.concurrent.locks.LockSupport;
|
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;
|
2021-10-21 11:55:59 +02:00
|
|
|
import org.warp.commonutils.log.Logger;
|
|
|
|
import org.warp.commonutils.log.LoggerFactory;
|
2021-04-03 19:09:06 +02:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
2021-10-21 11:55:59 +02:00
|
|
|
import reactor.core.publisher.Sinks;
|
|
|
|
import reactor.core.publisher.Sinks.EmitResult;
|
|
|
|
import reactor.core.publisher.Sinks.Empty;
|
|
|
|
import reactor.core.publisher.Sinks.Many;
|
|
|
|
import reactor.core.scheduler.Schedulers;
|
|
|
|
import reactor.util.concurrent.Queues;
|
2021-04-03 19:09:06 +02:00
|
|
|
import reactor.util.function.Tuple2;
|
|
|
|
|
|
|
|
public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
|
|
|
|
|
2021-10-21 11:55:59 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(LuceneIndex.class);
|
|
|
|
private static final Cleaner cleaner = Cleaner.create();
|
2021-04-03 19:09:06 +02:00
|
|
|
private final LLLuceneIndex luceneIndex;
|
|
|
|
private final Indicizer<T,U> indicizer;
|
2021-10-21 11:55:59 +02:00
|
|
|
private final Many<IndexAction> actions;
|
|
|
|
private final Empty<Void> actionsClosed;
|
2021-04-03 19:09:06 +02:00
|
|
|
|
|
|
|
public LuceneIndexImpl(LLLuceneIndex luceneIndex, Indicizer<T, U> indicizer) {
|
|
|
|
this.luceneIndex = luceneIndex;
|
|
|
|
this.indicizer = indicizer;
|
2021-10-21 11:55:59 +02:00
|
|
|
this.actions = Sinks
|
|
|
|
.many()
|
|
|
|
.unicast()
|
|
|
|
.onBackpressureBuffer(Queues.<IndexAction>get(1024).get());
|
|
|
|
this.actionsClosed = Sinks.empty();
|
|
|
|
|
|
|
|
subscribeToActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void subscribeToActions() {
|
|
|
|
var d = actions
|
|
|
|
.asFlux()
|
|
|
|
.doAfterTerminate(actionsClosed::tryEmitEmpty)
|
|
|
|
.flatMap(this::onParallelAction)
|
|
|
|
.concatMap(this::onOrderedAction)
|
|
|
|
.then()
|
|
|
|
.subscribeOn(Schedulers.boundedElastic())
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
cleaner.register(LuceneIndexImpl.this, d::dispose);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions that don't require any kind of order
|
|
|
|
*/
|
|
|
|
private Mono<IndexAction> onParallelAction(IndexAction action) {
|
|
|
|
return (switch (action.getType()) {
|
|
|
|
case TAKE_SNAPSHOT, RELEASE_SNAPSHOT, FLUSH, CLOSE -> Mono.empty();
|
|
|
|
|
|
|
|
case ADD -> luceneIndex.addDocument(((Add) action).key(), ((Add) action).doc())
|
|
|
|
.doOnError(e -> ((Add) action).addedFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((Add) action).addedFuture().success());
|
|
|
|
case ADD_MULTI -> luceneIndex.addDocuments(((AddMulti) action).docsFlux())
|
|
|
|
.doOnError(e -> ((AddMulti) action).addedMultiFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((AddMulti) action).addedMultiFuture().success());
|
|
|
|
case UPDATE -> luceneIndex
|
|
|
|
.updateDocument(((Update) action).key(),((Update) action).doc())
|
|
|
|
.doOnError(e -> ((Update) action).updatedFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((Update) action).updatedFuture().success());
|
|
|
|
case UPDATE_MULTI -> luceneIndex.updateDocuments(Mono.just(((UpdateMulti) action).docs()))
|
|
|
|
.doOnError(e -> ((UpdateMulti) action).updatedMultiFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((UpdateMulti) action).updatedMultiFuture().success());
|
|
|
|
case DELETE -> luceneIndex.deleteDocument(((Delete) action).key())
|
|
|
|
.doOnError(e -> ((Delete) action).deletedFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((Delete) action).deletedFuture().success());
|
|
|
|
case DELETE_ALL -> luceneIndex.deleteAll()
|
|
|
|
.doOnError(e -> ((DeleteAll) action).deletedAllFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((DeleteAll) action).deletedAllFuture().success());
|
|
|
|
case REFRESH -> luceneIndex.refresh(((Refresh) action).force())
|
|
|
|
.doOnError(e -> ((Refresh) action).refreshFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((Refresh) action).refreshFuture().success());
|
|
|
|
})
|
|
|
|
.doOnError(ex -> log.error("Uncaught error when handling parallel index action " + action.getType(), ex))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.thenReturn(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions that require absolute order
|
|
|
|
*/
|
|
|
|
private Mono<IndexAction> onOrderedAction(IndexAction action) {
|
|
|
|
return (switch (action.getType()) {
|
|
|
|
case ADD, REFRESH, DELETE_ALL, DELETE, UPDATE_MULTI, UPDATE, ADD_MULTI -> Mono.empty();
|
|
|
|
|
|
|
|
case TAKE_SNAPSHOT -> luceneIndex.takeSnapshot().single()
|
|
|
|
.doOnError(e -> ((TakeSnapshot) action).snapshotFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnNext(s -> ((TakeSnapshot) action).snapshotFuture().success(s));
|
|
|
|
case RELEASE_SNAPSHOT -> luceneIndex.releaseSnapshot(((ReleaseSnapshot) action).snapshot())
|
|
|
|
.doOnError(e -> ((ReleaseSnapshot) action).releasedFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((ReleaseSnapshot) action).releasedFuture().success());
|
|
|
|
case FLUSH -> luceneIndex.flush()
|
|
|
|
.doOnError(e -> ((Flush) action).flushFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((Flush) action).flushFuture().success());
|
|
|
|
case CLOSE -> luceneIndex.close()
|
|
|
|
.doOnError(e -> ((Close) action).closeFuture().error(e))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.doOnSuccess(s -> ((Close) action).closeFuture().success())
|
|
|
|
.doAfterTerminate(() -> emitActionOptimistically(null));
|
|
|
|
})
|
|
|
|
.doOnError(ex -> log.error("Uncaught error when handling ordered index action " + action.getType(), ex))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.thenReturn(action);
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
.toDocument(key, value)
|
2021-10-21 11:55:59 +02:00
|
|
|
.flatMap(doc -> Mono
|
|
|
|
.create(sink -> emitActionOptimistically(new IndexAction.Add(indicizer.toIndex(key), doc, sink))));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> addDocuments(Flux<Entry<T, U>> entries) {
|
2021-10-21 11:55:59 +02:00
|
|
|
var convertedEntries = entries.flatMap(entry -> indicizer
|
|
|
|
.toDocument(entry.getKey(), entry.getValue())
|
|
|
|
.map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc))
|
|
|
|
);
|
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.AddMulti(convertedEntries, sink)));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> deleteDocument(T key) {
|
|
|
|
LLTerm id = indicizer.toIndex(key);
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.Delete(id, sink)));
|
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
|
|
|
|
.toDocument(key, value)
|
2021-10-21 11:55:59 +02:00
|
|
|
.flatMap(doc -> Mono.create(sink -> emitActionOptimistically(new Update(indicizer.toIndex(key), doc, sink))));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> updateDocuments(Flux<Entry<T, U>> entries) {
|
2021-10-21 11:55:59 +02:00
|
|
|
return entries
|
|
|
|
.flatMap(entry -> indicizer
|
|
|
|
.toDocument(entry.getKey(), entry.getValue())
|
|
|
|
.map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc)))
|
|
|
|
.collectMap(Entry::getKey, Entry::getValue)
|
|
|
|
.flatMap(docs -> Mono.create(sink -> emitActionOptimistically(new IndexAction.UpdateMulti(docs, sink))));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> deleteAll() {
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.DeleteAll(sink)));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-10-28 23:48:25 +02:00
|
|
|
public Mono<Send<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()),
|
2021-07-06 00:30:14 +02:00
|
|
|
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-10-28 23:48:25 +02:00
|
|
|
public Mono<Send<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()),
|
2021-07-06 00:30:14 +02:00
|
|
|
queryParams.toQueryParams(),
|
2021-04-03 19:09:06 +02:00
|
|
|
indicizer.getKeyFieldName()
|
|
|
|
)
|
2021-10-28 23:48:25 +02:00
|
|
|
.map(this::mapResults)
|
2021-10-13 00:23:56 +02:00
|
|
|
.single();
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 23:48:25 +02:00
|
|
|
private Send<Hits<HitKey<T>>> mapResults(Send<LLSearchResultShard> llSearchResultToReceive) {
|
|
|
|
var llSearchResult = llSearchResultToReceive.receive();
|
|
|
|
var scoresWithKeysFlux = llSearchResult
|
|
|
|
.results()
|
|
|
|
.map(hit -> new HitKey<>(indicizer.getKey(hit.key()), hit.score()));
|
2021-04-03 19:09:06 +02:00
|
|
|
|
2021-10-28 23:48:25 +02:00
|
|
|
return new Hits<>(scoresWithKeysFlux, llSearchResult.totalHitsCount(), llSearchResult::close).send();
|
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-10-28 23:48:25 +02:00
|
|
|
.search(ClientQueryParams.<LazyHitKey<T>>builder().snapshot(snapshot).query(query).limit(0).build())
|
2021-10-13 00:23:56 +02:00
|
|
|
.single()
|
2021-09-19 19:59:37 +02:00
|
|
|
.map(searchResultKeysSend -> {
|
|
|
|
try (var searchResultKeys = searchResultKeysSend.receive()) {
|
|
|
|
return searchResultKeys.totalHitsCount();
|
|
|
|
}
|
|
|
|
});
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLowMemoryMode() {
|
|
|
|
return luceneIndex.isLowMemoryMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> close() {
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono
|
|
|
|
.<Void>create(sink -> emitActionOptimistically(new Close(sink)))
|
|
|
|
.then(this.actionsClosed.asMono());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void emitActionOptimistically(IndexAction action) {
|
|
|
|
EmitResult emitResult;
|
|
|
|
while ((emitResult = (action == null ? actions.tryEmitComplete() : actions.tryEmitNext(action)))
|
|
|
|
== EmitResult.FAIL_NON_SERIALIZED || emitResult == EmitResult.FAIL_OVERFLOW) {
|
|
|
|
// 10ms
|
|
|
|
LockSupport.parkNanos(10000000);
|
|
|
|
}
|
|
|
|
emitResult.orThrow();
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush writes to disk
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Mono<Void> flush() {
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.Flush(sink)));
|
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) {
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.Refresh(force, sink)));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<LLSnapshot> takeSnapshot() {
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.TakeSnapshot(sink)));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> releaseSnapshot(LLSnapshot snapshot) {
|
2021-10-21 11:55:59 +02:00
|
|
|
return Mono.create(sink -> emitActionOptimistically(new IndexAction.ReleaseSnapshot(snapshot, sink)));
|
2021-04-03 19:09:06 +02:00
|
|
|
}
|
|
|
|
}
|