REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestDataBase.h
1#ifndef RestCore_TRestDataBase
2#define RestCore_TRestDataBase
3
4#include <iostream>
5#include <map>
6#include <vector>
7
8#include "TRestTools.h"
9
10// Contains basic information of the row. These information also appears in TRestRun.
11// One or more DBEntry objects stores the information of one run.
12//
13// * During run querying, the information shows the corresponding run information
14//
15// * During metadata querying:
16// runNr: the corresponding run of this metadata.
17// type: It can be the name of one of the store metadata, or the name of some saved constants.
18// tag: when type is "META_RML", it indicates which REST class the rml is for.
19// value: the value of the stored metadata. for example, type="gas_pressure", value="10"
20// type="META_RML", tag="TRestDetectorGas", value="https://sultan.unizar.es/gasFiles/gases.rml"
21//
22// When used for database searching, if the struct field value is not default value,
23// they will be regarded as "any"
24struct DBEntry {
25 DBEntry(int _runNr = 0, std::string _type = "", std::string _tag = "", std::string _description = "",
26 std::string _version = "") {
27 runNr = _runNr;
28 type = _type;
29 tag = _tag;
30 description = _description;
31 version = _version;
32 }
33 DBEntry(std::vector<std::string> defs);
34 int runNr = 0;
35 std::string type = "";
36 std::string tag = "";
37 std::string user = "";
38 std::string description = "";
39 std::string version = "";
40 time_t tstart = 0;
41 time_t tend = 0;
42
43 std::string value = "";
44
45 bool IsZombie() { return runNr == 0 || (type == "" && value == ""); }
46};
47
48struct DBFile {
49 DBFile() {
50 filename = "";
51 fileSize = 0;
52 evtRate = 0;
53 for (int i = 0; i < 41; i++) {
54 sha1sum[i] = 0;
55 }
56 quality = true;
57 start = 0;
58 stop = 0;
59 }
60
61 DBFile(std::string filename) { this->filename = filename; }
62 static DBFile ParseFile(std::string filename);
63
64 void Print();
65 operator std::string() { return filename; }
66
67 std::string filename;
68 long fileSize;
69 double evtRate;
70 char sha1sum[41]; // last bit is \0
71 bool quality;
72 time_t start;
73 time_t stop;
74};
75
76struct DBTable : public std::vector<std::vector<std::string>> {
77 std::vector<std::string> headerline;
78 int rows() { return size(); }
79 int columns() { return headerline.size(); }
80};
81
83 protected:
84 std::vector<DBEntry> fDataEntries;
85 std::string fConnectionString;
86
87 public:
92
94 virtual void Initialize();
96 virtual void test() {}
98 virtual void print(std::string cmd) {}
100 virtual DBTable exec(std::string cmd) { return DBTable(); }
101
104 virtual DBEntry query_run(int runnumber) { return DBEntry(runnumber); }
106 virtual DBFile query_run_file(int runnumber, int fileid) { return DBFile(); }
108 virtual std::vector<DBFile> query_run_files(int runnumber) { return std::vector<DBFile>(); }
109
111 virtual std::vector<int> search_run(DBEntry info) { return std::vector<int>{0}; }
113 virtual std::vector<int> search_run_with_file(std::string filepattern) { return std::vector<int>{0}; }
114
116 virtual int get_lastrun();
117
119 virtual int set_run(DBEntry info, bool overwrite = true);
121 virtual int set_runfile(int runnumber, std::string filename) { return 0; }
122
125 virtual DBEntry query_data(DBEntry info);
127 virtual int set_data(DBEntry info, bool overwrite = true) { return 0; }
128};
129
130EXTERN_DEF TRestDataBase* gDataBase;
131
132#endif
virtual void Initialize()
default: read the dataURL file
TRestDataBase()
default constructor, setting fConnectionString according to the env
virtual std::vector< int > search_run_with_file(std::string filepattern)
search runs according to the file name. return a list of run numbers
virtual void print(std::string cmd)
print the table after query std::string cmd
Definition: TRestDataBase.h:98
virtual int get_lastrun()
get the latest run id in database
virtual DBEntry query_data(DBEntry info)
virtual void test()
test function
Definition: TRestDataBase.h:96
virtual DBFile query_run_file(int runnumber, int fileid)
return file of certain file id in certain run
~TRestDataBase()
destructor
Definition: TRestDataBase.h:91
virtual int set_run(DBEntry info, bool overwrite=true)
add/update a run, with run info as struct DBEntry. returns the added run id
virtual DBTable exec(std::string cmd)
return a table from query std::string cmd
virtual int set_runfile(int runnumber, std::string filename)
add/update a runfile to the specified run
virtual DBEntry query_run(int runnumber)
virtual std::vector< int > search_run(DBEntry info)
search runs according to the run info. return a list of run numbers
virtual int set_data(DBEntry info, bool overwrite=true)
add/update a new record of metadata in database
virtual std::vector< DBFile > query_run_files(int runnumber)
return all the files of the run