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