]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBLocal.cxx
Fixes for some mem-leaks: most changes where pretty basic (i.e. adding deletes).
[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 AliCDBId* AliCDBLocal::GetId(const AliCDBId& query) {
366 // look for filename matching query (called by GetEntryId)
367
368         TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
369
370         void* dirPtr = gSystem->OpenDirectory(dirName);
371         if (!dirPtr) {
372                 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
373                 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
374                 return NULL;
375         }
376
377         const char* filename;
378         AliCDBId *result = new AliCDBId();
379         result->SetPath(query.GetPath());
380
381         AliCDBRunRange aRunRange; // the runRange got from filename
382         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
383
384         if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
385
386                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
387
388                         TString aString(filename);
389                         if (aString == "." || aString == "..") continue;
390
391                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
392                         // aRunRange, aVersion, aSubVersion filled from filename
393
394                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
395                         // aRunRange contains requested run!
396
397                         if (result->GetVersion() < aVersion) {
398                                 result->SetVersion(aVersion);
399                                 result->SetSubVersion(aSubVersion);
400
401                                 result->SetFirstRun(
402                                         aRunRange.GetFirstRun());
403                                 result->SetLastRun(
404                                         aRunRange.GetLastRun());
405
406                         } else if (result->GetVersion() == aVersion
407                                 && result->GetSubVersion()
408                                         < aSubVersion) {
409
410                                 result->SetSubVersion(aSubVersion);
411
412                                 result->SetFirstRun(
413                                         aRunRange.GetFirstRun());
414                                 result->SetLastRun(
415                                         aRunRange.GetLastRun());
416                         } else if (result->GetVersion() == aVersion
417                                 && result->GetSubVersion() == aSubVersion){
418                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
419                                         query.GetFirstRun(), aVersion, aSubVersion));
420                                 gSystem->FreeDirectory(dirPtr);
421                                 delete result;
422                                 return NULL;
423                                 }
424                 }
425
426         } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
427
428                 result->SetVersion(query.GetVersion());
429
430                 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
431
432                         TString aString(filename);
433                         if (aString == "." || aString == "..") continue;
434
435                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
436                         // aRunRange, aVersion, aSubVersion filled from filename
437
438                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; 
439                         // aRunRange contains requested run!
440
441                         if(query.GetVersion() != aVersion) continue;
442                         // aVersion is requested version!
443
444                         if(result->GetSubVersion() == aSubVersion){
445                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
446                                         query.GetFirstRun(), aVersion, aSubVersion));
447                                 gSystem->FreeDirectory(dirPtr);
448                                 delete result;
449                                 return NULL;
450                         }
451                         if( result->GetSubVersion() < aSubVersion) {
452
453                                 result->SetSubVersion(aSubVersion);
454
455                                 result->SetFirstRun(
456                                         aRunRange.GetFirstRun());
457                                 result->SetLastRun(
458                                         aRunRange.GetLastRun());
459                         } 
460                 }
461
462         } else { // both version and subversion specified
463
464                 //AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
465         //Bool_t result;
466         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
467
468                         TString aString(filename);
469                         if (aString == "." || aString == "..") continue;
470
471                         if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
472                         // aRunRange, aVersion, aSubVersion filled from filename
473
474                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
475                         // aRunRange contains requested run!
476
477                         if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
478                         // aVersion and aSubVersion are requested version and subVersion!
479
480                         if(result->GetVersion() == aVersion && result->GetSubVersion() == aSubVersion){
481                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
482                                         query.GetFirstRun(), aVersion, aSubVersion));
483                                 gSystem->FreeDirectory(dirPtr);
484                                 delete result;
485                                 return NULL;
486                         }
487                         result->SetVersion(aVersion);
488                         result->SetSubVersion(aSubVersion);
489                         result->SetFirstRun(aRunRange.GetFirstRun());
490                         result->SetLastRun(aRunRange.GetLastRun());
491                         
492                 }
493         }
494
495         gSystem->FreeDirectory(dirPtr);
496
497         return result;
498 }
499
500 //_____________________________________________________________________________
501 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
502 // get AliCDBEntry from the database
503
504         AliCDBId* dataId = GetEntryId(queryId);
505
506         if (!dataId || !dataId->IsSpecified()) return NULL;
507
508         TString filename;
509         if (!IdToFilename(*dataId, filename)) {
510
511                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
512                 delete dataId;
513                 return NULL;
514         }
515
516         TFile file(filename, "READ"); // open file
517         if (!file.IsOpen()) {
518                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
519                 delete dataId;
520                 return NULL;
521         }
522
523         // get the only AliCDBEntry object from the file
524         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
525
526         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
527         if (!anEntry) {
528                 AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
529                 file.Close();
530                 delete dataId;
531                 return NULL;
532         }
533
534         AliCDBId& entryId = anEntry->GetId();
535
536         // The object's Id are not reset during storage
537         // If object's Id runRange or version do not match with filename,
538         // it means that someone renamed file by hand. In this case a warning msg is issued.
539
540         anEntry-> SetLastStorage("local");
541
542         if(!entryId.IsEqual(dataId)){
543                 AliWarning(Form("Mismatch between file name and object's Id!"));
544                 AliWarning(Form("File name: %s", dataId->ToString().Data()));
545                 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
546         }
547
548         // Check whether entry contains a TTree. In case load the tree in memory!
549         LoadTreeFromFile(anEntry);
550
551         // close file, return retieved entry
552         file.Close();
553         delete dataId;
554         return anEntry;
555 }
556
557 //_____________________________________________________________________________
558 AliCDBId* AliCDBLocal::GetEntryId(const AliCDBId& queryId) {
559 // get AliCDBId from the database
560
561         AliCDBId* dataId = 0;
562
563         // look for a filename matching query requests (path, runRange, version, subVersion)
564         if (!queryId.HasVersion()) {
565                 // if version is not specified, first check the selection criteria list
566                 AliCDBId selectedId(queryId);
567                 GetSelection(&selectedId);
568                 dataId = GetId(selectedId);
569         } else {
570                 dataId = GetId(queryId);
571         }
572
573         if (dataId && !dataId->IsSpecified()) {
574                 delete dataId;
575                 return NULL;
576         }
577
578         return dataId;
579 }
580
581 //_____________________________________________________________________________
582 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
583         const AliCDBId& queryId, TList* result) {
584 // multiple request (AliCDBStorage::GetAll)
585
586         TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
587
588         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
589         if (!level0DirPtr) {
590                 AliDebug(2,Form("Can't open level0 directory <%s>!",
591                         level0Dir.Data()));
592                 return;
593         }
594
595         const char* level1;
596         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
597
598                 TString level1Str(level1);
599                 if (level1Str == "." || level1Str == "..") {
600                         continue;
601                 }
602
603                 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
604                         GetEntriesForLevel1(level0, level1, queryId, result);
605                 }
606         }
607
608         gSystem->FreeDirectory(level0DirPtr);
609 }
610
611 //_____________________________________________________________________________
612 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
613         const AliCDBId& queryId, TList* result) {
614 // multiple request (AliCDBStorage::GetAll)
615
616         TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
617
618         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
619         if (!level1DirPtr) {
620                 AliDebug(2,Form("Can't open level1 directory <%s>!",
621                         level1Dir.Data()));
622                 return;
623         }
624
625         const char* level2;
626         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
627
628                 TString level2Str(level2);
629                 if (level2Str == "." || level2Str == "..") {
630                         continue;
631                 }
632
633                 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
634
635                         AliCDBPath entryPath(level0, level1, level2);
636                         AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
637                                 queryId.GetVersion(), queryId.GetSubVersion());
638
639                         AliCDBEntry* anEntry = GetEntry(entryId);
640                         if (anEntry) {
641                                 result->Add(anEntry);
642                         }
643                 }
644         }
645
646         gSystem->FreeDirectory(level1DirPtr);
647 }
648
649 //_____________________________________________________________________________
650 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
651 // multiple request (AliCDBStorage::GetAll)
652
653         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
654         if (!storageDirPtr) {
655                 AliDebug(2,Form("Can't open storage directory <%s>",
656                         fBaseDirectory.Data()));
657                 return NULL;
658         }
659
660         TList* result = new TList();
661         result->SetOwner();
662
663         const char* level0;
664         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
665
666                 TString level0Str(level0);
667                 if (level0Str == "." || level0Str == "..") {
668                         continue;
669                 }
670
671                 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
672                         GetEntriesForLevel0(level0, queryId, result);
673                 }
674         }
675
676         gSystem->FreeDirectory(storageDirPtr);
677
678         return result;  
679 }
680
681 //_____________________________________________________________________________
682 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
683 // put an AliCDBEntry object into the database
684
685         AliCDBId& id = entry->GetId();
686
687         // set version and subVersion for the entry to be stored
688         if (!PrepareId(id)) return kFALSE;
689
690         
691         // build filename from entry's id
692         TString filename="";
693         if (!IdToFilename(id, filename)) {
694
695                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
696                 return kFALSE;
697         }
698
699         // open file
700         TFile file(filename, "CREATE");
701         if (!file.IsOpen()) {
702                 AliError(Form("Can't open file <%s>!", filename.Data()));
703                 return kFALSE;
704         }
705         
706         //SetTreeToFile(entry, &file);
707
708         entry->SetVersion(id.GetVersion());
709         entry->SetSubVersion(id.GetSubVersion());
710
711         // write object (key name: "AliCDBEntry")
712         Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
713         if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
714
715         file.Close();
716         if(result) {
717                 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
718                         AliInfo(Form("CDB object stored into file %s",filename.Data()));
719         }
720
721         return result;
722 }
723
724 //_____________________________________________________________________________
725 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
726
727         TString fullFileName(fileName);
728         fullFileName.Prepend(fBaseDirectory+'/');
729         TFile *file = TFile::Open(fullFileName);
730         if (!file) {
731                 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
732                 return NULL;
733         }
734         file->cd();
735
736         TList *list = new TList();
737         list->SetOwner();
738         int i=0;
739         TString keycycle;
740         
741         AliCDBId *id;
742         while(1){
743                 i++;
744                 keycycle = "AliCDBId;";
745                 keycycle+=i;
746                 
747                 id = (AliCDBId*) file->Get(keycycle);
748                 if(!id) break;
749                 list->AddFirst(id);
750         }
751         file->Close(); delete file; file=0;     
752         return list;
753 }
754
755 //_____________________________________________________________________________
756 Bool_t AliCDBLocal::Contains(const char* path) const{
757 // check for path in storage's fBaseDirectory
758
759         TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
760         Bool_t result=kFALSE;
761
762         void* dirPtr = gSystem->OpenDirectory(dirName); 
763         if (dirPtr) result=kTRUE;
764         gSystem->FreeDirectory(dirPtr);
765
766         return result;
767 }
768
769 //_____________________________________________________________________________
770 void AliCDBLocal::QueryValidFiles()
771 {
772 // Query the CDB for files valid for AliCDBStorage::fRun
773 // fills list fValidFileIds with AliCDBId objects created from file name
774
775         if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
776         if(fMetaDataFilter) {
777                 AliWarning ("CDB meta data parameters are not used by local storage query!");
778                 delete fMetaDataFilter; fMetaDataFilter=0;
779         }
780
781         void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
782
783         const char* level0;
784         while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
785
786                 TString level0Str(level0);
787                 if (level0Str == "." || level0Str == "..") {
788                         continue;
789                 }
790
791                 if (fPathFilter.Level0Comprises(level0)) {
792                         TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
793                         void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
794                         const char* level1;
795                         while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
796
797                                 TString level1Str(level1);
798                                 if (level1Str == "." || level1Str == "..") {
799                                         continue;
800                                 }
801
802                                 if (fPathFilter.Level1Comprises(level1)) {
803                                         TString level1Dir = Form("%s/%s/%s",
804                                                         fBaseDirectory.Data(),level0,level1);
805
806                                         void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
807                                         const char* level2;
808                                         while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
809
810                                                 TString level2Str(level2);
811                                                 if (level2Str == "." || level2Str == "..") {
812                                                         continue;
813                                                 }
814
815                                                 if (fPathFilter.Level2Comprises(level2)) {
816                                                         TString dirName = Form("%s/%s/%s/%s",
817                                                                         fBaseDirectory.Data(),level0,level1,level2);
818
819                                                         void* dirPtr = gSystem->OpenDirectory(dirName);
820
821                                                         const char* filename;
822
823                                                         AliCDBRunRange aRunRange; // the runRange got from filename
824                                                         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
825
826                                                         while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
827
828                                                                 TString aString(filename);
829                                                                 if (aString == "." || aString == "..") continue;
830
831                                                                 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
832                                                                 AliCDBRunRange runrg(fRun,fRun);
833                                                                 if (!aRunRange.Comprises(runrg)) continue;
834                                                                 // aRunRange contains requested run!
835                                                                 AliCDBPath validPath(level0,level1,level2);
836                                                                 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
837                                                                 fValidFileIds.AddLast(validId.Clone());
838                                                         }
839                                                         gSystem->FreeDirectory(dirPtr);
840                                                 }
841                                         }
842                                         gSystem->FreeDirectory(level1DirPtr);
843                                 }
844                         }
845                         gSystem->FreeDirectory(level0DirPtr);
846                 }
847         }
848         gSystem->FreeDirectory(storageDirPtr);
849
850 }
851
852 //_____________________________________________________________________________
853 Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
854 // get last version found in the database valid for run and path
855
856         AliCDBPath aCDBPath(path);
857         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
858                 AliError(Form("Invalid path in request: %s", path));
859                 return -1;
860         }
861
862         AliCDBId query(path, run, run, -1, -1);
863         AliCDBId* dataId = GetId(query);
864
865         if(!dataId) return -1;
866
867         Int_t version = dataId->GetVersion();
868         delete dataId;
869
870         return version;
871
872 }
873
874 //_____________________________________________________________________________
875 Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
876 // get last version found in the database valid for run and path
877
878         AliCDBPath aCDBPath(path);
879         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
880                 AliError(Form("Invalid path in request: %s", path));
881                 return -1;
882         }
883
884         AliCDBId query(path, run, run, version, -1);
885         AliCDBId *dataId = GetId(query);
886
887         if(!dataId) return -1;
888
889         Int_t subVersion = dataId->GetSubVersion();
890
891         delete dataId;
892         return subVersion;
893
894 }
895
896 /////////////////////////////////////////////////////////////////////////////////////////////////
897 //                                                                                             //
898 // AliCDBLocal factory                                                                         //
899 //                                                                                             //
900 /////////////////////////////////////////////////////////////////////////////////////////////////
901
902 ClassImp(AliCDBLocalFactory)
903
904 //_____________________________________________________________________________
905 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
906 // check if the string is valid local URI
907
908         TRegexp dbPattern("^local://.+$");
909
910         return TString(dbString).Contains(dbPattern);
911 }
912
913 //_____________________________________________________________________________
914 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
915 // create AliCDBLocalParam class from the URI string
916
917         if (!Validate(dbString)) {
918                 return NULL;
919         }
920
921         TString pathname(dbString + sizeof("local://") - 1);
922         
923         gSystem->ExpandPathName(pathname);
924
925         if (pathname[0] != '/') {
926                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
927         }
928
929         return new AliCDBLocalParam(pathname);
930 }
931
932 //_____________________________________________________________________________
933 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
934 // create AliCDBLocal storage instance from parameters
935         
936         if (AliCDBLocalParam::Class() == param->IsA()) {
937                 
938                 const AliCDBLocalParam* localParam = 
939                         (const AliCDBLocalParam*) param;
940                 
941                 return new AliCDBLocal(localParam->GetPath());
942         }
943
944         return NULL;
945 }
946
947 /////////////////////////////////////////////////////////////////////////////////////////////////
948 //                                                                                             //
949 // AliCDBLocal Parameter class                                                                 //                                          //
950 //                                                                                             //
951 /////////////////////////////////////////////////////////////////////////////////////////////////
952
953 ClassImp(AliCDBLocalParam)
954
955 //_____________________________________________________________________________
956 AliCDBLocalParam::AliCDBLocalParam():
957  AliCDBParam(),
958  fDBPath()
959  {
960 // default constructor
961
962 }
963
964 //_____________________________________________________________________________
965 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
966  AliCDBParam(),
967  fDBPath(dbPath)
968 {
969 // constructor
970
971         SetType("local");
972         SetURI(TString("local://") + dbPath);
973 }
974
975 //_____________________________________________________________________________
976 AliCDBLocalParam::~AliCDBLocalParam() {
977 // destructor
978
979 }
980
981 //_____________________________________________________________________________
982 AliCDBParam* AliCDBLocalParam::CloneParam() const {
983 // clone parameter
984
985         return new AliCDBLocalParam(fDBPath);
986 }
987
988 //_____________________________________________________________________________
989 ULong_t AliCDBLocalParam::Hash() const {
990 // return Hash function
991
992        return fDBPath.Hash();
993 }
994
995 //_____________________________________________________________________________
996 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
997 // check if this object is equal to AliCDBParam obj
998
999         if (this == obj) {
1000                 return kTRUE;
1001         }
1002
1003         if (AliCDBLocalParam::Class() != obj->IsA()) {
1004                 return kFALSE;
1005         }
1006
1007         AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
1008
1009         return fDBPath == other->fDBPath;
1010 }
1011