NGen
Loading...
Searching...
No Matches
serialization_helper.hpp
1#ifndef NGEN_REALIZATION_SERIALIZATION_HELPER_H
2#define NGEN_REALIZATION_SERIALIZATION_HELPER_H
3
4#include "JSONProperty.hpp"
5#include "global_config.hpp"
6#include "path_tokens.hpp"
7
8#include <boost/property_tree/ptree.hpp>
9
10#include <string>
11#include <vector>
12
13namespace realization {
14namespace config {
15
32 boost::property_tree::ptree& block)
33{
34 for (const auto* path_key : {"path", "save.path", "restore.path"}) {
35 auto path_val = block.get_optional<std::string>(path_key);
36 if (path_val) {
37 block.put(path_key, utilities::resolve_path_tokens(*path_val));
38 }
39 }
40}
41
66 geojson::PropertyMap& global_configs,
67 const std::vector<std::string>& known_ids)
68{
69 if (known_ids.empty()) return;
70
71 const char* ser_key = to_key_string(GlobalConfigKey::SERIALIZATION);
72 auto ser_it = global_configs.find(ser_key);
73 if (ser_it == global_configs.end()) return;
74 if (ser_it->second.get_type() != geojson::PropertyType::Object) return;
75
76 geojson::PropertyMap ser_map = ser_it->second.get_values();
77
78 auto restore_it = ser_map.find("restore");
79 if (restore_it == ser_map.end()) return;
80 if (restore_it->second.get_type() != geojson::PropertyType::Object) return;
81
82 geojson::PropertyMap restore_map = restore_it->second.get_values();
83 if (restore_map.count("id_subset") != 0) return; // caller already set it
84
85 // Build the id_subset list as a List-typed JSONProperty whose
86 // elements are string-typed JSONProperties (matching how the
87 // ptree-based constructor emits JSON arrays of strings).
88 std::vector<geojson::JSONProperty> id_list;
89 id_list.reserve(known_ids.size());
90 for (const auto& id : known_ids) {
91 id_list.emplace_back(std::string(""), id);
92 }
93 restore_map.emplace(
94 "id_subset",
95 geojson::JSONProperty(std::string("id_subset"), std::move(id_list))
96 );
97
98 // Rebuild the nested objects with the new map. JSONProperty's
99 // PropertyMap-ctor takes a non-const reference and captures the
100 // map by value into its internal field, so these locals are the
101 // actual owners of the reconstituted tree.
102 geojson::JSONProperty restore_prop(std::string("restore"), restore_map);
103 ser_map.erase("restore");
104 ser_map.emplace("restore", std::move(restore_prop));
105
106 geojson::JSONProperty ser_prop(std::string(ser_key), ser_map);
107 global_configs.erase(ser_key);
108 global_configs.emplace(ser_key, std::move(ser_prop));
109}
110
111} // namespace config
112} // namespace realization
113
114#endif // NGEN_REALIZATION_SERIALIZATION_HELPER_H
@TODO: Convert JSONProperty into a variant of the supported types
Definition JSONProperty.hpp:161
std::map< std::string, JSONProperty > PropertyMap
Shorthand for a mapping between strings and properties.
Definition JSONProperty.hpp:21
@ SERIALIZATION
The serialization block consumed by the BMI serialization protocols (Ngen{Serialization,...
void apply_serialization_restore_subset_default(geojson::PropertyMap &global_configs, const std::vector< std::string > &known_ids)
Auto-populate serialization.restore.id_subset with the set of catchment identifiers that this realiza...
Definition serialization_helper.hpp:65
void apply_serialization_path_token_resolution(boost::property_tree::ptree &block)
Resolve {{rank}}/{{pid}}/{{host}}/{{date}} tokens in every path field the serialization block may car...
Definition serialization_helper.hpp:31
const char * to_key_string(GlobalConfigKey which)
Top-level JSON key string for a given GlobalConfigKey.
Definition global_config.hpp:33
Definition HY_Features.hpp:13
std::string resolve_path_tokens(const std::string &path)
Expand {{rank}}, {{pid}}, {{host}}, {{date}} tokens in path and return the resolved string.
Definition path_tokens.cpp:102