otsdaq  v2_05_02_indev
TableView.h
1 #ifndef _ots_TableView_h_
2 #define _ots_TableView_h_
3 
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/Macros/StringMacros.h"
6 #include "otsdaq/MessageFacility/MessageFacility.h"
7 
8 #include <stdlib.h>
9 #include <time.h> /* time_t, time, ctime */
10 #include <cassert>
11 #include <iostream>
12 #include <set>
13 #include <vector>
14 #include "otsdaq/TableCore/TableVersion.h"
15 #include "otsdaq/TableCore/TableViewColumnInfo.h"
16 
17 namespace ots
18 {
19 // clang-format off
20 class TableView
21 {
22  public:
23  static const unsigned int INVALID;
24  typedef std::vector<std::vector<std::string> > DataView;
25  typedef DataView::iterator iterator;
26  typedef DataView::const_iterator const_iterator;
27 
28  TableView (const std::string& tableName);// = "");
29  virtual ~TableView (void);
30 
31  void init (void);
32  TableView& copy (const TableView& src,
33  TableVersion destinationVersion,
34  const std::string& author);
35  unsigned int copyRows (const std::string& author,
36  const TableView& src,
37  unsigned int srcOffsetRow = 0,
38  unsigned int srcRowsToCopy = (unsigned int)-1,
39  unsigned int destOffsetRow = (unsigned int)-1,
40  unsigned char generateUniqueDataColumns = false, // leave as unsigned char rather than
41  // bool, too many things (e.g. strings)
42  // evaluate successfully to bool values
43  const std::string& baseNameAutoUID = ""
44  );
45 
46  template<class T> // in included .icc source
47  unsigned int findRow (unsigned int col,
48  const T& value,
49  unsigned int offsetRow = 0,
50  bool doNotThrow = false) const;
51  unsigned int findRow (unsigned int col,
52  const std::string& value,
53  unsigned int offsetRow = 0,
54  bool doNotThrow = false) const;
55 
56  template<class T> // in included .icc source
57  unsigned int findRowInGroup (unsigned int col,
58  const T& value,
59  const std::string& groupId,
60  const std::string& childLinkIndex,
61  unsigned int offsetRow = 0) const;
62  unsigned int findRowInGroup (unsigned int col,
63  const std::string& value,
64  const std::string& groupId,
65  const std::string& childLinkIndex,
66  unsigned int offsetRow = 0) const;
67  unsigned int findCol (const std::string& name) const;
68  unsigned int findColByType (const std::string& type, int startingCol = 0) const;
69 
70  // Getters
71  const std::string& getUniqueStorageIdentifier (void) const;
72  const std::string& getTableName (void) const;
73  const TableVersion& getVersion (void) const;
74  const std::string& getComment (void) const;
75  const std::string& getAuthor (void) const;
76  const time_t& getCreationTime (void) const;
77  const time_t& getLastAccessTime (void) const;
78  const bool& getLooseColumnMatching (void) const;
79  unsigned int getDataColumnSize (void) const;
80  const unsigned int& getSourceColumnMismatch (void) const;
81  const unsigned int& getSourceColumnMissing (void) const;
82  const std::set<std::string /*col name*/>& getSourceColumnNames (void) const;
83  std::set<std::string /*col name*/> getColumnNames (void) const;
84  std::map<std::string, unsigned int /*col*/> getColumnNamesMap (void) const;
85  std::set<std::string /*storage name*/> getColumnStorageNames (void) const;
86  std::vector<std::string /*default value*/> getDefaultRowValues (void) const;
87 
88  unsigned int getNumberOfRows (void) const;
89  unsigned int getNumberOfColumns (void) const;
90  unsigned int getColUID (void) const;
91  unsigned int getColStatus (void) const;
92  unsigned int getColPriority (void) const;
93 
94  // Note: Group link handling should be done in this TableView class
95  // only by using isEntryInGroup ...
96  // This is so that multiple group handling is consistent
97  private:
98  bool isEntryInGroupCol (const unsigned int& row,
99  const unsigned int& groupCol,
100  const std::string& groupNeedle,
101  std::set<std::string>* groupIDList = 0) const;
102 
103  public:
104 
105  std::set<std::string /*groupID*/> getSetOfGroupIDs (const std::string& childLinkIndex, unsigned int row = -1) const;
106  std::set<std::string /*groupID*/> getSetOfGroupIDs (const unsigned int& col, unsigned int row = -1) const;
107  bool isEntryInGroup (const unsigned int& row,
108  const std::string& childLinkIndex,
109  const std::string& groupNeedle) const;
110  bool getChildLink (const unsigned int& col,
111  bool& isGroup,
112  std::pair<unsigned int /*link col*/,
113  unsigned int /*link id col*/>& linkPair) const;
114  unsigned int getLinkGroupIDColumn (const std::string& childLinkIndex) const;
115  std::vector<unsigned int /*row*/> getGroupRows (const unsigned int groupIdCol, const std::string& groupID, bool onlyStatusTrue = false, bool orderedByPriority = false) const;
116  std::vector<std::vector<unsigned int /*row*/>> getGroupRowsByPriority (const unsigned int groupIdCol, const std::string& groupID, bool onlyStatusTrue = false) const;
117 private:
118  std::vector<std::vector<unsigned int /*row*/>> getGroupRowsInVectors (const unsigned int groupIdCol, const std::string& groupID, bool onlyStatusTrue, bool orderedByPriority) const;
119 public:
120  void addRowToGroup (const unsigned int& row,
121  const unsigned int& col,
122  const std::string& groupID); //, const std::string& colDefault);
123  bool removeRowFromGroup (const unsigned int& row,
124  const unsigned int& col,
125  const std::string& groupID,
126  bool deleteRowIfNoGroupLeft = false);
127 
128  template<class T> // in included .icc source
129  void getValue (T& value,
130  unsigned int row,
131  unsigned int col,
132  bool doConvertEnvironmentVariables = true) const;
133  // special version of getValue for string type
134  // Note: necessary because types of std::basic_string<char> cause compiler problems
135  // if no string specific function
136  void getValue (std::string& value,
137  unsigned int row,
138  unsigned int col,
139  bool doConvertEnvironmentVariables = true) const;
140 
141  template<class T> // in included .icc source
142  T validateValueForColumn (const std::string& value,
143  unsigned int col,
144  bool doConvertEnvironmentVariables = true) const;
145  // special version of getValue for string type
146  // Note: necessary because types of std::basic_string<char> cause compiler problems
147  // if no string specific function
148  std::string validateValueForColumn (const std::string& value,
149  unsigned int col,
150  bool convertEnvironmentVariables = true) const;
151  std::string getValueAsString (unsigned int row,
152  unsigned int col,
153  bool convertEnvironmentVariables = true) const;
154  std::string getEscapedValueAsString (unsigned int row,
155  unsigned int col,
156  bool convertEnvironmentVariables = true) const;
157  bool isURIEncodedCommentTheSame (const std::string& comment) const;
158 
159  const DataView& getDataView (void) const;
160  const std::vector<TableViewColumnInfo>& getColumnsInfo (void) const;
161  std::vector<TableViewColumnInfo>* getColumnsInfoP (void);
162  const TableViewColumnInfo& getColumnInfo (unsigned int column) const;
163 
164  // Setters
165 
166  void setUniqueStorageIdentifier (const std::string& storageUID);
167  //void setTableName (const std::string& name);
168  void setComment (const std::string& comment);
169  void setURIEncodedComment (const std::string& uriComment);
170  void setAuthor (const std::string& author);
171  void setCreationTime (time_t t);
172  void setLastAccessTime (time_t t = time(0));
173  void setLooseColumnMatching (bool setValue);
174 
175  template<class T> // in included .icc source
176  void setVersion (const T& version);
177  template<class T> // in included .icc source
178  void setValue (const T& value, unsigned int row, unsigned int col);
179  void setValue (const std::string& value, unsigned int row, unsigned int col);
180  void setValue (const char* value, unsigned int row, unsigned int col);
181 
182  // Careful: The setValueAsString method is used to set the value without any
183  // consistency check with the data type
184  void setValueAsString (const std::string& value, unsigned int row, unsigned int col);
185  const std::string& setUniqueColumnValue (unsigned int row, unsigned int col, std::string baseValueAsString = "", bool doMathAppendStrategy = false);
186 
187  void resizeDataView (unsigned int nRows, unsigned int nCols);
188  unsigned int addRow (const std::string& author = "",
189  unsigned char incrementUniqueData = false, // leave as unsigned char rather than
190  // bool, too many things (e.g. strings)
191  // evaluate successfully to bool values
192  const std::string& baseNameAutoUID = "",
193  unsigned int rowToAdd =
194  (unsigned int)-1); // returns index of added row, default is last row
195  void deleteRow (int r);
196  void deleteAllRows (void) {theDataView_.clear();}
197 
198 
199  // Lore did not like this.. wants special access through separate Supervisor for
200  // "Database Management" int addColumn(std::string name, std::string viewName,
201  // std::string viewType); //returns index of added column, always is last column
202  // unless
203 
204  iterator begin (void) { return theDataView_.begin(); }
205  iterator end (void) { return theDataView_.end(); }
206  const_iterator begin (void) const { return theDataView_.begin(); }
207  const_iterator end (void) const { return theDataView_.end(); }
208  void reset (void);
209  void print (std::ostream& out = std::cout) const;
210  void printJSON (std::ostream& out = std::cout) const;
211  int fillFromJSON (const std::string& json);
212  int fillFromCSV (const std::string& data,
213  const int& dataOffset = 0,
214  const std::string& author = "");
215  bool setURIEncodedValue (const std::string& value,
216  const unsigned int& row,
217  const unsigned int& col,
218  const std::string& author = "");
219 
220  private:
221  unsigned int initColUID (void);
222  unsigned int initColStatus (void);
223  unsigned int initColPriority (void);
224 
225  TableView& operator= (const TableView src); // operator= is purposely undefined and
226  // private (DO NOT USE IT!) - should use
227  // TableView::copy()
228 
229  std::string uniqueStorageIdentifier_; // starts empty "", used to implement re-writable views ("temporary views") in artdaq db
230  const std::string tableName_; // View name (extensionTableName in xml)
231  TableVersion version_; // Table version
232  std::string comment_; // Table version comment
233  std::string author_;
234  time_t creationTime_; // used more like "construction"(constructor) time
235  time_t lastAccessTime_; // last time the ConfigurationInterface:get() retrieved this view
236 
237  unsigned int colUID_, colStatus_, colPriority_; // special column pointers
238  std::map<std::string, unsigned int> colLinkGroupIDs_; // map from child link index to column
239 
240  bool fillWithLooseColumnMatching_;
241  unsigned int sourceColumnMismatchCount_, sourceColumnMissingCount_;
242  std::set<std::string> sourceColumnNames_;
243 
244  std::vector<TableViewColumnInfo> columnsInfo_;
245  DataView theDataView_;
246 };
247 
248 #include "otsdaq/TableCore/TableView.icc" //define template functions
249 
250 // clang-format on
251 } // namespace ots
252 
253 #endif