net.ravendb.client.documents.queries.suggestions.SuggestionQueryBase Maven / Gradle / Ivy
package net.ravendb.client.documents.queries.suggestions;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Stopwatch;
import net.ravendb.client.documents.Lazy;
import net.ravendb.client.documents.commands.QueryCommand;
import net.ravendb.client.documents.conventions.DocumentConventions;
import net.ravendb.client.documents.queries.IndexQuery;
import net.ravendb.client.documents.queries.QueryResult;
import net.ravendb.client.documents.session.DocumentSession;
import net.ravendb.client.documents.session.InMemoryDocumentSessionOperations;
import net.ravendb.client.documents.session.operations.QueryOperation;
import net.ravendb.client.documents.session.operations.lazy.LazySuggestionQueryOperation;
import net.ravendb.client.extensions.JsonExtensions;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public abstract class SuggestionQueryBase {
private final InMemoryDocumentSessionOperations _session;
private IndexQuery _query;
private Stopwatch _duration;
protected SuggestionQueryBase(InMemoryDocumentSessionOperations session) {
_session = session;
}
public Map execute() {
QueryCommand command = getCommand();
_duration = Stopwatch.createStarted();
_session.incrementRequestCount();
_session.getRequestExecutor().execute(command);
return processResults(command.getResult(), _session.getConventions());
}
private Map processResults(QueryResult queryResult, DocumentConventions conventions) {
invokeAfterQueryExecuted(queryResult);
try {
Map results = new HashMap<>();
for (JsonNode result : queryResult.getResults()) {
SuggestionResult suggestionResult = JsonExtensions.getDefaultMapper().treeToValue(result, SuggestionResult.class);
results.put(suggestionResult.getName(), suggestionResult);
}
QueryOperation.ensureIsAcceptable(queryResult, _query.isWaitForNonStaleResults(), _duration, _session);
return results;
} catch (IOException e) {
throw new RuntimeException("Unable to process suggestions results: " + e.getMessage(), e);
}
}
public Lazy
© 2015 - 2024 Weber Informatics LLC | Privacy Policy