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