This commit is contained in:
Andrea Cavalli 2021-11-07 18:00:11 +01:00
parent b34b620082
commit 52be944b5f
2 changed files with 18 additions and 7 deletions

View File

@ -3,6 +3,8 @@ package it.cavallium.dbengine.client;
import it.cavallium.dbengine.database.LLIndexRequest; import it.cavallium.dbengine.database.LLIndexRequest;
import it.cavallium.dbengine.database.LLUpdateDocument; import it.cavallium.dbengine.database.LLUpdateDocument;
import it.cavallium.dbengine.database.LLTerm; import it.cavallium.dbengine.database.LLTerm;
import it.cavallium.dbengine.database.LLUpdateFields;
import it.cavallium.dbengine.database.LLUtils;
import java.util.Set; import java.util.Set;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@ -16,6 +18,18 @@ public abstract class Indicizer<T, U> {
*/ */
public abstract @NotNull Mono<? extends LLIndexRequest> toIndexRequest(@NotNull T key, @NotNull U value); public abstract @NotNull Mono<? extends LLIndexRequest> toIndexRequest(@NotNull T key, @NotNull U value);
public final @NotNull Mono<LLUpdateDocument> toDocument(@NotNull T key, @NotNull U value) {
return toIndexRequest(key, value).map(req -> {
if (req instanceof LLUpdateFields updateFields) {
return new LLUpdateDocument(updateFields.items());
} else if (req instanceof LLUpdateDocument updateDocument) {
return updateDocument;
} else {
throw new UnsupportedOperationException("Unexpected request type: " + req);
}
});
}
public abstract @NotNull LLTerm toIndex(@NotNull T key); public abstract @NotNull LLTerm toIndex(@NotNull T key);
public abstract @NotNull String getKeyFieldName(); public abstract @NotNull String getKeyFieldName();

View File

@ -13,15 +13,12 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.warp.commonutils.log.Logger;
import org.warp.commonutils.log.LoggerFactory;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2; import reactor.util.function.Tuple2;
public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> { public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
private static final Logger log = LoggerFactory.getLogger(LuceneIndex.class);
private final LLLuceneIndex luceneIndex; private final LLLuceneIndex luceneIndex;
private final Indicizer<T,U> indicizer; private final Indicizer<T,U> indicizer;
@ -41,7 +38,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
@Override @Override
public Mono<Void> addDocument(T key, U value) { public Mono<Void> addDocument(T key, U value) {
return indicizer return indicizer
.toIndexRequest(key, value) .toDocument(key, value)
.flatMap(doc -> luceneIndex.addDocument(indicizer.toIndex(key), doc)); .flatMap(doc -> luceneIndex.addDocument(indicizer.toIndex(key), doc));
} }
@ -50,7 +47,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
return luceneIndex return luceneIndex
.addDocuments(entries .addDocuments(entries
.flatMap(entry -> indicizer .flatMap(entry -> indicizer
.toIndexRequest(entry.getKey(), entry.getValue()) .toDocument(entry.getKey(), entry.getValue())
.map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc))) .map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc)))
); );
} }
@ -73,7 +70,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
return luceneIndex return luceneIndex
.updateDocuments(entries .updateDocuments(entries
.flatMap(entry -> indicizer .flatMap(entry -> indicizer
.toIndexRequest(entry.getKey(), entry.getValue()) .toDocument(entry.getKey(), entry.getValue())
.map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc))) .map(doc -> Map.entry(indicizer.toIndex(entry.getKey()), doc)))
.collectMap(Entry::getKey, Entry::getValue) .collectMap(Entry::getKey, Entry::getValue)
); );
@ -124,7 +121,7 @@ public class LuceneIndexImpl<T, U> implements LuceneIndex<T, U> {
@Override @Override
public Mono<TotalHitsCount> count(@Nullable CompositeSnapshot snapshot, Query query) { public Mono<TotalHitsCount> count(@Nullable CompositeSnapshot snapshot, Query query) {
return this return this
.search(ClientQueryParams.<LazyHitKey<T>>builder().snapshot(snapshot).query(query).limit(0).build()) .search(ClientQueryParams.builder().snapshot(snapshot).query(query).limit(0).build())
.single() .single()
.map(searchResultKeysSend -> { .map(searchResultKeysSend -> {
try (var searchResultKeys = searchResultKeysSend.receive()) { try (var searchResultKeys = searchResultKeysSend.receive()) {