List event tab embedded in left panel of ED + other minor changes.
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageDatabase.cxx
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
12 using namespace std;
13
14 AliStorageDatabase::AliStorageDatabase() :
15         fHost(""),
16         fPort(""),
17         fDatabase(""),
18         fUID(""),
19         fPassword(""),
20         fTable(""),
21         fServer(0),
22         fStoragePath("")
23 {
24         TThread::Lock();        
25         ifstream configFile (GetConfigFilePath());
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
83 AliStorageDatabase::~AliStorageDatabase(){
84   if (fServer) {delete fServer;}
85 }
86
87 void AliStorageDatabase::InsertEvent(int runNumber,
88                                      int eventNumber,
89                                      char *system,
90                                      int multiplicity,
91                                      char *filePath)
92 {
93   TSQLResult* res;
94   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));
95   delete res;
96
97 }
98
99 bool AliStorageDatabase::MarkEvent(struct eventStruct event)
100 {  
101   TSQLResult* res;
102   res = fServer->Query(Form("UPDATE %s SET permanent = 1 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
103   if(res) {
104     delete res;
105     return 1;
106   }
107   else {
108     delete res;
109     return 0;
110   }
111 }
112
113 vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list)
114 {
115         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;",
116                                                  fTable.c_str(),
117                                                  list.runNumber[0],
118                                                  list.runNumber[1],
119                                                  list.eventNumber[0],
120                                                  list.eventNumber[1],
121                                                  list.multiplicity[0],
122                                                  list.multiplicity[1],
123                                                  list.marked[0],
124                                                  list.marked[1],
125                                                  list.system[0],
126                                                  list.system[1]));
127
128         
129         TSQLRow *row;
130         vector<serverListStruct> eventsVector;
131         
132         while((row = result->Next()))
133         {
134                 serverListStruct resultList;
135
136                 resultList.runNumber = atoi(row->GetField(0));
137                 resultList.eventNumber = atoi(row->GetField(1));
138                 strcpy(resultList.system, row->GetField(2));
139                 resultList.multiplicity = atoi(row->GetField(3));
140                 resultList.marked = atoi(row->GetField(4));
141
142                 eventsVector.push_back(resultList);
143                 delete row;
144         }
145         delete result;
146         return eventsVector;
147 }
148
149 AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event)
150 {
151         cout<<"database - get event"<<endl;
152         string pathToFile = GetFilePath(event);
153
154         if(!strcmp(pathToFile.c_str(),""))
155         {
156                 cout<<"DATABASE -- no such file in database"<<endl;
157                 return NULL;
158         }
159         
160         TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
161         if(!tmpFile)
162         {
163                 cout<<"DATABASE -- couldn't open temp file"<<endl;
164                 return NULL;
165         }
166         AliESDEvent *data;
167         tmpFile->GetObject(Form("event%d;1",event.eventNumber),data);
168
169         return data;
170 }
171
172 void AliStorageDatabase::RemoveEvent(struct eventStruct event)
173 {
174   TSQLResult* res;
175   res = fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber));
176   delete res;
177 }
178
179 void AliStorageDatabase::RemoveEventsWithPath(string path)
180 {
181     TSQLResult *res = fServer->Query(Form("DELETE FROM %s WHERE file_path = \"%s\";",fTable.c_str(),path.c_str()));
182     delete res;
183 }
184
185 string AliStorageDatabase::GetFilePath(struct eventStruct event)
186 {
187         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
188         TSQLRow *row;
189         row = result->Next();
190         if(row)
191         {
192                 string path(row->GetField(5));
193                 delete row;
194                 return path;
195         }
196         else
197         {
198                 return "";
199         }
200 }
201
202 AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event)
203 {
204     cout<<"Database:"<<event.runNumber<<"\t"<<event.eventNumber<<endl;
205     
206         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
207
208         TSQLRow *row;
209         bool isCurrentEvent=false;
210         struct eventStruct nextEvent;
211         
212         while((row = result->Next()))
213         {
214                 if(isCurrentEvent)
215                 {
216                         nextEvent.runNumber = atoi(row->GetField(0));
217                         nextEvent.eventNumber = atoi(row->GetField(1));
218                         return GetEvent(nextEvent);
219                 }
220
221                 //if current event found
222                 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
223                 {
224                         isCurrentEvent=true;
225                 }
226                 
227                 delete row;
228         }
229
230         return NULL;
231 }
232
233 AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event)
234 {
235         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
236
237         TSQLRow *row;
238         bool isCurrentEvent=false;
239         struct eventStruct nextEvent;
240         
241         while((row = result->Next()))
242         {
243                 if(isCurrentEvent)
244                 {
245                         nextEvent.runNumber = atoi(row->GetField(0));
246                         nextEvent.eventNumber = atoi(row->GetField(1));
247                         return GetEvent(nextEvent);
248                 }
249
250                 //if current event found
251                 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
252                 {
253                         isCurrentEvent=true;
254                 }
255                 
256                 delete row;
257         }
258         delete result;
259         return NULL;
260 }
261
262 struct eventStruct AliStorageDatabase::GetOldestEvent()
263 {
264     TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
265     
266     TSQLRow *row;
267     struct eventStruct oldestEvent = {0,0};
268     
269     if((row = result->Next()))
270     {
271         oldestEvent.runNumber = atoi(row->GetField(0));
272         oldestEvent.eventNumber = atoi(row->GetField(1));
273         delete row;
274     }
275     else
276     {
277         cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl;
278     }
279     return oldestEvent;
280 }
281
282 AliESDEvent* AliStorageDatabase::GetLastEvent()
283 {
284         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
285
286         TSQLRow *row;
287         struct eventStruct lastEvent = {0,0};
288
289         while((row = result->Next()))
290         {
291                 lastEvent.runNumber = atoi(row->GetField(0));
292                 lastEvent.eventNumber = atoi(row->GetField(1));
293                 delete row;
294         }
295         cout<<"Last event is:"<<lastEvent.eventNumber<<endl;
296         return GetEvent(lastEvent);
297
298 }
299
300 AliESDEvent* AliStorageDatabase::GetFirstEvent()
301 {
302     cout<<"Database - first"<<endl;
303         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
304
305         TSQLRow *row;
306         struct eventStruct firstEvent = {0,0};
307
308         while((row = result->Next()))
309         {
310                 firstEvent.runNumber = atoi(row->GetField(0));
311                 firstEvent.eventNumber = atoi(row->GetField(1));
312                 delete row;
313         }
314         cout<<"First event is:"<<firstEvent.eventNumber<<endl;
315         return GetEvent(firstEvent);
316
317 }