Line data Source code
1 : #include "DatabaseConfigurationInterface.h"
2 :
3 : #include <algorithm>
4 : #include <iterator>
5 :
6 : #include "artdaq-database/ConfigurationDB/configurationdbifc.h"
7 :
8 : #include "artdaq-database/BasicTypes/basictypes.h"
9 : using artdaq::database::basictypes::JsonData;
10 :
11 : using ots::DatabaseConfigurationInterface;
12 : using config_version_map_t = ots::DatabaseConfigurationInterface::config_version_map_t;
13 :
14 : namespace db = artdaq::database::configuration;
15 : using VersionInfoList_t = db::ConfigurationInterface::VersionInfoList_t;
16 :
17 : constexpr auto default_dbprovider = "filesystem";
18 : constexpr auto default_entity = "OTSDAQROOT";
19 :
20 2336 : int DatabaseConfigurationInterface::fill(ConfigurationBase* configuration, int version) const noexcept {
21 2336 : auto ifc = db::ConfigurationInterface{default_dbprovider};
22 :
23 2336 : auto versionstring = std::to_string(version);
24 :
25 2336 : if (-1 == version) {
26 0 : versionstring = std::to_string(findLatestVersion(configuration));
27 : }
28 :
29 2336 : auto result = ifc.template loadVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
30 :
31 2333 : if (result.first) {
32 2329 : return 0;
33 : }
34 :
35 4 : std::cout << "DBI Error:" << result.second << "\n";
36 :
37 4 : return -1;
38 2333 : }
39 :
40 2739 : int DatabaseConfigurationInterface::saveActiveVersion(const ConfigurationBase* configuration, bool overwrite) const noexcept {
41 2739 : auto ifc = db::ConfigurationInterface{default_dbprovider};
42 :
43 2739 : auto versionstring = std::to_string(configuration->getViewVersion());
44 :
45 36 : auto result = overwrite ? ifc.template overwriteVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity)
46 8181 : : ifc.template storeVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
47 :
48 2739 : if (result.first) {
49 2720 : return 0;
50 : }
51 :
52 19 : std::cout << "DBI Error:" << result.second << "\n";
53 :
54 19 : return -1;
55 2739 : }
56 :
57 22 : int DatabaseConfigurationInterface::markActiveVersionReadonly(const ConfigurationBase* configuration) const noexcept {
58 22 : auto ifc = db::ConfigurationInterface{default_dbprovider};
59 :
60 22 : auto versionstring = std::to_string(configuration->getViewVersion());
61 :
62 22 : auto result = ifc.template markVersionReadonly<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
63 :
64 22 : if (result.first) {
65 20 : return 0;
66 : }
67 :
68 2 : std::cout << "DBI Error:" << result.second << "\n";
69 :
70 2 : return -1;
71 22 : }
72 :
73 64 : int DatabaseConfigurationInterface::findLatestVersion(const ConfigurationBase* configuration) const noexcept {
74 64 : auto versions = getVersions(configuration);
75 :
76 64 : if (versions.empty()) {
77 2 : return -1;
78 : }
79 :
80 62 : return *(versions.rbegin());
81 64 : }
82 :
83 2113 : std::set<int> DatabaseConfigurationInterface::getVersions(const ConfigurationBase* configuration) const noexcept try {
84 4226 : auto ifc = db::ConfigurationInterface{default_dbprovider};
85 2113 : auto result = ifc.template getVersions<decltype(configuration)>(configuration, default_entity);
86 :
87 2113 : auto to_set = [](auto const& inputList) {
88 2113 : auto resultSet = std::set<int>{};
89 2113 : std::for_each(inputList.begin(), inputList.end(),
90 9135 : [&resultSet](std::string const& version) { resultSet.insert(std::stol(version, nullptr, 10)); });
91 2113 : return resultSet;
92 0 : };
93 :
94 2113 : return to_set(result);
95 2113 : } catch (std::exception const& e) {
96 0 : std::cout << "DBI Exception:" << e.what() << "\n";
97 0 : return {};
98 0 : }
99 :
100 18 : std::set<std::string> DatabaseConfigurationInterface::listConfigurationsTypes() const try {
101 18 : auto ifc = db::ConfigurationInterface{default_dbprovider};
102 :
103 18 : auto collection_name_prefix = std::string{};
104 :
105 36 : return ifc.listCollections(collection_name_prefix);
106 18 : } catch (std::exception const& e) {
107 0 : std::cout << "DBI Exception:" << e.what() << "\n";
108 0 : throw std::runtime_error(e.what());
109 0 : } catch (...) {
110 0 : std::cout << "DBI Unknown exception.\n";
111 0 : throw std::runtime_error("DBI Unknown exception.");
112 0 : }
113 :
114 66 : std::set<std::string> DatabaseConfigurationInterface::findAllGlobalConfigurations(std::string const& search) const try {
115 66 : auto ifc = db::ConfigurationInterface{default_dbprovider};
116 :
117 129 : return ifc.findGlobalConfigurations(search);
118 3 : } catch (std::exception const& e) {
119 3 : std::cout << "DBI Exception:" << e.what() << "\n";
120 3 : throw std::runtime_error(e.what());
121 3 : } catch (...) {
122 0 : std::cout << "DBI Unknown exception.\n";
123 0 : throw std::runtime_error("DBI Unknown exception.");
124 0 : }
125 :
126 107 : config_version_map_t DatabaseConfigurationInterface::loadGlobalConfiguration(std::string const& configuration) const try {
127 107 : auto ifc = db::ConfigurationInterface{default_dbprovider};
128 107 : auto result = ifc.loadGlobalConfiguration(configuration);
129 :
130 101 : auto to_map = [](auto const& inputList) {
131 101 : auto resultMap = config_version_map_t{};
132 :
133 101 : std::for_each(inputList.begin(), inputList.end(),
134 139 : [&resultMap](auto const& info) { resultMap[info.configuration] = std::stol(info.version, 0, 10); });
135 :
136 101 : return resultMap;
137 0 : };
138 :
139 202 : return to_map(result);
140 107 : } catch (std::exception const& e) {
141 6 : std::cout << "DBI Exception:" << e.what() << "\n";
142 6 : throw std::runtime_error(e.what());
143 6 : } catch (...) {
144 0 : std::cout << "DBI Unknown exception.\n";
145 0 : throw std::runtime_error("DBI Unknown exception.");
146 0 : }
147 :
148 : namespace {
149 284 : VersionInfoList_t to_version_info_list(config_version_map_t const& inputMap) {
150 284 : auto resultList = VersionInfoList_t{};
151 284 : std::transform(inputMap.begin(), inputMap.end(), std::back_inserter(resultList), [](auto const& mapEntry) {
152 338 : return VersionInfoList_t::value_type{mapEntry.first, std::to_string(mapEntry.second), default_entity};
153 1014 : });
154 284 : return resultList;
155 0 : }
156 : } // namespace
157 :
158 243 : void DatabaseConfigurationInterface::storeGlobalConfiguration(config_version_map_t const& configurationMap, std::string const& configuration,
159 : bool allowOverwrite) const try {
160 243 : auto ifc = db::ConfigurationInterface{default_dbprovider};
161 :
162 243 : auto result = ifc.storeGlobalConfiguration(to_version_info_list(configurationMap), configuration, allowOverwrite);
163 :
164 243 : if (result.first) {
165 231 : return;
166 : }
167 :
168 12 : throw std::runtime_error(result.second);
169 255 : } catch (std::exception const& e) {
170 12 : std::cout << "DBI Exception:" << e.what() << "\n";
171 12 : throw std::runtime_error(e.what());
172 12 : } catch (...) {
173 0 : std::cout << "DBI Unknown exception.\n";
174 0 : throw std::runtime_error("DBI Unknown exception.");
175 0 : }
176 :
177 41 : void DatabaseConfigurationInterface::storeGlobalConfiguration_mt(const config_version_map_t& configurationMap, const std::string& configuration,
178 : bool allowOverwrite) const try {
179 41 : auto ifc = db::ConfigurationInterface{default_dbprovider};
180 :
181 41 : auto result = ifc.storeGlobalConfiguration_mt(to_version_info_list(configurationMap), configuration, allowOverwrite);
182 :
183 41 : if (result.first) {
184 34 : return;
185 : }
186 :
187 7 : throw std::runtime_error(result.second);
188 48 : } catch (std::exception const& e) {
189 7 : std::cout << "DBI Exception:" << e.what() << "\n";
190 7 : throw std::runtime_error(e.what());
191 7 : } catch (...) {
192 0 : std::cout << "DBI Unknown exception.\n";
193 0 : throw std::runtime_error("DBI Unknown exception.");
194 0 : }
195 :
196 83 : std::set<std::string> DatabaseConfigurationInterface::findCompositionsContaining(std::string const& configurationType,
197 : std::string const& version) const try {
198 166 : auto ifc = db::ConfigurationInterface{default_dbprovider};
199 :
200 166 : return ifc.findGlobalConfigurationsContaining(configurationType, version);
201 :
202 0 : } catch (std::exception const& e) {
203 0 : std::cout << "DBI Exception:" << e.what() << "\n";
204 0 : throw std::runtime_error(e.what());
205 0 : } catch (...) {
206 0 : std::cout << "DBI Unknown exception.\n";
207 0 : throw std::runtime_error("DBI Unknown exception.");
208 0 : }
209 :
210 8 : DatabaseConfigurationInterface::result_t DatabaseConfigurationInterface::getVersions_safe(const ConfigurationBase* configuration,
211 : std::set<int>& versions) const noexcept try {
212 8 : auto ifc = db::ConfigurationInterface{default_dbprovider};
213 :
214 8 : std::list<std::string> versionStrings;
215 8 : auto result = ifc.template getVersions_safe<decltype(configuration)>(configuration, default_entity, versionStrings);
216 :
217 8 : if (!result.first) {
218 0 : versions.clear();
219 0 : return result;
220 : }
221 :
222 8 : versions.clear();
223 18 : for (auto const& v : versionStrings) {
224 10 : versions.insert(std::stol(v, nullptr, 10));
225 : }
226 :
227 8 : return {true, "Success"};
228 8 : } catch (std::exception const& e) {
229 0 : versions.clear();
230 0 : return {false, std::string("Exception: ") + e.what()};
231 0 : } catch (...) {
232 0 : versions.clear();
233 0 : return {false, "Unknown exception"};
234 0 : }
235 :
236 2 : DatabaseConfigurationInterface::result_t DatabaseConfigurationInterface::listConfigurationsTypes_safe(
237 : std::set<std::string>& collections) const noexcept try {
238 2 : auto ifc = db::ConfigurationInterface{default_dbprovider};
239 :
240 2 : auto collection_name_prefix = std::string{};
241 :
242 2 : return ifc.listCollections_safe(collection_name_prefix, collections);
243 2 : } catch (std::exception const& e) {
244 0 : collections.clear();
245 0 : return {false, std::string("Exception: ") + e.what()};
246 0 : } catch (...) {
247 0 : collections.clear();
248 0 : return {false, "Unknown exception"};
249 0 : }
250 :
251 6 : DatabaseConfigurationInterface::result_t DatabaseConfigurationInterface::findAllGlobalConfigurations_safe(
252 : std::string const& search, std::set<std::string>& configurations) const noexcept try {
253 6 : auto ifc = db::ConfigurationInterface{default_dbprovider};
254 :
255 6 : return ifc.findGlobalConfigurations_safe(search, configurations);
256 0 : } catch (std::exception const& e) {
257 0 : configurations.clear();
258 0 : return {false, std::string("Exception: ") + e.what()};
259 0 : } catch (...) {
260 0 : configurations.clear();
261 0 : return {false, "Unknown exception"};
262 0 : }
263 :
264 6 : DatabaseConfigurationInterface::result_t DatabaseConfigurationInterface::loadGlobalConfiguration_safe(std::string const& configuration,
265 : config_version_map_t& configMap) const noexcept
266 : try {
267 6 : auto ifc = db::ConfigurationInterface{default_dbprovider};
268 :
269 6 : VersionInfoList_t members;
270 6 : auto result = ifc.loadGlobalConfiguration_safe(configuration, members);
271 :
272 6 : if (!result.first) {
273 2 : configMap.clear();
274 2 : return result;
275 : }
276 :
277 4 : configMap.clear();
278 12 : for (auto const& info : members) {
279 8 : configMap[info.configuration] = std::stol(info.version, nullptr, 10);
280 : }
281 :
282 4 : return {true, "Success"};
283 6 : } catch (std::exception const& e) {
284 0 : configMap.clear();
285 0 : return {false, std::string("Exception: ") + e.what()};
286 0 : } catch (...) {
287 0 : configMap.clear();
288 0 : return {false, "Unknown exception"};
289 0 : }
290 :
291 4 : DatabaseConfigurationInterface::result_t DatabaseConfigurationInterface::findCompositionsContaining_safe(
292 : std::string const& configurationType, std::string const& version, std::set<std::string>& compositions) const noexcept try {
293 8 : auto ifc = db::ConfigurationInterface{default_dbprovider};
294 :
295 8 : return ifc.findGlobalConfigurationsContaining_safe(configurationType, version, "", compositions);
296 0 : } catch (std::exception const& e) {
297 0 : compositions.clear();
298 0 : return {false, std::string("Exception: ") + e.what()};
299 0 : } catch (...) {
300 0 : compositions.clear();
301 0 : return {false, "Unknown exception"};
302 0 : }
|