in source/Registry.cpp [253:318]
void Registry::dump_models(
const boost::filesystem::path& path,
const std::size_t batch_size) const {
// Remove existing model files under this directory.
for (auto& file : boost::filesystem::directory_iterator(path)) {
const auto& file_path = file.path();
if (boost::filesystem::is_regular_file(file_path) &&
boost::starts_with(file_path.filename().string(), "model@")) {
boost::filesystem::remove(file_path);
}
}
std::vector<Model> models;
for (const auto& model : models_) {
models.push_back(model.second);
}
std::vector<FieldModel> field_models;
for (const auto& field_model : field_models_) {
field_models.push_back(field_model.second);
}
const auto total_batch =
(models_.size() + field_models_.size()) / batch_size + 1;
const auto padded_total_batch = fmt::format("{:0>5}", total_batch);
auto queue = sparta::work_queue<std::size_t>(
[&](std::size_t batch) {
// Construct a valid sharded file name for SAPP.
const auto padded_batch = fmt::format("{:0>5}", batch);
const auto batch_path = path /
("model@" + padded_batch + "-of-" + padded_total_batch + ".json");
std::ofstream batch_stream(batch_path.native(), std::ios_base::out);
if (!batch_stream.is_open()) {
ERROR(1, "Unable to write models to `{}`.", batch_path.native());
return;
}
batch_stream << "// @"
<< "generated\n";
// Write the current batch of models to file.
auto writer = JsonValidation::compact_writer();
for (std::size_t i = batch_size * batch; i < batch_size * (batch + 1) &&
i < models.size() + field_models.size();
i++) {
if (i < models.size()) {
writer->write(models[i].to_json(context_), &batch_stream);
} else {
writer->write(
field_models[i - models.size()].to_json(context_),
&batch_stream);
}
batch_stream << "\n";
}
batch_stream.close();
},
sparta::parallel::default_num_threads());
for (std::size_t batch = 0; batch < total_batch; batch++) {
queue.add_item(batch);
}
queue.run_all();
LOG(1, "Wrote models to {} shards.", total_batch);
}