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 16 : int DatabaseConfigurationInterface::fill(ConfigurationBase* configuration, int version) const noexcept {
21 16 : auto ifc = db::ConfigurationInterface{default_dbprovider};
22 :
23 16 : auto versionstring = std::to_string(version);
24 :
25 16 : if (-1 == version) {
26 0 : versionstring = std::to_string(findLatestVersion(configuration));
27 : }
28 :
29 16 : auto result = ifc.template loadVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
30 :
31 16 : if (result.first) {
32 16 : return 0;
33 : }
34 :
35 0 : std::cout << "DBI Error:" << result.second << "\n";
36 :
37 0 : confirm(result.first);
38 :
39 0 : return -1;
40 16 : }
41 :
42 389 : int DatabaseConfigurationInterface::saveActiveVersion(const ConfigurationBase* configuration, bool overwrite) const noexcept {
43 389 : auto ifc = db::ConfigurationInterface{default_dbprovider};
44 :
45 389 : auto versionstring = std::to_string(configuration->getViewVersion());
46 :
47 4 : auto result = overwrite ? ifc.template overwriteVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity)
48 1163 : : ifc.template storeVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
49 :
50 389 : if (result.first) {
51 329 : return 0;
52 : }
53 :
54 60 : std::cout << "DBI Error:" << result.second << "\n";
55 :
56 60 : return -1;
57 389 : }
58 :
59 0 : int DatabaseConfigurationInterface::markActiveVersionReadonly(const ConfigurationBase* configuration) const noexcept {
60 0 : auto ifc = db::ConfigurationInterface{default_dbprovider};
61 :
62 0 : auto versionstring = std::to_string(configuration->getViewVersion());
63 :
64 0 : auto result = ifc.template markVersionReadonly<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
65 :
66 0 : if (result.first) {
67 0 : return 0;
68 : }
69 :
70 0 : std::cout << "DBI Error:" << result.second << "\n";
71 :
72 0 : confirm(result.first);
73 :
74 0 : return -1;
75 0 : }
76 :
77 2 : int DatabaseConfigurationInterface::findLatestVersion(const ConfigurationBase* configuration) const noexcept {
78 2 : auto versions = getVersions(configuration);
79 :
80 2 : if (versions.empty()) {
81 0 : return -1;
82 : }
83 :
84 2 : return *(versions.rbegin());
85 2 : }
86 :
87 2010 : std::set<int> DatabaseConfigurationInterface::getVersions(const ConfigurationBase* configuration) const noexcept try {
88 6030 : auto ifc = db::ConfigurationInterface{default_dbprovider};
89 2010 : auto result = ifc.template getVersions<decltype(configuration)>(configuration, default_entity);
90 :
91 2010 : auto to_set = [](auto const& inputList) {
92 2010 : auto resultSet = std::set<int>{};
93 2010 : std::for_each(inputList.begin(), inputList.end(),
94 6076 : [&resultSet](std::string const& version) { resultSet.insert(std::stol(version, nullptr, 10)); });
95 2010 : return resultSet;
96 0 : };
97 :
98 2010 : return to_set(result);
99 2010 : } catch (std::exception const& e) {
100 0 : std::cout << "DBI Exception:" << e.what() << "\n";
101 0 : return {};
102 0 : }
103 :
104 2 : std::set<std::string> DatabaseConfigurationInterface::listConfigurationsTypes() const try {
105 2 : auto ifc = db::ConfigurationInterface{default_dbprovider};
106 :
107 2 : auto collection_name_prefix = std::string{};
108 :
109 4 : return ifc.listCollections(collection_name_prefix);
110 2 : } catch (std::exception const& e) {
111 0 : std::cout << "DBI Exception:" << e.what() << "\n";
112 0 : throw std::runtime_error(e.what());
113 0 : } catch (...) {
114 0 : std::cout << "DBI Unknown exception.\n";
115 0 : throw std::runtime_error("DBI Unknown exception.");
116 0 : }
117 :
118 28 : std::set<std::string> DatabaseConfigurationInterface::findAllGlobalConfigurations(std::string const& search) const try {
119 28 : auto ifc = db::ConfigurationInterface{default_dbprovider};
120 :
121 56 : return ifc.findGlobalConfigurations(search);
122 0 : } catch (std::exception const& e) {
123 0 : std::cout << "DBI Exception:" << e.what() << "\n";
124 0 : throw std::runtime_error(e.what());
125 0 : } catch (...) {
126 0 : std::cout << "DBI Unknown exception.\n";
127 0 : throw std::runtime_error("DBI Unknown exception.");
128 0 : }
129 :
130 56 : config_version_map_t DatabaseConfigurationInterface::loadGlobalConfiguration(std::string const& configuration) const try {
131 56 : auto ifc = db::ConfigurationInterface{default_dbprovider};
132 56 : auto result = ifc.loadGlobalConfiguration(configuration);
133 :
134 54 : auto to_map = [](auto const& inputList) {
135 54 : auto resultMap = config_version_map_t{};
136 :
137 54 : std::for_each(inputList.begin(), inputList.end(),
138 96 : [&resultMap](auto const& info) { resultMap[info.configuration] = std::stol(info.version, 0, 10); });
139 :
140 54 : return resultMap;
141 0 : };
142 :
143 108 : return to_map(result);
144 56 : } catch (std::exception const& e) {
145 2 : std::cout << "DBI Exception:" << e.what() << "\n";
146 2 : throw std::runtime_error(e.what());
147 2 : } catch (...) {
148 0 : std::cout << "DBI Unknown exception.\n";
149 0 : throw std::runtime_error("DBI Unknown exception.");
150 0 : }
151 :
152 238 : void DatabaseConfigurationInterface::storeGlobalConfiguration(config_version_map_t const& configurationMap, std::string const& configuration,
153 : bool allowOverwrite) const try {
154 238 : auto ifc = db::ConfigurationInterface{default_dbprovider};
155 :
156 238 : auto to_list = [](auto const& inputMap) {
157 238 : auto resultList = VersionInfoList_t{};
158 1207 : std::transform(inputMap.begin(), inputMap.end(), std::back_inserter(resultList), [](auto const& mapEntry) {
159 646 : return VersionInfoList_t::value_type{mapEntry.first, std::to_string(mapEntry.second), default_entity};
160 : });
161 :
162 238 : return resultList;
163 0 : };
164 :
165 238 : auto result = ifc.storeGlobalConfiguration(to_list(configurationMap), configuration, allowOverwrite);
166 :
167 238 : if (result.first) {
168 232 : return;
169 : }
170 :
171 6 : throw std::runtime_error(result.second);
172 244 : } catch (std::exception const& e) {
173 6 : std::cout << "DBI Exception:" << e.what() << "\n";
174 6 : throw std::runtime_error(e.what());
175 6 : } catch (...) {
176 0 : std::cout << "DBI Unknown exception.\n";
177 0 : throw std::runtime_error("DBI Unknown exception.");
178 0 : }
179 :
180 2 : void DatabaseConfigurationInterface::storeGlobalConfiguration_mt(const config_version_map_t& configurationMap, const std::string& configuration,
181 : bool allowOverwrite) const try {
182 2 : auto ifc = db::ConfigurationInterface{default_dbprovider};
183 :
184 2 : auto to_list = [](auto const& inputMap) {
185 2 : auto resultList = VersionInfoList_t{};
186 14 : std::transform(inputMap.begin(), inputMap.end(), std::back_inserter(resultList), [](auto const& mapEntry) {
187 8 : return VersionInfoList_t::value_type{mapEntry.first, std::to_string(mapEntry.second), default_entity};
188 : });
189 :
190 2 : return resultList;
191 0 : };
192 :
193 2 : auto result = ifc.storeGlobalConfiguration_mt(to_list(configurationMap), configuration, allowOverwrite);
194 :
195 2 : if (result.first) {
196 2 : return;
197 : }
198 :
199 0 : throw std::runtime_error(result.second);
200 2 : } catch (std::exception const& e) {
201 0 : std::cout << "DBI Exception:" << e.what() << "\n";
202 0 : throw std::runtime_error(e.what());
203 0 : } catch (...) {
204 0 : std::cout << "DBI Unknown exception.\n";
205 0 : throw std::runtime_error("DBI Unknown exception.");
206 0 : }
207 :
208 99 : std::set<std::string> DatabaseConfigurationInterface::findCompositionsContaining(std::string const& configurationType,
209 : std::string const& version) const try {
210 198 : auto ifc = db::ConfigurationInterface{default_dbprovider};
211 :
212 198 : return ifc.findGlobalConfigurationsContaining(configurationType, version);
213 :
214 0 : } catch (std::exception const& e) {
215 0 : std::cout << "DBI Exception:" << e.what() << "\n";
216 0 : throw std::runtime_error(e.what());
217 0 : } catch (...) {
218 0 : std::cout << "DBI Unknown exception.\n";
219 0 : throw std::runtime_error("DBI Unknown exception.");
220 0 : }
|