Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dict_compiler): rebuild packs on demand #816

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions src/rime/dict/dict_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,38 +149,59 @@ bool DictCompiler::Compile(const path& schema_file) {
return false;
}
syllabary = std::move(collector.syllabary);
} else if (packs_.size() > 0) {
if (primary_table->Load() && primary_table->GetSyllabary(&syllabary))
primary_table->Close();
else
LOG(WARNING) << "couldn't load syllabary from '" << schema_file << "'";
}
if (rebuild_prism &&
!BuildPrism(schema_file, dict_file_checksum, schema_file_checksum)) {
return false;
}
if (rebuild_table) {
for (int table_index = 1; table_index < tables_.size(); ++table_index) {
const auto& pack_name = packs_[table_index - 1];
EntryCollector collector(std::move(syllabary));
DictSettings settings;
auto dict_file = source_resolver_->ResolvePath(pack_name + ".dict.yaml");
if (!std::filesystem::exists(dict_file)) {
LOG(ERROR) << "source file '" << dict_file << "' does not exist.";
continue;
}
if (!load_dict_settings_from_file(&settings, dict_file)) {
LOG(ERROR) << "failed to load settings from '" << dict_file << "'.";
continue;
}
vector<path> dict_files;
if (!get_dict_files_from_settings(&dict_files, settings,
source_resolver_.get())) {
continue;
}
uint32_t pack_file_checksum =
compute_dict_file_checksum(dict_file_checksum, dict_files, settings);
for (int table_index = 1; table_index < tables_.size(); ++table_index) {
const auto& pack_name = packs_[table_index - 1];
auto pack_table = tables_[table_index];
EntryCollector collector(std::move(syllabary));
DictSettings settings;
auto dict_file = source_resolver_->ResolvePath(pack_name + ".dict.yaml");
if (!std::filesystem::exists(dict_file)) {
if (pack_table->Exists())
LOG(INFO) << "pack source file '" << dict_file
<< "' does not exist, using prebuilt table '"
<< pack_table->file_path() << "'";
else
LOG(ERROR) << "neither pack source file '" << dict_file
<< "' nor a prebuilt table exists";
continue;
}
if (!load_dict_settings_from_file(&settings, dict_file)) {
LOG(ERROR) << "failed to load settings from '" << dict_file << "'.";
continue;
}
vector<path> dict_files;
if (!get_dict_files_from_settings(&dict_files, settings,
source_resolver_.get())) {
continue;
}
uint32_t pack_file_checksum =
compute_dict_file_checksum(dict_file_checksum, dict_files, settings);
bool rebuild_pack = true;
if (pack_table->Exists() && pack_table->Load()) {
rebuild_pack = pack_table->dict_file_checksum() != pack_file_checksum;
}
if (rebuild_pack) {
LOG(INFO) << "rebuilding pack '" << pack_name << "'";
if (!BuildTable(table_index, collector, &settings, dict_files,
pack_file_checksum)) {
LOG(ERROR) << "failed to build pack: " << pack_name;
}
syllabary = std::move(collector.syllabary);
} else {
LOG(INFO) << "pack '" << pack_name << "' reuses up-to-date table '"
<< pack_table->file_path() << "'";
}
syllabary = std::move(collector.syllabary);
pack_table->Close();
}
// done!
return true;
Expand Down
Loading