Fixed bug in AliEveEventManager causing frequent crashes. Fixed bug in Storage Manage...
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageDatabase.cxx
CommitLineData
5eb34a26 1#include "AliStorageDatabase.h"
2
3#include <iostream>
4#include <fstream>
5
6#include <TSQLRow.h>
7#include <TSQLResult.h>
8#include <TThread.h>
9#include <TSystem.h>
10#include <TFile.h>
11
12using namespace std;
13
14AliStorageDatabase::AliStorageDatabase() :
15 fHost(""),
16 fPort(""),
17 fDatabase(""),
18 fUID(""),
19 fPassword(""),
20 fTable(""),
21 fServer(0),
22 fStoragePath("")
23{
24 TThread::Lock();
164d3d29 25 ifstream configFile (GetConfigFilePath());
5eb34a26 26
27
28 if (configFile.is_open())
29 {
30 string line;
31 int from,to;
32 while(configFile.good())
33 {
34 getline(configFile,line);
35 from = line.find("\"")+1;
36 to = line.find_last_of("\"");
37 if(line.find("HOST=")==0)
38 {
39 fHost=line.substr(from,to-from);
40 }
41 else if(line.find("PORT=")==0)
42 {
43 fPort=line.substr(from,to-from);
44 }
45 else if(line.find("DATABASE=")==0)
46 {
47 fDatabase=line.substr(from,to-from);
48 }
49 else if(line.find("USER=")==0)
50 {
51 fUID=line.substr(from,to-from);
52 }
53 else if(line.find("PASS=")==0)
54 {
55 fPassword=line.substr(from,to-from);
56 }
57 else if(line.find("TABLE=")==0)
58 {
59 fTable=line.substr(from,to-from);
60 }
61 else if(line.find("STORAGE_PATH=")==0)
62 {
63 fStoragePath=line.substr(from,to-from);
64 }
65
66 }
67 if(configFile.eof())
68 {
69 configFile.clear();
70 }
71 configFile.close();
72 }
73 else
74 {
75 cout << "DATABASE -- Unable to open file" <<endl;
76 }
77 TThread::UnLock();
78
79
80 fServer = TSQLServer::Connect(Form("mysql://%s:%s/%s",fHost.c_str(),fPort.c_str(),fDatabase.c_str()),fUID.c_str(),fPassword.c_str());
81}
82
a410aca4 83AliStorageDatabase::~AliStorageDatabase(){
84 if (fServer) {delete fServer;}
85}
5eb34a26 86
87void AliStorageDatabase::InsertEvent(int runNumber,
efaef546 88 int eventNumber,
89 char *system,
90 int multiplicity,
91 char *filePath)
5eb34a26 92{
efaef546 93 TSQLResult *res = fServer->Query(Form("select * FROM %s WHERE run_number = %d AND event_number = %d AND permanent = 1;",fTable.c_str(),runNumber,eventNumber));
94 TSQLRow *row = res->Next();
95
96 if(!row)
97 {
98 res = fServer->Query(Form("REPLACE INTO %s (run_number,event_number,system,multiplicity,permanent,file_path) VALUES (%d,%d,'%s',%d,0,'%s');",fTable.c_str(),runNumber,eventNumber,system,multiplicity,filePath));
99 }
5eb34a26 100
efaef546 101 delete row;
102 delete res;
5eb34a26 103}
104
105bool AliStorageDatabase::MarkEvent(struct eventStruct event)
a410aca4 106{
107 TSQLResult* res;
108 res = fServer->Query(Form("UPDATE %s SET permanent = 1 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
6ce16778 109 if(!res)
110 {
111 cout<<"DATABASE -- couldn't update permanent flag"<<endl;
112 delete res;
113 return 0;
114 }
115 else
116 {
117 cout<<"DATABASE -- permanent flag updated"<<endl;
118
119 res = fServer->Query(Form("UPDATE %s SET file_path = '%s' WHERE run_number = %d AND event_number = %d;",fTable.c_str(),Form("%s/permEvents.root",fStoragePath.c_str()), event.runNumber,event.eventNumber));
120 if(!res)
121 {
122 cout<<"DATABASE -- couldn't update file's path. Unsetting permanent flag"<<endl;
123 res = fServer->Query(Form("UPDATE %s SET permanent = 0 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
124 delete res;
125 return 0;
126 }
127 else
128 {
129 cout<<"DATABASE -- event marked"<<endl;
130 delete res;
131 return 1;
132 }
133 }
134}
135
136bool AliStorageDatabase::UpdateEventPath(struct eventStruct event,const char *newPath)
137{
138 TSQLResult* res;
139 res = fServer->Query(Form("UPDATE %s SET file_path = '%s' WHERE run_number = %d AND event_number = %d;",fTable.c_str(),newPath,event.runNumber,event.eventNumber));
140 if(!res)
141 {
142 cout<<"DATABASE -- couldn't update file's path"<<endl;
143 delete res;
144 return 0;
145 }
146 else
147 {
148 cout<<"DATABASE -- path updated for event:"<<event.eventNumber<<endl;
149 delete res;
150 return 1;
151 }
5eb34a26 152}
153
6ce16778 154
5eb34a26 155vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list)
156{
157 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number >= %d AND run_number <= %d AND event_number >= %d AND event_number <= %d AND multiplicity >= %d AND multiplicity <= %d AND (permanent = %d OR permanent = %d) AND (system = '%s' OR system = '%s') ORDER BY run_number,event_number;",
158 fTable.c_str(),
159 list.runNumber[0],
160 list.runNumber[1],
161 list.eventNumber[0],
162 list.eventNumber[1],
163 list.multiplicity[0],
164 list.multiplicity[1],
165 list.marked[0],
166 list.marked[1],
167 list.system[0],
168 list.system[1]));
169
170
171 TSQLRow *row;
172 vector<serverListStruct> eventsVector;
173
174 while((row = result->Next()))
175 {
176 serverListStruct resultList;
177
178 resultList.runNumber = atoi(row->GetField(0));
179 resultList.eventNumber = atoi(row->GetField(1));
180 strcpy(resultList.system, row->GetField(2));
181 resultList.multiplicity = atoi(row->GetField(3));
182 resultList.marked = atoi(row->GetField(4));
183
184 eventsVector.push_back(resultList);
185 delete row;
186 }
a410aca4 187 delete result;
5eb34a26 188 return eventsVector;
189}
190
191AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event)
192{
193 cout<<"database - get event"<<endl;
194 string pathToFile = GetFilePath(event);
195
196 if(!strcmp(pathToFile.c_str(),""))
197 {
198 cout<<"DATABASE -- no such file in database"<<endl;
199 return NULL;
200 }
201
202 TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
203 if(!tmpFile)
204 {
205 cout<<"DATABASE -- couldn't open temp file"<<endl;
206 return NULL;
207 }
208 AliESDEvent *data;
6ce16778 209 tmpFile->cd(Form("run%d",event.runNumber));
210 data = (AliESDEvent*)gDirectory->Get(Form("event%d;1",event.eventNumber));
211
212
213 // tmpFile->GetObject(Form("event%d;1",event.eventNumber),data);
5eb34a26 214
215 return data;
5eb34a26 216}
217
5eb34a26 218void AliStorageDatabase::RemoveEvent(struct eventStruct event)
219{
a410aca4 220 TSQLResult* res;
221 res = fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber));
222 delete res;
fae81379 223}
a410aca4 224
fae81379 225void AliStorageDatabase::RemoveEventsWithPath(string path)
226{
227 TSQLResult *res = fServer->Query(Form("DELETE FROM %s WHERE file_path = \"%s\";",fTable.c_str(),path.c_str()));
228 delete res;
5eb34a26 229}
230
231string AliStorageDatabase::GetFilePath(struct eventStruct event)
232{
233 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
234 TSQLRow *row;
235 row = result->Next();
236 if(row)
237 {
238 string path(row->GetField(5));
239 delete row;
240 return path;
241 }
242 else
243 {
244 return "";
245 }
246}
247
248AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event)
249{
fae81379 250 cout<<"Database:"<<event.runNumber<<"\t"<<event.eventNumber<<endl;
251
5eb34a26 252 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
253
254 TSQLRow *row;
255 bool isCurrentEvent=false;
256 struct eventStruct nextEvent;
257
258 while((row = result->Next()))
259 {
260 if(isCurrentEvent)
261 {
262 nextEvent.runNumber = atoi(row->GetField(0));
263 nextEvent.eventNumber = atoi(row->GetField(1));
264 return GetEvent(nextEvent);
265 }
266
267 //if current event found
268 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
269 {
270 isCurrentEvent=true;
271 }
272
273 delete row;
274 }
275
276 return NULL;
277}
278
a410aca4 279AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event)
280{
281 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
282
283 TSQLRow *row;
284 bool isCurrentEvent=false;
285 struct eventStruct nextEvent;
286
287 while((row = result->Next()))
288 {
289 if(isCurrentEvent)
290 {
291 nextEvent.runNumber = atoi(row->GetField(0));
292 nextEvent.eventNumber = atoi(row->GetField(1));
293 return GetEvent(nextEvent);
294 }
295
296 //if current event found
297 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
298 {
299 isCurrentEvent=true;
300 }
301
302 delete row;
303 }
304 delete result;
305 return NULL;
306}
307
fae81379 308struct eventStruct AliStorageDatabase::GetOldestEvent()
309{
6ce16778 310 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE permanent = 0 ORDER BY run_number,event_number;",fTable.c_str()));
fae81379 311
312 TSQLRow *row;
313 struct eventStruct oldestEvent = {0,0};
314
315 if((row = result->Next()))
316 {
317 oldestEvent.runNumber = atoi(row->GetField(0));
318 oldestEvent.eventNumber = atoi(row->GetField(1));
319 delete row;
320 }
321 else
322 {
323 cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl;
324 }
325 return oldestEvent;
326}
a410aca4 327
5eb34a26 328AliESDEvent* AliStorageDatabase::GetLastEvent()
329{
330 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
331
332 TSQLRow *row;
333 struct eventStruct lastEvent = {0,0};
334
335 while((row = result->Next()))
336 {
337 lastEvent.runNumber = atoi(row->GetField(0));
338 lastEvent.eventNumber = atoi(row->GetField(1));
339 delete row;
340 }
341 cout<<"Last event is:"<<lastEvent.eventNumber<<endl;
342 return GetEvent(lastEvent);
343
344}
a410aca4 345
346AliESDEvent* AliStorageDatabase::GetFirstEvent()
347{
fae81379 348 cout<<"Database - first"<<endl;
a410aca4 349 TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
350
351 TSQLRow *row;
fae81379 352 struct eventStruct firstEvent = {0,0};
a410aca4 353
354 while((row = result->Next()))
355 {
fae81379 356 firstEvent.runNumber = atoi(row->GetField(0));
357 firstEvent.eventNumber = atoi(row->GetField(1));
a410aca4 358 delete row;
359 }
fae81379 360 cout<<"First event is:"<<firstEvent.eventNumber<<endl;
361 return GetEvent(firstEvent);
a410aca4 362
363}