1#include "TRestDataBase.h"
15#include "TRestStringHelper.h"
16#include "TRestStringOutput.h"
17#include "TRestTools.h"
64DBEntry::DBEntry(vector<string> items) {
65 this->runNr = atoi(items[0].c_str());
66 this->type = items[1];
68 this->description = items[3];
69 this->version = items[4];
74 string metaFilename = REST_PATH +
"/data/dataURL";
79 ifstream infile(metaFilename);
83 vector<string> items =
Split(s,
"\t",
true);
84 if (items.size() <= 2)
continue;
85 for (
auto item : items) {
86 vector<string> pair =
Split(item,
"=",
true);
87 if (pair.size() == 2) {
89 info.runNr = atoi(pair[1].c_str());
90 else if (pair[0] ==
"type")
92 else if (pair[0] ==
"tag")
94 else if (pair[0] ==
"description")
95 info.description = pair[1];
96 else if (pair[0] ==
"version")
97 info.version = pair[1];
98 else if (pair[0] ==
"value")
103 fDataEntries.push_back(info);
108DBFile DBFile::ParseFile(
string _filename) {
110 file.filename = _filename;
114 int result = stat(_fullname.c_str(), &buf);
117 RESTError <<
"DBFile::ParseFile: Failed to load file \"" << _fullname <<
"\"!" << RESTendl;
119 file.fileSize = buf.st_size;
122 file.start = buf.st_ctime;
123 file.stop = buf.st_mtime;
125 string sha1 =
Split(sha1result,
" ")[0];
126 if (sha1.size() == 40) {
127 for (
int i = 0; i < 40; i++) {
128 file.sha1sum[i] = sha1[i];
130 file.sha1sum[40] = 0;
136void DBFile::Print() {
137 cout <<
"----DBFile struct----" << endl;
138 cout <<
"size: " << fileSize << endl;
139 cout <<
"event rate: " << evtRate << endl;
140 cout <<
"sha1sum: " << sha1sum << endl;
141 cout <<
"quality: " << quality << endl;
147 auto url = getenv(
"REST_DBURL");
148 if (url !=
nullptr) {
149 fConnectionString = url;
162 string runFilename = REST_USER_PATH +
"/runNumber";
164 int fd = open(runFilename.c_str(), O_RDWR | O_CREAT, 0666);
166 RESTError <<
"Error opening file " << runFilename << strerror(errno) << RESTendl;
171 string newRun = to_string(runNr) +
"\n";
172 if (write(fd, newRun.c_str(), newRun.size()) == -1)
173 RESTError <<
"Error writing file " << runFilename << strerror(errno) << RESTendl;
176 lseek(fd, 0, SEEK_SET);
177 char buffer[64] = {0};
178 ssize_t bytesReaded = read(fd, buffer,
sizeof(buffer) - 1);
179 if (bytesReaded > 0) runNr = std::atoi(buffer);
201 if (info.runNr == 0) {
203 }
else if (info.runNr > 0) {
204 newRunNr = info.runNr;
209 string runFilename = REST_USER_PATH +
"/runNumber";
211 int fd = open(runFilename.c_str(), O_RDWR | O_CREAT, 0666);
213 RESTError <<
"Error opening file " << runFilename << strerror(errno) << RESTendl;
217 string newRun = to_string(newRunNr + 1) +
"\n";
218 if (write(fd, newRun.c_str(), newRun.size()) == -1)
219 RESTError <<
"Error writing file " << runFilename << strerror(errno) << RESTendl;
225 RESTWarning <<
"runNumber file not writable. auto run number "
226 "increment is disabled"
237 vector<DBEntry> match;
238 if (_info.runNr <= 0 && _info.type ==
"" && _info.tag ==
"" && _info.description ==
"" &&
242 for (
unsigned int i = 0; i < fDataEntries.size(); i++) {
243 DBEntry info = fDataEntries[i];
245 bool runmatch = (_info.runNr == 0 || info.runNr == 0 || info.runNr == _info.type);
246 bool typematch = (_info.type ==
"" || info.type == _info.type);
247 bool tagmatch = (_info.tag ==
"" || info.tag == _info.tag);
248 bool descriptionmatch = (_info.description ==
"" || info.description == _info.description);
249 bool versionmatch = (_info.version ==
"" || info.version == _info.version);
250 bool valuematch = (_info.value ==
"" || info.value == _info.value);
252 if (runmatch && typematch && tagmatch && descriptionmatch && versionmatch && valuematch) {
253 match.push_back(info);
257 if (match.size() == 1) {
259 }
else if (match.size() > 1) {
260 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.