/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/stable/src/db/include/RAT/DB.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/stable/src/db/include/RAT/DB.hh Source File
Ratpac-two
DB.hh
1 
82 #ifndef __RAT_DB__
83 #define __RAT_DB__
84 
85 #include <RAT/DBFieldCallback.hh>
86 #include <RAT/DBTable.hh>
87 #include <RAT/HTTPDownloader.hh>
88 #include <RAT/Log.hh>
89 #include <deque>
90 #include <limits>
91 #include <list>
92 #include <map>
93 #include <set>
94 #include <smart_ptr.hpp>
95 #include <string>
96 
97 namespace RAT {
98 
99 class DB; // Forward decl to allow for static DB member inside itself
100 class DBTable;
101 class DBLink;
102 
103 class DBTableKey {
104  public:
105  DBTableKey() : name(""), index(""), run(0){};
106  DBTableKey(const std::string &_name, const std::string &_index, const int _run = 0)
107  : name(_name), index(_index), run(_run){};
108 
109  std::string name;
110  std::string index;
111  int run;
112 
113  bool operator<(const DBTableKey &rhs) const {
114  return (name < rhs.name) || (name == rhs.name && index < rhs.index) ||
115  (name == rhs.name && index == rhs.index && run < rhs.run);
116  };
117 
118  bool operator==(const DBTableKey &rhs) const {
119  return (name == rhs.name) && (index == rhs.index) && (run == rhs.run);
120  };
121 };
122 
123 typedef std::map<DBTableKey, simple_ptr_nocopy<DBTable>> DBTableSet;
125 typedef std::map<std::string, DBLinkPtr> DBLinkGroup;
126 
127 class DB : public DBFieldCallback {
128  public:
136  static inline DB *Get() { return primary == 0 ? primary = new DB : primary; };
137 
151  static bool ParseTableName(std::string descriptor, std::string &table, std::string &index);
152 
155  static std::vector<RAT::DBTable *> ReadRATDBFile(const std::string &filename);
156 
157  protected:
159  static DB *primary;
160 
161  public:
163  DB();
164  virtual ~DB();
165 
180  int Load(std::string filename, bool printPath = false);
181 
185  int LoadTable(DBTable *table);
186 
193  int LoadFile(std::string filename);
194 
201  int LoadAll(std::string dirname, std::string pattern = "*.ratdb");
202 
207  int LoadDefaults();
208 
210  void Clear();
211 
213  void SetServer(std::string url);
214 
216  void SetDefaultRun(int _run);
217 
219  int GetDefaultRun() const;
220 
227  DBLinkPtr GetLink(std::string tblname, std::string index = "");
228 
233  DBLinkPtr GetLink(std::string tblname, std::string index, int run);
234 
241  DBLinkGroup GetLinkGroup(std::string tblname);
242 
243  // Manually set fields on the user plane. Also for user use.
244  // DO THIS AFTER loading files from disk, or your changes will be
245  // stomped on later.
246 
253  const DBTableSet &GetAllTables() { return tables; };
254 
256  template <typename T>
257  void Set(const std::string &tblname, const std::string &fieldname, const T &val);
258 
260  template <typename T>
261  void Set(const std::string &tblname, const std::string &index, const std::string &fieldname, const T &val);
262 
264  template <typename T>
265  void SetArrayIndex(const std::string &tblname, const std::string &fieldname, size_t idx, const T &val);
266 
268  template <typename T>
269  void SetArrayIndex(const std::string &tblname, const std::string &index, const std::string &fieldname, size_t idx,
270  const T &val);
271 
274  static constexpr int kUseDefaultRun = std::numeric_limits<int>::min();
275 
283  void DumpContentsToJson(std::ostream &stream, int run = kUseDefaultRun);
284 
285  /************************DBLink interface********************/
286  // This is the low level interface that DBLinks use.
287  // You should not call these methods.
288 
291  DBTable *GetUserTable(std::string tblname, std::string index) { return FindTable(tblname, index, -1); };
292 
295  DBTable *GetRunTable(std::string tblname, std::string index, int runNumber) {
296  return FindTable(tblname, index, runNumber);
297  };
298 
301  DBTable *GetDefaultTable(std::string tblname, std::string index) { return FindTable(tblname, index, 0); };
302 
310  void RemoveLink(DBLink *link);
311 
319  int NumLinks() { return links.size(); };
320 
321  virtual std::vector<int> FetchIArray(const std::string &tableID, const std::string &fieldname);
322  virtual std::vector<double> FetchDArray(const std::string &tableID, const std::string &fieldname);
323 
324  protected:
329  DBTable *FindTable(std::string tblname, std::string index, int runNumber);
330 
337  DBTable *FindOrCreateTable(std::string tblname, std::string index, int runNumber);
338 
351  std::list<RAT::DBLink *> links;
352 
355  std::string server;
356 
359  std::set<std::string> tableNamesOnServer;
360 
364  std::set<RAT::DBTableKey> tablesNotOnServer;
365 
368 
371  DBTableSet tables;
372 
376  std::deque<std::pair<RAT::DBTableKey, bool>> tablesFromServer;
377 
379  int run;
380 };
381 
382 } // namespace RAT
383 
384 // Unfortunately necessary to include this after definition of DB to avoid
385 // circular references
386 #include <RAT/DBLink.hh>
387 
388 template <typename T>
389 void RAT::DB::Set(const std::string &tblname, const std::string &fieldname, const T &val) {
390  Set(tblname, "", fieldname, val);
391 }
392 
394 template <typename T>
395 void RAT::DB::Set(const std::string &tblname, const std::string &index, const std::string &fieldname, const T &val) {
396  DBTable *t = FindOrCreateTable(tblname, index, -1);
397  t->Set(fieldname, val);
398 }
399 
401 template <typename T>
402 void RAT::DB::SetArrayIndex(const std::string &tblname, const std::string &fieldname, size_t idx, const T &val) {
403  SetArrayIndex(tblname, "", fieldname, idx, val);
404 }
405 
407 template <typename T>
408 void RAT::DB::SetArrayIndex(const std::string &tblname, const std::string &index, const std::string &fieldname,
409  size_t idx, const T &val) {
410  DBTable *t = FindOrCreateTable(tblname, index, -1);
411  DBLinkPtr p = GetLink(tblname,
412  index); // This ensures we always grab either the previously set
413  // or default plane array without keeping track explicitly
414  json::Value jval = p->Get<json::Value>(fieldname);
415  Log::Assert(jval.getType() == json::TARRAY, "RATDB: Cannot set an index for an item that is not an array!");
416  jval[index] = val;
417  t->Set(fieldname, jval);
418 }
419 
420 #endif
Definition: DBFieldCallback.hh:9
Definition: DB.hh:103
Definition: DBTable.hh:25
void Set(const std::string &name, const T &value)
Definition: DBTable.hh:196
Definition: DB.hh:127
void SetServer(std::string url)
Definition: DB.cc:168
DBTable * FindTable(std::string tblname, std::string index, int runNumber)
Definition: DB.cc:301
DB()
Definition: DB.cc:23
int LoadAll(std::string dirname, std::string pattern="*.ratdb")
Definition: DB.cc:137
std::set< std::string > tableNamesOnServer
Definition: DB.hh:359
int Load(std::string filename, bool printPath=false)
Definition: DB.cc:37
static DB * primary
Definition: DB.hh:159
DBLinkPtr GetLink(std::string tblname, std::string index="")
Definition: DB.cc:200
void SetDefaultRun(int _run)
Definition: DB.cc:196
DBLinkGroup GetLinkGroup(std::string tblname)
Definition: DB.cc:212
std::deque< std::pair< RAT::DBTableKey, bool > > tablesFromServer
Definition: DB.hh:376
std::string server
Definition: DB.hh:355
const DBTableSet & GetAllTables()
Definition: DB.hh:253
std::list< RAT::DBLink * > links
Definition: DB.hh:351
static bool ParseTableName(std::string descriptor, std::string &table, std::string &index)
Definition: DB.cc:427
int LoadDefaults()
Definition: DB.cc:161
DBTable * GetUserTable(std::string tblname, std::string index)
Definition: DB.hh:291
DBTableSet tables
Definition: DB.hh:371
void DumpContentsToJson(std::ostream &stream, int run=kUseDefaultRun)
Definition: DB.cc:469
static DB * Get()
Definition: DB.hh:136
HTTPDownloader downloader
Definition: DB.hh:367
static std::vector< RAT::DBTable * > ReadRATDBFile(const std::string &filename)
Definition: DB.cc:461
std::set< RAT::DBTableKey > tablesNotOnServer
Definition: DB.hh:364
void Set(const std::string &tblname, const std::string &fieldname, const T &val)
Definition: DB.hh:389
int NumLinks()
Definition: DB.hh:319
int LoadTable(DBTable *table)
Definition: DB.cc:65
int run
Definition: DB.hh:379
void Clear()
Definition: DB.cc:166
static constexpr int kUseDefaultRun
Definition: DB.hh:274
void RemoveLink(DBLink *link)
Definition: DB.cc:425
DBTable * GetRunTable(std::string tblname, std::string index, int runNumber)
Definition: DB.hh:295
DBTable * FindOrCreateTable(std::string tblname, std::string index, int runNumber)
Definition: DB.cc:411
DBTable * GetDefaultTable(std::string tblname, std::string index)
Definition: DB.hh:301
void SetArrayIndex(const std::string &tblname, const std::string &fieldname, size_t idx, const T &val)
Definition: DB.hh:402
int LoadFile(std::string filename)
Definition: DB.cc:108
int GetDefaultRun() const
Definition: DB.cc:198
Definition: HTTPDownloader.hh:21
static void Assert(bool condition, std::string message, int return_code=1)
Definition: Log.cc:95
Definition: json.hh:59
Definition: CCCrossSecMessenger.hh:29