]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBManager.cxx
New revision of the CTP configuration and simulation. For more details look in the...
[u/mrichter/AliRoot.git] / STEER / 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 "AliCDBManager.h"
22 #include "AliCDBStorage.h"
23 #include "AliLog.h"
24 #include "AliCDBDump.h"
25 #include "AliCDBLocal.h"
26 #include "AliCDBGrid.h"
27 #include "AliCDBEntry.h"
28 #include "AliCDBMetaData.h"
29
30 #include <TObjString.h>
31 #include <TSystem.h>
32
33 ClassImp(AliCDBParam)
34
35 ClassImp(AliCDBManager)
36
37 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
38 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
39 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
40 AliCDBManager* AliCDBManager::fgInstance = 0x0;
41
42 //_____________________________________________________________________________
43 AliCDBManager* AliCDBManager::Instance()
44 {
45 // returns AliCDBManager instance (singleton)
46
47         if (!fgInstance) {
48                 fgInstance = new AliCDBManager();
49                 fgInstance->Init();
50         }
51
52         return fgInstance;
53 }
54
55 //_____________________________________________________________________________
56 void AliCDBManager::Init() {
57 // factory registering
58
59         RegisterFactory(new AliCDBDumpFactory());
60         RegisterFactory(new AliCDBLocalFactory()); 
61         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
62         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
63                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
64                 RegisterFactory(new AliCDBGridFactory());
65                 fCondParam = CreateParameter(fgkCondUri);
66                 fRefParam = CreateParameter(fgkRefUri);
67         }
68
69         InitShortLived();
70 }
71 //_____________________________________________________________________________
72 void AliCDBManager::Destroy() {
73 // delete ALCDBManager instance and active storages
74
75         if (fgInstance) {
76                 //fgInstance->Delete();
77                 delete fgInstance;
78                 fgInstance = 0x0;
79         }
80 }
81
82 //_____________________________________________________________________________
83 AliCDBManager::AliCDBManager():
84   TObject(),
85   fFactories(),
86   fActiveStorages(),
87   fSpecificStorages(),
88   fEntryCache(),
89   fIds(0),
90   fStorageMap(0),
91   fShortLived(0),
92   fDefaultStorage(NULL),
93   fDrainStorage(NULL),
94   fCondParam(0),
95   fRefParam(0),
96   fRun(-1),
97   fCache(kTRUE),
98   fLock(kFALSE)
99 {
100 // default constuctor
101         fFactories.SetOwner(1);
102         fActiveStorages.SetOwner(1);
103         fSpecificStorages.SetOwner(1);
104         fEntryCache.SetOwner(1);
105
106         fStorageMap = new TMap();
107         fStorageMap->SetOwner(1);
108         fIds = new TList();
109         fIds->SetOwner(1);
110 }
111
112 //_____________________________________________________________________________
113 AliCDBManager::~AliCDBManager() {
114 // destructor
115         ClearCache();
116         DestroyActiveStorages();
117         fFactories.Delete();
118         fDrainStorage = 0x0;
119         fDefaultStorage = 0x0;
120         delete fStorageMap; fStorageMap = 0;
121         delete fIds; fIds = 0;
122         delete fCondParam;
123         delete fRefParam;
124         delete fShortLived; fShortLived = 0x0;
125 }
126
127 //_____________________________________________________________________________
128 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
129 // put a storage object into the list of active storages
130
131         fActiveStorages.Add(param, storage);
132         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
133 }
134
135 //_____________________________________________________________________________
136 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
137 // add a storage factory to the list of registerd factories
138  
139         if (!fFactories.Contains(factory)) {
140                 fFactories.Add(factory);
141         }
142 }
143
144 //_____________________________________________________________________________
145 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
146 // check if dbString is a URI valid for one of the registered factories
147
148         TIter iter(&fFactories);
149
150         AliCDBStorageFactory* factory=0;
151         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
152
153                 if (factory->Validate(dbString)) {
154                         return kTRUE;
155                 }
156         }
157
158         return kFALSE;
159 }
160
161 //_____________________________________________________________________________
162 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
163 // create AliCDBParam object from URI string
164
165         TIter iter(&fFactories);
166
167         AliCDBStorageFactory* factory=0;
168         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
169                 AliCDBParam* param = factory->CreateParameter(dbString);
170                 if(param) return param;
171         }
172
173         return NULL;
174 }
175
176 //_____________________________________________________________________________
177 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
178 // get storage object from URI string
179                 
180         AliCDBParam* param = CreateParameter(dbString);
181         if (!param) {
182                 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
183                 return NULL;
184         }
185
186         AliCDBStorage* aStorage = GetStorage(param);
187
188         delete param;
189         return aStorage;
190 }
191
192 //_____________________________________________________________________________
193 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
194 // get storage object from AliCDBParam object
195         
196         // if the list of active storages already contains
197         // the requested storage, return it
198         AliCDBStorage* aStorage = GetActiveStorage(param);
199         if (aStorage) {
200                 return aStorage;
201         }
202
203         // if lock is ON, cannot activate more storages!
204         if(fLock) {
205                 if (fDefaultStorage) {
206                         AliFatal("Lock is ON, and default storage is already set: "
207                                 "cannot reset it or activate more storages!");
208                 }
209         }       
210         
211         TIter iter(&fFactories);
212
213         AliCDBStorageFactory* factory=0;
214
215         // loop on the list of registered factories
216         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
217
218                 // each factory tries to create its storage from the parameter
219                 aStorage = factory->Create(param);
220                 if (aStorage) {
221                         PutActiveStorage(param->CloneParam(), aStorage);
222                         aStorage->SetURI(param->GetURI());
223                         if(fRun >= 0) {
224                                 if(aStorage->GetType() == "alien"){
225                                         aStorage->QueryCDB(fRun);
226                                 } else {
227                                         AliDebug(2,
228                                                 "Skipping query for valid files, it is used only in grid...");
229                                 }
230                         }
231                         return aStorage;
232                 }
233         }
234
235         AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
236
237         return NULL;
238 }
239
240 //_____________________________________________________________________________
241 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
242 // get a storage object from the list of active storages
243
244         return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
245 }
246
247 //_____________________________________________________________________________
248 TList* AliCDBManager::GetActiveStorages() {
249 // return list of active storages
250 // user has responsibility to delete returned object
251
252         TList* result = new TList();
253
254         TIter iter(fActiveStorages.GetTable());
255         TPair* aPair=0;
256         while ((aPair = (TPair*) iter.Next())) {
257                 result->Add(aPair->Value());
258         }
259
260         return result;
261 }
262
263 //_____________________________________________________________________________
264 void AliCDBManager::SetDrain(const char* dbString) {
265 // set drain storage from URI string
266
267         fDrainStorage = GetStorage(dbString);   
268 }
269
270 //_____________________________________________________________________________
271 void AliCDBManager::SetDrain(const AliCDBParam* param) {
272 // set drain storage from AliCDBParam
273
274         fDrainStorage = GetStorage(param);
275 }
276
277 //_____________________________________________________________________________
278 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
279 // set drain storage from another active storage
280         
281         fDrainStorage = storage;
282 }
283
284 //_____________________________________________________________________________
285 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
286 // drain retrieved object to drain storage
287
288         AliDebug(2, "Draining into drain storage...");
289         return fDrainStorage->Put(entry);
290 }
291
292 //_____________________________________________________________________________
293 void AliCDBManager::SetDefaultStorage(const char* dbString) {
294 // sets default storage from URI string
295         
296         AliInfo(Form("Setting Default storage to: %s",dbString));
297         AliCDBStorage* bckStorage = fDefaultStorage;
298
299         fDefaultStorage = GetStorage(dbString);
300
301         if(bckStorage && (fDefaultStorage != bckStorage)){
302                 AliWarning("Existing default storage replaced: clearing cache!");
303                 ClearCache();
304         }
305
306         if (fStorageMap->Contains("default")) {
307                 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
308         }
309         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
310 }
311
312 //_____________________________________________________________________________
313 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
314 // set default storage from AliCDBParam object
315         
316         AliCDBStorage* bckStorage = fDefaultStorage;
317
318         fDefaultStorage = GetStorage(param);
319
320         if(bckStorage && (fDefaultStorage != bckStorage)){
321                 AliWarning("Existing default storage replaced: clearing cache!");
322                 ClearCache();
323         }
324
325         if (fStorageMap->Contains("default")) {
326                 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
327         }
328         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
329 }
330
331 //_____________________________________________________________________________
332 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
333 // set default storage from another active storage
334         
335         // if lock is ON, cannot activate more storages!
336         if(fLock) {
337                 if (fDefaultStorage) {
338                         AliFatal("Lock is ON, and default storage is already set: "
339                                 "cannot reset it or activate more storages!");
340                 }
341         }       
342         
343         if (!storage) {
344                 UnsetDefaultStorage();
345                 return;
346         }
347         
348         AliCDBStorage* bckStorage = fDefaultStorage;
349
350         fDefaultStorage = storage;
351
352         if(bckStorage && (fDefaultStorage != bckStorage)){
353                 AliWarning("Existing default storage replaced: clearing cache!");
354                 ClearCache();
355         }
356
357         if (fStorageMap->Contains("default")) {
358                 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
359         }
360         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
361 }
362
363 //_____________________________________________________________________________
364 void AliCDBManager::UnsetDefaultStorage() {
365 // Unset default storage
366         
367         // if lock is ON, action is forbidden!
368         if(fLock) {
369                 if (fDefaultStorage) {
370                         AliFatal("Lock is ON: cannot unset default storage!");
371                 }
372         }
373         
374         if (fDefaultStorage) {
375                 AliWarning("Clearing cache!");
376                 ClearCache();
377         }
378         
379         fDefaultStorage = 0x0;
380 }
381
382 //_____________________________________________________________________________
383 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
384 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
385
386         AliCDBParam *aPar = CreateParameter(dbString);
387         if(!aPar) return;
388         SetSpecificStorage(calibType, aPar);
389         delete aPar;
390 }
391
392 //_____________________________________________________________________________
393 void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
394 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
395 // Default storage should be defined prior to any specific storages, e.g.:
396 // AliCDBManager::instance()->SetDefaultStorage("alien://");
397 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
398 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
399 // calibType must be a valid CDB path! (3 level folder structure)
400
401         if(!fDefaultStorage) {
402                 AliError("Please activate a default storage first!");
403                 return;
404         }
405
406         AliCDBPath aPath(calibType);
407         if(!aPath.IsValid()){
408                 AliError(Form("Not a valid path: %s", calibType));
409                 return;
410         }
411
412         TObjString *objCalibType = new TObjString(aPath.GetPath());
413         if(fSpecificStorages.Contains(objCalibType)){
414                 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
415                                         calibType));
416                 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
417                 if(checkPar) delete checkPar;
418                 delete fSpecificStorages.Remove(objCalibType);
419         }
420         GetStorage(param);
421
422         fSpecificStorages.Add(objCalibType, param->CloneParam());
423
424         if(fStorageMap->Contains(objCalibType)){
425                 delete fStorageMap->Remove(objCalibType);
426         }
427         fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
428
429 }
430
431 //_____________________________________________________________________________
432 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
433 // get storage specific for detector or calibration type 
434
435         AliCDBPath calibPath(calibType);
436         if(!calibPath.IsValid()) return NULL;
437
438         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
439         if(!checkPar){
440                 AliError(Form("%s storage not found!", calibType));
441                 return NULL;
442         } else {
443                 return GetStorage(checkPar);
444         }
445
446 }
447
448 //_____________________________________________________________________________
449 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
450 // select storage valid for path from the list of specific storages
451
452         AliCDBPath aPath(path);
453         if(!aPath.IsValid()) return NULL;
454
455         TIter iter(&fSpecificStorages);
456         TObjString *aCalibType=0;
457         AliCDBPath tmpPath("null/null/null");
458         AliCDBParam* aPar=0;
459         while((aCalibType = (TObjString*) iter.Next())){
460                 AliCDBPath calibTypePath(aCalibType->GetName());
461                 if(calibTypePath.Comprises(aPath)) {
462                         if(calibTypePath.Comprises(tmpPath)) continue;
463                         aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
464                         tmpPath.SetPath(calibTypePath.GetPath());
465                 }
466         }
467         return aPar;
468 }
469
470 //_____________________________________________________________________________
471 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
472         Int_t version, Int_t subVersion) {
473 // get an AliCDBEntry object from the database
474
475         if(runNumber < 0){
476                 // RunNumber is not specified. Try with fRun
477                 if (fRun < 0){
478                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
479                         return NULL;
480                 }
481                 runNumber = fRun;
482         }
483
484         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
485 }
486
487 //_____________________________________________________________________________
488 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
489         const AliCDBRunRange& runRange, Int_t version,
490         Int_t subVersion) {
491 // get an AliCDBEntry object from the database!
492
493         return Get(AliCDBId(path, runRange, version, subVersion));
494 }
495
496 //_____________________________________________________________________________
497 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
498 // get an AliCDBEntry object from the database
499         
500         if(!fDefaultStorage) {
501                 AliError("No storage set!");
502                 return NULL;
503         }
504
505         // check if query's path and runRange are valid
506         // query is invalid also if version is not specified and subversion is!
507         if (!query.IsValid()) {
508                 AliError(Form("Invalid query: %s", query.ToString().Data()));
509                 return NULL;
510         }
511         
512         // query is not specified if path contains wildcard or run range= [-1,-1]
513         if (!query.IsSpecified()) {
514                 AliError(Form("Unspecified query: %s",
515                                 query.ToString().Data()));
516                 return NULL;
517         }
518
519         if(fLock && query.GetFirstRun() != fRun)
520                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
521         
522         if(fCache && query.GetFirstRun() != fRun)
523                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
524
525         AliCDBEntry *entry=0;
526
527         // first look into map of cached objects
528         if(fCache && query.GetFirstRun() == fRun)
529                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
530
531         if(entry) {
532                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
533                 return entry;
534         }
535
536         // Entry is not in cache -> retrieve it from CDB and cache it!!
537         AliCDBStorage *aStorage=0;
538         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
539 //      Bool_t usedDefStorage=kTRUE;
540
541         if(aPar) {
542                 aStorage=GetStorage(aPar);
543                 TString str = aPar->GetURI();
544                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
545 //              usedDefStorage=kFALSE;
546
547         } else {
548                 aStorage=GetDefaultStorage();
549                 AliDebug(2,"Looking into default storage");
550         }
551
552         entry = aStorage->Get(query);
553
554         if(entry && fCache && (query.GetFirstRun() == fRun)){
555                 CacheEntry(query.GetPath(), entry);
556         }
557
558         if(entry && !fIds->Contains(&entry->GetId())){
559                 fIds->Add(entry->GetId().Clone());
560         }
561
562
563         return entry;
564
565 }
566
567 //_____________________________________________________________________________
568 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
569         Int_t version, Int_t subVersion) {
570 // get the AliCDBId of the valid object from the database (does not retrieve the object)
571 // User must delete returned object!
572
573         if(runNumber < 0){
574                 // RunNumber is not specified. Try with fRun
575                 if (fRun < 0){
576                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
577                         return NULL;
578                 }
579                 runNumber = fRun;
580         }
581
582         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
583 }
584
585 //_____________________________________________________________________________
586 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
587         const AliCDBRunRange& runRange, Int_t version,
588         Int_t subVersion) {
589 // get the AliCDBId of the valid object from the database (does not retrieve the object)
590 // User must delete returned object!
591
592         return GetId(AliCDBId(path, runRange, version, subVersion));
593 }
594
595 //_____________________________________________________________________________
596 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
597 // get the AliCDBId of the valid object from the database (does not retrieve the object)
598 // User must delete returned object!
599
600         if(!fDefaultStorage) {
601                 AliError("No storage set!");
602                 return NULL;
603         }
604
605         // check if query's path and runRange are valid
606         // query is invalid also if version is not specified and subversion is!
607         if (!query.IsValid()) {
608                 AliError(Form("Invalid query: %s", query.ToString().Data()));
609                 return NULL;
610         }
611         
612         // query is not specified if path contains wildcard or run range= [-1,-1]
613         if (!query.IsSpecified()) {
614                 AliError(Form("Unspecified query: %s",
615                                 query.ToString().Data()));
616                 return NULL;
617         }
618
619         if(fCache && query.GetFirstRun() != fRun)
620                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
621
622         AliCDBEntry* entry = 0;
623
624         // first look into map of cached objects
625         if(fCache && query.GetFirstRun() == fRun)
626                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
627
628         if(entry) {
629                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
630                 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
631         }
632
633         // Entry is not in cache -> retrieve it from CDB and cache it!!
634         AliCDBStorage *aStorage=0;
635         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
636
637         if(aPar) {
638                 aStorage=GetStorage(aPar);
639                 TString str = aPar->GetURI();
640                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
641                 
642         } else {
643                 aStorage=GetDefaultStorage();
644                 AliDebug(2,"Looking into default storage");
645         }
646
647         return aStorage->GetId(query);
648
649 }
650
651 //_____________________________________________________________________________
652 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
653         Int_t version, Int_t subVersion) {
654 // get multiple AliCDBEntry objects from the database
655
656         if(runNumber < 0){
657                 // RunNumber is not specified. Try with fRun
658                 if (fRun < 0){
659                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
660                         return NULL;
661                 }
662                 runNumber = fRun;
663         }
664
665         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
666                         subVersion));
667 }
668
669 //_____________________________________________________________________________
670 TList* AliCDBManager::GetAll(const AliCDBPath& path,
671         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
672 // get multiple AliCDBEntry objects from the database
673
674         return GetAll(AliCDBId(path, runRange, version, subVersion));
675 }
676
677 //_____________________________________________________________________________
678 TList* AliCDBManager::GetAll(const AliCDBId& query) {
679 // get multiple AliCDBEntry objects from the database
680 // Warning: this method works correctly only for queries of the type "Detector/*"
681 //              and not for more specific queries e.g. "Detector/Calib/*" !
682 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
683 //              To get an object from cache use Get() function
684
685         if(!fDefaultStorage) {
686                 AliError("No storage set!");
687                 return NULL;
688         }
689
690         if (!query.IsValid()) {
691                 AliError(Form("Invalid query: %s", query.ToString().Data()));
692                 return NULL;
693         }
694
695         if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
696                 // if specific storages are active a query with "*" is ambiguous
697                 AliError("Query too generic in this context!");
698                 return NULL;
699         }
700
701         if (query.IsAnyRange()) {
702                 AliError(Form("Unspecified run or runrange: %s",
703                                 query.ToString().Data()));
704                 return NULL;
705         }
706
707         if(fLock && query.GetFirstRun() != fRun)
708                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
709         
710         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
711
712         AliCDBStorage *aStorage;
713         if(aPar) {
714                 aStorage=GetStorage(aPar);
715                 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
716
717         } else {
718                 aStorage=GetDefaultStorage();
719                 AliDebug(2,"Looking into default storage");
720         }
721
722         TList *result = 0;
723         if(aStorage) result = aStorage->GetAll(query);
724         if(!result) return 0;
725
726        // loop on result to check whether entries should be re-queried with specific storages
727         if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
728                 AliInfo("Now look into all other specific storages...");
729
730                 TIter iter(result);
731                 AliCDBEntry* chkEntry=0;
732
733                 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
734                         AliCDBId& chkId = chkEntry->GetId();
735                         AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
736                         AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
737                         if (!chkPar || aPar == chkPar) continue;
738                         AliCDBStorage *chkStorage = GetStorage(chkPar);
739                         AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
740
741                         AliCDBEntry *newEntry=0;
742                         chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
743                         chkId.SetVersion(query.GetVersion());
744                         chkId.SetSubVersion(query.GetSubVersion());
745
746                         if(chkStorage) newEntry = chkStorage->Get(chkId);
747                         if(!newEntry) continue;
748
749                         // object is found in specific storage: replace entry in the result list!
750                         chkEntry->SetOwner(1);
751                         delete result->Remove(chkEntry);
752                         result->AddFirst(newEntry);
753                 }
754
755                 Int_t nEntries = result->GetEntries();
756                 AliInfo("After look into other specific storages, result list is:");
757                 for(int i=0; i<nEntries;i++){
758                         AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
759                         AliInfo(Form("%s",entry->GetId().ToString().Data()));
760                 }
761         }
762
763         // caching entries
764         TIter iter(result);
765         AliCDBEntry* entry=0;
766         while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
767
768                 if(!fIds->Contains(&entry->GetId())){
769                         fIds->Add(entry->GetId().Clone());
770                 }
771                 if(fCache && (query.GetFirstRun() == fRun)){
772                         CacheEntry(entry->GetId().GetPath(), entry);
773                 }
774         }
775
776
777         return result;
778 }
779
780 //_____________________________________________________________________________
781 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData, DataType type){
782 // store an AliCDBEntry object into the database
783
784         AliCDBEntry anEntry(object, id, metaData);
785         return Put(&anEntry, type);
786
787 }
788
789
790 //_____________________________________________________________________________
791 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
792 // store an AliCDBEntry object into the database
793
794         if(type == kPrivate && !fDefaultStorage) {
795                 AliError("No storage set!");
796                 return kFALSE;
797         }
798
799         if (!entry){
800                 AliError("No entry!");
801                 return kFALSE;
802         }
803
804         if (!entry->GetId().IsValid()) {
805                 AliError(Form("Invalid entry ID: %s", 
806                         entry->GetId().ToString().Data()));
807                 return kFALSE;
808         }       
809
810         if (!entry->GetId().IsSpecified()) {
811                 AliError(Form("Unspecified entry ID: %s", 
812                         entry->GetId().ToString().Data()));
813                 return kFALSE;
814         }
815
816         AliCDBId id = entry->GetId();
817         AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
818
819         AliCDBStorage *aStorage=0;
820         
821         if(aPar) {
822                 aStorage=GetStorage(aPar);
823         } else {
824                 switch(type){
825                         case kCondition:
826                                 aStorage = GetStorage(fCondParam);
827                                 break;
828                         case kReference:
829                                 aStorage = GetStorage(fRefParam);
830                                 break;
831                         case kPrivate:
832                                 aStorage = GetDefaultStorage();
833                                 break;
834                 }
835         }
836
837         AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
838
839         Bool_t result = aStorage->Put(entry, type);
840
841         if(fRun >= 0) QueryCDB();
842
843         return result;
844
845
846 }
847
848 //_____________________________________________________________________________
849 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
850 {
851 // cache AliCDBEntry. Cache is valid until run number is changed.
852
853         AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
854
855         if(chkEntry) {
856                 AliDebug(2, Form("Object %s already in cache !!", path));
857                 return;
858         } else {
859                 AliDebug(2,Form("Caching entry %s", path));
860         }
861
862         fEntryCache.Add(new TObjString(path), entry);
863         AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
864
865 }
866
867 //_____________________________________________________________________________
868 void AliCDBManager::Print(Option_t* /*option*/) const
869 {
870 // Print list of active storages and their URIs
871
872         TString output=Form("Run number = %d; ",fRun);
873         output += "Cache is ";
874         if(!fCache) output += "NOT ";
875         output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
876
877         if(fDefaultStorage) {
878                 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
879 //              AliInfo(output.Data());
880         }
881         if(fSpecificStorages.GetEntries()>0) {
882                 TIter iter(fSpecificStorages.GetTable());
883                 TPair *aPair=0;
884                 Int_t i=1;
885                 while((aPair = (TPair*) iter.Next())){
886                         output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
887                                 i++, ((TObjString*) aPair->Key())->GetName(),
888                                 ((AliCDBParam*) aPair->Value())->GetURI().Data());
889                 }
890         }
891         if(fDrainStorage) {
892                 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
893         }
894         AliInfo(output.Data());
895 }
896
897 //_____________________________________________________________________________
898 void AliCDBManager::SetRun(Int_t run)
899 {
900 // Sets current run number.
901 // When the run number changes the caching is cleared.
902         
903         if (fRun == run)
904                 return;
905   
906         if(fLock && fRun >= 0) {
907                 AliFatal("Lock is ON, cannot reset run number!");
908         }       
909                 
910         fRun = run;
911         ClearCache();
912         QueryCDB();
913 }
914
915 //_____________________________________________________________________________
916 void AliCDBManager::ClearCache(){
917 // clear AliCDBEntry cache
918
919         AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
920         
921         /*
922         // To clean entries one by one
923         TIter iter(fEntryCache.GetTable());
924         TPair* pair=0;
925         while((pair= dynamic_cast<TPair*> (iter.Next()))){
926         
927                 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
928                 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
929                 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
930                 if (entry) delete entry;
931                 delete fEntryCache.Remove(key);
932         }
933         */
934         fEntryCache.DeleteAll();
935         AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
936 }
937
938 //_____________________________________________________________________________
939 void AliCDBManager::UnloadFromCache(const char* path){
940 // unload cached object
941
942         AliCDBPath queryPath(path);
943         if(!queryPath.IsValid()) return;
944
945         if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
946                 if(fEntryCache.Contains(path)){
947                         AliInfo(Form("Unloading object \"%s\" from cache", path));
948                         TObjString pathStr(path);
949                         AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
950                         if(entry) delete entry;
951                         delete fEntryCache.Remove(&pathStr);
952                 } else {
953                         AliError(Form("Cache does not contain object \"%s\"!", path))
954                 }
955                 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
956                 return;
957         }
958
959         // path is wildcard: loop on the cache and unload all comprised objects!
960         TIter iter(fEntryCache.GetTable());
961         TPair* pair = 0;
962
963         while((pair = dynamic_cast<TPair*> (iter.Next()))){
964                 AliCDBPath entryPath = pair->Key()->GetName();
965                 if(queryPath.Comprises(entryPath)) {
966                         AliInfo(Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
967                         TObjString pathStr(entryPath.GetPath().Data());
968                         AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
969                         if(entry) delete entry;
970                         delete fEntryCache.Remove(&pathStr);
971                 }
972         }
973         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
974 }
975
976 //_____________________________________________________________________________
977 void AliCDBManager::DestroyActiveStorages() {
978 // delete list of active storages
979
980         fActiveStorages.DeleteAll();
981         fSpecificStorages.DeleteAll();
982 }
983
984 //_____________________________________________________________________________
985 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
986 // destroys active storage
987
988 /*
989         TIter iter(fActiveStorages.GetTable());
990         TPair* aPair;
991         while ((aPair = (TPair*) iter.Next())) {
992                 if(storage == (AliCDBStorage*) aPair->Value())
993                         delete fActiveStorages.Remove(aPair->Key());
994                         storage->Delete(); storage=0x0;
995         }
996 */
997
998 }
999
1000 //_____________________________________________________________________________
1001 void AliCDBManager::QueryCDB() {
1002 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1003
1004         if (fRun < 0){
1005                 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1006         return;
1007         }
1008         if (!fDefaultStorage){
1009                 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1010         return;
1011         }
1012         if(fDefaultStorage->GetType() == "alien"){
1013                 fDefaultStorage->QueryCDB(fRun);
1014         } else {
1015                 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1016         }
1017
1018         TIter iter(&fSpecificStorages);
1019         TObjString *aCalibType=0;
1020         AliCDBParam* aPar=0;
1021         while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1022                 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1023                 if(aPar) {
1024                         AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1025                         AliCDBStorage *aStorage = GetStorage(aPar);
1026                         if(aStorage->GetType() == "alien"){
1027                                 aStorage->QueryCDB(fRun,aCalibType->GetName());
1028                         } else {
1029                                 AliDebug(2,
1030                                         "Skipping query for valid files, it is used only in grid...");
1031                         }
1032                 }
1033         }
1034 }
1035
1036 //______________________________________________________________________________________________
1037 const char* AliCDBManager::GetDataTypeName(DataType type)
1038 {
1039   // returns the name (string) of the data type
1040
1041       switch (type){
1042             case kCondition: return "Conditions";
1043             case kReference: return "Reference";
1044             case kPrivate: return "Private";
1045      }
1046      return 0;
1047
1048 }
1049
1050 //______________________________________________________________________________________________
1051 void AliCDBManager::InitShortLived()
1052 {
1053   // Init the list of short-lived objects
1054   // currently disabled
1055
1056         fShortLived=0x0;
1057
1058 //      fShortLived = new TList();
1059 //      fShortLived->SetOwner(1);
1060 //
1061 //      fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1062 // 
1063 //      fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1064 //      fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1065 // 
1066 //      fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1067 // 
1068 //      fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1069 //      fShortLived->Add(new TObjString("MUON/Calib/HV"));
1070 //      fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1071 // 
1072 //      fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1073 //      fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1074 // 
1075 //      fShortLived->Add(new TObjString("PMD/Calib/Data"));
1076 // 
1077 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1078 //      fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1079 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1080 //      fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1081 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1082 //      fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1083 // 
1084 //      fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1085
1086 }
1087
1088 //______________________________________________________________________________________________
1089 Bool_t AliCDBManager::IsShortLived(const char* path)
1090 {
1091   // returns the name (string) of the data type
1092
1093         if(!fShortLived) return kFALSE;
1094
1095         AliCDBPath aPath(path);
1096         if(!aPath.IsValid()){
1097                 AliError(Form("Not a valid path: %s", path));
1098                 return kFALSE;
1099         }
1100
1101         return fShortLived->Contains(path);
1102
1103 }
1104
1105 //______________________________________________________________________________________________
1106 void AliCDBManager::SetLock(Bool_t lock){
1107
1108         if(fLock == kTRUE && lock == kFALSE) {
1109                 AliFatal("Lock is ON: cannot reset it!");
1110         }
1111         
1112         fLock=lock;
1113 }
1114
1115 ///////////////////////////////////////////////////////////
1116 // AliCDBManager Parameter class                         //
1117 // interface to specific AliCDBParameter class           //
1118 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
1119 ///////////////////////////////////////////////////////////
1120
1121 AliCDBParam::AliCDBParam():
1122   fType(),
1123   fURI()
1124 {
1125 // constructor
1126
1127 }
1128
1129 //_____________________________________________________________________________
1130 AliCDBParam::~AliCDBParam() {
1131 // destructor
1132
1133 }
1134