]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCDBManager.cxx
Coverity
[u/mrichter/AliRoot.git] / STEER / CDB / AliCDBManager.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 //   Implementation of AliCDBManager and AliCDBParam classe
17 //   Author: Alberto Colla 
18 //   e-mail: Alberto.Colla@cern.ch
19 //-------------------------------------------------------------------------
20
21 #include <stdlib.h>
22
23 #include "AliCDBManager.h"
24 #include "AliCDBStorage.h"
25 #include "AliLog.h"
26 #include "AliCDBDump.h"
27 #include "AliCDBLocal.h"
28 #include "AliCDBGrid.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBHandler.h"
31
32 #include <TObjString.h>
33 #include <TSAXParser.h>
34 #include <TFile.h>
35 #include <TKey.h>
36 #include <TUUID.h>
37 #include <TGrid.h>
38
39 ClassImp(AliCDBParam)
40
41 ClassImp(AliCDBManager)
42
43 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
44 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
45 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
46 TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
47 TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
48 TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
49 TString AliCDBManager::fgkOCDBFolderXMLfile("alien:///alice/data/OCDBFoldervsRunRange.xml");
50 AliCDBManager* AliCDBManager::fgInstance = 0x0;
51
52 //_____________________________________________________________________________
53 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run)
54 {
55 // returns AliCDBManager instance (singleton)
56
57         if (!fgInstance) {
58                 fgInstance = new AliCDBManager();
59                 if (!entryCache)
60                   fgInstance->Init();
61                 else
62                   fgInstance->InitFromCache(entryCache,run);
63         }
64
65         return fgInstance;
66 }
67
68 //_____________________________________________________________________________
69 void AliCDBManager::Init() {
70 // factory registering
71
72         RegisterFactory(new AliCDBDumpFactory());
73         RegisterFactory(new AliCDBLocalFactory()); 
74         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
75         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
76                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
77                 RegisterFactory(new AliCDBGridFactory());
78                 fCondParam = CreateParameter(fgkCondUri);
79                 fRefParam = CreateParameter(fgkRefUri);
80         }
81
82         InitShortLived();
83 }
84
85 //_____________________________________________________________________________
86 void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
87 // initialize manager from existing cache
88 // used on the slaves in case of parallel reconstruction
89   SetRun(run);
90
91   TIter iter(entryCache->GetTable());
92   TPair* pair = 0;
93
94   while((pair = dynamic_cast<TPair*> (iter.Next()))){
95     fEntryCache.Add(pair->Key(),pair->Value());
96   }
97   // fEntry is the new owner of the cache
98   fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
99   entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
100   AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
101 }
102
103 //_____________________________________________________________________________
104 void  AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys){
105 // 
106 // dump the entries map and the ids list to
107 // the output file
108
109     // open the file
110     TFile *f = TFile::Open(snapshotFileName,"RECREATE");
111     if (!f || f->IsZombie()){
112         AliError(Form("Cannot open file %s",snapshotFileName));
113         return;
114     }
115
116     Printf("\ndumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries()); 
117     Printf("\ndumping entriesList with %d entries!\n", fIds->GetEntries());
118
119     f->cd();                                                                                           
120
121     if(singleKeys){
122         f->WriteObject(&fEntryCache,"CDBentriesMap");
123         f->WriteObject(fIds,"CDBidsList");
124     }else{
125         // We write the entries one by one named by their calibration path
126         /*
127         fEntryCache.Write("CDBentriesMap");
128         fIds->Write("CDBidsList");
129         */
130         TIter iter(fEntryCache.GetTable());
131         TPair* pair = 0;
132         while((pair = dynamic_cast<TPair*> (iter.Next()))){
133             TObjString *os = dynamic_cast<TObjString*>(pair->Key());
134             if (!os) continue;
135             TString path = os->GetString();
136             AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(pair->Value());
137             if (!entry) continue;
138             path.ReplaceAll("/","*");
139             entry->Write(path.Data());
140         }
141     }
142     f->Close();
143     delete f;
144
145     exit(0);
146 }
147
148 //_____________________________________________________________________________
149 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
150 // initialize manager from a CDB snapshot, that is add the entries
151 // to the entries map and the ids to the ids list taking them from
152 // the map and the list found in the input file
153
154 // if the manager is locked it cannot initialize from a snapshot
155     if(fLock) {
156         AliError("Being locked I cannot initialize from the snapshot!");
157         return kFALSE;
158     }   
159
160     // open the file
161     TString snapshotFile(snapshotFileName);
162     if(snapshotFile.BeginsWith("alien://")){
163         if(!gGrid) {
164             TGrid::Connect("alien://","");
165             if(!gGrid) {
166                 AliError("Connection to alien failed!");
167                 return kFALSE;
168             }
169         }
170     }
171
172     TFile *f = TFile::Open(snapshotFileName);
173     if (!f || f->IsZombie()){
174         AliError(Form("Cannot open file %s",snapshotFileName));
175         return kFALSE;
176     }
177
178     // retrieve entries' map from snapshot file
179     TMap *entriesMap = 0;
180     TIter next(f->GetListOfKeys());
181     TKey *key;
182     while ((key = (TKey*)next())) {
183         if (strcmp(key->GetClassName(),"TMap") != 0) continue;
184         entriesMap = (TMap*)key->ReadObj();
185         break;
186     }
187     if (!entriesMap || entriesMap->GetEntries()==0){
188         AliError("Cannot get valid map of CDB entries from snapshot file");
189         return kFALSE;
190     }
191
192     // retrieve ids' list from snapshot file
193     TList *idsList = 0;
194     TIter nextKey(f->GetListOfKeys());
195     TKey *keyN;
196     while ((keyN = (TKey*)nextKey())) {
197         if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
198         idsList = (TList*)keyN->ReadObj();
199         break;
200     }
201     if (!idsList || idsList->GetEntries()==0){
202         AliError("Cannot get valid list of CDB entries from snapshot file");
203         return kFALSE;
204     }
205
206     // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
207     // If "overwrite" is false: add the entry to the cache and its id to the list of ids
208     // only if neither of them is already there.
209     // If "overwrite" is true: write the snapshot entry,id in any case. If something
210     // was already there for that calibration type, remove it and issue a warning
211     TIter iterObj(entriesMap->GetTable());
212     TPair* pair = 0;
213     Int_t nAdded=0;
214     while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
215         TObjString* os = (TObjString*) pair->Key();
216         TString path = os->GetString();
217         TIter iterId(idsList);
218         AliCDBId* id=0;
219         AliCDBId* correspondingId=0;
220         while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
221             TString idpath(id->GetPath());
222             if(idpath==path){
223                 correspondingId=id;
224                 break;
225             }
226         }
227         if(!correspondingId){
228             AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
229             break;
230         }
231         Bool_t cached = fEntryCache.Contains(path.Data());
232         Bool_t registeredId = kFALSE;
233         TIter iter(fIds);
234         AliCDBId *idT = 0;
235         while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
236             if(idT->GetPath()==path){
237                 registeredId = kTRUE;
238                 break;
239             }
240         }
241
242         if(overwrite){
243             if(cached || registeredId){
244                 AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
245                 UnloadFromCache(path.Data());
246             }
247             fEntryCache.Add(pair->Key(),pair->Value());
248             fIds->Add(id);
249             nAdded++;
250         }else{
251             if(cached || registeredId){
252                 AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
253             }else{
254                 fEntryCache.Add(pair->Key(),pair->Value());
255                 fIds->Add(id);
256                 nAdded++;
257             }
258         }
259     }
260
261     // fEntry is the new owner of the cache
262     fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
263     entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
264     fIds->SetOwner(kTRUE);
265     idsList->SetOwner(kFALSE);
266     AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
267
268     f->Close();
269     delete f;
270
271     return kTRUE;
272 }
273
274 //_____________________________________________________________________________
275 void AliCDBManager::Destroy() {
276 // delete ALCDBManager instance and active storages
277
278         if (fgInstance) {
279                 //fgInstance->Delete();
280                 delete fgInstance;
281                 fgInstance = 0x0;
282         }
283 }
284
285 //_____________________________________________________________________________
286 AliCDBManager::AliCDBManager():
287   TObject(),
288   fFactories(),
289   fActiveStorages(),
290   fSpecificStorages(),
291   fEntryCache(),
292   fIds(0),
293   fStorageMap(0),
294   fShortLived(0),
295   fDefaultStorage(NULL),
296   fDrainStorage(NULL),
297   fCondParam(0),
298   fRefParam(0),
299   fRun(-1),
300   fCache(kTRUE),
301   fLock(kFALSE),
302   fSnapshotMode(kFALSE),
303   fSnapshotFile(0),
304 //  fSnapshotCache(0),
305 //  fSnapshotIdsList(0),
306   fRaw(kFALSE),
307   fStartRunLHCPeriod(-1),
308   fEndRunLHCPeriod(-1),
309   fLHCPeriod(""),
310   fKey(0)
311 {
312 // default constuctor
313         fFactories.SetOwner(1);
314         fActiveStorages.SetOwner(1);
315         fSpecificStorages.SetOwner(1);
316         fEntryCache.SetName("CDBEntryCache");
317         fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
318
319         fStorageMap = new TMap();
320         fStorageMap->SetOwner(1);
321         fIds = new TList();
322         fIds->SetOwner(1);
323 }
324
325 //_____________________________________________________________________________
326 AliCDBManager::~AliCDBManager() {
327 // destructor
328         ClearCache();
329         DestroyActiveStorages();
330         fFactories.Delete();
331         fDrainStorage = 0x0;
332         fDefaultStorage = 0x0;
333         delete fStorageMap; fStorageMap = 0;
334         delete fIds; fIds = 0;
335         delete fCondParam;
336         delete fRefParam;
337         delete fShortLived; fShortLived = 0x0;
338         //fSnapshotCache = 0;
339         //fSnapshotIdsList = 0;
340         if(fSnapshotMode){
341             fSnapshotFile->Close();
342             fSnapshotFile = 0;
343         }
344 }
345
346 //_____________________________________________________________________________
347 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
348 // put a storage object into the list of active storages
349
350         fActiveStorages.Add(param, storage);
351         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
352 }
353
354 //_____________________________________________________________________________
355 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
356 // add a storage factory to the list of registerd factories
357  
358         if (!fFactories.Contains(factory)) {
359                 fFactories.Add(factory);
360         }
361 }
362
363 //_____________________________________________________________________________
364 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
365 // check if dbString is a URI valid for one of the registered factories
366
367         TIter iter(&fFactories);
368
369         AliCDBStorageFactory* factory=0;
370         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
371
372                 if (factory->Validate(dbString)) {
373                         return kTRUE;
374                 }
375         }
376
377         return kFALSE;
378 }
379
380 //_____________________________________________________________________________
381 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
382 // create AliCDBParam object from URI string
383
384         TIter iter(&fFactories);
385
386         AliCDBStorageFactory* factory=0;
387         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
388                 AliCDBParam* param = factory->CreateParameter(dbString);
389                 if(param) return param;
390         }
391
392         return NULL;
393 }
394
395 //_____________________________________________________________________________
396 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
397 // get storage object from URI string
398                 
399         AliCDBParam* param = CreateParameter(dbString);
400         if (!param) {
401                 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
402                 return NULL;
403         }
404
405         AliCDBStorage* aStorage = GetStorage(param);
406
407         delete param;
408         return aStorage;
409 }
410
411 //_____________________________________________________________________________
412 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
413 // get storage object from AliCDBParam object
414         
415         // if the list of active storages already contains
416         // the requested storage, return it
417         AliCDBStorage* aStorage = GetActiveStorage(param);
418         if (aStorage) {
419                 return aStorage;
420         }
421
422         // if lock is ON, cannot activate more storages!
423         if(fLock) {
424                 if (fDefaultStorage) {
425                         AliFatal("Lock is ON, and default storage is already set: "
426                                 "cannot reset it or activate more storages!");
427                 }
428         }       
429         
430         TIter iter(&fFactories);
431
432         AliCDBStorageFactory* factory=0;
433
434         // loop on the list of registered factories
435         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
436
437                 // each factory tries to create its storage from the parameter
438                 aStorage = factory->Create(param);
439                 if (aStorage) {
440                         PutActiveStorage(param->CloneParam(), aStorage);
441                         aStorage->SetURI(param->GetURI());
442                         if(fRun >= 0) {
443                                 if(aStorage->GetType() == "alien"){
444                                         aStorage->QueryCDB(fRun);
445                                 } else {
446                                         AliDebug(2,
447                                                 "Skipping query for valid files, it is used only in grid...");
448                                 }
449                         }
450                         return aStorage;
451                 }
452         }
453
454         AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
455
456         return NULL;
457 }
458
459 //_____________________________________________________________________________
460 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
461 // get a storage object from the list of active storages
462
463         return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
464 }
465
466 //_____________________________________________________________________________
467 TList* AliCDBManager::GetActiveStorages() {
468 // return list of active storages
469 // user has responsibility to delete returned object
470
471         TList* result = new TList();
472
473         TIter iter(fActiveStorages.GetTable());
474         TPair* aPair=0;
475         while ((aPair = (TPair*) iter.Next())) {
476                 result->Add(aPair->Value());
477         }
478
479         return result;
480 }
481
482 //_____________________________________________________________________________
483 void AliCDBManager::SetDrain(const char* dbString) {
484 // set drain storage from URI string
485
486         fDrainStorage = GetStorage(dbString);   
487 }
488
489 //_____________________________________________________________________________
490 void AliCDBManager::SetDrain(const AliCDBParam* param) {
491 // set drain storage from AliCDBParam
492
493         fDrainStorage = GetStorage(param);
494 }
495
496 //_____________________________________________________________________________
497 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
498 // set drain storage from another active storage
499         
500         fDrainStorage = storage;
501 }
502
503 //_____________________________________________________________________________
504 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
505 // drain retrieved object to drain storage
506
507         AliDebug(2, "Draining into drain storage...");
508         return fDrainStorage->Put(entry);
509 }
510
511 //____________________________________________________________________________
512 void AliCDBManager::SetDefaultStorage(const char* dbString) {
513 // sets default storage from URI string
514         
515         // checking whether we are in the raw case
516         TString dbStringTemp(dbString);
517         if (dbStringTemp == "raw://")
518         {
519                 fRaw = kTRUE;
520                 AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
521                 AliInfo("Connecting to the grid...");
522                 if(!gGrid) {
523                         TGrid::Connect("alien://","");
524                         if(!gGrid) {
525                                 AliError("Connection to alien failed!");
526                                 return;
527                         }
528                 }
529                 return;
530         }
531
532         AliCDBStorage* bckStorage = fDefaultStorage;
533         
534         fDefaultStorage = GetStorage(dbString);
535         
536         if(!fDefaultStorage) return;
537         
538         if(bckStorage && (fDefaultStorage != bckStorage)){
539                 AliWarning("Existing default storage replaced: clearing cache!");
540                 ClearCache();
541         }
542         
543         if (fStorageMap->Contains("default")) {
544                 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
545         }
546         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
547 }
548 //_____________________________________________________________________________
549 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
550 // set default storage from AliCDBParam object
551         
552         AliCDBStorage* bckStorage = fDefaultStorage;
553
554         fDefaultStorage = GetStorage(param);
555
556         if(!fDefaultStorage) return;
557
558         if(bckStorage && (fDefaultStorage != bckStorage)){
559                 AliWarning("Existing default storage replaced: clearing cache!");
560                 ClearCache();
561         }
562
563         if (fStorageMap->Contains("default")) {
564                 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
565         }
566         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
567 }
568
569 //_____________________________________________________________________________
570 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
571 // set default storage from another active storage
572         
573         // if lock is ON, cannot activate more storages!
574         if(fLock) {
575                 if (fDefaultStorage) {
576                         AliFatal("Lock is ON, and default storage is already set: "
577                                 "cannot reset it or activate more storages!");
578                 }
579         }       
580         
581         if (!storage) {
582                 UnsetDefaultStorage();
583                 return;
584         }
585         
586         AliCDBStorage* bckStorage = fDefaultStorage;
587
588         fDefaultStorage = storage;
589
590         if(bckStorage && (fDefaultStorage != bckStorage)){
591                 AliWarning("Existing default storage replaced: clearing cache!");
592                 ClearCache();
593         }
594
595         if (fStorageMap->Contains("default")) {
596                 delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
597         }
598         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
599 }
600
601 //_____________________________________________________________________________
602 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
603 // sets default storage for MC data
604 // mcString MUST be "MC", 
605 // simType can be "Ideal","Residual","Full"
606         
607         TString strmcString(mcString);
608         TString strsimType(simType);
609         TString dbString; 
610         if (strmcString != "MC"){
611                 AliFatal("Method requires first string to be MC!");
612         }
613         else {
614                 if (strsimType == "Ideal"){
615                         dbString = fgkMCIdealStorage;
616                 }
617                 else if (strsimType == "Full"){
618                         dbString = fgkMCFullStorage;
619                 }
620                 else if (strsimType == "Residual"){
621                         dbString = fgkMCResidualStorage;
622                 }
623                 else {
624                         AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
625                 }
626
627                 SetDefaultStorage(dbString.Data());
628                 if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
629         }
630 }
631 //_____________________________________________________________________________
632 void AliCDBManager::SetDefaultStorageFromRun(Int_t run) {
633 // set default storage from the run number - to be used only with raw data      
634
635         // if lock is ON, cannot activate more storages!
636         if(fLock) {
637                 if (fDefaultStorage) {
638                         AliFatal("Lock is ON, and default storage is already set: "
639                                 "cannot activate default storage from run number");
640                 }
641         }       
642
643         // retrieve XML file from alien
644         if(!gGrid) {
645             TGrid::Connect("alien://","");
646             if(!gGrid) {
647                 AliError("Connection to alien failed!");
648                 return;
649             }
650         }
651         TUUID uuid;
652         TString rndname = "/tmp/";
653         rndname += "OCDBFolderXML.";
654         rndname += uuid.AsString();
655         rndname += ".xml";
656         AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
657         if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
658                 AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
659         }
660         AliCDBHandler* saxcdb = new AliCDBHandler();
661         saxcdb->SetRun(run);
662         TSAXParser *saxParser = new TSAXParser();
663         saxParser->ConnectToHandler("AliCDBHandler", saxcdb);  
664         saxParser->ParseFile(rndname.Data()); 
665         AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
666         AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
667         AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
668         fLHCPeriod = saxcdb->GetOCDBFolder();
669         fStartRunLHCPeriod = saxcdb->GetStartRunRange();
670         fEndRunLHCPeriod = saxcdb->GetEndRunRange();
671
672         SetDefaultStorage(fLHCPeriod.Data());
673         if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
674
675 }
676
677 //_____________________________________________________________________________
678 void AliCDBManager::UnsetDefaultStorage() {
679 // Unset default storage
680         
681         // if lock is ON, action is forbidden!
682         if(fLock) {
683                 if (fDefaultStorage) {
684                         AliFatal("Lock is ON: cannot unset default storage!");
685                 }
686         }
687         
688         if (fDefaultStorage) {
689                 AliWarning("Clearing cache!");
690                 ClearCache();
691         }
692
693         fRun = fStartRunLHCPeriod = fEndRunLHCPeriod = -1;
694         fRaw = kFALSE;
695         
696         fDefaultStorage = 0x0;
697 }
698
699 //_____________________________________________________________________________
700 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
701 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
702
703         AliCDBParam *aPar = CreateParameter(dbString);
704         if(!aPar) return;
705         SetSpecificStorage(calibType, aPar);
706         delete aPar;
707 }
708
709 //_____________________________________________________________________________
710 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param) {
711 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
712 // Default storage should be defined prior to any specific storages, e.g.:
713 // AliCDBManager::instance()->SetDefaultStorage("alien://");
714 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
715 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
716 // calibType must be a valid CDB path! (3 level folder structure)
717
718
719         if(!fDefaultStorage && !fRaw) {
720                 AliError("Please activate a default storage first!");
721                 return;
722         }
723
724
725         AliCDBPath aPath(calibType);
726         if(!aPath.IsValid()){
727                 AliError(Form("Not a valid path: %s", calibType));
728                 return;
729         }
730
731         TObjString *objCalibType = new TObjString(aPath.GetPath());
732         if(fSpecificStorages.Contains(objCalibType)){
733                 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
734                                         calibType));
735                 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
736                 if(checkPar) delete checkPar;
737                 delete fSpecificStorages.Remove(objCalibType);
738         }
739         AliCDBStorage *aStorage = GetStorage(param);
740         if(!aStorage) return;
741
742         fSpecificStorages.Add(objCalibType, param->CloneParam());
743
744         if(fStorageMap->Contains(objCalibType)){
745                 delete fStorageMap->Remove(objCalibType);
746         }
747         fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
748
749 }
750
751 //_____________________________________________________________________________
752 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
753 // get storage specific for detector or calibration type 
754
755         AliCDBPath calibPath(calibType);
756         if(!calibPath.IsValid()) return NULL;
757
758         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
759         if(!checkPar){
760                 AliError(Form("%s storage not found!", calibType));
761                 return NULL;
762         } else {
763                 return GetStorage(checkPar);
764         }
765
766 }
767
768 //_____________________________________________________________________________
769 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
770 // select storage valid for path from the list of specific storages
771
772         AliCDBPath aPath(path);
773         if(!aPath.IsValid()) return NULL;
774
775         TIter iter(&fSpecificStorages);
776         TObjString *aCalibType=0;
777         AliCDBPath tmpPath("null/null/null");
778         AliCDBParam* aPar=0;
779         while((aCalibType = (TObjString*) iter.Next())){
780                 AliCDBPath calibTypePath(aCalibType->GetName());
781                 if(calibTypePath.Comprises(aPath)) {
782                         if(calibTypePath.Comprises(tmpPath)) continue;
783                         aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
784                         tmpPath.SetPath(calibTypePath.GetPath());
785                 }
786         }
787         return aPar;
788 }
789
790 //_____________________________________________________________________________
791 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
792         Int_t version, Int_t subVersion) {
793 // get an AliCDBEntry object from the database
794
795         if(runNumber < 0){
796                 // RunNumber is not specified. Try with fRun
797                 if (fRun < 0){
798                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
799                         return NULL;
800                 }
801                 runNumber = fRun;
802         }
803
804         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
805 }
806
807 //_____________________________________________________________________________
808 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
809         const AliCDBRunRange& runRange, Int_t version,
810         Int_t subVersion) {
811 // get an AliCDBEntry object from the database!
812
813         return Get(AliCDBId(path, runRange, version, subVersion));
814 }
815
816 //_____________________________________________________________________________
817 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
818 // get an AliCDBEntry object from the database
819         
820         // check if query's path and runRange are valid
821         // query is invalid also if version is not specified and subversion is!
822         if (!query.IsValid()) {
823                 AliError(Form("Invalid query: %s", query.ToString().Data()));
824                 return NULL;
825         }
826         
827         // query is not specified if path contains wildcard or run range= [-1,-1]
828         if (!query.IsSpecified()) {
829                 AliError(Form("Unspecified query: %s",
830                                 query.ToString().Data()));
831                 return NULL;
832         }
833
834         if(fLock && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun())) 
835                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
836         
837         if(fCache && !(fRun >= query.GetFirstRun() && fRun <= query.GetLastRun())) 
838                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
839
840         AliCDBEntry *entry=0;
841
842         // first look into map of cached objects
843         if(fCache && query.GetFirstRun() == fRun)
844                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
845         if(entry) {
846                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
847                 return entry;
848         }
849
850         // if snapshot flag is set, try getting from the snapshot
851         if(fSnapshotMode && query.GetFirstRun() == fRun)
852             // entry = (AliCDBEntry*) fSnapshotCache->GetValue(query.GetPath()); // not possible,
853             // all the map would be charged in memory from the snapshot anyway.
854             entry = GetEntryFromSnapshot(query.GetPath());
855         if(entry) {
856                 AliDebug(2, Form("Object %s retrieved from the snapshot !!",query.GetPath().Data()));
857                 if(query.GetFirstRun() == fRun) // no need to check fCache, fSnapshotMode not possible otherwise
858                     CacheEntry(query.GetPath(), entry);
859
860                 if(!fIds->Contains(&entry->GetId()))
861                     fIds->Add(entry->GetId().Clone());
862
863                 return entry;
864         }
865
866         // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either)
867         // => retrieve it from the storage and cache it!!
868         if(!fDefaultStorage) {
869                 AliError("No storage set!");
870                 return NULL;
871         }
872         AliCDBStorage *aStorage=0;
873         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
874
875         if(aPar) {
876                 aStorage=GetStorage(aPar);
877                 TString str = aPar->GetURI();
878                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
879         } else {
880                 aStorage=GetDefaultStorage();
881                 AliDebug(2,"Looking into default storage");
882         }
883
884         entry = aStorage->Get(query);
885
886         if(entry && fCache && (query.GetFirstRun() == fRun)){
887                 CacheEntry(query.GetPath(), entry);
888         }
889
890         if(entry && !fIds->Contains(&entry->GetId())){
891                 fIds->Add(entry->GetId().Clone());
892         }
893
894
895         return entry;
896
897 }
898
899 //_____________________________________________________________________________
900 AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) {
901     // get the entry from the open snapshot file
902
903     TString sPath(path);
904     sPath.ReplaceAll("/","*");
905     AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(fSnapshotFile->Get(sPath.Data()));
906     if(!entry){
907         AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path));
908         return 0;
909     }
910
911     return entry;
912 }
913
914 //_____________________________________________________________________________
915 Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
916 // set the manager in snapshot mode
917     
918     if(!fCache){
919         AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
920         return kFALSE;
921     }
922
923     fSnapshotMode = kTRUE;
924
925     //open snapshot file
926     TString snapshotFile(snapshotFileName);
927     if(snapshotFile.BeginsWith("alien://")){
928         if(!gGrid) {
929             TGrid::Connect("alien://","");
930             if(!gGrid) {
931                 AliError("Connection to alien failed!");
932                 return kFALSE;
933             }
934         }
935     }
936
937     fSnapshotFile = TFile::Open(snapshotFileName);
938     if (!fSnapshotFile || fSnapshotFile->IsZombie()){
939         AliError(Form("Cannot open file %s",snapshotFileName));
940         return kFALSE;
941     }
942
943     /*
944     // retrieve pointer to entries' map from snapshot file
945     TIter next(fSnapshotFile->GetListOfKeys());
946     TKey *key;
947     while ((key = (TKey*)next())) {
948         if (strcmp(key->GetClassName(),"TMap") != 0) continue;
949         fSnapshotCache = (TMap*)key->ReadObj();
950         break;
951     }
952     if (!fSnapshotCache || fSnapshotCache->GetEntries()==0){
953         AliError("Cannot get valid map of CDB entries from snapshot file");
954         return kFALSE;
955     }
956
957     // retrieve pointer to ids' list from snapshot file
958     TIter nextKey(fSnapshotFile->GetListOfKeys());
959     TKey *keyN;
960     while ((keyN = (TKey*)nextKey())) {
961         if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
962         fSnapshotIdsList = (TList*)keyN->ReadObj();
963         break;
964     }
965     if (!fSnapshotIdsList || fSnapshotIdsList->GetEntries()==0){
966         AliError("Cannot get valid list of CDB entries from snapshot file");
967         return kFALSE;
968     }
969     */
970     return kTRUE;
971
972 }
973
974 //_____________________________________________________________________________
975 const char* AliCDBManager::GetURI(const char* path) {
976 // return the URI of the storage where to look for path
977
978         if(!IsDefaultStorageSet()) return 0;
979         
980         AliCDBParam *aPar=SelectSpecificStorage(path);
981
982         if(aPar) {
983                 return aPar->GetURI().Data();
984
985         } else {
986                 return GetDefaultStorage()->GetURI().Data();
987         }
988         
989         return 0;
990 }
991
992 //_____________________________________________________________________________
993 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
994         Int_t version, Int_t subVersion) {
995 // get the AliCDBId of the valid object from the database (does not retrieve the object)
996 // User must delete returned object!
997
998         if(runNumber < 0){
999                 // RunNumber is not specified. Try with fRun
1000                 if (fRun < 0){
1001                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1002                         return NULL;
1003                 }
1004                 runNumber = fRun;
1005         }
1006
1007         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
1008 }
1009
1010 //_____________________________________________________________________________
1011 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
1012         const AliCDBRunRange& runRange, Int_t version,
1013         Int_t subVersion) {
1014 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1015 // User must delete returned object!
1016
1017         return GetId(AliCDBId(path, runRange, version, subVersion));
1018 }
1019
1020 //_____________________________________________________________________________
1021 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
1022 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1023 // User must delete returned object!
1024
1025         if(!fDefaultStorage) {
1026                 AliError("No storage set!");
1027                 return NULL;
1028         }
1029
1030         // check if query's path and runRange are valid
1031         // query is invalid also if version is not specified and subversion is!
1032         if (!query.IsValid()) {
1033                 AliError(Form("Invalid query: %s", query.ToString().Data()));
1034                 return NULL;
1035         }
1036         
1037         // query is not specified if path contains wildcard or run range= [-1,-1]
1038         if (!query.IsSpecified()) {
1039                 AliError(Form("Unspecified query: %s",
1040                                 query.ToString().Data()));
1041                 return NULL;
1042         }
1043
1044         if(fCache && query.GetFirstRun() != fRun)
1045                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1046
1047         AliCDBEntry* entry = 0;
1048
1049         // first look into map of cached objects
1050         if(fCache && query.GetFirstRun() == fRun)
1051                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
1052
1053         if(entry) {
1054                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
1055                 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
1056         }
1057
1058         // Entry is not in cache -> retrieve it from CDB and cache it!!
1059         AliCDBStorage *aStorage=0;
1060         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1061
1062         if(aPar) {
1063                 aStorage=GetStorage(aPar);
1064                 TString str = aPar->GetURI();
1065                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1066                 
1067         } else {
1068                 aStorage=GetDefaultStorage();
1069                 AliDebug(2,"Looking into default storage");
1070         }
1071
1072         return aStorage->GetId(query);
1073
1074 }
1075
1076 //_____________________________________________________________________________
1077 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
1078         Int_t version, Int_t subVersion) {
1079 // get multiple AliCDBEntry objects from the database
1080
1081         if(runNumber < 0){
1082                 // RunNumber is not specified. Try with fRun
1083                 if (fRun < 0){
1084                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1085                         return NULL;
1086                 }
1087                 runNumber = fRun;
1088         }
1089
1090         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
1091                         subVersion));
1092 }
1093
1094 //_____________________________________________________________________________
1095 TList* AliCDBManager::GetAll(const AliCDBPath& path,
1096         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1097 // get multiple AliCDBEntry objects from the database
1098
1099         return GetAll(AliCDBId(path, runRange, version, subVersion));
1100 }
1101
1102 //_____________________________________________________________________________
1103 TList* AliCDBManager::GetAll(const AliCDBId& query) {
1104 // get multiple AliCDBEntry objects from the database
1105 // Warning: this method works correctly only for queries of the type "Detector/*"
1106 //              and not for more specific queries e.g. "Detector/Calib/*" !
1107 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
1108 //              To get an object from cache use Get() function
1109
1110         if(!fDefaultStorage) {
1111                 AliError("No storage set!");
1112                 return NULL;
1113         }
1114
1115         if (!query.IsValid()) {
1116                 AliError(Form("Invalid query: %s", query.ToString().Data()));
1117                 return NULL;
1118         }
1119
1120         if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1121                 // if specific storages are active a query with "*" is ambiguous
1122                 AliError("Query too generic in this context!");
1123                 return NULL;
1124         }
1125
1126         if (query.IsAnyRange()) {
1127                 AliError(Form("Unspecified run or runrange: %s",
1128                                 query.ToString().Data()));
1129                 return NULL;
1130         }
1131
1132         if(fLock && query.GetFirstRun() != fRun)
1133                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
1134         
1135         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1136
1137         AliCDBStorage *aStorage;
1138         if(aPar) {
1139                 aStorage=GetStorage(aPar);
1140                 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1141
1142         } else {
1143                 aStorage=GetDefaultStorage();
1144                 AliDebug(2,"Looking into default storage");
1145         }
1146
1147         TList *result = 0;
1148         if(aStorage) result = aStorage->GetAll(query);
1149         if(!result) return 0;
1150
1151        // loop on result to check whether entries should be re-queried with specific storages
1152         if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1153                 AliInfo("Now look into all other specific storages...");
1154
1155                 TIter iter(result);
1156                 AliCDBEntry* chkEntry=0;
1157
1158                 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1159                         AliCDBId& chkId = chkEntry->GetId();
1160                         AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1161                         AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1162                         if (!chkPar || aPar == chkPar) continue;
1163                         AliCDBStorage *chkStorage = GetStorage(chkPar);
1164                         AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1165
1166                         AliCDBEntry *newEntry=0;
1167                         chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1168                         chkId.SetVersion(query.GetVersion());
1169                         chkId.SetSubVersion(query.GetSubVersion());
1170
1171                         if(chkStorage) newEntry = chkStorage->Get(chkId);
1172                         if(!newEntry) continue;
1173
1174                         // object is found in specific storage: replace entry in the result list!
1175                         chkEntry->SetOwner(1);
1176                         delete result->Remove(chkEntry);
1177                         result->AddFirst(newEntry);
1178                 }
1179
1180                 Int_t nEntries = result->GetEntries();
1181                 AliInfo("After look into other specific storages, result list is:");
1182                 for(int i=0; i<nEntries;i++){
1183                         AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1184                         AliInfo(Form("%s",entry->GetId().ToString().Data()));
1185                 }
1186         }
1187
1188         // caching entries
1189         TIter iter(result);
1190         AliCDBEntry* entry=0;
1191         while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1192
1193                 if(!fIds->Contains(&entry->GetId())){
1194                         fIds->Add(entry->GetId().Clone());
1195                 }
1196                 if(fCache && (query.GetFirstRun() == fRun)){
1197                         CacheEntry(entry->GetId().GetPath(), entry);
1198                 }
1199         }
1200
1201
1202         return result;
1203 }
1204
1205 //_____________________________________________________________________________
1206 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){
1207 // store an AliCDBEntry object into the database
1208
1209         if (object==0x0) {
1210                 AliError("Null Entry! No storage will be done!");
1211                 return kFALSE;
1212         } 
1213
1214         AliCDBEntry anEntry(object, id, metaData);
1215         return Put(&anEntry, type);
1216
1217 }
1218
1219
1220 //_____________________________________________________________________________
1221 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
1222 // store an AliCDBEntry object into the database
1223
1224         if(type == kPrivate && !fDefaultStorage) {
1225                 AliError("No storage set!");
1226                 return kFALSE;
1227         }
1228
1229         if (!entry){
1230                 AliError("No entry!");
1231                 return kFALSE;
1232         }
1233
1234         if (entry->GetObject()==0x0){
1235                 AliError("No valid object in CDB entry!");
1236                 return kFALSE;
1237         }
1238
1239         if (!entry->GetId().IsValid()) {
1240                 AliError(Form("Invalid entry ID: %s", 
1241                         entry->GetId().ToString().Data()));
1242                 return kFALSE;
1243         }       
1244
1245         if (!entry->GetId().IsSpecified()) {
1246                 AliError(Form("Unspecified entry ID: %s", 
1247                         entry->GetId().ToString().Data()));
1248                 return kFALSE;
1249         }
1250
1251         AliCDBId id = entry->GetId();
1252         AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1253
1254         AliCDBStorage *aStorage=0;
1255         
1256         if(aPar) {
1257                 aStorage=GetStorage(aPar);
1258         } else {
1259                 switch(type){
1260                         case kCondition:
1261                                 aStorage = GetStorage(fCondParam);
1262                                 break;
1263                         case kReference:
1264                                 aStorage = GetStorage(fRefParam);
1265                                 break;
1266                         case kPrivate:
1267                                 aStorage = GetDefaultStorage();
1268                                 break;
1269                 }
1270         }
1271
1272         AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1273
1274         Bool_t result = aStorage->Put(entry, type);
1275
1276         if(fRun >= 0) QueryCDB();
1277
1278         return result;
1279
1280
1281 }
1282
1283 //_____________________________________________________________________________
1284 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
1285 {
1286 // cache AliCDBEntry. Cache is valid until run number is changed.
1287
1288         AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1289
1290         if(chkEntry) {
1291                 AliDebug(2, Form("Object %s already in cache !!", path));
1292                 return;
1293         } else {
1294                 AliDebug(2,Form("Caching entry %s", path));
1295         }
1296
1297         fEntryCache.Add(new TObjString(path), entry);
1298         AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1299
1300 }
1301
1302 //_____________________________________________________________________________
1303 void AliCDBManager::Print(Option_t* /*option*/) const
1304 {
1305 // Print list of active storages and their URIs
1306
1307         TString output=Form("Run number = %d; ",fRun);
1308         output += "Cache is ";
1309         if(!fCache) output += "NOT ";
1310         output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1311
1312         if(fDefaultStorage) {
1313                 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1314 //              AliInfo(output.Data());
1315         }
1316         if(fSpecificStorages.GetEntries()>0) {
1317                 TIter iter(fSpecificStorages.GetTable());
1318                 TPair *aPair=0;
1319                 Int_t i=1;
1320                 while((aPair = (TPair*) iter.Next())){
1321                         output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1322                                 i++, ((TObjString*) aPair->Key())->GetName(),
1323                                 ((AliCDBParam*) aPair->Value())->GetURI().Data());
1324                 }
1325         }
1326         if(fDrainStorage) {
1327                 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1328         }
1329         AliInfo(output.Data());
1330 }
1331
1332 //_____________________________________________________________________________
1333 void AliCDBManager::SetRun(Int_t run)
1334 {
1335 // Sets current run number.
1336 // When the run number changes the caching is cleared.
1337         
1338         if(fRun == run)
1339                 return;
1340   
1341         if(fLock && fRun >= 0) {
1342                 AliFatal("Lock is ON, cannot reset run number!");
1343         }       
1344                 
1345         fRun = run;
1346         if(fRaw){
1347                 // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1348                 if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1349                         AliInfo("LHCPeriod alien folder for current run already in memory");
1350                 }else{
1351                         SetDefaultStorageFromRun(run);
1352                         if(fEntryCache.GetEntries()!=0) ClearCache();
1353                         return;
1354                 }
1355         }
1356         ClearCache();
1357         QueryCDB();
1358 }
1359
1360 //_____________________________________________________________________________
1361 void AliCDBManager::ClearCache(){
1362 // clear AliCDBEntry cache
1363
1364         AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1365         
1366         /*
1367         // To clean entries one by one
1368         TIter iter(fEntryCache.GetTable());
1369         TPair* pair=0;
1370         while((pair= dynamic_cast<TPair*> (iter.Next()))){
1371         
1372                 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1373                 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1374                 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1375                 if (entry) delete entry;
1376                 delete fEntryCache.Remove(key);
1377         }
1378         */
1379         fEntryCache.DeleteAll();
1380         AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1381 }
1382
1383 //_____________________________________________________________________________
1384 void AliCDBManager::UnloadFromCache(const char* path){
1385 // unload cached object
1386 // that is remove the entry from the cache and the id from the list of ids
1387 //
1388         if(!fActiveStorages.GetEntries()) {
1389                 AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1390                 return;
1391         }
1392
1393         AliCDBPath queryPath(path);
1394         if(!queryPath.IsValid()) return;
1395
1396         if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1397                 if(fEntryCache.Contains(path)){
1398                         AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1399                         TObjString pathStr(path);
1400                         delete fEntryCache.Remove(&pathStr);
1401                         TIter iter(fIds);
1402                         AliCDBId *id = 0;
1403                         while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1404                             if(queryPath.Comprises(id->GetPath()))
1405                                 delete fIds->Remove(id);
1406                         }
1407                 } else {
1408                   AliError(Form("Cache does not contain object \"%s\"!", path));
1409                 }
1410                 AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1411                 return;
1412         }
1413
1414         // path is wildcard: loop on the cache and unload all comprised objects!
1415         TIter iter(fEntryCache.GetTable());
1416         TPair* pair = 0;
1417         Int_t removed=0;
1418
1419         while((pair = dynamic_cast<TPair*> (iter.Next()))){
1420                 AliCDBPath entryPath = pair->Key()->GetName();
1421                 if(queryPath.Comprises(entryPath)) {
1422                         AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1423                         TObjString pathStr(entryPath.GetPath());
1424                         delete fEntryCache.Remove(&pathStr);
1425
1426                         TIter iterids(fIds);
1427                         AliCDBId *anId = 0;
1428                         while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1429                             AliCDBPath aPath = anId->GetPath();
1430                             TString aPathStr = aPath.GetPath();
1431                             if(queryPath.Comprises(aPath)) {
1432                                 delete fIds->Remove(anId);
1433                                 removed++;
1434                             }
1435                         }
1436                 }
1437         }
1438         AliDebug(2,Form("Cache entries and ids removed: %d   Remaining: %d",removed,fEntryCache.GetEntries()));
1439 }
1440
1441 //_____________________________________________________________________________
1442 void AliCDBManager::DestroyActiveStorages() {
1443 // delete list of active storages
1444
1445         fActiveStorages.DeleteAll();
1446         fSpecificStorages.DeleteAll();
1447 }
1448
1449 //_____________________________________________________________________________
1450 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1451 // destroys active storage
1452
1453 /*
1454         TIter iter(fActiveStorages.GetTable());
1455         TPair* aPair;
1456         while ((aPair = (TPair*) iter.Next())) {
1457                 if(storage == (AliCDBStorage*) aPair->Value())
1458                         delete fActiveStorages.Remove(aPair->Key());
1459                         storage->Delete(); storage=0x0;
1460         }
1461 */
1462
1463 }
1464
1465 //_____________________________________________________________________________
1466 void AliCDBManager::QueryCDB() {
1467 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1468
1469         if (fRun < 0){
1470                 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1471         return;
1472         }
1473         if (!fDefaultStorage){
1474                 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1475         return;
1476         }
1477         if(fDefaultStorage->GetType() == "alien"){
1478                 fDefaultStorage->QueryCDB(fRun);
1479         } else {
1480                 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1481         }
1482
1483         TIter iter(&fSpecificStorages);
1484         TObjString *aCalibType=0;
1485         AliCDBParam* aPar=0;
1486         while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1487                 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1488                 if(aPar) {
1489                         AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1490                         AliCDBStorage *aStorage = GetStorage(aPar);
1491                         if(aStorage->GetType() == "alien"){
1492                                 aStorage->QueryCDB(fRun,aCalibType->GetName());
1493                         } else {
1494                                 AliDebug(2,
1495                                         "Skipping query for valid files, it is used only in grid...");
1496                         }
1497                 }
1498         }
1499 }
1500
1501 //______________________________________________________________________________________________
1502 const char* AliCDBManager::GetDataTypeName(DataType type)
1503 {
1504   // returns the name (string) of the data type
1505
1506       switch (type){
1507             case kCondition: return "Conditions";
1508             case kReference: return "Reference";
1509             case kPrivate: return "Private";
1510      }
1511      return 0;
1512
1513 }
1514
1515 //______________________________________________________________________________________________
1516 void AliCDBManager::InitShortLived()
1517 {
1518   // Init the list of short-lived objects
1519   // currently disabled
1520
1521         fShortLived=0x0;
1522
1523 //      fShortLived = new TList();
1524 //      fShortLived->SetOwner(1);
1525 //
1526 //      fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1527 // 
1528 //      fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1529 //      fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1530 // 
1531 //      fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1532 // 
1533 //      fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1534 //      fShortLived->Add(new TObjString("MUON/Calib/HV"));
1535 //      fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1536 // 
1537 //      fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1538 //      fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1539 // 
1540 //      fShortLived->Add(new TObjString("PMD/Calib/Data"));
1541 // 
1542 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1543 //      fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1544 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1545 //      fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1546 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1547 //      fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1548 // 
1549 //      fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1550
1551 }
1552
1553 //______________________________________________________________________________________________
1554 Bool_t AliCDBManager::IsShortLived(const char* path)
1555 {
1556   // returns the name (string) of the data type
1557
1558         if(!fShortLived) return kFALSE;
1559
1560         AliCDBPath aPath(path);
1561         if(!aPath.IsValid()){
1562                 AliError(Form("Not a valid path: %s", path));
1563                 return kFALSE;
1564         }
1565
1566         return fShortLived->Contains(path);
1567
1568 }
1569
1570 //______________________________________________________________________________________________
1571 ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){
1572   // To lock/unlock user must provide the key. A new key is provided after
1573   // each successful lock. User should always backup the returned key and
1574   // use it on next access.
1575   if (fLock == lock) return 0;  // nothing to be done
1576   if (lock) {
1577     // User wants to lock - check his identity
1578     if (fKey) {
1579       // Lock has a user - check his key
1580       if (fKey != key) {
1581         AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1582         return 0;
1583       }  
1584     }  
1585     // Provide new key 
1586     fKey = gSystem->Now();
1587     fLock = kTRUE;
1588     return fKey;
1589   }
1590   // User wants to unlock - check the provided key
1591   if (key != fKey) {
1592     AliFatal("Lock is ON: wrong key provided");
1593     return 0;
1594   }  
1595   fLock = kFALSE;
1596   return key;  
1597 }
1598
1599 ///////////////////////////////////////////////////////////
1600 // AliCDBManager Parameter class                         //
1601 // interface to specific AliCDBParameter class           //
1602 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
1603 ///////////////////////////////////////////////////////////
1604
1605 AliCDBParam::AliCDBParam():
1606   fType(),
1607   fURI()
1608 {
1609 // constructor
1610
1611 }
1612
1613 //_____________________________________________________________________________
1614 AliCDBParam::~AliCDBParam() {
1615 // destructor
1616
1617 }
1618