Skip to content

Commit

Permalink
Rename EngineBuilder to DateLoader.
Browse files Browse the repository at this point in the history
#codehealth

PiperOrigin-RevId: 609291753
  • Loading branch information
hiroyuki-komatsu committed Feb 22, 2024
1 parent f6f92e9 commit 303b05b
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 201 deletions.
10 changes: 5 additions & 5 deletions src/engine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ package(default_visibility = ["//:__subpackages__"])

mozc_cc_library(
name = "engine_builder",
srcs = ["engine_builder.cc"],
hdrs = ["engine_builder.h"],
srcs = ["data_loader.cc"],
hdrs = ["data_loader.h"],
deps = [
":modules",
"//base:file_util",
Expand All @@ -57,10 +57,10 @@ mozc_cc_library(
)

mozc_cc_test(
name = "engine_builder_test",
srcs = ["engine_builder_test.cc"],
name = "data_loader_test",
srcs = ["data_loader_test.cc"],
data = [
"engine_builder_test.cc",
"data_loader_test.cc",
"//data_manager/testing:mock_mozc.data",
],
requires_full_emulation = False,
Expand Down
14 changes: 7 additions & 7 deletions src/engine/engine_builder.cc → src/engine/data_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "engine/engine_builder.h"
#include "engine/data_loader.h"

#include <algorithm>
#include <cstdint>
Expand Down Expand Up @@ -68,7 +68,7 @@ EngineReloadResponse::Status ConvertStatus(DataManager::Status status) {
}
} // namespace

uint64_t EngineBuilder::RegisterRequest(const EngineReloadRequest &request) {
uint64_t DataLoader::RegisterRequest(const EngineReloadRequest &request) {
const uint64_t id = Fingerprint(request.SerializeAsString());

absl::WriterMutexLock lock(&mutex_);
Expand Down Expand Up @@ -105,7 +105,7 @@ uint64_t EngineBuilder::RegisterRequest(const EngineReloadRequest &request) {
return requests_.front().id;
}

uint64_t EngineBuilder::UnregisterRequest(uint64_t id) {
uint64_t DataLoader::UnregisterRequest(uint64_t id) {
absl::WriterMutexLock lock(&mutex_);

const auto it =
Expand All @@ -119,10 +119,10 @@ uint64_t EngineBuilder::UnregisterRequest(uint64_t id) {
return requests_.empty() ? 0 : requests_.front().id;
}

std::unique_ptr<EngineBuilder::EngineResponseFuture> EngineBuilder::Build(
std::unique_ptr<DataLoader::ResponseFuture> DataLoader::Build(
uint64_t id) const {
return std::make_unique<BackgroundFuture<EngineResponse>>([&, id]() {
EngineResponse result;
return std::make_unique<BackgroundFuture<Response>>([&, id]() {
Response result;
result.id = id;
result.response.set_status(EngineReloadResponse::DATA_MISSING);

Expand Down Expand Up @@ -189,7 +189,7 @@ std::unique_ptr<EngineBuilder::EngineResponseFuture> EngineBuilder::Build(
});
}

void EngineBuilder::Clear() {
void DataLoader::Clear() {
absl::WriterMutexLock lock(&mutex_);
requests_.clear();
sequence_id_ = 0;
Expand Down
22 changes: 11 additions & 11 deletions src/engine/engine_builder.h → src/engine/data_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef MOZC_ENGINE_ENGINE_BUILDER_H_
#define MOZC_ENGINE_ENGINE_BUILDER_H_
#ifndef MOZC_ENGINE_DATA_LOADER_H_
#define MOZC_ENGINE_DATA_LOADER_H_

#include <atomic>
#include <cstdint>
Expand All @@ -44,22 +44,22 @@

namespace mozc {

class EngineBuilder {
class DataLoader {
public:
EngineBuilder() = default;
EngineBuilder(const EngineBuilder &) = delete;
EngineBuilder &operator=(const EngineBuilder &) = delete;
virtual ~EngineBuilder() = default;
DataLoader() = default;
DataLoader(const DataLoader &) = delete;
DataLoader &operator=(const DataLoader &) = delete;
virtual ~DataLoader() = default;

struct EngineResponse {
struct Response {
uint64_t id = 0; // engine id. Fingerprint of EngineReloadRequest.
EngineReloadResponse response;
std::unique_ptr<engine::Modules> modules;
};

// Wrapped with BackgroundFuture so the data loading is
// executed asynchronously.
using EngineResponseFuture = BackgroundFuture<EngineResponse>;
using ResponseFuture = BackgroundFuture<Response>;

// Accepts engine reload request and immediately returns the engine id with
// the highest priority defined as follows:
Expand All @@ -81,7 +81,7 @@ class EngineBuilder {
// Since BackgroundFuture is not movable/copyable, we wrap it with
// std::unique_ptr. This method doesn't return nullptr. All errors
// are stored in EngineReloadResponse::response::status.
virtual std::unique_ptr<EngineResponseFuture> Build(uint64_t id) const;
virtual std::unique_ptr<ResponseFuture> Build(uint64_t id) const;

void Clear();

Expand All @@ -103,4 +103,4 @@ class EngineBuilder {

} // namespace mozc

#endif // MOZC_ENGINE_ENGINE_BUILDER_H_
#endif // MOZC_ENGINE_DATA_LOADER_H_
102 changes: 51 additions & 51 deletions src/engine/engine_builder_test.cc → src/engine/data_loader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "engine/engine_builder.h"
#include "engine/data_loader.h"

#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -56,39 +56,39 @@ struct Param {
std::string predictor_name;
};

class EngineBuilderTest : public testing::TestWithTempUserProfile,
public ::testing::WithParamInterface<Param> {
class DataLoaderTest : public testing::TestWithTempUserProfile,
public ::testing::WithParamInterface<Param> {
protected:
EngineBuilderTest()
DataLoaderTest()
: mock_data_path_(
testing::GetSourcePath({MOZC_SRC_COMPONENTS("data_manager"),
"testing", "mock_mozc.data"})) {
LOG(INFO) << mock_data_path_;
}

void Clear() {
builder_.Clear();
loader_.Clear();
request_.Clear();
}

const std::string mock_data_path_;
EngineBuilder builder_;
DataLoader loader_;
EngineReloadRequest request_;
};

TEST_P(EngineBuilderTest, BasicTest) {
TEST_P(DataLoaderTest, BasicTest) {
{
// Test request without install.
request_.set_engine_type(GetParam().type);
request_.set_file_path(mock_data_path_);
request_.set_magic_number(kMockMagicNumber);

const uint64_t id = builder_.RegisterRequest(request_);
std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
const uint64_t id = loader_.RegisterRequest(request_);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

DataManager data_manager;
data_manager.InitFromFile(mock_data_path_, kMockMagicNumber);
Expand Down Expand Up @@ -123,12 +123,12 @@ TEST_P(EngineBuilderTest, BasicTest) {
request_.set_file_path(src_path);
request_.set_install_location(install_path);
request_.set_magic_number(kMockMagicNumber);
const uint64_t id = builder_.RegisterRequest(request_);
const uint64_t id = loader_.RegisterRequest(request_);

std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);
response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

DataManager data_manager;
data_manager.InitFromFile(src_path, kMockMagicNumber);
Expand All @@ -151,7 +151,7 @@ TEST_P(EngineBuilderTest, BasicTest) {
}
}

TEST_P(EngineBuilderTest, AsyncBuildRepeatedly) {
TEST_P(DataLoaderTest, AsyncBuildRepeatedly) {
// Calls RegisterRequest multiple times.
// Makes sure that the last request is processed.
TempDirectory temp_dir = testing::MakeTempDirectoryOrDie();
Expand All @@ -169,15 +169,15 @@ TEST_P(EngineBuilderTest, AsyncBuildRepeatedly) {
ASSERT_OK(FileUtil::CopyFile(mock_data_path_, last_path));
request_.set_file_path(last_path);
request_.set_magic_number(kMockMagicNumber);
latest_id = builder_.RegisterRequest(request_);
latest_id = loader_.RegisterRequest(request_);
}
}

std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(latest_id);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(latest_id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

DataManager data_manager;
data_manager.InitFromFile(last_path, kMockMagicNumber);
Expand All @@ -196,18 +196,18 @@ TEST_P(EngineBuilderTest, AsyncBuildRepeatedly) {
EXPECT_EQ(response.id, latest_id);
}

TEST_P(EngineBuilderTest, AsyncBuildWithoutInstall) {
TEST_P(DataLoaderTest, AsyncBuildWithoutInstall) {
// Request preparation without install.
request_.set_engine_type(GetParam().type);
request_.set_file_path(mock_data_path_);
request_.set_magic_number(kMockMagicNumber);
const uint64_t id = builder_.RegisterRequest(request_);
const uint64_t id = loader_.RegisterRequest(request_);

std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

DataManager data_manager;
data_manager.InitFromFile(mock_data_path_, kMockMagicNumber);
Expand All @@ -225,7 +225,7 @@ TEST_P(EngineBuilderTest, AsyncBuildWithoutInstall) {
EXPECT_EQ(response.id, id);
}

TEST_P(EngineBuilderTest, AsyncBuildWithInstall) {
TEST_P(DataLoaderTest, AsyncBuildWithInstall) {
TempDirectory temp_dir = testing::MakeTempDirectoryOrDie();
const std::string tmp_src = FileUtil::JoinPath({temp_dir.path(), "src.data"});
const std::string install_path =
Expand All @@ -240,13 +240,13 @@ TEST_P(EngineBuilderTest, AsyncBuildWithInstall) {
request_.set_file_path(tmp_src);
request_.set_install_location(install_path);
request_.set_magic_number(kMockMagicNumber);
const uint64_t id = builder_.RegisterRequest(request_);
const uint64_t id = loader_.RegisterRequest(request_);

std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

// Builder should be ready now.
EXPECT_EQ(response.response.status(), EngineReloadResponse::RELOAD_READY);
Expand All @@ -270,63 +270,63 @@ TEST_P(EngineBuilderTest, AsyncBuildWithInstall) {
EXPECT_EQ(response.id, id);
}

TEST_P(EngineBuilderTest, FailureCaseDataBroken) {
TEST_P(DataLoaderTest, FailureCaseDataBroken) {
// Test the case where input file is invalid.
request_.set_engine_type(GetParam().type);
request_.set_file_path(testing::GetSourceFileOrDie(
{MOZC_SRC_COMPONENTS("engine"), "engine_builder_test.cc"}));
{MOZC_SRC_COMPONENTS("engine"), "data_loader_test.cc"}));
request_.set_magic_number(kMockMagicNumber);
const uint64_t id = builder_.RegisterRequest(request_);
const uint64_t id = loader_.RegisterRequest(request_);

std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

EXPECT_EQ(response.response.status(), EngineReloadResponse::DATA_BROKEN);
EXPECT_FALSE(response.modules);
EXPECT_EQ(response.id, id);
}

TEST_P(EngineBuilderTest, InvalidId) {
TEST_P(DataLoaderTest, InvalidId) {
// Test the case where input file is invalid.
request_.set_engine_type(GetParam().type);
request_.set_file_path(mock_data_path_);
request_.set_magic_number(kMockMagicNumber);
const uint64_t id =
builder_.RegisterRequest(request_) + 1; // + 1 to make invalid id.
loader_.RegisterRequest(request_) + 1; // + 1 to make invalid id.

std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

EXPECT_EQ(response.response.status(), EngineReloadResponse::DATA_MISSING);
EXPECT_FALSE(response.modules);
EXPECT_EQ(response.id, id);
}

TEST_P(EngineBuilderTest, FailureCaseFileDoesNotExist) {
TEST_P(DataLoaderTest, FailureCaseFileDoesNotExist) {
// Test the case where input file doesn't exist.
request_.set_engine_type(GetParam().type);
request_.set_file_path("file_does_not_exist");
request_.set_magic_number(kMockMagicNumber);

const uint64_t id = builder_.RegisterRequest(request_);
std::unique_ptr<EngineBuilder::EngineResponseFuture> response_future =
builder_.Build(id);
const uint64_t id = loader_.RegisterRequest(request_);
std::unique_ptr<DataLoader::ResponseFuture> response_future =
loader_.Build(id);

response_future->Wait();
const EngineBuilder::EngineResponse &response = response_future->Get();
const DataLoader::Response &response = response_future->Get();

EXPECT_EQ(response.response.status(), EngineReloadResponse::MMAP_FAILURE);
EXPECT_FALSE(response.modules);
EXPECT_EQ(response.id, id);
}

TEST_P(EngineBuilderTest, RegisterRequestTest) {
TEST_P(DataLoaderTest, RegisterRequestTest) {
Clear();

auto id = [&](absl::string_view file_path, int32_t priority) {
Expand All @@ -342,11 +342,11 @@ TEST_P(EngineBuilderTest, RegisterRequestTest) {
request.set_engine_type(GetParam().type);
request.set_file_path(file_path);
request.set_priority(priority);
return builder_.RegisterRequest(request);
return loader_.RegisterRequest(request);
};

auto unregister_request = [&](absl::string_view file_path, int32_t priority) {
return builder_.UnregisterRequest(id(file_path, priority));
return loader_.UnregisterRequest(id(file_path, priority));
};

// Register request.
Expand Down Expand Up @@ -379,10 +379,10 @@ TEST_P(EngineBuilderTest, RegisterRequestTest) {
}

INSTANTIATE_TEST_SUITE_P(
EngineBuilderTest, EngineBuilderTest,
DataLoaderTest, DataLoaderTest,
::testing::Values(Param{EngineReloadRequest::DESKTOP, "DefaultPredictor"},
Param{EngineReloadRequest::MOBILE, "MobilePredictor"}),
[](const ::testing::TestParamInfo<Param>& info) -> std::string {
[](const ::testing::TestParamInfo<Param> &info) -> std::string {
return info.param.predictor_name;
});

Expand Down
Loading

0 comments on commit 303b05b

Please sign in to comment.