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

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/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 <list>
91 #include <map>
92 #include <set>
93 #include <smart_ptr.hpp>
94 #include <string>
95 
96 namespace RAT {
97 
98 class DB; // Forward decl to allow for static DB member inside itself
99 class DBTable;
100 class DBLink;
101 
102 class DBTableKey {
103  public:
104  DBTableKey() : name(""), index(""), run(0){};
105  DBTableKey(const std::string &_name, const std::string &_index, const int _run = 0)
106  : name(_name), index(_index), run(_run){};
107 
108  std::string name;
109  std::string index;
110  int run;
111 
112  bool operator<(const DBTableKey &rhs) const {
113  return (name < rhs.name) || (name == rhs.name && index < rhs.index) ||
114  (name == rhs.name && index == rhs.index && run < rhs.run);
115  };
116 
117  bool operator==(const DBTableKey &rhs) const {
118  return (name == rhs.name) && (index == rhs.index) && (run == rhs.run);
119  };
120 };
121 
122 typedef std::map<DBTableKey, simple_ptr_nocopy<DBTable>> DBTableSet;
124 typedef std::map<std::string, DBLinkPtr> DBLinkGroup;
125 
126 class DB : public DBFieldCallback {
127  public:
135  static inline DB *Get() { return primary == 0 ? primary = new DB : primary; };
136 
150  static bool ParseTableName(std::string descriptor, std::string &table, std::string &index);
151 
154  static std::vector<RAT::DBTable *> ReadRATDBFile(const std::string &filename);
155 
156  protected:
158  static DB *primary;
159 
160  public:
162  DB();
163  virtual ~DB();
164 
179  int Load(std::string filename, bool printPath = false);
180 
184  int LoadTable(DBTable *table);
185 
192  int LoadFile(std::string filename);
193 
200  int LoadAll(std::string dirname, std::string pattern = "*.ratdb");
201 
206  int LoadDefaults();
207 
209  void Clear();
210 
212  void SetServer(std::string url);
213 
215  void SetDefaultRun(int _run);
216 
218  int GetDefaultRun() const;
219 
226  DBLinkPtr GetLink(std::string tblname, std::string index = "");
227 
232  DBLinkPtr GetLink(std::string tblname, std::string index, int run);
233 
240  DBLinkGroup GetLinkGroup(std::string tblname);
241 
242  // Manually set fields on the user plane. Also for user use.
243  // DO THIS AFTER loading files from disk, or your changes will be
244  // stomped on later.
245 
252  const DBTableSet &GetAllTables() { return tables; };
253 
255  template <typename T>
256  void Set(const std::string &tblname, const std::string &fieldname, const T &val);
257 
259  template <typename T>
260  void Set(const std::string &tblname, const std::string &index, const std::string &fieldname, const T &val);
261 
263  template <typename T>
264  void SetArrayIndex(const std::string &tblname, const std::string &fieldname, size_t idx, const T &val);
265 
267  template <typename T>
268  void SetArrayIndex(const std::string &tblname, const std::string &index, const std::string &fieldname, size_t idx,
269  const T &val);
270 
274  void DumpContentsToJson(std::ostream &stream);
275 
276  /************************DBLink interface********************/
277  // This is the low level interface that DBLinks use.
278  // You should not call these methods.
279 
282  DBTable *GetUserTable(std::string tblname, std::string index) { return FindTable(tblname, index, -1); };
283 
286  DBTable *GetRunTable(std::string tblname, std::string index, int runNumber) {
287  return FindTable(tblname, index, runNumber);
288  };
289 
292  DBTable *GetDefaultTable(std::string tblname, std::string index) { return FindTable(tblname, index, 0); };
293 
301  void RemoveLink(DBLink *link);
302 
310  int NumLinks() { return links.size(); };
311 
312  virtual std::vector<int> FetchIArray(const std::string &tableID, const std::string &fieldname);
313  virtual std::vector<double> FetchDArray(const std::string &tableID, const std::string &fieldname);
314 
315  protected:
320  DBTable *FindTable(std::string tblname, std::string index, int runNumber);
321 
328  DBTable *FindOrCreateTable(std::string tblname, std::string index, int runNumber);
329 
342  std::list<RAT::DBLink *> links;
343 
346  std::string server;
347 
350  std::set<std::string> tableNamesOnServer;
351 
355  std::set<RAT::DBTableKey> tablesNotOnServer;
356 
359 
362  DBTableSet tables;
363 
367  std::deque<std::pair<RAT::DBTableKey, bool>> tablesFromServer;
368 
370  int run;
371 };
372 
373 } // namespace RAT
374 
375 // Unfortunately necessary to include this after definition of DB to avoid
376 // circular references
377 #include <RAT/DBLink.hh>
378 
379 template <typename T>
380 void RAT::DB::Set(const std::string &tblname, const std::string &fieldname, const T &val) {
381  Set(tblname, "", fieldname, val);
382 }
383 
385 template <typename T>
386 void RAT::DB::Set(const std::string &tblname, const std::string &index, const std::string &fieldname, const T &val) {
387  DBTable *t = FindOrCreateTable(tblname, index, -1);
388  t->Set(fieldname, val);
389 }
390 
392 template <typename T>
393 void RAT::DB::SetArrayIndex(const std::string &tblname, const std::string &fieldname, size_t idx, const T &val) {
394  SetArrayIndex(tblname, "", fieldname, idx, val);
395 }
396 
398 template <typename T>
399 void RAT::DB::SetArrayIndex(const std::string &tblname, const std::string &index, const std::string &fieldname,
400  size_t idx, const T &val) {
401  DBTable *t = FindOrCreateTable(tblname, index, -1);
402  DBLinkPtr p = GetLink(tblname,
403  index); // This ensures we always grab either the previously set
404  // or default plane array without keeping track explicitly
405  json::Value jval = p->Get<json::Value>(fieldname);
406  Log::Assert(jval.getType() == json::TARRAY, "RATDB: Cannot set an index for an item that is not an array!");
407  jval[index] = val;
408  t->Set(fieldname, jval);
409 }
410 
411 #endif
Definition: DBFieldCallback.hh:9
Definition: DB.hh:102
Definition: DBTable.hh:25
void Set(const std::string &name, const T &value)
Definition: DBTable.hh:196
Definition: DB.hh:126
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:350
int Load(std::string filename, bool printPath=false)
Definition: DB.cc:37
static DB * primary
Definition: DB.hh:158
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:367
std::string server
Definition: DB.hh:346
const DBTableSet & GetAllTables()
Definition: DB.hh:252
std::list< RAT::DBLink * > links
Definition: DB.hh:342
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:282
DBTableSet tables
Definition: DB.hh:362
static DB * Get()
Definition: DB.hh:135
HTTPDownloader downloader
Definition: DB.hh:358
static std::vector< RAT::DBTable * > ReadRATDBFile(const std::string &filename)
Definition: DB.cc:461
void DumpContentsToJson(std::ostream &stream)
Definition: DB.cc:469
std::set< RAT::DBTableKey > tablesNotOnServer
Definition: DB.hh:355
void Set(const std::string &tblname, const std::string &fieldname, const T &val)
Definition: DB.hh:380
int NumLinks()
Definition: DB.hh:310
int LoadTable(DBTable *table)
Definition: DB.cc:65
int run
Definition: DB.hh:370
void Clear()
Definition: DB.cc:166
void RemoveLink(DBLink *link)
Definition: DB.cc:425
DBTable * GetRunTable(std::string tblname, std::string index, int runNumber)
Definition: DB.hh:286
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:292
void SetArrayIndex(const std::string &tblname, const std::string &fieldname, size_t idx, const T &val)
Definition: DB.hh:393
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