Commit dfa4a7a6 authored by Jia's avatar Jia Committed by Commit Bot

[cros search service] Removes max latency and changes max results

This cl changes type of max results to uint32 and removes max
latency (to be supported later).
It only changes the sync functions as the async versions will be
deleted in the next cl.

Bug: 1018613,1063505
Change-Id: Ie186f7bc02ded01d297d1d001b25d2a622466e58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2121675Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Jia Meng <jiameng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754373}
parent 62d77833
......@@ -156,7 +156,10 @@ void IndexImpl::Find(const base::string16& query,
int32_t max_results,
FindCallback callback) {
std::vector<local_search_service::Result> results;
const auto response = Find(query, max_latency_in_ms, max_results, &results);
// TODO(jiameng): |max_latency| isn't supported yet. We're
// temporarily ignoring it before the next cl removes the async call.
const auto response =
Find(query, max_results < 0 ? 0u : max_results, &results);
mojom::ResponseStatus mresponse = mojom::ResponseStatus::UNKNOWN_ERROR;
switch (response) {
......@@ -197,8 +200,7 @@ void IndexImpl::Find(const base::string16& query,
local_search_service::ResponseStatus IndexImpl::Find(
const base::string16& query,
int32_t max_latency_in_ms,
int32_t max_results,
uint32_t max_results,
std::vector<local_search_service::Result>* results) {
DCHECK(results);
results->clear();
......@@ -209,7 +211,7 @@ local_search_service::ResponseStatus IndexImpl::Find(
return local_search_service::ResponseStatus::kEmptyIndex;
}
*results = GetSearchResults(query);
*results = GetSearchResults(query, max_results);
return local_search_service::ResponseStatus::kSuccess;
}
......@@ -250,7 +252,8 @@ void IndexImpl::GetSearchParamsForTesting(double* relevance_threshold,
}
std::vector<local_search_service::Result> IndexImpl::GetSearchResults(
const base::string16& query) const {
const base::string16& query,
uint32_t max_results) const {
std::vector<local_search_service::Result> results;
const TokenizedString tokenized_query(query);
......@@ -272,6 +275,9 @@ std::vector<local_search_service::Result> IndexImpl::GetSearchResults(
}
std::sort(results.begin(), results.end(), CompareResults);
if (results.size() > max_results && max_results > 0u) {
results.resize(max_results);
}
return results;
}
......
......@@ -114,10 +114,10 @@ class IndexImpl : public mojom::Index {
int32_t max_latency_in_ms,
int32_t max_results,
FindCallback callback) override;
// Zero |max_results| means no max.
local_search_service::ResponseStatus Find(
const base::string16& query,
int32_t max_latency_in_ms,
int32_t max_results,
uint32_t max_results,
std::vector<local_search_service::Result>* results);
void SetSearchParams(mojom::SearchParamsPtr search_params,
......@@ -133,7 +133,8 @@ class IndexImpl : public mojom::Index {
private:
// Returns all search results for a given query.
std::vector<local_search_service::Result> GetSearchResults(
const base::string16& query) const;
const base::string16& query,
uint32_t max_results) const;
// A map from key to tokenized search-tags.
std::map<std::string, std::vector<std::unique_ptr<TokenizedString>>> data_;
......
......@@ -137,4 +137,21 @@ TEST_F(IndexImplTest, RelevanceThreshold) {
}
}
TEST_F(IndexImplTest, MaxResults) {
const std::map<std::string, std::vector<std::string>> data_to_register = {
{"id1", {"Clash Of Clan"}}, {"id2", {"famous"}}};
std::vector<mojom::DataPtr> data = CreateTestData(data_to_register);
AddOrUpdateAndCheck(index_remote_.get(), std::move(data));
GetSizeAndCheck(index_remote_.get(), 2u);
mojom::SearchParamsPtr search_params = mojom::SearchParams::New();
search_params->relevance_threshold = 0.0;
SetSearchParamsAndCheck(index_remote_.get(), std::move(search_params));
FindAndCheck(index_remote_.get(), "CC", /*max_latency_in_ms=*/-1,
/*max_results=*/-1, mojom::ResponseStatus::SUCCESS,
{"id1", "id2"});
FindAndCheck(index_remote_.get(), "CC", /*max_latency_in_ms=*/-1,
/*max_results=*/1, mojom::ResponseStatus::SUCCESS, {"id1"});
}
} // namespace local_search_service
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment