Commit a1b039a7 authored by Ce Chen's avatar Ce Chen Committed by Commit Bot

[omnibox] rename on_device_head_serving to on_device_head_model.

Bug: 925072
Change-Id: I8929e0bcdd89009320a35cb3c95d4daa349cb6ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1946458Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Commit-Queue: Ce Chen <cch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721090}
parent 53550932
......@@ -170,10 +170,10 @@ jumbo_static_library("browser") {
"omnibox_pref_names.h",
"omnibox_view.cc",
"omnibox_view.h",
"on_device_head_model.cc",
"on_device_head_model.h",
"on_device_head_provider.cc",
"on_device_head_provider.h",
"on_device_head_serving.cc",
"on_device_head_serving.h",
"on_device_model_update_listener.cc",
"on_device_model_update_listener.h",
"remote_suggestions_service.cc",
......@@ -409,8 +409,8 @@ source_set("unit_tests") {
"omnibox_pedal_unittest.cc",
"omnibox_popup_model_unittest.cc",
"omnibox_view_unittest.cc",
"on_device_head_model_unittest.cc",
"on_device_head_provider_unittest.cc",
"on_device_head_serving_unittest.cc",
"remote_suggestions_service_unittest.cc",
"scored_history_match_unittest.cc",
"search_suggestion_parser_unittest.cc",
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/on_device_head_serving.h"
#include "components/omnibox/browser/on_device_head_model.h"
#include <algorithm>
......@@ -27,63 +27,63 @@ uint32_t ConvertByteArrayToInt(char byte_array[], uint32_t num_bytes) {
} // namespace
// static
std::unique_ptr<OnDeviceHeadServing> OnDeviceHeadServing::Create(
std::unique_ptr<OnDeviceHeadModel> OnDeviceHeadModel::Create(
const std::string& model_filename,
int max_num_matches_to_return) {
std::unique_ptr<OnDeviceHeadServing> serving = base::WrapUnique(
new OnDeviceHeadServing(model_filename, max_num_matches_to_return));
std::unique_ptr<OnDeviceHeadModel> model = base::WrapUnique(
new OnDeviceHeadModel(model_filename, max_num_matches_to_return));
// TODO(crbug.com/925072): Add DCHECK and code to report failures to UMA
// histogram.
if (!serving->OpenModelFileStream(0)) {
DVLOG(1) << "On Device Head Serving: cannot create on device head "
<< "serving instance because model file cannot be opened";
if (!model->OpenModelFileStream(0)) {
DVLOG(1) << "On Device Head model: cannot create on device head "
<< "model instance because model file cannot be opened";
return nullptr;
}
char sizes[2];
if (!serving->ReadNextNumBytes(2, sizes)) {
DVLOG(1) << "On Device Head Serving: failed to read size information "
if (!model->ReadNextNumBytes(2, sizes)) {
DVLOG(1) << "On Device Head model: failed to read size information "
<< "in the first 2 bytes of the model file: " << model_filename;
serving->MaybeCloseModelFileStream();
model->MaybeCloseModelFileStream();
return nullptr;
}
serving->MaybeCloseModelFileStream();
model->MaybeCloseModelFileStream();
serving->address_size_ = sizes[0];
serving->score_size_ = sizes[1];
if (!serving->AreSizesValid()) {
model->address_size_ = sizes[0];
model->score_size_ = sizes[1];
if (!model->AreSizesValid()) {
return nullptr;
}
return serving;
return model;
}
OnDeviceHeadServing::OnDeviceHeadServing(const std::string& model_filename,
uint32_t max_num_matches_to_return)
OnDeviceHeadModel::OnDeviceHeadModel(const std::string& model_filename,
uint32_t max_num_matches_to_return)
: model_filename_(model_filename),
max_num_matches_to_return_(max_num_matches_to_return) {}
OnDeviceHeadServing::~OnDeviceHeadServing() {
OnDeviceHeadModel::~OnDeviceHeadModel() {
MaybeCloseModelFileStream();
}
bool OnDeviceHeadServing::AreSizesValid() {
bool OnDeviceHeadModel::AreSizesValid() {
bool is_score_size_valid = (score_size_ >= 2 && score_size_ <= 4);
bool is_address_size_valid = (address_size_ >= 3 && address_size_ <= 4);
if (!is_score_size_valid) {
DVLOG(1) << "On Device Head Serving: score size [" << score_size_
DVLOG(1) << "On Device Head model: score size [" << score_size_
<< "] is not valid; valid size should 2, 3 or 4 bytes.";
}
if (!is_address_size_valid) {
DVLOG(1) << "On Device Head Serving: address size [" << address_size_
DVLOG(1) << "On Device Head model: address size [" << address_size_
<< "] is not valid; valid size should be 3 or 4 bytes.";
}
return is_score_size_valid && is_address_size_valid;
}
std::vector<std::pair<std::string, uint32_t>>
OnDeviceHeadServing::GetSuggestionsForPrefix(const std::string& prefix) {
OnDeviceHeadModel::GetSuggestionsForPrefix(const std::string& prefix) {
std::vector<std::pair<std::string, uint32_t>> suggestions;
if (prefix.empty()) {
return suggestions;
......@@ -99,7 +99,7 @@ OnDeviceHeadServing::GetSuggestionsForPrefix(const std::string& prefix) {
return suggestions;
}
std::vector<std::pair<std::string, uint32_t>> OnDeviceHeadServing::DoSearch(
std::vector<std::pair<std::string, uint32_t>> OnDeviceHeadModel::DoSearch(
const MatchCandidate& start_match) {
std::vector<std::pair<std::string, uint32_t>> suggestions;
......@@ -147,10 +147,9 @@ std::vector<std::pair<std::string, uint32_t>> OnDeviceHeadServing::DoSearch(
return suggestions;
}
void OnDeviceHeadServing::InsertCandidateToQueue(
const MatchCandidate& candidate,
CandidateQueue* leaf_queue,
CandidateQueue* non_leaf_queue) {
void OnDeviceHeadModel::InsertCandidateToQueue(const MatchCandidate& candidate,
CandidateQueue* leaf_queue,
CandidateQueue* non_leaf_queue) {
CandidateQueue* queue_ptr =
candidate.is_complete_suggestion ? leaf_queue : non_leaf_queue;
......@@ -164,7 +163,7 @@ void OnDeviceHeadServing::InsertCandidateToQueue(
}
}
uint32_t OnDeviceHeadServing::GetMinScoreFromQueues(
uint32_t OnDeviceHeadModel::GetMinScoreFromQueues(
const CandidateQueue& queue_1,
const CandidateQueue& queue_2) {
uint32_t min_score = 0x1 << (score_size_ * 8 - 1);
......@@ -177,8 +176,8 @@ uint32_t OnDeviceHeadServing::GetMinScoreFromQueues(
return min_score;
}
bool OnDeviceHeadServing::FindStartNode(const std::string& prefix,
MatchCandidate* start_match) {
bool OnDeviceHeadModel::FindStartNode(const std::string& prefix,
MatchCandidate* start_match) {
if (start_match == nullptr) {
return false;
}
......@@ -219,11 +218,11 @@ bool OnDeviceHeadServing::FindStartNode(const std::string& prefix,
return start_match->text.size() >= prefix.size();
}
uint32_t OnDeviceHeadServing::ReadMaxScoreAsRoot(uint32_t address,
MatchCandidate* leaf_candidate,
bool* is_successful) {
uint32_t OnDeviceHeadModel::ReadMaxScoreAsRoot(uint32_t address,
MatchCandidate* leaf_candidate,
bool* is_successful) {
if (is_successful == nullptr) {
DVLOG(1) << "On Device Head Serving: a boolean var is_successful "
DVLOG(1) << "On Device Head model: a boolean var is_successful "
<< "is required when calling function ReadMaxScoreAsRoot";
return 0;
}
......@@ -250,7 +249,7 @@ uint32_t OnDeviceHeadServing::ReadMaxScoreAsRoot(uint32_t address,
return max_score;
}
bool OnDeviceHeadServing::ReadNextChild(MatchCandidate* candidate) {
bool OnDeviceHeadModel::ReadNextChild(MatchCandidate* candidate) {
if (candidate == nullptr) {
return false;
}
......@@ -318,8 +317,8 @@ bool OnDeviceHeadServing::ReadNextChild(MatchCandidate* candidate) {
return is_successful;
}
std::vector<OnDeviceHeadServing::MatchCandidate>
OnDeviceHeadServing::ReadTreeNode(const MatchCandidate& current) {
std::vector<OnDeviceHeadModel::MatchCandidate> OnDeviceHeadModel::ReadTreeNode(
const MatchCandidate& current) {
std::vector<MatchCandidate> candidates;
// The current candidate passed in is a leaf node and we shall stop here.
if (current.is_complete_suggestion) {
......@@ -333,7 +332,7 @@ OnDeviceHeadServing::ReadTreeNode(const MatchCandidate& current) {
uint32_t max_score_as_root =
ReadMaxScoreAsRoot(current.address, &leaf_candidate, &is_successful);
if (!is_successful) {
DVLOG(1) << "On Device Head Serving: read max_score_as_root failed at "
DVLOG(1) << "On Device Head model: read max_score_as_root failed at "
<< "address [" << current.address << "]";
return candidates;
}
......@@ -358,19 +357,19 @@ OnDeviceHeadServing::ReadTreeNode(const MatchCandidate& current) {
return candidates;
}
bool OnDeviceHeadServing::ReadNextNumBytes(uint32_t num_bytes, char* buf) {
bool OnDeviceHeadModel::ReadNextNumBytes(uint32_t num_bytes, char* buf) {
uint32_t address = model_filestream_.tellg();
model_filestream_.read(buf, num_bytes);
if (model_filestream_.fail()) {
DVLOG(1) << "On Device Head Serving: ifstream read error at address ["
DVLOG(1) << "On Device Head model: ifstream read error at address ["
<< address << "], when trying to read [" << num_bytes << "] bytes";
return false;
}
return true;
}
uint32_t OnDeviceHeadServing::ReadNextNumBytesAsInt(uint32_t num_bytes,
bool* is_successful) {
uint32_t OnDeviceHeadModel::ReadNextNumBytesAsInt(uint32_t num_bytes,
bool* is_successful) {
char* buf = new char[num_bytes];
*is_successful = ReadNextNumBytes(num_bytes, buf);
if (!*is_successful) {
......@@ -384,7 +383,7 @@ uint32_t OnDeviceHeadServing::ReadNextNumBytesAsInt(uint32_t num_bytes,
return result;
}
bool OnDeviceHeadServing::OpenModelFileStream(const uint32_t start_address) {
bool OnDeviceHeadModel::OpenModelFileStream(const uint32_t start_address) {
// First close the file if it's still open.
if (model_filestream_.is_open()) {
LOG(WARNING) << "Previous file is still open";
......@@ -403,7 +402,7 @@ bool OnDeviceHeadServing::OpenModelFileStream(const uint32_t start_address) {
return true;
}
void OnDeviceHeadServing::MaybeCloseModelFileStream() {
void OnDeviceHeadModel::MaybeCloseModelFileStream() {
if (model_filestream_.is_open()) {
model_filestream_.close();
}
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_OMNIBOX_BROWSER_ON_DEVICE_HEAD_SERVING_H_
#define COMPONENTS_OMNIBOX_BROWSER_ON_DEVICE_HEAD_SERVING_H_
#ifndef COMPONENTS_OMNIBOX_BROWSER_ON_DEVICE_HEAD_MODEL_H_
#define COMPONENTS_OMNIBOX_BROWSER_ON_DEVICE_HEAD_MODEL_H_
#include <fstream>
#include <list>
......@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
// On device head serving feature uses an on device model which encodes some
// On device head suggest feature uses an on device model which encodes some
// top queries into a radix tree (https://en.wikipedia.org/wiki/Radix_tree), to
// help users quickly get head suggestions when they are under poor network
// condition. When serving, it performs a search in the tree similar as BFS but
......@@ -20,7 +20,7 @@
// the given prefix.
//
// Each node in the tree is encoded using following format to optimize storage
// (see on_device_head_serving_unittest.cc for an example tree model):
// (see on_device_head_model_unittest.cc for an example tree model):
// ------------------------------------------------------------------------
// | max_score_as_root | child_0 | child_1 | ... | child_n-1 | 0 (1 byte) |
// ------------------------------------------------------------------------
......@@ -66,11 +66,10 @@
// The size of score and address will be given in the first two bytes of the
// model file.
// TODO(crbug.com/925072): rename OnDeviceHeadServing to *Model.
class OnDeviceHeadServing {
class OnDeviceHeadModel {
public:
// Creates and returns an instance for serving on device head model.
static std::unique_ptr<OnDeviceHeadServing> Create(
// Creates and returns an on device head model instance.
static std::unique_ptr<OnDeviceHeadModel> Create(
const std::string& model_filename,
int max_num_matches_to_return);
......@@ -91,11 +90,11 @@ class OnDeviceHeadServing {
std::vector<std::pair<std::string, uint32_t>> GetSuggestionsForPrefix(
const std::string& prefix);
~OnDeviceHeadServing();
~OnDeviceHeadModel();
private:
OnDeviceHeadServing(const std::string& model_filename,
uint32_t max_num_matches_to_return);
OnDeviceHeadModel(const std::string& model_filename,
uint32_t max_num_matches_to_return);
// A useful data structure to keep track of the tree nodes should be and have
// been visited during tree traversal.
......@@ -182,4 +181,4 @@ class OnDeviceHeadServing {
uint32_t max_num_matches_to_return_;
};
#endif // COMPONENTS_OMNIBOX_BROWSER_ON_DEVICE_HEAD_SERVING_H_
#endif // COMPONENTS_OMNIBOX_BROWSER_ON_DEVICE_HEAD_MODEL_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/on_device_head_serving.h"
#include "components/omnibox/browser/on_device_head_model.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
......@@ -59,91 +59,90 @@ base::FilePath GetTestModelPath() {
} // namespace
class OnDeviceHeadServingTest : public testing::Test {
class OnDeviceHeadModelTest : public testing::Test {
protected:
void SetUp() override {
base::FilePath file_path = GetTestModelPath();
ASSERT_TRUE(base::PathExists(file_path));
#if defined(OS_WIN)
serving_ =
OnDeviceHeadServing::Create(base::WideToUTF8(file_path.value()), 4);
model_ = OnDeviceHeadModel::Create(base::WideToUTF8(file_path.value()), 4);
#else
serving_ = OnDeviceHeadServing::Create(file_path.value(), 4);
model_ = OnDeviceHeadModel::Create(file_path.value(), 4);
#endif
ASSERT_TRUE(serving_);
ASSERT_TRUE(model_);
}
void TearDown() override { serving_.reset(); }
void TearDown() override { model_.reset(); }
std::unique_ptr<OnDeviceHeadServing> serving_;
std::unique_ptr<OnDeviceHeadModel> model_;
};
TEST_F(OnDeviceHeadServingTest, SizeOfScoreAndAddress) {
EXPECT_EQ((int)serving_->num_bytes_of_score(), 2);
EXPECT_EQ((int)serving_->num_bytes_of_address(), 3);
TEST_F(OnDeviceHeadModelTest, SizeOfScoreAndAddress) {
EXPECT_EQ((int)model_->num_bytes_of_score(), 2);
EXPECT_EQ((int)model_->num_bytes_of_address(), 3);
}
TEST_F(OnDeviceHeadServingTest, GetSuggestions) {
auto suggestions = serving_->GetSuggestionsForPrefix("go");
TEST_F(OnDeviceHeadModelTest, GetSuggestions) {
auto suggestions = model_->GetSuggestionsForPrefix("go");
EXPECT_THAT(suggestions,
ElementsAre(Pair("google maps", 32765), Pair("google", 32764),
Pair("googler", 32762)));
suggestions = serving_->GetSuggestionsForPrefix("ge");
suggestions = model_->GetSuggestionsForPrefix("ge");
EXPECT_THAT(suggestions, ElementsAre(Pair("get out", 32763)));
suggestions = serving_->GetSuggestionsForPrefix("ga");
suggestions = model_->GetSuggestionsForPrefix("ga");
EXPECT_THAT(suggestions, ElementsAre(Pair("gamestop", 32761)));
}
TEST_F(OnDeviceHeadServingTest, NoMatch) {
auto suggestions = serving_->GetSuggestionsForPrefix("x");
TEST_F(OnDeviceHeadModelTest, NoMatch) {
auto suggestions = model_->GetSuggestionsForPrefix("x");
EXPECT_TRUE(suggestions.empty());
}
TEST_F(OnDeviceHeadServingTest, MatchTheEndOfSuggestion) {
auto suggestions = serving_->GetSuggestionsForPrefix("ap");
TEST_F(OnDeviceHeadModelTest, MatchTheEndOfSuggestion) {
auto suggestions = model_->GetSuggestionsForPrefix("ap");
EXPECT_TRUE(suggestions.empty());
}
TEST_F(OnDeviceHeadServingTest, MatchAtTheMiddleOfSuggestion) {
auto suggestions = serving_->GetSuggestionsForPrefix("st");
TEST_F(OnDeviceHeadModelTest, MatchAtTheMiddleOfSuggestion) {
auto suggestions = model_->GetSuggestionsForPrefix("st");
EXPECT_TRUE(suggestions.empty());
}
TEST_F(OnDeviceHeadServingTest, EmptyInput) {
auto suggestions = serving_->GetSuggestionsForPrefix("");
TEST_F(OnDeviceHeadModelTest, EmptyInput) {
auto suggestions = model_->GetSuggestionsForPrefix("");
EXPECT_TRUE(suggestions.empty());
}
TEST_F(OnDeviceHeadServingTest, SetMaxSuggestionsToReturn) {
serving_->set_max_num_matches_to_return(5);
auto suggestions = serving_->GetSuggestionsForPrefix("g");
TEST_F(OnDeviceHeadModelTest, SetMaxSuggestionsToReturn) {
model_->set_max_num_matches_to_return(5);
auto suggestions = model_->GetSuggestionsForPrefix("g");
EXPECT_THAT(suggestions,
ElementsAre(Pair("g", 32767), Pair("gmail", 32766),
Pair("google maps", 32765), Pair("google", 32764),
Pair("get out", 32763)));
serving_->set_max_num_matches_to_return(2);
suggestions = serving_->GetSuggestionsForPrefix("ma");
model_->set_max_num_matches_to_return(2);
suggestions = model_->GetSuggestionsForPrefix("ma");
EXPECT_THAT(suggestions,
ElementsAre(Pair("maps", 32761), Pair("mail", 32760)));
}
TEST_F(OnDeviceHeadServingTest, NonEnglishLanguage) {
TEST_F(OnDeviceHeadModelTest, NonEnglishLanguage) {
// Chinese.
auto suggestions = serving_->GetSuggestionsForPrefix("谷");
auto suggestions = model_->GetSuggestionsForPrefix("谷");
EXPECT_THAT(suggestions, ElementsAre(Pair("谷歌", 32759)));
// Japanese.
suggestions = serving_->GetSuggestionsForPrefix("ガツガツ");
suggestions = model_->GetSuggestionsForPrefix("ガツガツ");
EXPECT_THAT(suggestions, ElementsAre(Pair("ガツガツしてる人", 32759)));
// Korean.
suggestions = serving_->GetSuggestionsForPrefix("비데 ");
suggestions = model_->GetSuggestionsForPrefix("비데 ");
EXPECT_THAT(suggestions, ElementsAre(Pair("비데 두꺼비", 32759)));
// Russian.
suggestions = serving_->GetSuggestionsForPrefix("пере");
suggestions = model_->GetSuggestionsForPrefix("пере");
EXPECT_THAT(suggestions, ElementsAre(Pair("переводчик", 32759)));
}
......@@ -224,8 +224,7 @@ void OnDeviceHeadProvider::ResetModelInstanceFromNewModel(
const std::string& new_model_filename) {
if (new_model_filename.empty())
return;
model_ =
OnDeviceHeadServing::Create(new_model_filename, provider_max_matches_);
model_ = OnDeviceHeadModel::Create(new_model_filename, provider_max_matches_);
}
std::unique_ptr<OnDeviceHeadProvider::OnDeviceHeadProviderParams>
......
......@@ -11,7 +11,7 @@
#include "base/files/file_path.h"
#include "components/omnibox/browser/autocomplete_provider.h"
#include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/omnibox/browser/on_device_head_serving.h"
#include "components/omnibox/browser/on_device_head_model.h"
#include "components/omnibox/browser/on_device_model_update_listener.h"
class AutocompleteProviderListener;
......@@ -92,7 +92,7 @@ class OnDeviceHeadProvider : public AutocompleteProvider {
// The model instance which serves top suggestions matching the Autocomplete
// input and is only accessed in |worker_task_runner_|.
std::unique_ptr<OnDeviceHeadServing> model_;
std::unique_ptr<OnDeviceHeadModel> model_;
// The request id used to trace current request to the on device head model.
// The id will be increased whenever a new request is received from the
......
......@@ -13,7 +13,7 @@
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_provider_listener.h"
#include "components/omnibox/browser/fake_autocomplete_provider_client.h"
#include "components/omnibox/browser/on_device_head_serving.h"
#include "components/omnibox/browser/on_device_head_model.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -47,7 +47,7 @@ class OnDeviceHeadProviderTest : public testing::Test,
void SetTestOnDeviceHeadModel() {
base::FilePath file_path;
base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
// The same test model also used in ./on_device_head_serving_unittest.cc.
// The same test model also used in ./on_device_head_model_unittest.cc.
file_path = file_path.AppendASCII("components/test/data/omnibox");
ASSERT_TRUE(base::PathExists(file_path));
auto* update_listener = OnDeviceModelUpdateListener::GetInstance();
......
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