1 #include "TRestDataBase.h"
9 #include "TRestStringHelper.h"
10 #include "TRestStringOutput.h"
11 #include "TRestTools.h"
58 DBEntry::DBEntry(vector<string> items) {
59 this->runNr = atoi(items[0].c_str());
60 this->type = items[1];
62 this->description = items[3];
63 this->version = items[4];
68 string metaFilename = REST_PATH +
"/data/dataURL";
73 ifstream infile(metaFilename);
77 vector<string> items =
Split(s,
"\t",
true);
78 if (items.size() <= 2)
continue;
79 for (
auto item : items) {
80 vector<string> pair =
Split(item,
"=",
true);
81 if (pair.size() == 2) {
83 info.runNr = atoi(pair[1].c_str());
84 else if (pair[0] ==
"type")
86 else if (pair[0] ==
"tag")
88 else if (pair[0] ==
"description")
89 info.description = pair[1];
90 else if (pair[0] ==
"version")
91 info.version = pair[1];
92 else if (pair[0] ==
"value")
97 fDataEntries.push_back(info);
102 DBFile DBFile::ParseFile(
string _filename) {
104 file.filename = _filename;
108 int result = stat(_fullname.c_str(), &buf);
111 RESTError <<
"DBFile::ParseFile: Failed to load file \"" << _fullname <<
"\"!" << RESTendl;
113 file.fileSize = buf.st_size;
116 file.start = buf.st_ctime;
117 file.stop = buf.st_mtime;
119 string sha1 =
Split(sha1result,
" ")[0];
120 if (sha1.size() == 40) {
121 for (
int i = 0; i < 40; i++) {
122 file.sha1sum[i] = sha1[i];
124 file.sha1sum[40] = 0;
130 void DBFile::Print() {
131 cout <<
"----DBFile struct----" << endl;
132 cout <<
"size: " << fileSize << endl;
133 cout <<
"event rate: " << evtRate << endl;
134 cout <<
"sha1sum: " << sha1sum << endl;
135 cout <<
"quality: " << quality << endl;
141 auto url = getenv(
"REST_DBURL");
142 if (url !=
nullptr) {
143 fConnectionString = url;
156 string runFilename = REST_USER_PATH +
"/runNumber";
164 ifstream ifs(runFilename);
185 if (info.runNr == 0) {
186 newRunNr = get_lastrun() + 1;
187 }
else if (info.runNr > 0) {
188 newRunNr = info.runNr;
193 string runFilename = REST_USER_PATH +
"/runNumber";
197 RESTWarning <<
"runNumber file not writable. auto run number "
198 "increment is disabled"
210 vector<DBEntry> match;
211 if (_info.runNr <= 0 && _info.type ==
"" && _info.tag ==
"" && _info.description ==
"" &&
215 for (
unsigned int i = 0; i < fDataEntries.size(); i++) {
216 DBEntry info = fDataEntries[i];
218 bool runmatch = (_info.runNr == 0 || info.runNr == 0 || info.runNr == _info.type);
219 bool typematch = (_info.type ==
"" || info.type == _info.type);
220 bool tagmatch = (_info.tag ==
"" || info.tag == _info.tag);
221 bool descriptionmatch = (_info.description ==
"" || info.description == _info.description);
222 bool versionmatch = (_info.version ==
"" || info.version == _info.version);
223 bool valuematch = (_info.value ==
"" || info.value == _info.value);
225 if (runmatch && typematch && tagmatch && descriptionmatch && versionmatch && valuematch) {
226 match.push_back(info);
230 if (match.size() == 1) {
232 }
else if (match.size() > 1) {
233 RESTWarning <<
"multiple metadata found! returning the first!" << RESTendl;
virtual void Initialize()
default: read the dataURL file
TRestDataBase()
default constructor, setting fConnectionString according to the env
virtual int get_lastrun()
get the latest run id in database
virtual DBEntry query_data(DBEntry info)
virtual int set_run(DBEntry info, bool overwrite=true)
add/update a run, with run info as struct DBEntry. returns the added run id
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.
std::string ToDateTimeString(time_t time)
Format time_t into string.