otsdaq  v2_05_02_indev
ConfigurationManagerRW.h
1 #ifndef _ots_ConfigurationManagerRW_h_
2 #define _ots_ConfigurationManagerRW_h_
3 
4 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
5 
6 namespace ots
7 {
8 struct TableInfo
9 {
10  TableInfo()
11  : // constructor
12  tablePtr_(0)
13  {
14  }
15 
16  std::set<TableVersion> versions_;
17  TableBase* tablePtr_;
18 };
19 
20 struct GroupInfo
21 {
22  std::set<TableGroupKey> keys_;
23  std::string latestKeyGroupAuthor_, latestKeyGroupComment_, latestKeyGroupCreationTime_, latestKeyGroupTypeString_;
24  std::map<std::string /*name*/, TableVersion /*version*/> latestKeyMemberMap_;
25 
26  TableGroupKey getLatestKey() { return *(keys_.rbegin()); }
27 };
28 
29 #define Q(X) #X
30 #define QUOTE(X) Q(X)
31 #define __GETCFG_RW__(X) getConfigurationRW<X>(QUOTE(X))
32 
33 //==============================================================================
34 // ConfigurationManagerRW
35 // This is the ConfigurationManger with write access
36 // This class inherits all public function from ConfigurationManager
37 // and is a "Friend" class of ConfigurationManager so has access to private members.
39 {
40  // clang-format off
41  public:
42  ConfigurationManagerRW(const std::string& username);
43 
44  //==============================================================================
45  // Getters
46  const std::string& getUsername (void) const { return username_; }
47  ConfigurationInterface* getConfigurationInterface (void) const { return theInterface_; }
48 
49  const std::map<std::string, TableInfo>& getAllTableInfo (bool refresh = false, std::string* accumulatedWarnings = 0, const std::string& errorFilterName = "");
50  std::map<std::string /*tableName*/,
51  std::map<std::string /*aliasName*/,
52  TableVersion /*version*/> > getVersionAliases (void) const;
53 
54  template<class T>
55  T* getConfigurationRW (std::string name) { return (T*)getTableByName(name); }
56  TableBase* getVersionedTableByName (const std::string& tableName, TableVersion version, bool looseColumnMatching = false, std::string* accumulatedErrors = 0);
57  TableBase* getTableByName (const std::string& tableName);
58  TableGroupKey findTableGroup (const std::string& groupName, const std::map<std::string, TableVersion>& groupMembers,
59  const std::map<std::string /*name*/, std::string /*alias*/>& groupAliases = std::map<std::string /*name*/, std::string /*alias*/>());
60  TableBase* getMetadataTable (void) { return &groupMetadataTable_; /* created for use in otsdaq_flatten_system_aliases, e.g. */ }
61 
62  //==============================================================================
63  // modifiers of generic TableBase
64  TableVersion saveNewTable (const std::string& tableName, TableVersion temporaryVersion = TableVersion(), bool makeTemporary = false); //, bool saveToScratchVersion = false);
65  TableVersion saveModifiedVersion (
66  const std::string& tableName,
67  TableVersion originalVersion,
68  bool makeTemporary,
69  TableBase* config,
70  TableVersion temporaryModifiedVersion,
71  bool ignoreDuplicates = false,
72  bool lookForEquivalent = false,
73  bool* foundEquivalent = nullptr);
74 
75  TableVersion copyViewToCurrentColumns (const std::string& tableName, TableVersion sourceVersion);
76  void eraseTemporaryVersion (const std::string& tableName, TableVersion targetVersion = TableVersion());
77  void clearCachedVersions (const std::string& tableName);
78  void clearAllCachedVersions (void);
79 
80  //==============================================================================
81  // modifiers of table groups
82  void activateTableGroup (const std::string& tableGroupName, TableGroupKey tableGroupKey, std::string* accumulatedTreeErrors = 0);
83 
84  TableVersion createTemporaryBackboneView (TableVersion sourceViewVersion = TableVersion()); //-1, from MockUp, else from valid backbone view version
85  TableVersion saveNewBackbone (TableVersion temporaryVersion = TableVersion());
86 
87  //==============================================================================
88  // modifiers of a table group based on alias, e.g. "Physics"
89  TableGroupKey saveNewTableGroup (const std::string& groupName, std::map<std::string, TableVersion>& groupMembers,
90  const std::string& groupComment = TableViewColumnInfo::DATATYPE_COMMENT_DEFAULT,
91  std::map<std::string /*table*/, std::string /*alias*/>* groupAliases = 0);
92 
93  //==============================================================================
94  // public group cache handling
95  const GroupInfo& getGroupInfo (const std::string& groupName);
96  const std::map<std::string, GroupInfo>& getAllGroupInfo (void) { return allGroupInfo_; }
97 
98  void testXDAQContext (void); // for debugging
99 
100  private:
101  //==============================================================================
102  // group cache handling
103  void cacheGroupKey (const std::string& groupName, TableGroupKey key);
104 
105  //==============================================================================
106  // private members
107  std::map<std::string, TableInfo> allTableInfo_;
108  std::map<std::string, GroupInfo> allGroupInfo_;
109 };
110 
111 //==============================================================================
112 // TableEditStruct public class
113 //
115 {
116  // everything needed for editing a table
117  TableBase* table_;
118  TableView* tableView_;
119  TableVersion temporaryVersion_, originalVersion_;
120  bool createdTemporaryVersion_; // indicates if temp version was created here
121  bool modified_; // indicates if temp version was modified
122  std::string tableName_;
125  {
126  __SS__ << "impossible!" << std::endl;
127  ss << StringMacros::stackTrace();
128  __SS_THROW__;
129  }
130  TableEditStruct(const std::string& tableName, ConfigurationManagerRW* cfgMgr, bool markModified = false)
131  : createdTemporaryVersion_(false), modified_(markModified), tableName_(tableName)
132  {
133  //__COUT__ << "Creating Table-Edit Struct for " << tableName_ << std::endl;
134  table_ = cfgMgr->getTableByName(tableName_);
135 
136  //if no active version or if not temporary, setup new temporary version
137  if(!table_->isActive() ||
138  !(originalVersion_ = table_->getView().getVersion()).isTemporaryVersion())
139  {
140  //__COUT__ << "Original '" << tableName_ << "' version is v" << originalVersion_ << std::endl;
141 
142  // create temporary version for editing
143  temporaryVersion_ = table_->createTemporaryView(originalVersion_);
144  cfgMgr->saveNewTable(
145  tableName_,
146  temporaryVersion_,
147  true); // proper bookkeeping for temporary version with the new version
148 
149  __COUT__ << "Created '" << tableName_ << "' temporary version " << temporaryVersion_ << std::endl;
150  createdTemporaryVersion_ = true;
151  }
152  //else // else table is already temporary version
153  //__COUT__ << "Using '" << tableName_ << "' temporary version " << temporaryVersion_ << std::endl;
154 
155  tableView_ = table_->getViewP();
156  }
157 }; // end TableEditStruct declaration
158 
159 
160 //==============================================================================
161 // GroupEditStruct public class
162 //
164 {
165  // everything needed for editing a group and its tables
166 private:
167  std::map<std::string, TableVersion> groupMembers_;
168  std::map<std::string, TableEditStruct> groupTables_;
169 public:
170  const ConfigurationManager::GroupType groupType_;
171  const std::string originalGroupName_;
172  const TableGroupKey originalGroupKey_;
173 private:
174  ConfigurationManagerRW* cfgMgr_;
175 public:
178  : groupType_(ConfigurationManager::GroupType::CONFIGURATION_TYPE) {__SS__ << "impossible!" << __E__; __SS_THROW__;}
179  GroupEditStruct(const ConfigurationManager::GroupType& groupType, ConfigurationManagerRW* cfgMgr);
180 
181  ~GroupEditStruct();
182 
183  void dropChanges (void);
184  void saveChanges (
185  const std::string& groupNameToSave,
186  TableGroupKey& newGroupKey,
187  bool* foundEquivalentGroupKey = nullptr,
188  bool activateNewGroup = false,
189  bool updateGroupAliases = false,
190  bool updateTableAliases = false,
191  TableGroupKey* newBackboneKey = nullptr,
192  bool* foundEquivalentBackboneKey = nullptr,
193  std::string* accumulatedWarnings = nullptr);
194 
195  TableEditStruct& getTableEditStruct (const std::string& tableName, bool markModified = false);
196 
197 }; // end GroupEditStruct declaration
198 
199 // clang-format on
200 } // namespace ots
201 
202 #endif