All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ravendb.client.documents.queries.suggestions.SuggestionQueryBase Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
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> executeLazy() {
        return executeLazy(null);
    }

    @SuppressWarnings("unchecked")
    public Lazy> executeLazy(Consumer> onEval) {
        _query = getIndexQuery();

        return ((DocumentSession)_session).addLazyOperation((Class>)(Class)Map.class,
                new LazySuggestionQueryOperation(_session.getConventions(), _query, this::invokeAfterQueryExecuted, this::processResults), onEval);
    }

    protected abstract IndexQuery getIndexQuery();

    protected abstract IndexQuery getIndexQuery(boolean updateAfterQueryExecuted);

    protected abstract void invokeAfterQueryExecuted(QueryResult result);

    private QueryCommand getCommand() {
        _query = getIndexQuery();

        return new QueryCommand(_session.getConventions(), _query, false, false);
    }


    @Override
    public String toString() {
        return getIndexQuery(false).toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy