58df7008132591597e98acf4243e29e80710457f
[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 struct eventStruct AliStorageDatabase::GetOldestEvent()
173 {
174         struct eventStruct oldestEvent = {0,0};
175
176         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
177
178         TSQLRow *row;   
179         if((row = result->Next()))
180         {
181                 oldestEvent.runNumber = atoi(row->GetField(0));
182                 oldestEvent.eventNumber = atoi(row->GetField(1));
183         
184                 delete row;
185         }
186         else
187         {
188                 cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl;
189         }
190         return oldestEvent;
191 }
192
193 void AliStorageDatabase::RemoveEvent(struct eventStruct event)
194 {
195   TSQLResult* res;
196   res = fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber));
197   delete res;
198
199 }
200
201 string AliStorageDatabase::GetFilePath(struct eventStruct event)
202 {
203         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
204         TSQLRow *row;
205         row = result->Next();
206         if(row)
207         {
208                 string path(row->GetField(5));
209                 delete row;
210                 return path;
211         }
212         else
213         {
214                 return "";
215         }
216 }
217
218 AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event)
219 {
220         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
221
222         TSQLRow *row;
223         bool isCurrentEvent=false;
224         struct eventStruct nextEvent;
225         
226         while((row = result->Next()))
227         {
228                 if(isCurrentEvent)
229                 {
230                         nextEvent.runNumber = atoi(row->GetField(0));
231                         nextEvent.eventNumber = atoi(row->GetField(1));
232                         return GetEvent(nextEvent);
233                 }
234
235                 //if current event found
236                 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
237                 {
238                         isCurrentEvent=true;
239                 }
240                 
241                 delete row;
242         }
243
244         return NULL;
245 }
246
247 AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event)
248 {
249         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
250
251         TSQLRow *row;
252         bool isCurrentEvent=false;
253         struct eventStruct nextEvent;
254         
255         while((row = result->Next()))
256         {
257                 if(isCurrentEvent)
258                 {
259                         nextEvent.runNumber = atoi(row->GetField(0));
260                         nextEvent.eventNumber = atoi(row->GetField(1));
261                         return GetEvent(nextEvent);
262                 }
263
264                 //if current event found
265                 if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber)
266                 {
267                         isCurrentEvent=true;
268                 }
269                 
270                 delete row;
271         }
272         delete result;
273         return NULL;
274 }
275
276
277 AliESDEvent* AliStorageDatabase::GetLastEvent()
278 {
279         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
280
281         TSQLRow *row;
282         struct eventStruct lastEvent = {0,0};
283
284         while((row = result->Next()))
285         {
286                 lastEvent.runNumber = atoi(row->GetField(0));
287                 lastEvent.eventNumber = atoi(row->GetField(1));
288                 delete row;
289         }
290         cout<<"Last event is:"<<lastEvent.eventNumber<<endl;
291         return GetEvent(lastEvent);
292
293 }
294
295 AliESDEvent* AliStorageDatabase::GetFirstEvent()
296 {
297         TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str()));
298
299         TSQLRow *row;
300         struct eventStruct lastEvent = {0,0};
301
302         while((row = result->Next()))
303         {
304                 lastEvent.runNumber = atoi(row->GetField(0));
305                 lastEvent.eventNumber = atoi(row->GetField(1));
306                 delete row;
307         }
308         cout<<"Last event is:"<<lastEvent.eventNumber<<endl;
309         return GetEvent(lastEvent);
310
311 }