]>
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 | ||
83 | AliStorageDatabase::~AliStorageDatabase(){} | |
84 | ||
85 | void AliStorageDatabase::InsertEvent(int runNumber, | |
86 | int eventNumber, | |
87 | char *system, | |
88 | int multiplicity, | |
89 | char *filePath) | |
90 | { | |
91 | 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)); | |
92 | ||
93 | } | |
94 | ||
95 | bool AliStorageDatabase::MarkEvent(struct eventStruct event) | |
96 | { | |
97 | if(fServer->Query(Form("UPDATE %s SET permanent = 1 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber))) | |
98 | { | |
99 | return 1; | |
100 | } | |
101 | else | |
102 | { | |
103 | return 0; | |
104 | } | |
105 | } | |
106 | ||
107 | vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list) | |
108 | { | |
109 | 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;", | |
110 | fTable.c_str(), | |
111 | list.runNumber[0], | |
112 | list.runNumber[1], | |
113 | list.eventNumber[0], | |
114 | list.eventNumber[1], | |
115 | list.multiplicity[0], | |
116 | list.multiplicity[1], | |
117 | list.marked[0], | |
118 | list.marked[1], | |
119 | list.system[0], | |
120 | list.system[1])); | |
121 | ||
122 | ||
123 | TSQLRow *row; | |
124 | vector<serverListStruct> eventsVector; | |
125 | ||
126 | while((row = result->Next())) | |
127 | { | |
128 | serverListStruct resultList; | |
129 | ||
130 | resultList.runNumber = atoi(row->GetField(0)); | |
131 | resultList.eventNumber = atoi(row->GetField(1)); | |
132 | strcpy(resultList.system, row->GetField(2)); | |
133 | resultList.multiplicity = atoi(row->GetField(3)); | |
134 | resultList.marked = atoi(row->GetField(4)); | |
135 | ||
136 | eventsVector.push_back(resultList); | |
137 | delete row; | |
138 | } | |
139 | ||
140 | return eventsVector; | |
141 | } | |
142 | ||
143 | AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event) | |
144 | { | |
145 | cout<<"database - get event"<<endl; | |
146 | string pathToFile = GetFilePath(event); | |
147 | ||
148 | if(!strcmp(pathToFile.c_str(),"")) | |
149 | { | |
150 | cout<<"DATABASE -- no such file in database"<<endl; | |
151 | return NULL; | |
152 | } | |
153 | ||
154 | TFile *tmpFile = new TFile(pathToFile.c_str(),"read"); | |
155 | if(!tmpFile) | |
156 | { | |
157 | cout<<"DATABASE -- couldn't open temp file"<<endl; | |
158 | return NULL; | |
159 | } | |
160 | AliESDEvent *data; | |
161 | tmpFile->GetObject(Form("event%d;1",event.eventNumber),data); | |
162 | ||
163 | return data; | |
5eb34a26 | 164 | } |
165 | ||
166 | struct eventStruct AliStorageDatabase::GetOldestEvent() | |
167 | { | |
168 | struct eventStruct oldestEvent = {0,0}; | |
169 | ||
170 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str())); | |
171 | ||
172 | TSQLRow *row; | |
173 | if((row = result->Next())) | |
174 | { | |
175 | oldestEvent.runNumber = atoi(row->GetField(0)); | |
176 | oldestEvent.eventNumber = atoi(row->GetField(1)); | |
177 | ||
178 | delete row; | |
179 | } | |
180 | else | |
181 | { | |
182 | cout<<"DATABASE -- NO OLDEST EVENT FOUND. Storage may be corrupted."<<endl; | |
183 | } | |
184 | return oldestEvent; | |
185 | } | |
186 | ||
187 | void AliStorageDatabase::RemoveEvent(struct eventStruct event) | |
188 | { | |
189 | fServer->Query(Form("DELETE FROM %s WHERE run_number = %d AND event_number = %d",fTable.c_str(),event.runNumber,event.eventNumber)); | |
190 | } | |
191 | ||
192 | string AliStorageDatabase::GetFilePath(struct eventStruct event) | |
193 | { | |
194 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber)); | |
195 | TSQLRow *row; | |
196 | row = result->Next(); | |
197 | if(row) | |
198 | { | |
199 | string path(row->GetField(5)); | |
200 | delete row; | |
201 | return path; | |
202 | } | |
203 | else | |
204 | { | |
205 | return ""; | |
206 | } | |
207 | } | |
208 | ||
209 | AliESDEvent* AliStorageDatabase::GetNextEvent(struct eventStruct event) | |
210 | { | |
211 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str())); | |
212 | ||
213 | TSQLRow *row; | |
214 | bool isCurrentEvent=false; | |
215 | struct eventStruct nextEvent; | |
216 | ||
217 | while((row = result->Next())) | |
218 | { | |
219 | if(isCurrentEvent) | |
220 | { | |
221 | nextEvent.runNumber = atoi(row->GetField(0)); | |
222 | nextEvent.eventNumber = atoi(row->GetField(1)); | |
223 | return GetEvent(nextEvent); | |
224 | } | |
225 | ||
226 | //if current event found | |
227 | if(atoi(row->GetField(0))==event.runNumber && atoi(row->GetField(1))==event.eventNumber) | |
228 | { | |
229 | isCurrentEvent=true; | |
230 | } | |
231 | ||
232 | delete row; | |
233 | } | |
234 | ||
235 | return NULL; | |
236 | } | |
237 | ||
238 | AliESDEvent* AliStorageDatabase::GetLastEvent() | |
239 | { | |
240 | TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str())); | |
241 | ||
242 | TSQLRow *row; | |
243 | struct eventStruct lastEvent = {0,0}; | |
244 | ||
245 | while((row = result->Next())) | |
246 | { | |
247 | lastEvent.runNumber = atoi(row->GetField(0)); | |
248 | lastEvent.eventNumber = atoi(row->GetField(1)); | |
249 | delete row; | |
250 | } | |
251 | cout<<"Last event is:"<<lastEvent.eventNumber<<endl; | |
252 | return GetEvent(lastEvent); | |
253 | ||
254 | } |