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

[cros search service] Remove old APIs

Also remove unnecessary prefix that was used when we still have mojo
version of the functions.

Bug: 1090132
Change-Id: I00416ae10fa756e6107f60af915b6f2a56c08e3e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2230424Reviewed-by: default avatarThanh Nguyen <thanhdng@chromium.org>
Commit-Queue: Jia Meng <jiameng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775440}
parent cd5da994
......@@ -3046,8 +3046,6 @@ source_set("unit_tests") {
"local_search_service/index_unittest.cc",
"local_search_service/inverted_index_unittest.cc",
"local_search_service/local_search_service_unittest.cc",
"local_search_service/test_utils.cc",
"local_search_service/test_utils.h",
"locale_change_guard_unittest.cc",
"lock_screen_apps/app_manager_impl_unittest.cc",
"lock_screen_apps/lock_screen_profile_creator_impl_unittest.cc",
......
......@@ -20,24 +20,10 @@ using Positions = std::vector<local_search_service::Position>;
using TokenizedStringWithId =
std::pair<std::string, std::unique_ptr<TokenizedString>>;
// TODO(jiameng): |search_tags| will be removed in the next cl.
void TokenizeSearchTags(const std::vector<base::string16>& search_tags,
const std::vector<Content>& contents,
void TokenizeSearchTags(const std::vector<Content>& contents,
std::vector<TokenizedStringWithId>* tokenized) {
DCHECK(tokenized);
// Only one of them may be non-empty.
DCHECK(search_tags.empty() || contents.empty());
if (!search_tags.empty()) {
const std::string empty_content_id;
for (const auto& tag : search_tags) {
tokenized->push_back(std::make_pair(
empty_content_id, std::make_unique<TokenizedString>(tag)));
}
return;
}
for (const auto& content : contents) {
tokenized->push_back(std::make_pair(
content.id, std::make_unique<TokenizedString>(content.content)));
......@@ -90,9 +76,6 @@ local_search_service::Content::Content() = default;
local_search_service::Content::Content(const Content& content) = default;
local_search_service::Content::~Content() = default;
local_search_service::Data::Data(const std::string& id,
const std::vector<base::string16>& search_tags)
: id(id), search_tags(search_tags) {}
local_search_service::Data::Data(
const std::string& id,
const std::vector<local_search_service::Content>& contents)
......@@ -119,7 +102,7 @@ void Index::AddOrUpdate(const std::vector<local_search_service::Data>& data) {
// If a key already exists, it will overwrite earlier data.
data_[id] = std::vector<TokenizedStringWithId>();
TokenizeSearchTags(item.search_tags, item.contents, &data_[id]);
TokenizeSearchTags(item.contents, &data_[id]);
}
}
......@@ -138,25 +121,23 @@ uint32_t Index::Delete(const std::vector<std::string>& ids) {
return num_deleted;
}
local_search_service::ResponseStatus Index::Find(
const base::string16& query,
uint32_t max_results,
std::vector<local_search_service::Result>* results) {
ResponseStatus Index::Find(const base::string16& query,
uint32_t max_results,
std::vector<Result>* results) {
DCHECK(results);
results->clear();
if (query.empty()) {
return local_search_service::ResponseStatus::kEmptyQuery;
return ResponseStatus::kEmptyQuery;
}
if (data_.empty()) {
return local_search_service::ResponseStatus::kEmptyIndex;
return ResponseStatus::kEmptyIndex;
}
*results = GetSearchResults(query, max_results);
return local_search_service::ResponseStatus::kSuccess;
return ResponseStatus::kSuccess;
}
void Index::SetSearchParams(
const local_search_service::SearchParams& search_params) {
void Index::SetSearchParams(const SearchParams& search_params) {
search_params_ = search_params;
}
......@@ -164,10 +145,9 @@ SearchParams Index::GetSearchParamsForTesting() {
return search_params_;
}
std::vector<local_search_service::Result> Index::GetSearchResults(
const base::string16& query,
uint32_t max_results) const {
std::vector<local_search_service::Result> results;
std::vector<Result> Index::GetSearchResults(const base::string16& query,
uint32_t max_results) const {
std::vector<Result> results;
const TokenizedString tokenized_query(query);
for (const auto& item : data_) {
......@@ -178,7 +158,7 @@ std::vector<local_search_service::Result> Index::GetSearchResults(
search_params_.use_prefix_only, search_params_.use_edit_distance,
search_params_.partial_match_penalty_rate, &relevance_score,
&positions)) {
local_search_service::Result result;
Result result;
result.id = item.first;
result.score = relevance_score;
result.positions = positions;
......
......@@ -37,12 +37,8 @@ struct Data {
std::string id;
// Data item will be matched between its search tags and query term.
// TODO(jiameng): this will be deprecated in the next cl.
std::vector<base::string16> search_tags;
std::vector<Content> contents;
// TODO(jiameng): this will be deprecated in the next cl.
Data(const std::string& id, const std::vector<base::string16>& search_tags);
Data(const std::string& id, const std::vector<Content>& contents);
Data();
Data(const Data& data);
......@@ -70,6 +66,11 @@ struct Result {
// Id of the data.
std::string id;
// Relevance score.
// Currently only linear map is implemented with fuzzy matching and score will
// always be in [0,1]. In the future, when an inverted index is implemented,
// the score will not be in this range any more. Client will be able to select
// a search backend to use (linear map vs inverted index) and hence client
// will be able to expect the range of the scores.
double score;
// Position of the matching text.
// We currently use linear map, which will return one matching content, hence
......@@ -110,7 +111,7 @@ class Index {
// Adds or updates data.
// IDs of data should not be empty.
void AddOrUpdate(const std::vector<local_search_service::Data>& data);
void AddOrUpdate(const std::vector<Data>& data);
// Deletes data with |ids| and returns number of items deleted.
// If an id doesn't exist in the Index, no operation will be done.
......@@ -122,20 +123,18 @@ class Index {
// For each data in the index, we return the 1st search tag that matches
// the query (i.e. above the threshold). Client should put the most
// important search tag first when registering the data in the index.
local_search_service::ResponseStatus Find(
const base::string16& query,
uint32_t max_results,
std::vector<local_search_service::Result>* results);
ResponseStatus Find(const base::string16& query,
uint32_t max_results,
std::vector<Result>* results);
void SetSearchParams(const local_search_service::SearchParams& search_params);
void SetSearchParams(const SearchParams& search_params);
SearchParams GetSearchParamsForTesting();
private:
// Returns all search results for a given query.
std::vector<local_search_service::Result> GetSearchResults(
const base::string16& query,
uint32_t max_results) const;
std::vector<Result> GetSearchResults(const base::string16& query,
uint32_t max_results) const;
// A map from key to a vector of (tag-id, tokenized tag).
std::map<
......@@ -144,7 +143,7 @@ class Index {
data_;
// Search parameters.
local_search_service::SearchParams search_params_;
SearchParams search_params_;
base::WeakPtrFactory<Index> weak_ptr_factory_{this};
};
......
......@@ -11,7 +11,6 @@
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chromeos/local_search_service/index.h"
#include "chrome/browser/chromeos/local_search_service/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace local_search_service {
......@@ -182,9 +181,9 @@ TEST_F(IndexTest, MaxResults) {
}
TEST_F(IndexTest, ResultFound) {
// Register search tags but not contents to test backward compatibility.
const std::map<std::string, std::vector<std::string>> data_to_register = {
{"id1", {"id1", "tag1a", "tag1b"}}, {"xyz", {"xyz"}}};
const std::map<std::string, std::vector<ContentWithId>> data_to_register = {
{"id1", {{"cid1", "id1"}, {"cid2", "tag1a"}, {"cid3", "tag1b"}}},
{"xyz", {{"cid4", "xyz"}}}};
std::vector<Data> data = CreateTestData(data_to_register);
EXPECT_EQ(data.size(), 2u);
......@@ -192,7 +191,7 @@ TEST_F(IndexTest, ResultFound) {
EXPECT_EQ(index_.GetSize(), 2u);
// Find result with query "id1". It returns an exact match.
const std::vector<ResultWithIds> expected_results = {{"id1", {""}}};
const std::vector<ResultWithIds> expected_results = {{"id1", {"cid1"}}};
FindAndCheckResults(&index_, "id1",
/*max_results=*/-1, ResponseStatus::kSuccess,
expected_results);
......
......@@ -14,7 +14,7 @@ LocalSearchService::LocalSearchService() = default;
LocalSearchService::~LocalSearchService() = default;
Index* LocalSearchService::GetIndex(local_search_service::IndexId index_id) {
Index* LocalSearchService::GetIndex(IndexId index_id) {
auto it = indices_.find(index_id);
if (it == indices_.end())
it = indices_.emplace(index_id, std::make_unique<Index>()).first;
......
......@@ -26,10 +26,10 @@ class LocalSearchService : public KeyedService {
LocalSearchService(const LocalSearchService&) = delete;
LocalSearchService& operator=(const LocalSearchService&) = delete;
Index* GetIndex(local_search_service::IndexId index_id);
Index* GetIndex(IndexId index_id);
private:
std::map<local_search_service::IndexId, std::unique_ptr<Index>> indices_;
std::map<IndexId, std::unique_ptr<Index>> indices_;
};
} // namespace local_search_service
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/local_search_service/test_utils.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace local_search_service {
namespace {
std::vector<base::string16> MultiUTF8ToUTF16(
const std::vector<std::string>& input) {
std::vector<base::string16> output;
for (const auto& str : input) {
output.push_back(base::UTF8ToUTF16(str));
}
return output;
}
} // namespace
std::vector<Data> CreateTestData(
const std::map<std::string, std::vector<std::string>>& input) {
std::vector<Data> output;
for (const auto& item : input) {
const std::vector<base::string16> tags = MultiUTF8ToUTF16(item.second);
const Data data(item.first, tags);
output.push_back(data);
}
return output;
}
void FindAndCheck(Index* index,
std::string query,
int32_t max_results,
ResponseStatus expected_status,
const std::vector<std::string>& expected_result_ids) {
DCHECK(index);
std::vector<Result> results;
auto status = index->Find(base::UTF8ToUTF16(query), max_results, &results);
EXPECT_EQ(status, expected_status);
if (!results.empty()) {
// If results are returned, check size and values match the expected.
EXPECT_EQ(results.size(), expected_result_ids.size());
for (size_t i = 0; i < results.size(); ++i) {
EXPECT_EQ(results[i].id, expected_result_ids[i]);
// Scores should be non-increasing.
if (i < results.size() - 1) {
EXPECT_GE(results[i].score, results[i + 1].score);
}
}
return;
}
// If no results are returned, expected ids should be empty.
EXPECT_TRUE(expected_result_ids.empty());
}
} // namespace local_search_service
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_TEST_UTILS_H_
#define CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_TEST_UTILS_H_
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "chrome/browser/chromeos/local_search_service/index.h"
namespace local_search_service {
// Creates test data to be registered to the index. |input| is a map from
// id to search-tags.
std::vector<Data> CreateTestData(
const std::map<std::string, std::vector<std::string>>& input);
// Finds item for |query| from |index| and checks their ids are those in
// |expected_result_ids|.
void FindAndCheck(Index* index,
std::string query,
int32_t max_results,
ResponseStatus expected_status,
const std::vector<std::string>& expected_result_ids);
} // namespace local_search_service
#endif // CHROME_BROWSER_CHROMEOS_LOCAL_SEARCH_SERVICE_TEST_UTILS_H_
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