Skip to content

Commit

Permalink
feat(dict_compiler): rebuild packs on demand (#816)
Browse files Browse the repository at this point in the history
Rebuild packs if the pack source dict file is updated, even if the
primary table is not updated.

Previously, packs are only rebuilt after the primary table is updated.
  • Loading branch information
ksqsf committed Feb 13, 2024
1 parent 878dd5c commit 28d1e7e
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions src/rime/dict/dict_compiler.cc
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

0 comments on commit 28d1e7e

Please sign in to comment.