]>
Commit | Line | Data |
---|---|---|
fe913d8f | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
7 | * Permission to use, copy, modify and distribute this software and its * | |
8 | * documentation strictly for non-commercial purposes is hereby granted * | |
9 | * without fee, provided that the above copyright notice appears in all * | |
10 | * copies and that both the copyright notice and this permission notice * | |
11 | * appear in the supporting documentation. The authors make no claims * | |
12 | * about the suitability of this software for any purpose. It is * | |
13 | * provided "as is" without express or implied warranty. * | |
14 | **************************************************************************/ | |
15 | ||
fe913d8f | 16 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
17 | // // | |
9e1ceb13 | 18 | // AliCDBLocal // |
19 | // access class to a DataBase in a local storage // | |
fe913d8f | 20 | // // |
21 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
22 | ||
fe913d8f | 23 | #include <TSystem.h> |
fe913d8f | 24 | #include <TObjString.h> |
25 | #include <TRegexp.h> | |
9e1ceb13 | 26 | #include <TFile.h> |
27 | #include <TKey.h> | |
fe913d8f | 28 | |
fe913d8f | 29 | #include "AliCDBLocal.h" |
9e1ceb13 | 30 | #include "AliCDBEntry.h" |
31 | #include "AliLog.h" | |
fe913d8f | 32 | |
33 | ClassImp(AliCDBLocal) | |
34 | ||
35 | //_____________________________________________________________________________ | |
9e1ceb13 | 36 | AliCDBLocal::AliCDBLocal(const char* baseDir): |
37 | fBaseDirectory(baseDir) | |
fe913d8f | 38 | { |
39 | // constructor | |
9e1ceb13 | 40 | |
41 | // check baseDire: trying to cd to baseDir; if it does not exist, create it | |
42 | void* dir = gSystem->OpenDirectory(baseDir); | |
43 | if (dir == NULL) { | |
44 | if (gSystem->mkdir(baseDir, kTRUE)) { | |
45 | AliError(Form("Can't open directory <%s>!", baseDir)); | |
46 | } | |
47 | ||
48 | } else { | |
49 | AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data())); | |
50 | gSystem->FreeDirectory(dir); | |
51 | } | |
62032124 | 52 | fType="local"; |
53 | fBaseFolder = fBaseDirectory; | |
fe913d8f | 54 | } |
55 | ||
56 | //_____________________________________________________________________________ | |
9e1ceb13 | 57 | AliCDBLocal::~AliCDBLocal() { |
58 | // destructor | |
fe913d8f | 59 | |
60 | } | |
61 | ||
9e1ceb13 | 62 | |
fe913d8f | 63 | //_____________________________________________________________________________ |
9e1ceb13 | 64 | Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange, |
4b5e0dce | 65 | Int_t& version, Int_t& subVersion) { |
9e1ceb13 | 66 | // build AliCDBId from filename numbers |
67 | ||
9e1ceb13 | 68 | |
69 | Ssiz_t mSize; | |
fe913d8f | 70 | |
9e1ceb13 | 71 | // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root |
72 | TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$"); | |
73 | keyPattern.Index(filename, &mSize); | |
74 | if (!mSize) { | |
75 | AliDebug(2, Form("Bad filename <%s>.", filename)); | |
76 | return kFALSE; | |
77 | } | |
78 | ||
79 | TString idString(filename); | |
80 | idString.Resize(idString.Length() - sizeof(".root") + 1); | |
81 | ||
82 | TObjArray* strArray = (TObjArray*) idString.Tokenize("_"); | |
83 | ||
84 | TString firstRunString(((TObjString*) strArray->At(0))->GetString()); | |
85 | runRange.SetFirstRun(atoi(firstRunString.Data() + 3)); | |
86 | runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString())); | |
87 | ||
88 | TString verString(((TObjString*) strArray->At(2))->GetString()); | |
89 | version = atoi(verString.Data() + 1); | |
90 | ||
91 | TString subVerString(((TObjString*) strArray->At(3))->GetString()); | |
92 | subVersion = atoi(subVerString.Data() + 1); | |
93 | ||
94 | delete strArray; | |
95 | ||
96 | return kTRUE; | |
fe913d8f | 97 | } |
98 | ||
9e1ceb13 | 99 | |
fe913d8f | 100 | //_____________________________________________________________________________ |
c3a7b59a | 101 | Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const { |
9e1ceb13 | 102 | // build file name from AliCDBId data (run range, version, subVersion) |
103 | ||
c3a7b59a | 104 | if (!id.GetAliCDBRunRange().IsValid()) { |
b05400be | 105 | AliDebug(2,Form("Invalid run range <%d, %d>.", |
c3a7b59a | 106 | id.GetFirstRun(), id.GetLastRun())); |
9e1ceb13 | 107 | return kFALSE; |
108 | } | |
109 | ||
c3a7b59a | 110 | if (id.GetVersion() < 0) { |
111 | AliDebug(2,Form("Invalid version <%d>.", id.GetVersion())); | |
9e1ceb13 | 112 | return kFALSE; |
113 | } | |
fe913d8f | 114 | |
c3a7b59a | 115 | if (id.GetSubVersion() < 0) { |
116 | AliDebug(2,Form("Invalid subversion <%s>.", id.GetSubVersion())); | |
9e1ceb13 | 117 | return kFALSE; |
118 | } | |
119 | ||
c3a7b59a | 120 | filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(), |
121 | id.GetVersion(), id.GetSubVersion()); | |
122 | ||
123 | filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/'); | |
9e1ceb13 | 124 | |
125 | return kTRUE; | |
fe913d8f | 126 | } |
127 | ||
128 | //_____________________________________________________________________________ | |
9e1ceb13 | 129 | Bool_t AliCDBLocal::PrepareId(AliCDBId& id) { |
130 | // prepare id (version, subVersion) of the object that will be stored (called by PutEntry) | |
131 | ||
c3a7b59a | 132 | TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data()); |
9e1ceb13 | 133 | |
134 | // go to the path; if directory does not exist, create it | |
135 | void* dirPtr = gSystem->OpenDirectory(dirName); | |
136 | if (!dirPtr) { | |
137 | gSystem->mkdir(dirName, kTRUE); | |
138 | dirPtr = gSystem->OpenDirectory(dirName); | |
139 | ||
140 | if (!dirPtr) { | |
141 | AliError(Form("Can't create directory <%s>!", | |
142 | dirName.Data())); | |
143 | return kFALSE; | |
144 | } | |
145 | } | |
fe913d8f | 146 | |
9e1ceb13 | 147 | const char* filename; |
148 | AliCDBRunRange aRunRange; // the runRange got from filename | |
149 | AliCDBRunRange lastRunRange(-1,-1); // highest runRange found | |
150 | Int_t aVersion, aSubVersion; // the version subVersion got from filename | |
151 | Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found | |
152 | ||
153 | if (!id.HasVersion()) { // version not specified: look for highest version & subVersion | |
154 | ||
155 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files | |
156 | ||
157 | TString aString(filename); | |
158 | if (aString == "." || aString == "..") continue; | |
159 | ||
160 | if (!FilenameToId(filename, aRunRange, aVersion, | |
161 | aSubVersion)) { | |
b05400be | 162 | AliDebug(2,Form( |
9e1ceb13 | 163 | "Bad filename <%s>! I'll skip it.", |
164 | filename)); | |
165 | continue; | |
166 | } | |
167 | ||
168 | if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue; | |
169 | if(aVersion < lastVersion) continue; | |
170 | if(aVersion > lastVersion) lastSubVersion = -1; | |
171 | if(aSubVersion < lastSubVersion) continue; | |
172 | lastVersion = aVersion; | |
173 | lastSubVersion = aSubVersion; | |
174 | lastRunRange = aRunRange; | |
175 | } | |
176 | ||
177 | id.SetVersion(lastVersion); | |
178 | id.SetSubVersion(lastSubVersion + 1); | |
179 | ||
180 | } else { // version specified, look for highest subVersion only | |
181 | ||
182 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files | |
183 | ||
184 | TString aString(filename); | |
185 | if (aString == "." || aString == "..") { | |
186 | continue; | |
187 | } | |
188 | ||
189 | if (!FilenameToId(filename, aRunRange, aVersion, | |
190 | aSubVersion)) { | |
b05400be | 191 | AliDebug(2,Form( |
9e1ceb13 | 192 | "Bad filename <%s>!I'll skip it.", |
193 | filename)); | |
194 | continue; | |
195 | } | |
196 | ||
197 | if (aRunRange.Overlaps(id.GetAliCDBRunRange()) | |
198 | && aVersion == id.GetVersion() | |
199 | && aSubVersion > lastSubVersion) { | |
200 | lastSubVersion = aSubVersion; | |
201 | lastRunRange = aRunRange; | |
202 | } | |
203 | ||
204 | } | |
205 | ||
206 | id.SetSubVersion(lastSubVersion + 1); | |
207 | } | |
fe913d8f | 208 | |
9e1ceb13 | 209 | gSystem->FreeDirectory(dirPtr); |
fe913d8f | 210 | |
9e1ceb13 | 211 | TString lastStorage = id.GetLastStorage(); |
212 | if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) && | |
213 | id.GetSubVersion() > 0 ){ | |
214 | AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1)); | |
215 | AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion())); | |
216 | return kFALSE; | |
217 | } | |
218 | ||
219 | if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && | |
220 | id.GetSubVersion() > 0 ){ | |
84090f85 | 221 | AliDebug(2, Form("A NEW object is being stored with version v%d_s%d", |
9e1ceb13 | 222 | id.GetVersion(),id.GetSubVersion())); |
84090f85 | 223 | AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!", |
9e1ceb13 | 224 | id.GetVersion(),id.GetSubVersion()-1)); |
225 | } | |
226 | ||
227 | if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange()))) | |
228 | AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)", | |
229 | lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), | |
230 | id.GetVersion(), id.GetSubVersion()-1)); | |
231 | ||
232 | return kTRUE; | |
fe913d8f | 233 | } |
234 | ||
9e1ceb13 | 235 | //_____________________________________________________________________________ |
4b5e0dce | 236 | Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) { |
9e1ceb13 | 237 | // look for filename matching query (called by GetEntry) |
238 | ||
c3a7b59a | 239 | TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data()); |
9e1ceb13 | 240 | |
c3a7b59a | 241 | void* dirPtr = gSystem->OpenDirectory(dirName); |
9e1ceb13 | 242 | if (!dirPtr) { |
024cf675 | 243 | AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data())); |
244 | AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data())); | |
4b5e0dce | 245 | return kFALSE; |
9e1ceb13 | 246 | } |
247 | ||
c3a7b59a | 248 | const char* filename; |
249 | ||
9e1ceb13 | 250 | AliCDBRunRange aRunRange; // the runRange got from filename |
251 | Int_t aVersion, aSubVersion; // the version and subVersion got from filename | |
252 | ||
253 | if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion | |
c3a7b59a | 254 | |
9e1ceb13 | 255 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files |
c3a7b59a | 256 | |
9e1ceb13 | 257 | TString aString(filename); |
258 | if (aString == "." || aString == "..") continue; | |
259 | ||
260 | if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue; | |
261 | // aRunRange, aVersion, aSubVersion filled from filename | |
262 | ||
263 | if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; | |
264 | // aRunRange contains requested run! | |
c3a7b59a | 265 | |
9e1ceb13 | 266 | if (result.GetVersion() < aVersion) { |
267 | result.SetVersion(aVersion); | |
268 | result.SetSubVersion(aSubVersion); | |
269 | ||
270 | result.SetFirstRun( | |
271 | aRunRange.GetFirstRun()); | |
272 | result.SetLastRun( | |
273 | aRunRange.GetLastRun()); | |
274 | ||
275 | } else if (result.GetVersion() == aVersion | |
276 | && result.GetSubVersion() | |
277 | < aSubVersion) { | |
278 | ||
279 | result.SetSubVersion(aSubVersion); | |
280 | ||
281 | result.SetFirstRun( | |
282 | aRunRange.GetFirstRun()); | |
283 | result.SetLastRun( | |
284 | aRunRange.GetLastRun()); | |
285 | } else if (result.GetVersion() == aVersion | |
286 | && result.GetSubVersion() == aSubVersion){ | |
024cf675 | 287 | AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", |
9e1ceb13 | 288 | query.GetFirstRun(), aVersion, aSubVersion)); |
4b5e0dce | 289 | gSystem->FreeDirectory(dirPtr); |
290 | return kFALSE; | |
9e1ceb13 | 291 | } |
292 | } | |
293 | ||
294 | } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion | |
295 | ||
296 | result.SetVersion(query.GetVersion()); | |
297 | ||
298 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files | |
299 | ||
300 | TString aString(filename); | |
301 | if (aString == "." || aString == "..") continue; | |
302 | ||
303 | if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue; | |
304 | // aRunRange, aVersion, aSubVersion filled from filename | |
305 | ||
306 | if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; | |
307 | // aRunRange contains requested run! | |
308 | ||
309 | if(query.GetVersion() != aVersion) continue; | |
310 | // aVersion is requested version! | |
311 | ||
312 | if(result.GetSubVersion() == aSubVersion){ | |
024cf675 | 313 | AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", |
9e1ceb13 | 314 | query.GetFirstRun(), aVersion, aSubVersion)); |
4b5e0dce | 315 | gSystem->FreeDirectory(dirPtr); |
316 | return kFALSE; | |
9e1ceb13 | 317 | } |
318 | if( result.GetSubVersion() < aSubVersion) { | |
319 | ||
320 | result.SetSubVersion(aSubVersion); | |
321 | ||
322 | result.SetFirstRun( | |
323 | aRunRange.GetFirstRun()); | |
324 | result.SetLastRun( | |
325 | aRunRange.GetLastRun()); | |
326 | } | |
327 | } | |
328 | ||
329 | } else { // both version and subversion specified | |
330 | ||
331 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files | |
332 | ||
333 | TString aString(filename); | |
334 | if (aString == "." || aString == "..") continue; | |
335 | ||
336 | if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue; | |
337 | // aRunRange, aVersion, aSubVersion filled from filename | |
338 | ||
339 | if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; | |
340 | // aRunRange contains requested run! | |
341 | ||
342 | if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue; | |
343 | // aVersion and aSubVersion are requested version and subVersion! | |
344 | ||
345 | if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){ | |
024cf675 | 346 | AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", |
9e1ceb13 | 347 | query.GetFirstRun(), aVersion, aSubVersion)); |
4b5e0dce | 348 | gSystem->FreeDirectory(dirPtr); |
349 | return kFALSE; | |
9e1ceb13 | 350 | } |
351 | result.SetVersion(aVersion); | |
352 | result.SetSubVersion(aSubVersion); | |
353 | result.SetFirstRun(aRunRange.GetFirstRun()); | |
354 | result.SetLastRun(aRunRange.GetLastRun()); | |
355 | ||
356 | } | |
357 | } | |
358 | ||
359 | gSystem->FreeDirectory(dirPtr); | |
360 | ||
4b5e0dce | 361 | return kTRUE; |
9e1ceb13 | 362 | } |
fe913d8f | 363 | |
364 | //_____________________________________________________________________________ | |
9e1ceb13 | 365 | AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) { |
366 | // get AliCDBEntry from the database | |
367 | ||
4b5e0dce | 368 | AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1); |
369 | Bool_t result; | |
c3a7b59a | 370 | |
9e1ceb13 | 371 | // look for a filename matching query requests (path, runRange, version, subVersion) |
372 | if (!queryId.HasVersion()) { | |
373 | // if version is not specified, first check the selection criteria list | |
4b5e0dce | 374 | AliCDBId selectedId(queryId); |
375 | GetSelection(&selectedId); | |
376 | result = GetId(selectedId, dataId); | |
9e1ceb13 | 377 | } else { |
4b5e0dce | 378 | result = GetId(queryId, dataId); |
fe913d8f | 379 | } |
9e1ceb13 | 380 | |
4b5e0dce | 381 | if (!result || !dataId.IsSpecified()) return NULL; |
9e1ceb13 | 382 | |
383 | TString filename; | |
c3a7b59a | 384 | if (!IdToFilename(dataId, filename)) { |
9e1ceb13 | 385 | |
024cf675 | 386 | AliDebug(2,Form("Bad data ID encountered! Subnormal error!")); |
9e1ceb13 | 387 | return NULL; |
388 | } | |
9e1ceb13 | 389 | |
390 | TFile file(filename, "READ"); // open file | |
391 | if (!file.IsOpen()) { | |
024cf675 | 392 | AliDebug(2,Form("Can't open file <%s>!", filename.Data())); |
9e1ceb13 | 393 | return NULL; |
394 | } | |
395 | ||
396 | // get the only AliCDBEntry object from the file | |
397 | // the object in the file is an AliCDBEntry entry named "AliCDBEntry" | |
398 | ||
62032124 | 399 | AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry")); |
400 | if (!anEntry) { | |
401 | AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!")); | |
402 | file.Close(); | |
9e1ceb13 | 403 | return NULL; |
404 | } | |
405 | ||
62032124 | 406 | AliCDBId entryId = anEntry->GetId(); |
fe913d8f | 407 | |
9e1ceb13 | 408 | // The object's Id are not reset during storage |
409 | // If object's Id runRange or version do not match with filename, | |
410 | // it means that someone renamed file by hand. In this case a warning msg is issued. | |
62032124 | 411 | |
412 | anEntry-> SetLastStorage("local"); | |
fe913d8f | 413 | |
8e245d15 | 414 | if(!entryId.IsEqual(&dataId)){ |
b8ec52f6 | 415 | AliWarning(Form("Mismatch between file name and object's Id!")); |
416 | AliWarning(Form("File name: %s", dataId.ToString().Data())); | |
417 | AliWarning(Form("Object's Id: %s", entryId.ToString().Data())); | |
9e1ceb13 | 418 | } |
b8ec52f6 | 419 | |
9e1ceb13 | 420 | // close file, return retieved entry |
421 | file.Close(); | |
62032124 | 422 | return anEntry; |
9e1ceb13 | 423 | } |
fe913d8f | 424 | |
9e1ceb13 | 425 | //_____________________________________________________________________________ |
426 | void AliCDBLocal::GetEntriesForLevel0(const char* level0, | |
427 | const AliCDBId& queryId, TList* result) { | |
428 | // multiple request (AliCDBStorage::GetAll) | |
429 | ||
c3a7b59a | 430 | TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0); |
431 | ||
9e1ceb13 | 432 | void* level0DirPtr = gSystem->OpenDirectory(level0Dir); |
433 | if (!level0DirPtr) { | |
c3a7b59a | 434 | AliDebug(2,Form("Can't open level0 directory <%s>!", |
9e1ceb13 | 435 | level0Dir.Data())); |
436 | return; | |
c3a7b59a | 437 | } |
9e1ceb13 | 438 | |
439 | const char* level1; | |
440 | while ((level1 = gSystem->GetDirEntry(level0DirPtr))) { | |
441 | ||
442 | TString level1Str(level1); | |
443 | if (level1Str == "." || level1Str == "..") { | |
444 | continue; | |
445 | } | |
446 | ||
447 | if (queryId.GetAliCDBPath().Level1Comprises(level1)) { | |
448 | GetEntriesForLevel1(level0, level1, queryId, result); | |
449 | } | |
450 | } | |
fe913d8f | 451 | |
9e1ceb13 | 452 | gSystem->FreeDirectory(level0DirPtr); |
fe913d8f | 453 | } |
454 | ||
9e1ceb13 | 455 | //_____________________________________________________________________________ |
456 | void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1, | |
457 | const AliCDBId& queryId, TList* result) { | |
458 | // multiple request (AliCDBStorage::GetAll) | |
459 | ||
c3a7b59a | 460 | TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1); |
9e1ceb13 | 461 | |
462 | void* level1DirPtr = gSystem->OpenDirectory(level1Dir); | |
463 | if (!level1DirPtr) { | |
c3a7b59a | 464 | AliDebug(2,Form("Can't open level1 directory <%s>!", |
9e1ceb13 | 465 | level1Dir.Data())); |
466 | return; | |
467 | } | |
fe913d8f | 468 | |
9e1ceb13 | 469 | const char* level2; |
470 | while ((level2 = gSystem->GetDirEntry(level1DirPtr))) { | |
471 | ||
472 | TString level2Str(level2); | |
473 | if (level2Str == "." || level2Str == "..") { | |
474 | continue; | |
475 | } | |
476 | ||
477 | if (queryId.GetAliCDBPath().Level2Comprises(level2)) { | |
62032124 | 478 | |
9e1ceb13 | 479 | AliCDBPath entryPath(level0, level1, level2); |
480 | AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(), | |
481 | queryId.GetVersion(), queryId.GetSubVersion()); | |
482 | ||
483 | AliCDBEntry* anEntry = GetEntry(entryId); | |
484 | if (anEntry) { | |
485 | result->Add(anEntry); | |
486 | } | |
487 | } | |
488 | } | |
fe913d8f | 489 | |
9e1ceb13 | 490 | gSystem->FreeDirectory(level1DirPtr); |
491 | } | |
fe913d8f | 492 | |
9e1ceb13 | 493 | //_____________________________________________________________________________ |
494 | TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) { | |
495 | // multiple request (AliCDBStorage::GetAll) | |
496 | ||
497 | void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory); | |
498 | if (!storageDirPtr) { | |
024cf675 | 499 | AliDebug(2,Form("Can't open storage directory <%s>", |
9e1ceb13 | 500 | fBaseDirectory.Data())); |
501 | return NULL; | |
c3a7b59a | 502 | } |
9e1ceb13 | 503 | |
504 | TList* result = new TList(); | |
505 | result->SetOwner(); | |
506 | ||
507 | const char* level0; | |
508 | while ((level0 = gSystem->GetDirEntry(storageDirPtr))) { | |
c3a7b59a | 509 | |
9e1ceb13 | 510 | TString level0Str(level0); |
511 | if (level0Str == "." || level0Str == "..") { | |
512 | continue; | |
513 | } | |
514 | ||
515 | if (queryId.GetAliCDBPath().Level0Comprises(level0)) { | |
c3a7b59a | 516 | GetEntriesForLevel0(level0, queryId, result); |
9e1ceb13 | 517 | } |
c3a7b59a | 518 | } |
519 | ||
9e1ceb13 | 520 | gSystem->FreeDirectory(storageDirPtr); |
521 | ||
522 | return result; | |
fe913d8f | 523 | } |
524 | ||
525 | //_____________________________________________________________________________ | |
9e1ceb13 | 526 | Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) { |
527 | // put an AliCDBEntry object into the database | |
528 | ||
529 | AliCDBId& id = entry->GetId(); | |
530 | ||
531 | // set version and subVersion for the entry to be stored | |
c3a7b59a | 532 | if (!PrepareId(id)) return kFALSE; |
9e1ceb13 | 533 | |
534 | ||
535 | // build filename from entry's id | |
62032124 | 536 | TString filename=""; |
c3a7b59a | 537 | if (!IdToFilename(id, filename)) { |
9e1ceb13 | 538 | |
024cf675 | 539 | AliDebug(2,Form("Bad ID encountered! Subnormal error!")); |
9e1ceb13 | 540 | return kFALSE; |
541 | } | |
c3a7b59a | 542 | |
9e1ceb13 | 543 | // open file |
544 | TFile file(filename, "CREATE"); | |
545 | if (!file.IsOpen()) { | |
546 | AliError(Form("Can't open file <%s>!", filename.Data())); | |
547 | return kFALSE; | |
548 | } | |
549 | ||
550 | entry->SetVersion(id.GetVersion()); | |
551 | entry->SetSubVersion(id.GetSubVersion()); | |
0f678e35 | 552 | |
9e1ceb13 | 553 | // write object (key name: "AliCDBEntry") |
554 | Bool_t result = file.WriteTObject(entry, "AliCDBEntry"); | |
024cf675 | 555 | if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data())); |
9e1ceb13 | 556 | |
557 | file.Close(); | |
0f678e35 | 558 | if(result) { |
0f678e35 | 559 | if(!(id.GetPath().Contains("SHUTTLE/STATUS"))) |
560 | AliInfo(Form("CDB object stored into file %s",filename.Data())); | |
561 | } | |
b05400be | 562 | |
563 | return result; | |
564 | } | |
565 | ||
566 | //_____________________________________________________________________________ | |
567 | TList* AliCDBLocal::GetIdListFromFile(const char* fileName){ | |
568 | ||
569 | TString fullFileName(fileName); | |
570 | fullFileName.Prepend(fBaseDirectory+'/'); | |
571 | TFile *file = TFile::Open(fullFileName); | |
572 | if (!file) { | |
573 | AliError(Form("Can't open selection file <%s>!", fullFileName.Data())); | |
574 | return NULL; | |
575 | } | |
576 | file->cd(); | |
577 | ||
578 | TList *list = new TList(); | |
579 | list->SetOwner(); | |
580 | int i=0; | |
581 | TString keycycle; | |
582 | ||
583 | AliCDBId *id; | |
584 | while(1){ | |
585 | i++; | |
586 | keycycle = "AliCDBId;"; | |
587 | keycycle+=i; | |
588 | ||
589 | id = (AliCDBId*) file->Get(keycycle); | |
590 | if(!id) break; | |
591 | list->AddFirst(id); | |
592 | } | |
593 | file->Close(); delete file; file=0; | |
594 | return list; | |
595 | } | |
596 | ||
597 | //_____________________________________________________________________________ | |
598 | Bool_t AliCDBLocal::Contains(const char* path) const{ | |
599 | // check for path in storage's fBaseDirectory | |
600 | ||
c3a7b59a | 601 | TString dirName = Form("%s/%s", fBaseDirectory.Data(), path); |
b05400be | 602 | Bool_t result=kFALSE; |
603 | ||
604 | void* dirPtr = gSystem->OpenDirectory(dirName); | |
605 | if (dirPtr) result=kTRUE; | |
606 | gSystem->FreeDirectory(dirPtr); | |
9e1ceb13 | 607 | |
608 | return result; | |
fe913d8f | 609 | } |
610 | ||
62032124 | 611 | //_____________________________________________________________________________ |
612 | void AliCDBLocal::QueryValidFiles() | |
613 | { | |
c3a7b59a | 614 | // Query the CDB for files valid for AliCDBStorage::fRun |
615 | // fills list fValidFileIds with AliCDBId objects created from file name | |
616 | ||
617 | if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!"); | |
618 | if(fMetaDataFilter) { | |
619 | AliWarning ("CDB meta data parameters are not used by local storage query!"); | |
620 | delete fMetaDataFilter; fMetaDataFilter=0; | |
621 | } | |
622 | ||
623 | void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory); | |
624 | ||
625 | const char* level0; | |
626 | while ((level0 = gSystem->GetDirEntry(storageDirPtr))) { | |
627 | ||
628 | TString level0Str(level0); | |
629 | if (level0Str == "." || level0Str == "..") { | |
630 | continue; | |
631 | } | |
632 | ||
633 | if (fPathFilter.Level0Comprises(level0)) { | |
634 | TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0); | |
635 | void* level0DirPtr = gSystem->OpenDirectory(level0Dir); | |
636 | const char* level1; | |
637 | while ((level1 = gSystem->GetDirEntry(level0DirPtr))) { | |
638 | ||
639 | TString level1Str(level1); | |
640 | if (level1Str == "." || level1Str == "..") { | |
641 | continue; | |
642 | } | |
643 | ||
644 | if (fPathFilter.Level1Comprises(level1)) { | |
645 | TString level1Dir = Form("%s/%s/%s", | |
646 | fBaseDirectory.Data(),level0,level1); | |
647 | ||
648 | void* level1DirPtr = gSystem->OpenDirectory(level1Dir); | |
649 | const char* level2; | |
650 | while ((level2 = gSystem->GetDirEntry(level1DirPtr))) { | |
651 | ||
652 | TString level2Str(level2); | |
653 | if (level2Str == "." || level2Str == "..") { | |
654 | continue; | |
655 | } | |
656 | ||
657 | if (fPathFilter.Level2Comprises(level2)) { | |
658 | TString dirName = Form("%s/%s/%s/%s", | |
659 | fBaseDirectory.Data(),level0,level1,level2); | |
660 | ||
661 | void* dirPtr = gSystem->OpenDirectory(dirName); | |
662 | ||
663 | const char* filename; | |
664 | ||
665 | AliCDBRunRange aRunRange; // the runRange got from filename | |
666 | Int_t aVersion, aSubVersion; // the version and subVersion got from filename | |
667 | ||
668 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files | |
669 | ||
670 | TString aString(filename); | |
671 | if (aString == "." || aString == "..") continue; | |
672 | ||
673 | if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue; | |
674 | AliCDBRunRange runrg(fRun,fRun); | |
675 | if (!aRunRange.Comprises(runrg)) continue; | |
676 | // aRunRange contains requested run! | |
677 | AliCDBPath validPath(level0,level1,level2); | |
678 | AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion); | |
679 | fValidFileIds.AddLast(validId.Clone()); | |
680 | } | |
681 | gSystem->FreeDirectory(dirPtr); | |
682 | } | |
683 | } | |
684 | gSystem->FreeDirectory(level1DirPtr); | |
685 | } | |
686 | } | |
687 | gSystem->FreeDirectory(level0DirPtr); | |
688 | } | |
689 | } | |
690 | gSystem->FreeDirectory(storageDirPtr); | |
691 | ||
692 | } | |
693 | ||
694 | //_____________________________________________________________________________ | |
695 | Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){ | |
696 | // get last version found in the database valid for run and path | |
697 | ||
698 | AliCDBPath aCDBPath(path); | |
699 | if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) { | |
700 | AliError(Form("Invalid path in request: %s", path)); | |
701 | return -1; | |
702 | } | |
703 | ||
704 | AliCDBId query(path, run, run, -1, -1); | |
705 | AliCDBId dataId; | |
706 | ||
707 | GetId(query,dataId); | |
708 | ||
709 | return dataId.GetVersion(); | |
710 | ||
711 | } | |
712 | ||
713 | //_____________________________________________________________________________ | |
714 | Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){ | |
715 | // get last version found in the database valid for run and path | |
716 | ||
717 | AliCDBPath aCDBPath(path); | |
718 | if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) { | |
719 | AliError(Form("Invalid path in request: %s", path)); | |
720 | return -1; | |
721 | } | |
722 | ||
723 | AliCDBId query(path, run, run, version, -1); | |
724 | AliCDBId dataId; | |
725 | ||
726 | GetId(query,dataId); | |
727 | ||
728 | return dataId.GetSubVersion(); | |
62032124 | 729 | |
730 | } | |
731 | ||
9e1ceb13 | 732 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
733 | // // | |
734 | // AliCDBLocal factory // | |
735 | // // | |
736 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
737 | ||
738 | ClassImp(AliCDBLocalFactory) | |
739 | ||
740 | //_____________________________________________________________________________ | |
741 | Bool_t AliCDBLocalFactory::Validate(const char* dbString) { | |
742 | // check if the string is valid local URI | |
743 | ||
744 | TRegexp dbPattern("^local://.+$"); | |
745 | ||
746 | return TString(dbString).Contains(dbPattern); | |
fe913d8f | 747 | } |
748 | ||
9e1ceb13 | 749 | //_____________________________________________________________________________ |
750 | AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) { | |
751 | // create AliCDBLocalParam class from the URI string | |
752 | ||
753 | if (!Validate(dbString)) { | |
754 | return NULL; | |
755 | } | |
756 | ||
757 | TString pathname(dbString + sizeof("local://") - 1); | |
758 | ||
759 | gSystem->ExpandPathName(pathname); | |
760 | ||
761 | if (pathname[0] != '/') { | |
762 | pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/'); | |
763 | } | |
764 | ||
9e1ceb13 | 765 | return new AliCDBLocalParam(pathname); |
fe913d8f | 766 | } |
767 | ||
9e1ceb13 | 768 | //_____________________________________________________________________________ |
769 | AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) { | |
770 | // create AliCDBLocal storage instance from parameters | |
771 | ||
772 | if (AliCDBLocalParam::Class() == param->IsA()) { | |
773 | ||
774 | const AliCDBLocalParam* localParam = | |
775 | (const AliCDBLocalParam*) param; | |
776 | ||
777 | return new AliCDBLocal(localParam->GetPath()); | |
778 | } | |
fe913d8f | 779 | |
9e1ceb13 | 780 | return NULL; |
fe913d8f | 781 | } |
782 | ||
9e1ceb13 | 783 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
784 | // // | |
785 | // AliCDBLocal Parameter class // // | |
786 | // // | |
787 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
788 | ||
789 | ClassImp(AliCDBLocalParam) | |
790 | ||
fe913d8f | 791 | //_____________________________________________________________________________ |
62032124 | 792 | AliCDBLocalParam::AliCDBLocalParam(): |
793 | AliCDBParam(), | |
794 | fDBPath() | |
795 | { | |
9e1ceb13 | 796 | // default constructor |
fe913d8f | 797 | |
fe913d8f | 798 | } |
799 | ||
9e1ceb13 | 800 | //_____________________________________________________________________________ |
801 | AliCDBLocalParam::AliCDBLocalParam(const char* dbPath): | |
62032124 | 802 | AliCDBParam(), |
803 | fDBPath(dbPath) | |
804 | { | |
9e1ceb13 | 805 | // constructor |
806 | ||
807 | SetType("local"); | |
808 | SetURI(TString("local://") + dbPath); | |
809 | } | |
810 | ||
811 | //_____________________________________________________________________________ | |
812 | AliCDBLocalParam::~AliCDBLocalParam() { | |
813 | // destructor | |
814 | ||
815 | } | |
fe913d8f | 816 | |
817 | //_____________________________________________________________________________ | |
9e1ceb13 | 818 | AliCDBParam* AliCDBLocalParam::CloneParam() const { |
819 | // clone parameter | |
820 | ||
821 | return new AliCDBLocalParam(fDBPath); | |
fe913d8f | 822 | } |
fe913d8f | 823 | |
9e1ceb13 | 824 | //_____________________________________________________________________________ |
825 | ULong_t AliCDBLocalParam::Hash() const { | |
826 | // return Hash function | |
827 | ||
828 | return fDBPath.Hash(); | |
fe913d8f | 829 | } |
9e1ceb13 | 830 | |
831 | //_____________________________________________________________________________ | |
832 | Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const { | |
833 | // check if this object is equal to AliCDBParam obj | |
834 | ||
835 | if (this == obj) { | |
836 | return kTRUE; | |
837 | } | |
838 | ||
839 | if (AliCDBLocalParam::Class() != obj->IsA()) { | |
840 | return kFALSE; | |
841 | } | |
842 | ||
843 | AliCDBLocalParam* other = (AliCDBLocalParam*) obj; | |
844 | ||
845 | return fDBPath == other->fDBPath; | |
846 | } | |
847 |