]>
Commit | Line | Data |
---|---|---|
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 | ||
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(); | |
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 | 83 | AliStorageDatabase::~AliStorageDatabase(){ |
84 | if (fServer) {delete fServer;} | |
85 | } | |
5eb34a26 | 86 | |
87 | void AliStorageDatabase::InsertEvent(int runNumber, | |
88 | int eventNumber, | |
89 | char *system, | |
90 | int multiplicity, | |
91 | char *filePath) | |
92 | { | |
a410aca4 | 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; | |
5eb34a26 | 96 | |
97 | } | |
98 | ||
99 | bool AliStorageDatabase::MarkEvent(struct eventStruct event) | |
a410aca4 | 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)); | |
6ce16778 | 103 | if(!res) |
104 | { | |
105 | cout<<"DATABASE -- couldn't update permanent flag"<<endl; | |
106 | delete res; | |
107 | return 0; | |
108 | } | |
109 | else | |
110 | { | |
111 | cout<<"DATABASE -- permanent flag updated"<<endl; | |
112 | ||
113 | 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)); | |
114 | if(!res) | |
115 | { | |
116 | cout<<"DATABASE -- couldn't update file's path. Unsetting permanent flag"<<endl; | |
117 | res = fServer->Query(Form("UPDATE %s SET permanent = 0 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber)); | |
118 | delete res; | |
119 | return 0; | |
120 | } | |
121 | else | |
122 | { | |
123 | cout<<"DATABASE -- event marked"<<endl; | |
124 | delete res; | |
125 | return 1; | |
126 | } | |
127 | } | |
128 | } | |
129 | ||
130 | bool AliStorageDatabase::UpdateEventPath(struct eventStruct event,const char *newPath) | |
131 | { | |
132 | TSQLResult* res; | |
133 | 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)); | |
134 | if(!res) | |
135 | { | |
136 | cout<<"DATABASE -- couldn't update file's path"<<endl; | |
137 | delete res; | |
138 | return 0; | |
139 | } | |
140 | else | |
141 | { | |
142 | cout<<"DATABASE -- path updated for event:"<<event.eventNumber<<endl; | |
143 | delete res; | |
144 | return 1; | |
145 | } | |
5eb34a26 | 146 | } |
147 | ||
6ce16778 | 148 | |
5eb34a26 | 149 | vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list) |
150 | { | |
151 | 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;", | |
152 | fTable.c_str(), | |
153 | list.runNumber[0], | |
154 | list.runNumber[1], | |
155 | list.eventNumber[0], | |
156 | list.eventNumber[1], | |
157 | list.multiplicity[0], | |
158 | list.multiplicity[1], | |
159 | list.marked[0], | |
160 | list.marked[1], | |
161 | list.system[0], | |
162 | list.system[1])); | |
163 | ||
164 | ||
165 | TSQLRow *row; | |
166 | vector<serverListStruct> eventsVector; | |
167 | ||
168 | while((row = result->Next())) | |
169 | { | |
170 | serverListStruct resultList; | |
171 | ||
172 | resultList.runNumber = atoi(row->GetField(0)); | |
173 | resultList.eventNumber = atoi(row->GetField(1)); | |
174 | strcpy(resultList.system, row->GetField(2)); | |
175 | resultList.multiplicity = atoi(row->GetField(3)); | |
176 | resultList.marked = atoi(row->GetField(4)); | |
177 | ||
178 | eventsVector.push_back(resultList); | |
179 | delete row; | |
180 | } | |
a410aca4 | 181 | delete result; |
5eb34a26 | 182 | return eventsVector; |
183 | } | |
184 | ||
185 | AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event) | |
186 | { | |
187 | cout<<"database - get event"<<endl; | |
188 | string pathToFile = GetFilePath(event); | |
189 | ||
190 | if(!strcmp(pathToFile.c_str(),"")) | |
191 | { | |
192 | cout<<"DATABASE -- no such file in database"<<endl; | |
193 | return NULL; | |
194 | } | |
195 | ||
196 | TFile *tmpFile = new TFile(pathToFile.c_str(),"read"); | |
197 | if(!tmpFile) | |
198 | { | |
199 | cout<<"DATABASE -- couldn't open temp file"<<endl; | |
200 | return NULL; | |
201 | } | |
202 | AliESDEvent *data; | |
6ce16778 | 203 | tmpFile->cd(Form("run%d",event.runNumber)); |
204 | data = (AliESDEvent*)gDirectory->Get(Form("event%d;1",event.eventNumber)); | |
205 | ||
206 | ||
207 | // tmpFile->GetObject(Form("event%d;1",event.eventNumber),data); | |
5eb34a26 | 208 | |
209 | return data; | |
5eb34a26 | 210 | } |
211 | ||
5eb34a26 | 212 | void AliStorageDatabase::RemoveEvent(struct eventStruct event) |
213 | { | |
a410aca4 | 214 | TSQLResult* res; |
215 | res = fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber)); | |
216 | delete res; | |
fae81379 | 217 | } |
a410aca4 | 218 | |
fae81379 | 219 | void AliStorageDatabase::RemoveEventsWithPath(string path) |
220 | { | |
221 | TSQLResult *res = fServer->Query(Form("DELETE FROM %s WHERE file_path = \"%s\";",fTable.c_str(),path.c_str())); | |
222 | delete res; | |
5eb34a26 | 223 | } |
224 | ||
225 | string AliStorageDatabase::GetFilePath(struct eventStruct event) | |
226 | { | |
227 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber)); | |
228 | TSQLRow *row; | |
229 | row = result->Next(); | |
230 | if(row) | |
231 | { | |
232 | string path(row->GetField(5)); | |
233 | delete row; | |
234 | return path; | |
235 | } | |
236 | else | |
237 | { | |
238 | return ""; | |
239 | } | |
240 | } | |
241 | ||
242 | AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event) | |
243 | { | |
fae81379 | 244 | cout<<"Database:"<<event.runNumber<<"\t"<<event.eventNumber<<endl; |
245 | ||
5eb34a26 | 246 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str())); |
247 | ||
248 | TSQLRow *row; | |
249 | bool isCurrentEvent=false; | |
250 | struct eventStruct nextEvent; | |
251 | ||
252 | while((row = result->Next())) | |
253 | { | |
254 | if(isCurrentEvent) | |
255 | { | |
256 | nextEvent.runNumber = atoi(row->GetField(0)); | |
257 | nextEvent.eventNumber = atoi(row->GetField(1)); | |
258 | return GetEvent(nextEvent); | |
259 | } | |
260 | ||
261 | //if current event found | |
262 | if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber) | |
263 | { | |
264 | isCurrentEvent=true; | |
265 | } | |
266 | ||
267 | delete row; | |
268 | } | |
269 | ||
270 | return NULL; | |
271 | } | |
272 | ||
a410aca4 | 273 | AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event) |
274 | { | |
275 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str())); | |
276 | ||
277 | TSQLRow *row; | |
278 | bool isCurrentEvent=false; | |
279 | struct eventStruct nextEvent; | |
280 | ||
281 | while((row = result->Next())) | |
282 | { | |
283 | if(isCurrentEvent) | |
284 | { | |
285 | nextEvent.runNumber = atoi(row->GetField(0)); | |
286 | nextEvent.eventNumber = atoi(row->GetField(1)); | |
287 | return GetEvent(nextEvent); | |
288 | } | |
289 | ||
290 | //if current event found | |
291 | if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber) | |
292 | { | |
293 | isCurrentEvent=true; | |
294 | } | |
295 | ||
296 | delete row; | |
297 | } | |
298 | delete result; | |
299 | return NULL; | |
300 | } | |
301 | ||
fae81379 | 302 | struct eventStruct AliStorageDatabase::GetOldestEvent() |
303 | { | |
6ce16778 | 304 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE permanent = 0 ORDER BY run_number,event_number;",fTable.c_str())); |
fae81379 | 305 | |
306 | TSQLRow *row; | |
307 | struct eventStruct oldestEvent = {0,0}; | |
308 | ||
309 | if((row = result->Next())) | |
310 | { | |
311 | oldestEvent.runNumber = atoi(row->GetField(0)); | |
312 | oldestEvent.eventNumber = atoi(row->GetField(1)); | |
313 | delete row; | |
314 | } | |
315 | else | |
316 | { | |
317 | cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl; | |
318 | } | |
319 | return oldestEvent; | |
320 | } | |
a410aca4 | 321 | |
5eb34a26 | 322 | AliESDEvent* AliStorageDatabase::GetLastEvent() |
323 | { | |
324 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str())); | |
325 | ||
326 | TSQLRow *row; | |
327 | struct eventStruct lastEvent = {0,0}; | |
328 | ||
329 | while((row = result->Next())) | |
330 | { | |
331 | lastEvent.runNumber = atoi(row->GetField(0)); | |
332 | lastEvent.eventNumber = atoi(row->GetField(1)); | |
333 | delete row; | |
334 | } | |
335 | cout<<"Last event is:"<<lastEvent.eventNumber<<endl; | |
336 | return GetEvent(lastEvent); | |
337 | ||
338 | } | |
a410aca4 | 339 | |
340 | AliESDEvent* AliStorageDatabase::GetFirstEvent() | |
341 | { | |
fae81379 | 342 | cout<<"Database - first"<<endl; |
a410aca4 | 343 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number DESC;",fTable.c_str())); |
344 | ||
345 | TSQLRow *row; | |
fae81379 | 346 | struct eventStruct firstEvent = {0,0}; |
a410aca4 | 347 | |
348 | while((row = result->Next())) | |
349 | { | |
fae81379 | 350 | firstEvent.runNumber = atoi(row->GetField(0)); |
351 | firstEvent.eventNumber = atoi(row->GetField(1)); | |
a410aca4 | 352 | delete row; |
353 | } | |
fae81379 | 354 | cout<<"First event is:"<<firstEvent.eventNumber<<endl; |
355 | return GetEvent(firstEvent); | |
a410aca4 | 356 | |
357 | } |