1 #include "otsdaq/ConfigurationInterface/DatabaseConfigurationInterface.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
10 #include "artdaq-database/BasicTypes/basictypes.h"
11 #include "artdaq-database/ConfigurationDB/configurationdbifc.h"
12 #include "otsdaq/TableCore/TableBase.h"
14 #include "artdaq-database/ConfigurationDB/configuration_common.h"
15 #include "artdaq-database/ConfigurationDB/dispatch_common.h"
16 #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb.h"
17 #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h"
21 using artdaq::database::basictypes::FhiclData;
22 using artdaq::database::basictypes::JsonData;
25 using config_version_map_t = ots::DatabaseConfigurationInterface::config_version_map_t;
27 namespace db = artdaq::database::configuration;
28 using VersionInfoList_t = db::ConfigurationInterface::VersionInfoList_t;
30 constexpr
auto default_dbprovider =
"filesystem";
31 constexpr
auto default_entity =
"OTSROOT";
34 DatabaseConfigurationInterface::DatabaseConfigurationInterface()
40 artdaq::database::configuration::debug::ExportImport();
41 artdaq::database::configuration::debug::ManageAliases();
42 artdaq::database::configuration::debug::ManageConfigs();
43 artdaq::database::configuration::debug::ManageDocuments();
44 artdaq::database::configuration::debug::Metadata();
46 artdaq::database::configuration::debug::detail::ExportImport();
47 artdaq::database::configuration::debug::detail::ManageAliases();
48 artdaq::database::configuration::debug::detail::ManageConfigs();
49 artdaq::database::configuration::debug::detail::ManageDocuments();
50 artdaq::database::configuration::debug::detail::Metadata();
52 artdaq::database::configuration::debug::options::OperationBase();
53 artdaq::database::configuration::debug::options::BulkOperations();
54 artdaq::database::configuration::debug::options::ManageDocuments();
55 artdaq::database::configuration::debug::options::ManageConfigs();
56 artdaq::database::configuration::debug::options::ManageAliases();
58 artdaq::database::configuration::debug::MongoDB();
59 artdaq::database::configuration::debug::UconDB();
60 artdaq::database::configuration::debug::FileSystemDB();
62 artdaq::database::filesystem::index::debug::enable();
64 artdaq::database::filesystem::debug::enable();
65 artdaq::database::mongo::debug::enable();
67 artdaq::database::docrecord::debug::JSONDocumentBuilder();
68 artdaq::database::docrecord::debug::JSONDocument();
82 auto ifc = db::ConfigurationInterface{default_dbprovider};
84 auto versionstring = version.toString();
89 auto result = ifc.template loadVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
94 configuration->getViewP()->setVersion(version);
97 __SS__ <<
"\n\nDBI Error while filling '" << configuration->getTableName() <<
"' version '" << versionstring <<
"' - are you sure this version exists?\n"
98 <<
"Here is the error:\n\n"
99 << result.second << __E__;
105 void DatabaseConfigurationInterface::saveActiveVersion(
const TableBase* configuration,
bool overwrite)
const
108 auto ifc = db::ConfigurationInterface{default_dbprovider};
112 auto versionstring = configuration->getView().getVersion().toString();
118 auto result = overwrite ? ifc.template overwriteVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity)
119 : ifc.template storeVersion<decltype(configuration), JsonData>(configuration, versionstring, default_entity);
124 __SS__ <<
"DBI Error:" << result.second << __E__;
130 TableVersion DatabaseConfigurationInterface::findLatestVersion(
const TableBase* table)
const noexcept
132 auto versions = getVersions(table);
134 __COUT__ <<
"Table Name: " << table->getTableName() << __E__;
135 __SS__ <<
"All Versions: ";
136 for(
auto& v : versions)
139 __COUT__ << ss.str();
144 return *(versions.rbegin());
149 std::set<TableVersion> DatabaseConfigurationInterface::getVersions(
const TableBase* table)
const noexcept
try
151 auto ifc = db::ConfigurationInterface{default_dbprovider};
152 auto result = ifc.template getVersions<decltype(table)>(table, default_entity);
154 auto resultSet = std::set<TableVersion>{};
155 for(std::string
const& version : result)
156 resultSet.insert(
TableVersion(std::stol(version, 0, 10)));
173 catch(std::exception
const& e)
175 __COUT__ <<
"DBI Exception:" << e.what() <<
"\n";
181 std::set<std::string > DatabaseConfigurationInterface::getAllTableNames() const try
183 auto ifc = db::ConfigurationInterface{default_dbprovider};
185 auto collection_name_prefix = std::string{};
187 return ifc.listCollections(collection_name_prefix);
189 catch(std::exception
const& e)
191 __SS__ <<
"DBI Exception:" << e.what() <<
"\n";
196 __SS__ <<
"DBI Unknown exception.\n";
202 std::set<std::string > DatabaseConfigurationInterface::getAllTableGroupNames(std::string
const& filterString)
const try
204 auto ifc = db::ConfigurationInterface{default_dbprovider};
206 if(filterString ==
"")
207 return ifc.findGlobalConfigurations(
"*");
211 return ifc.findGlobalConfigurations(filterString +
"*");
217 catch(std::exception
const& e)
219 __SS__ <<
"Filter string '" << filterString <<
"' yielded DBI Exception:" << e.what() <<
"\n";
224 __SS__ <<
"Filter string '" << filterString <<
"' yielded DBI Unknown exception.\n";
231 TableGroupKey DatabaseConfigurationInterface::findLatestGroupKey(
const std::string& groupName)
const noexcept
233 std::set<TableGroupKey> keys = DatabaseConfigurationInterface::getKeys(groupName);
235 return *(keys.crbegin());
243 std::set<
TableGroupKey > DatabaseConfigurationInterface::getKeys(
const std::string& groupName)
const
245 std::set<TableGroupKey> retSet;
246 std::set<std::string > names = getAllTableGroupNames();
248 if(n.find(groupName) == 0)
255 config_version_map_t DatabaseConfigurationInterface::getTableGroupMembers(std::string
const& tableGroup,
bool includeMetaDataTable)
const try
257 auto ifc = db::ConfigurationInterface{default_dbprovider};
258 auto result = ifc.loadGlobalConfiguration(tableGroup);
264 auto to_map = [](
auto const& inputList,
bool includeMetaDataTable) {
265 auto resultMap = config_version_map_t{};
267 std::for_each(inputList.begin(), inputList.end(), [&resultMap](
auto const& info) { resultMap[info.configuration] = std::stol(info.version, 0, 10); });
269 if(!includeMetaDataTable)
272 auto metaTable = resultMap.find(GROUP_METADATA_TABLE_NAME);
273 if(metaTable != resultMap.end())
274 resultMap.erase(metaTable);
279 return to_map(result, includeMetaDataTable);
281 catch(std::exception
const& e)
283 __SS__ <<
"DBI Exception getting Group's member tables for '" << tableGroup <<
"':\n\n" << e.what() <<
"\n";
284 __COUT_ERR__ << ss.str();
289 __SS__ <<
"DBI Unknown exception getting Group's member tables for '" << tableGroup <<
".'\n";
290 __COUT_ERR__ << ss.str();
296 void DatabaseConfigurationInterface::saveTableGroup(config_version_map_t
const& configurationMap, std::string
const& configurationGroup)
const try
298 auto ifc = db::ConfigurationInterface{default_dbprovider};
300 auto to_list = [](
auto const& inputMap) {
301 auto resultList = VersionInfoList_t{};
302 std::transform(inputMap.begin(), inputMap.end(), std::back_inserter(resultList), [](
auto const& mapEntry) {
303 return VersionInfoList_t::value_type{mapEntry.first, mapEntry.second.toString(), default_entity};
309 auto result = ifc.storeGlobalConfiguration(to_list(configurationMap), configurationGroup);
314 __THROW__(result.second);
316 catch(std::exception
const& e)
318 __SS__ <<
"DBI Exception:" << e.what() <<
"\n";
319 __COUT_ERR__ << ss.str();
324 __SS__ <<
"DBI Unknown exception.\n";
325 __COUT_ERR__ << ss.str();