]>
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 | |
9e1ceb13 | 414 | if(!((entryId.GetAliCDBRunRange()).IsEqual(& dataId.GetAliCDBRunRange())) || |
415 | (entryId.GetVersion() != dataId.GetVersion()) || (entryId.GetSubVersion() != dataId.GetSubVersion())){ | |
b8ec52f6 | 416 | AliWarning(Form("Mismatch between file name and object's Id!")); |
417 | AliWarning(Form("File name: %s", dataId.ToString().Data())); | |
418 | AliWarning(Form("Object's Id: %s", entryId.ToString().Data())); | |
9e1ceb13 | 419 | } |
b8ec52f6 | 420 | |
9e1ceb13 | 421 | // close file, return retieved entry |
422 | file.Close(); | |
62032124 | 423 | return anEntry; |
9e1ceb13 | 424 | } |
fe913d8f | 425 | |
9e1ceb13 | 426 | //_____________________________________________________________________________ |
427 | void AliCDBLocal::GetEntriesForLevel0(const char* level0, | |
428 | const AliCDBId& queryId, TList* result) { | |
429 | // multiple request (AliCDBStorage::GetAll) | |
430 | ||
c3a7b59a | 431 | TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0); |
432 | ||
9e1ceb13 | 433 | void* level0DirPtr = gSystem->OpenDirectory(level0Dir); |
434 | if (!level0DirPtr) { | |
c3a7b59a | 435 | AliDebug(2,Form("Can't open level0 directory <%s>!", |
9e1ceb13 | 436 | level0Dir.Data())); |
437 | return; | |
c3a7b59a | 438 | } |
9e1ceb13 | 439 | |
440 | const char* level1; | |
441 | while ((level1 = gSystem->GetDirEntry(level0DirPtr))) { | |
442 | ||
443 | TString level1Str(level1); | |
444 | if (level1Str == "." || level1Str == "..") { | |
445 | continue; | |
446 | } | |
447 | ||
448 | if (queryId.GetAliCDBPath().Level1Comprises(level1)) { | |
449 | GetEntriesForLevel1(level0, level1, queryId, result); | |
450 | } | |
451 | } | |
fe913d8f | 452 | |
9e1ceb13 | 453 | gSystem->FreeDirectory(level0DirPtr); |
fe913d8f | 454 | } |
455 | ||
9e1ceb13 | 456 | //_____________________________________________________________________________ |
457 | void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1, | |
458 | const AliCDBId& queryId, TList* result) { | |
459 | // multiple request (AliCDBStorage::GetAll) | |
460 | ||
c3a7b59a | 461 | TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1); |
9e1ceb13 | 462 | |
463 | void* level1DirPtr = gSystem->OpenDirectory(level1Dir); | |
464 | if (!level1DirPtr) { | |
c3a7b59a | 465 | AliDebug(2,Form("Can't open level1 directory <%s>!", |
9e1ceb13 | 466 | level1Dir.Data())); |
467 | return; | |
468 | } | |
fe913d8f | 469 | |
9e1ceb13 | 470 | const char* level2; |
471 | while ((level2 = gSystem->GetDirEntry(level1DirPtr))) { | |
472 | ||
473 | TString level2Str(level2); | |
474 | if (level2Str == "." || level2Str == "..") { | |
475 | continue; | |
476 | } | |
477 | ||
478 | if (queryId.GetAliCDBPath().Level2Comprises(level2)) { | |
62032124 | 479 | |
9e1ceb13 | 480 | AliCDBPath entryPath(level0, level1, level2); |
481 | AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(), | |
482 | queryId.GetVersion(), queryId.GetSubVersion()); | |
483 | ||
484 | AliCDBEntry* anEntry = GetEntry(entryId); | |
485 | if (anEntry) { | |
486 | result->Add(anEntry); | |
487 | } | |
488 | } | |
489 | } | |
fe913d8f | 490 | |
9e1ceb13 | 491 | gSystem->FreeDirectory(level1DirPtr); |
492 | } | |
fe913d8f | 493 | |
9e1ceb13 | 494 | //_____________________________________________________________________________ |
495 | TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) { | |
496 | // multiple request (AliCDBStorage::GetAll) | |
497 | ||
498 | void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory); | |
499 | if (!storageDirPtr) { | |
024cf675 | 500 | AliDebug(2,Form("Can't open storage directory <%s>", |
9e1ceb13 | 501 | fBaseDirectory.Data())); |
502 | return NULL; | |
c3a7b59a | 503 | } |
9e1ceb13 | 504 | |
505 | TList* result = new TList(); | |
506 | result->SetOwner(); | |
507 | ||
508 | const char* level0; | |
509 | while ((level0 = gSystem->GetDirEntry(storageDirPtr))) { | |
c3a7b59a | 510 | |
9e1ceb13 | 511 | TString level0Str(level0); |
512 | if (level0Str == "." || level0Str == "..") { | |
513 | continue; | |
514 | } | |
515 | ||
516 | if (queryId.GetAliCDBPath().Level0Comprises(level0)) { | |
c3a7b59a | 517 | GetEntriesForLevel0(level0, queryId, result); |
9e1ceb13 | 518 | } |
c3a7b59a | 519 | } |
520 | ||
9e1ceb13 | 521 | gSystem->FreeDirectory(storageDirPtr); |
522 | ||
523 | return result; | |
fe913d8f | 524 | } |
525 | ||
526 | //_____________________________________________________________________________ | |
9e1ceb13 | 527 | Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) { |
528 | // put an AliCDBEntry object into the database | |
529 | ||
530 | AliCDBId& id = entry->GetId(); | |
531 | ||
532 | // set version and subVersion for the entry to be stored | |
c3a7b59a | 533 | if (!PrepareId(id)) return kFALSE; |
9e1ceb13 | 534 | |
535 | ||
536 | // build filename from entry's id | |
62032124 | 537 | TString filename=""; |
c3a7b59a | 538 | if (!IdToFilename(id, filename)) { |
9e1ceb13 | 539 | |
024cf675 | 540 | AliDebug(2,Form("Bad ID encountered! Subnormal error!")); |
9e1ceb13 | 541 | return kFALSE; |
542 | } | |
c3a7b59a | 543 | |
9e1ceb13 | 544 | // open file |
545 | TFile file(filename, "CREATE"); | |
546 | if (!file.IsOpen()) { | |
547 | AliError(Form("Can't open file <%s>!", filename.Data())); | |
548 | return kFALSE; | |
549 | } | |
550 | ||
551 | entry->SetVersion(id.GetVersion()); | |
552 | entry->SetSubVersion(id.GetSubVersion()); | |
0f678e35 | 553 | |
9e1ceb13 | 554 | // write object (key name: "AliCDBEntry") |
555 | Bool_t result = file.WriteTObject(entry, "AliCDBEntry"); | |
024cf675 | 556 | if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data())); |
9e1ceb13 | 557 | |
558 | file.Close(); | |
0f678e35 | 559 | if(result) { |
0f678e35 | 560 | if(!(id.GetPath().Contains("SHUTTLE/STATUS"))) |
561 | AliInfo(Form("CDB object stored into file %s",filename.Data())); | |
562 | } | |
b05400be | 563 | |
564 | return result; | |
565 | } | |
566 | ||
567 | //_____________________________________________________________________________ | |
568 | TList* AliCDBLocal::GetIdListFromFile(const char* fileName){ | |
569 | ||
570 | TString fullFileName(fileName); | |
571 | fullFileName.Prepend(fBaseDirectory+'/'); | |
572 | TFile *file = TFile::Open(fullFileName); | |
573 | if (!file) { | |
574 | AliError(Form("Can't open selection file <%s>!", fullFileName.Data())); | |
575 | return NULL; | |
576 | } | |
577 | file->cd(); | |
578 | ||
579 | TList *list = new TList(); | |
580 | list->SetOwner(); | |
581 | int i=0; | |
582 | TString keycycle; | |
583 | ||
584 | AliCDBId *id; | |
585 | while(1){ | |
586 | i++; | |
587 | keycycle = "AliCDBId;"; | |
588 | keycycle+=i; | |
589 | ||
590 | id = (AliCDBId*) file->Get(keycycle); | |
591 | if(!id) break; | |
592 | list->AddFirst(id); | |
593 | } | |
594 | file->Close(); delete file; file=0; | |
595 | return list; | |
596 | } | |
597 | ||
598 | //_____________________________________________________________________________ | |
599 | Bool_t AliCDBLocal::Contains(const char* path) const{ | |
600 | // check for path in storage's fBaseDirectory | |
601 | ||
c3a7b59a | 602 | TString dirName = Form("%s/%s", fBaseDirectory.Data(), path); |
b05400be | 603 | Bool_t result=kFALSE; |
604 | ||
605 | void* dirPtr = gSystem->OpenDirectory(dirName); | |
606 | if (dirPtr) result=kTRUE; | |
607 | gSystem->FreeDirectory(dirPtr); | |
9e1ceb13 | 608 | |
609 | return result; | |
fe913d8f | 610 | } |
611 | ||
62032124 | 612 | //_____________________________________________________________________________ |
613 | void AliCDBLocal::QueryValidFiles() | |
614 | { | |
c3a7b59a | 615 | // Query the CDB for files valid for AliCDBStorage::fRun |
616 | // fills list fValidFileIds with AliCDBId objects created from file name | |
617 | ||
618 | if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!"); | |
619 | if(fMetaDataFilter) { | |
620 | AliWarning ("CDB meta data parameters are not used by local storage query!"); | |
621 | delete fMetaDataFilter; fMetaDataFilter=0; | |
622 | } | |
623 | ||
624 | void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory); | |
625 | ||
626 | const char* level0; | |
627 | while ((level0 = gSystem->GetDirEntry(storageDirPtr))) { | |
628 | ||
629 | TString level0Str(level0); | |
630 | if (level0Str == "." || level0Str == "..") { | |
631 | continue; | |
632 | } | |
633 | ||
634 | if (fPathFilter.Level0Comprises(level0)) { | |
635 | TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0); | |
636 | void* level0DirPtr = gSystem->OpenDirectory(level0Dir); | |
637 | const char* level1; | |
638 | while ((level1 = gSystem->GetDirEntry(level0DirPtr))) { | |
639 | ||
640 | TString level1Str(level1); | |
641 | if (level1Str == "." || level1Str == "..") { | |
642 | continue; | |
643 | } | |
644 | ||
645 | if (fPathFilter.Level1Comprises(level1)) { | |
646 | TString level1Dir = Form("%s/%s/%s", | |
647 | fBaseDirectory.Data(),level0,level1); | |
648 | ||
649 | void* level1DirPtr = gSystem->OpenDirectory(level1Dir); | |
650 | const char* level2; | |
651 | while ((level2 = gSystem->GetDirEntry(level1DirPtr))) { | |
652 | ||
653 | TString level2Str(level2); | |
654 | if (level2Str == "." || level2Str == "..") { | |
655 | continue; | |
656 | } | |
657 | ||
658 | if (fPathFilter.Level2Comprises(level2)) { | |
659 | TString dirName = Form("%s/%s/%s/%s", | |
660 | fBaseDirectory.Data(),level0,level1,level2); | |
661 | ||
662 | void* dirPtr = gSystem->OpenDirectory(dirName); | |
663 | ||
664 | const char* filename; | |
665 | ||
666 | AliCDBRunRange aRunRange; // the runRange got from filename | |
667 | Int_t aVersion, aSubVersion; // the version and subVersion got from filename | |
668 | ||
669 | while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files | |
670 | ||
671 | TString aString(filename); | |
672 | if (aString == "." || aString == "..") continue; | |
673 | ||
674 | if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue; | |
675 | AliCDBRunRange runrg(fRun,fRun); | |
676 | if (!aRunRange.Comprises(runrg)) continue; | |
677 | // aRunRange contains requested run! | |
678 | AliCDBPath validPath(level0,level1,level2); | |
679 | AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion); | |
680 | fValidFileIds.AddLast(validId.Clone()); | |
681 | } | |
682 | gSystem->FreeDirectory(dirPtr); | |
683 | } | |
684 | } | |
685 | gSystem->FreeDirectory(level1DirPtr); | |
686 | } | |
687 | } | |
688 | gSystem->FreeDirectory(level0DirPtr); | |
689 | } | |
690 | } | |
691 | gSystem->FreeDirectory(storageDirPtr); | |
692 | ||
693 | } | |
694 | ||
695 | //_____________________________________________________________________________ | |
696 | Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){ | |
697 | // get last version found in the database valid for run and path | |
698 | ||
699 | AliCDBPath aCDBPath(path); | |
700 | if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) { | |
701 | AliError(Form("Invalid path in request: %s", path)); | |
702 | return -1; | |
703 | } | |
704 | ||
705 | AliCDBId query(path, run, run, -1, -1); | |
706 | AliCDBId dataId; | |
707 | ||
708 | GetId(query,dataId); | |
709 | ||
710 | return dataId.GetVersion(); | |
711 | ||
712 | } | |
713 | ||
714 | //_____________________________________________________________________________ | |
715 | Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){ | |
716 | // get last version found in the database valid for run and path | |
717 | ||
718 | AliCDBPath aCDBPath(path); | |
719 | if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) { | |
720 | AliError(Form("Invalid path in request: %s", path)); | |
721 | return -1; | |
722 | } | |
723 | ||
724 | AliCDBId query(path, run, run, version, -1); | |
725 | AliCDBId dataId; | |
726 | ||
727 | GetId(query,dataId); | |
728 | ||
729 | return dataId.GetSubVersion(); | |
62032124 | 730 | |
731 | } | |
732 | ||
9e1ceb13 | 733 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
734 | // // | |
735 | // AliCDBLocal factory // | |
736 | // // | |
737 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
738 | ||
739 | ClassImp(AliCDBLocalFactory) | |
740 | ||
741 | //_____________________________________________________________________________ | |
742 | Bool_t AliCDBLocalFactory::Validate(const char* dbString) { | |
743 | // check if the string is valid local URI | |
744 | ||
745 | TRegexp dbPattern("^local://.+$"); | |
746 | ||
747 | return TString(dbString).Contains(dbPattern); | |
fe913d8f | 748 | } |
749 | ||
9e1ceb13 | 750 | //_____________________________________________________________________________ |
751 | AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) { | |
752 | // create AliCDBLocalParam class from the URI string | |
753 | ||
754 | if (!Validate(dbString)) { | |
755 | return NULL; | |
756 | } | |
757 | ||
758 | TString pathname(dbString + sizeof("local://") - 1); | |
759 | ||
760 | gSystem->ExpandPathName(pathname); | |
761 | ||
762 | if (pathname[0] != '/') { | |
763 | pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/'); | |
764 | } | |
765 | ||
9e1ceb13 | 766 | return new AliCDBLocalParam(pathname); |
fe913d8f | 767 | } |
768 | ||
9e1ceb13 | 769 | //_____________________________________________________________________________ |
770 | AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) { | |
771 | // create AliCDBLocal storage instance from parameters | |
772 | ||
773 | if (AliCDBLocalParam::Class() == param->IsA()) { | |
774 | ||
775 | const AliCDBLocalParam* localParam = | |
776 | (const AliCDBLocalParam*) param; | |
777 | ||
778 | return new AliCDBLocal(localParam->GetPath()); | |
779 | } | |
fe913d8f | 780 | |
9e1ceb13 | 781 | return NULL; |
fe913d8f | 782 | } |
783 | ||
9e1ceb13 | 784 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
785 | // // | |
786 | // AliCDBLocal Parameter class // // | |
787 | // // | |
788 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
789 | ||
790 | ClassImp(AliCDBLocalParam) | |
791 | ||
fe913d8f | 792 | //_____________________________________________________________________________ |
62032124 | 793 | AliCDBLocalParam::AliCDBLocalParam(): |
794 | AliCDBParam(), | |
795 | fDBPath() | |
796 | { | |
9e1ceb13 | 797 | // default constructor |
fe913d8f | 798 | |
fe913d8f | 799 | } |
800 | ||
9e1ceb13 | 801 | //_____________________________________________________________________________ |
802 | AliCDBLocalParam::AliCDBLocalParam(const char* dbPath): | |
62032124 | 803 | AliCDBParam(), |
804 | fDBPath(dbPath) | |
805 | { | |
9e1ceb13 | 806 | // constructor |
807 | ||
808 | SetType("local"); | |
809 | SetURI(TString("local://") + dbPath); | |
810 | } | |
811 | ||
812 | //_____________________________________________________________________________ | |
813 | AliCDBLocalParam::~AliCDBLocalParam() { | |
814 | // destructor | |
815 | ||
816 | } | |
fe913d8f | 817 | |
818 | //_____________________________________________________________________________ | |
9e1ceb13 | 819 | AliCDBParam* AliCDBLocalParam::CloneParam() const { |
820 | // clone parameter | |
821 | ||
822 | return new AliCDBLocalParam(fDBPath); | |
fe913d8f | 823 | } |
fe913d8f | 824 | |
9e1ceb13 | 825 | //_____________________________________________________________________________ |
826 | ULong_t AliCDBLocalParam::Hash() const { | |
827 | // return Hash function | |
828 | ||
829 | return fDBPath.Hash(); | |
fe913d8f | 830 | } |
9e1ceb13 | 831 | |
832 | //_____________________________________________________________________________ | |
833 | Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const { | |
834 | // check if this object is equal to AliCDBParam obj | |
835 | ||
836 | if (this == obj) { | |
837 | return kTRUE; | |
838 | } | |
839 | ||
840 | if (AliCDBLocalParam::Class() != obj->IsA()) { | |
841 | return kFALSE; | |
842 | } | |
843 | ||
844 | AliCDBLocalParam* other = (AliCDBLocalParam*) obj; | |
845 | ||
846 | return fDBPath == other->fDBPath; | |
847 | } | |
848 |