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