]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBLocal.cxx
Fixing a memory leak.
[u/mrichter/AliRoot.git] / STEER / AliCDBLocal.cxx
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
16 /////////////////////////////////////////////////////////////////////////////////////////////////
17 //                                                                                             //
18 // AliCDBLocal                                                                                 //
19 // access class to a DataBase in a local storage                                               //
20 //                                                                                             //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
22
23 #include <TSystem.h>
24 #include <TObjString.h>
25 #include <TRegexp.h>
26 #include <TFile.h>
27 #include <TKey.h>
28
29 #include "AliCDBLocal.h"
30 #include "AliCDBEntry.h"
31 #include "AliLog.h"
32
33 ClassImp(AliCDBLocal)
34
35 //_____________________________________________________________________________
36 AliCDBLocal::AliCDBLocal(const char* baseDir):
37 fBaseDirectory(baseDir) 
38 {
39 // constructor
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         }
52         fType="local";
53         fBaseFolder = fBaseDirectory;
54 }
55
56 //_____________________________________________________________________________
57 AliCDBLocal::~AliCDBLocal() {
58 // destructor
59
60 }
61
62
63 //_____________________________________________________________________________
64 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
65         Int_t& version, Int_t& subVersion) {
66 // build AliCDBId from filename numbers
67
68
69         Ssiz_t mSize;
70
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;
97 }
98
99
100 //_____________________________________________________________________________
101 Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
102 // build file name from AliCDBId data (run range, version, subVersion)
103
104         if (!id.GetAliCDBRunRange().IsValid()) {
105                 AliDebug(2,Form("Invalid run range <%d, %d>.", 
106                         id.GetFirstRun(), id.GetLastRun()));
107                 return kFALSE;
108         }
109
110         if (id.GetVersion() < 0) {
111                 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
112                 return kFALSE;
113         }
114
115         if (id.GetSubVersion() < 0) {
116                 AliDebug(2,Form("Invalid subversion <%s>.", id.GetSubVersion()));
117                 return kFALSE;
118         }
119  
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() + '/');
124
125         return kTRUE;
126 }
127
128 //_____________________________________________________________________________
129 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
130 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
131
132         TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
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         }
146
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)) {
162                                 AliDebug(2,Form(
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)) {
191                                 AliDebug(2,Form(
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         }
208
209         gSystem->FreeDirectory(dirPtr);
210
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 ){
221                 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
222                                         id.GetVersion(),id.GetSubVersion()));
223                 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
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;
233 }
234
235 //_____________________________________________________________________________
236 Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
237 // look for filename matching query (called by GetEntry)
238
239         TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
240
241         void* dirPtr = gSystem->OpenDirectory(dirName);
242         if (!dirPtr) {
243                 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
244                 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
245                 return kFALSE;
246         }
247
248         const char* filename;
249
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
254
255                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
256
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!
265
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){
287                                 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
288                                         query.GetFirstRun(), aVersion, aSubVersion));
289                                 gSystem->FreeDirectory(dirPtr);
290                                 return kFALSE; 
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){
313                                 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
314                                         query.GetFirstRun(), aVersion, aSubVersion));
315                                 gSystem->FreeDirectory(dirPtr);
316                                 return kFALSE; 
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){
346                                 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
347                                         query.GetFirstRun(), aVersion, aSubVersion));
348                                 gSystem->FreeDirectory(dirPtr);
349                                 return kFALSE; 
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
361         return kTRUE;
362 }
363
364 //_____________________________________________________________________________
365 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
366 // get AliCDBEntry from the database
367
368         AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
369         Bool_t result;
370
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
374                 AliCDBId selectedId(queryId);
375                 GetSelection(&selectedId);
376                 result = GetId(selectedId, dataId);
377         } else {
378                 result = GetId(queryId, dataId);
379         }
380
381         if (!result || !dataId.IsSpecified()) return NULL;
382
383         TString filename;
384         if (!IdToFilename(dataId, filename)) {
385
386                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
387                 return NULL;
388         }
389
390         TFile file(filename, "READ"); // open file
391         if (!file.IsOpen()) {
392                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
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         
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();
403                 return NULL;
404         }
405
406         AliCDBId entryId = anEntry->GetId();
407  
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.
411
412         anEntry-> SetLastStorage("local");
413  
414         if(!((entryId.GetAliCDBRunRange()).IsEqual(& dataId.GetAliCDBRunRange())) || 
415                 (entryId.GetVersion() != dataId.GetVersion()) || (entryId.GetSubVersion() != dataId.GetSubVersion())){
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()));
419         }
420
421         // close file, return retieved entry
422         file.Close();
423         return anEntry;
424 }
425
426 //_____________________________________________________________________________
427 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
428         const AliCDBId& queryId, TList* result) {
429 // multiple request (AliCDBStorage::GetAll)
430
431         TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
432
433         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
434         if (!level0DirPtr) {
435                 AliDebug(2,Form("Can't open level0 directory <%s>!",
436                         level0Dir.Data()));
437                 return;
438         }
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         }
452
453         gSystem->FreeDirectory(level0DirPtr);
454 }
455
456 //_____________________________________________________________________________
457 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
458         const AliCDBId& queryId, TList* result) {
459 // multiple request (AliCDBStorage::GetAll)
460
461         TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
462
463         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
464         if (!level1DirPtr) {
465                 AliDebug(2,Form("Can't open level1 directory <%s>!",
466                         level1Dir.Data()));
467                 return;
468         }
469
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)) {
479
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         }
490
491         gSystem->FreeDirectory(level1DirPtr);
492 }
493
494 //_____________________________________________________________________________
495 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
496 // multiple request (AliCDBStorage::GetAll)
497
498         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
499         if (!storageDirPtr) {
500                 AliDebug(2,Form("Can't open storage directory <%s>",
501                         fBaseDirectory.Data()));
502                 return NULL;
503         }
504
505         TList* result = new TList();
506         result->SetOwner();
507
508         const char* level0;
509         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
510
511                 TString level0Str(level0);
512                 if (level0Str == "." || level0Str == "..") {
513                         continue;
514                 }
515
516                 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
517                         GetEntriesForLevel0(level0, queryId, result);
518                 }
519         }
520
521         gSystem->FreeDirectory(storageDirPtr);
522
523         return result;  
524 }
525
526 //_____________________________________________________________________________
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
533         if (!PrepareId(id)) return kFALSE;
534
535         
536         // build filename from entry's id
537         TString filename="";
538         if (!IdToFilename(id, filename)) {
539
540                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
541                 return kFALSE;
542         }
543
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());
553
554         // write object (key name: "AliCDBEntry")
555         Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
556         if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
557
558         file.Close();
559         if(result) {
560                 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
561                         AliInfo(Form("CDB object stored into file %s",filename.Data()));
562         }
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
602         TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
603         Bool_t result=kFALSE;
604
605         void* dirPtr = gSystem->OpenDirectory(dirName); 
606         if (dirPtr) result=kTRUE;
607         gSystem->FreeDirectory(dirPtr);
608
609         return result;
610 }
611
612 //_____________________________________________________________________________
613 void AliCDBLocal::QueryValidFiles()
614 {
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();
730
731 }
732
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);
748 }
749
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
766         return new AliCDBLocalParam(pathname);       
767 }
768
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         }
780
781         return NULL;
782 }
783
784 /////////////////////////////////////////////////////////////////////////////////////////////////
785 //                                                                                             //
786 // AliCDBLocal Parameter class                                                                 //                                          //
787 //                                                                                             //
788 /////////////////////////////////////////////////////////////////////////////////////////////////
789
790 ClassImp(AliCDBLocalParam)
791
792 //_____________________________________________________________________________
793 AliCDBLocalParam::AliCDBLocalParam():
794  AliCDBParam(),
795  fDBPath()
796  {
797 // default constructor
798
799 }
800
801 //_____________________________________________________________________________
802 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
803  AliCDBParam(),
804  fDBPath(dbPath)
805 {
806 // constructor
807
808         SetType("local");
809         SetURI(TString("local://") + dbPath);
810 }
811
812 //_____________________________________________________________________________
813 AliCDBLocalParam::~AliCDBLocalParam() {
814 // destructor
815
816 }
817
818 //_____________________________________________________________________________
819 AliCDBParam* AliCDBLocalParam::CloneParam() const {
820 // clone parameter
821
822         return new AliCDBLocalParam(fDBPath);
823 }
824
825 //_____________________________________________________________________________
826 ULong_t AliCDBLocalParam::Hash() const {
827 // return Hash function
828
829        return fDBPath.Hash();
830 }
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