]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBManager.cxx
introducing SDD, SSD layer misal (Andrea Dainese)
[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(!fDefaultStorage) return;
302
303         if(bckStorage && (fDefaultStorage != bckStorage)){
304                 AliWarning("Existing default storage replaced: clearing cache!");
305                 ClearCache();
306         }
307
308         if (fStorageMap->Contains("default")) {
309                 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
310         }
311         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
312 }
313
314 //_____________________________________________________________________________
315 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
316 // set default storage from AliCDBParam object
317         
318         AliCDBStorage* bckStorage = fDefaultStorage;
319
320         fDefaultStorage = GetStorage(param);
321
322         if(!fDefaultStorage) return;
323
324         if(bckStorage && (fDefaultStorage != bckStorage)){
325                 AliWarning("Existing default storage replaced: clearing cache!");
326                 ClearCache();
327         }
328
329         if (fStorageMap->Contains("default")) {
330                 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
331         }
332         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
333 }
334
335 //_____________________________________________________________________________
336 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
337 // set default storage from another active storage
338         
339         // if lock is ON, cannot activate more storages!
340         if(fLock) {
341                 if (fDefaultStorage) {
342                         AliFatal("Lock is ON, and default storage is already set: "
343                                 "cannot reset it or activate more storages!");
344                 }
345         }       
346         
347         if (!storage) {
348                 UnsetDefaultStorage();
349                 return;
350         }
351         
352         AliCDBStorage* bckStorage = fDefaultStorage;
353
354         fDefaultStorage = storage;
355
356         if(bckStorage && (fDefaultStorage != bckStorage)){
357                 AliWarning("Existing default storage replaced: clearing cache!");
358                 ClearCache();
359         }
360
361         if (fStorageMap->Contains("default")) {
362                 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
363         }
364         fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
365 }
366
367 //_____________________________________________________________________________
368 void AliCDBManager::UnsetDefaultStorage() {
369 // Unset default storage
370         
371         // if lock is ON, action is forbidden!
372         if(fLock) {
373                 if (fDefaultStorage) {
374                         AliFatal("Lock is ON: cannot unset default storage!");
375                 }
376         }
377         
378         if (fDefaultStorage) {
379                 AliWarning("Clearing cache!");
380                 ClearCache();
381         }
382         
383         fDefaultStorage = 0x0;
384 }
385
386 //_____________________________________________________________________________
387 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
388 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
389
390         AliCDBParam *aPar = CreateParameter(dbString);
391         if(!aPar) return;
392         SetSpecificStorage(calibType, aPar);
393         delete aPar;
394 }
395
396 //_____________________________________________________________________________
397 void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
398 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
399 // Default storage should be defined prior to any specific storages, e.g.:
400 // AliCDBManager::instance()->SetDefaultStorage("alien://");
401 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
402 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
403 // calibType must be a valid CDB path! (3 level folder structure)
404
405         if(!fDefaultStorage) {
406                 AliError("Please activate a default storage first!");
407                 return;
408         }
409
410         AliCDBPath aPath(calibType);
411         if(!aPath.IsValid()){
412                 AliError(Form("Not a valid path: %s", calibType));
413                 return;
414         }
415
416         TObjString *objCalibType = new TObjString(aPath.GetPath());
417         if(fSpecificStorages.Contains(objCalibType)){
418                 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
419                                         calibType));
420                 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
421                 if(checkPar) delete checkPar;
422                 delete fSpecificStorages.Remove(objCalibType);
423         }
424         AliCDBStorage *aStorage = GetStorage(param);
425         if(!aStorage) return;
426
427         fSpecificStorages.Add(objCalibType, param->CloneParam());
428
429         if(fStorageMap->Contains(objCalibType)){
430                 delete fStorageMap->Remove(objCalibType);
431         }
432         fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
433
434 }
435
436 //_____________________________________________________________________________
437 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
438 // get storage specific for detector or calibration type 
439
440         AliCDBPath calibPath(calibType);
441         if(!calibPath.IsValid()) return NULL;
442
443         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
444         if(!checkPar){
445                 AliError(Form("%s storage not found!", calibType));
446                 return NULL;
447         } else {
448                 return GetStorage(checkPar);
449         }
450
451 }
452
453 //_____________________________________________________________________________
454 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
455 // select storage valid for path from the list of specific storages
456
457         AliCDBPath aPath(path);
458         if(!aPath.IsValid()) return NULL;
459
460         TIter iter(&fSpecificStorages);
461         TObjString *aCalibType=0;
462         AliCDBPath tmpPath("null/null/null");
463         AliCDBParam* aPar=0;
464         while((aCalibType = (TObjString*) iter.Next())){
465                 AliCDBPath calibTypePath(aCalibType->GetName());
466                 if(calibTypePath.Comprises(aPath)) {
467                         if(calibTypePath.Comprises(tmpPath)) continue;
468                         aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
469                         tmpPath.SetPath(calibTypePath.GetPath());
470                 }
471         }
472         return aPar;
473 }
474
475 //_____________________________________________________________________________
476 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
477         Int_t version, Int_t subVersion) {
478 // get an AliCDBEntry object from the database
479
480         if(runNumber < 0){
481                 // RunNumber is not specified. Try with fRun
482                 if (fRun < 0){
483                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
484                         return NULL;
485                 }
486                 runNumber = fRun;
487         }
488
489         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
490 }
491
492 //_____________________________________________________________________________
493 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
494         const AliCDBRunRange& runRange, Int_t version,
495         Int_t subVersion) {
496 // get an AliCDBEntry object from the database!
497
498         return Get(AliCDBId(path, runRange, version, subVersion));
499 }
500
501 //_____________________________________________________________________________
502 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
503 // get an AliCDBEntry object from the database
504         
505         if(!fDefaultStorage) {
506                 AliError("No storage set!");
507                 return NULL;
508         }
509
510         // check if query's path and runRange are valid
511         // query is invalid also if version is not specified and subversion is!
512         if (!query.IsValid()) {
513                 AliError(Form("Invalid query: %s", query.ToString().Data()));
514                 return NULL;
515         }
516         
517         // query is not specified if path contains wildcard or run range= [-1,-1]
518         if (!query.IsSpecified()) {
519                 AliError(Form("Unspecified query: %s",
520                                 query.ToString().Data()));
521                 return NULL;
522         }
523
524         if(fLock && query.GetFirstRun() != fRun)
525                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
526         
527         if(fCache && query.GetFirstRun() != fRun)
528                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
529
530         AliCDBEntry *entry=0;
531
532         // first look into map of cached objects
533         if(fCache && query.GetFirstRun() == fRun)
534                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
535
536         if(entry) {
537                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
538                 return entry;
539         }
540
541         // Entry is not in cache -> retrieve it from CDB and cache it!!
542         AliCDBStorage *aStorage=0;
543         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
544 //      Bool_t usedDefStorage=kTRUE;
545
546         if(aPar) {
547                 aStorage=GetStorage(aPar);
548                 TString str = aPar->GetURI();
549                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
550 //              usedDefStorage=kFALSE;
551
552         } else {
553                 aStorage=GetDefaultStorage();
554                 AliDebug(2,"Looking into default storage");
555         }
556
557         entry = aStorage->Get(query);
558
559         if(entry && fCache && (query.GetFirstRun() == fRun)){
560                 CacheEntry(query.GetPath(), entry);
561         }
562
563         if(entry && !fIds->Contains(&entry->GetId())){
564                 fIds->Add(entry->GetId().Clone());
565         }
566
567
568         return entry;
569
570 }
571
572 //_____________________________________________________________________________
573 const char* AliCDBManager::GetURI(const char* path) {
574 // return the URI of the storage where to look for path
575
576         if(!IsDefaultStorageSet()) return 0;
577         
578         AliCDBParam *aPar=SelectSpecificStorage(path);
579
580         if(aPar) {
581                 return aPar->GetURI().Data();
582
583         } else {
584                 return GetDefaultStorage()->GetURI().Data();
585         }
586         
587         return 0;
588 }
589
590 //_____________________________________________________________________________
591 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
592         Int_t version, Int_t subVersion) {
593 // get the AliCDBId of the valid object from the database (does not retrieve the object)
594 // User must delete returned object!
595
596         if(runNumber < 0){
597                 // RunNumber is not specified. Try with fRun
598                 if (fRun < 0){
599                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
600                         return NULL;
601                 }
602                 runNumber = fRun;
603         }
604
605         return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
606 }
607
608 //_____________________________________________________________________________
609 AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
610         const AliCDBRunRange& runRange, Int_t version,
611         Int_t subVersion) {
612 // get the AliCDBId of the valid object from the database (does not retrieve the object)
613 // User must delete returned object!
614
615         return GetId(AliCDBId(path, runRange, version, subVersion));
616 }
617
618 //_____________________________________________________________________________
619 AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
620 // get the AliCDBId of the valid object from the database (does not retrieve the object)
621 // User must delete returned object!
622
623         if(!fDefaultStorage) {
624                 AliError("No storage set!");
625                 return NULL;
626         }
627
628         // check if query's path and runRange are valid
629         // query is invalid also if version is not specified and subversion is!
630         if (!query.IsValid()) {
631                 AliError(Form("Invalid query: %s", query.ToString().Data()));
632                 return NULL;
633         }
634         
635         // query is not specified if path contains wildcard or run range= [-1,-1]
636         if (!query.IsSpecified()) {
637                 AliError(Form("Unspecified query: %s",
638                                 query.ToString().Data()));
639                 return NULL;
640         }
641
642         if(fCache && query.GetFirstRun() != fRun)
643                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
644
645         AliCDBEntry* entry = 0;
646
647         // first look into map of cached objects
648         if(fCache && query.GetFirstRun() == fRun)
649                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
650
651         if(entry) {
652                 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
653                 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
654         }
655
656         // Entry is not in cache -> retrieve it from CDB and cache it!!
657         AliCDBStorage *aStorage=0;
658         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
659
660         if(aPar) {
661                 aStorage=GetStorage(aPar);
662                 TString str = aPar->GetURI();
663                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
664                 
665         } else {
666                 aStorage=GetDefaultStorage();
667                 AliDebug(2,"Looking into default storage");
668         }
669
670         return aStorage->GetId(query);
671
672 }
673
674 //_____________________________________________________________________________
675 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
676         Int_t version, Int_t subVersion) {
677 // get multiple AliCDBEntry objects from the database
678
679         if(runNumber < 0){
680                 // RunNumber is not specified. Try with fRun
681                 if (fRun < 0){
682                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
683                         return NULL;
684                 }
685                 runNumber = fRun;
686         }
687
688         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
689                         subVersion));
690 }
691
692 //_____________________________________________________________________________
693 TList* AliCDBManager::GetAll(const AliCDBPath& path,
694         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
695 // get multiple AliCDBEntry objects from the database
696
697         return GetAll(AliCDBId(path, runRange, version, subVersion));
698 }
699
700 //_____________________________________________________________________________
701 TList* AliCDBManager::GetAll(const AliCDBId& query) {
702 // get multiple AliCDBEntry objects from the database
703 // Warning: this method works correctly only for queries of the type "Detector/*"
704 //              and not for more specific queries e.g. "Detector/Calib/*" !
705 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
706 //              To get an object from cache use Get() function
707
708         if(!fDefaultStorage) {
709                 AliError("No storage set!");
710                 return NULL;
711         }
712
713         if (!query.IsValid()) {
714                 AliError(Form("Invalid query: %s", query.ToString().Data()));
715                 return NULL;
716         }
717
718         if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
719                 // if specific storages are active a query with "*" is ambiguous
720                 AliError("Query too generic in this context!");
721                 return NULL;
722         }
723
724         if (query.IsAnyRange()) {
725                 AliError(Form("Unspecified run or runrange: %s",
726                                 query.ToString().Data()));
727                 return NULL;
728         }
729
730         if(fLock && query.GetFirstRun() != fRun)
731                 AliFatal("Lock is ON: cannot use different run number than the internal one!");
732         
733         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
734
735         AliCDBStorage *aStorage;
736         if(aPar) {
737                 aStorage=GetStorage(aPar);
738                 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
739
740         } else {
741                 aStorage=GetDefaultStorage();
742                 AliDebug(2,"Looking into default storage");
743         }
744
745         TList *result = 0;
746         if(aStorage) result = aStorage->GetAll(query);
747         if(!result) return 0;
748
749        // loop on result to check whether entries should be re-queried with specific storages
750         if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
751                 AliInfo("Now look into all other specific storages...");
752
753                 TIter iter(result);
754                 AliCDBEntry* chkEntry=0;
755
756                 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
757                         AliCDBId& chkId = chkEntry->GetId();
758                         AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
759                         AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
760                         if (!chkPar || aPar == chkPar) continue;
761                         AliCDBStorage *chkStorage = GetStorage(chkPar);
762                         AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
763
764                         AliCDBEntry *newEntry=0;
765                         chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
766                         chkId.SetVersion(query.GetVersion());
767                         chkId.SetSubVersion(query.GetSubVersion());
768
769                         if(chkStorage) newEntry = chkStorage->Get(chkId);
770                         if(!newEntry) continue;
771
772                         // object is found in specific storage: replace entry in the result list!
773                         chkEntry->SetOwner(1);
774                         delete result->Remove(chkEntry);
775                         result->AddFirst(newEntry);
776                 }
777
778                 Int_t nEntries = result->GetEntries();
779                 AliInfo("After look into other specific storages, result list is:");
780                 for(int i=0; i<nEntries;i++){
781                         AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
782                         AliInfo(Form("%s",entry->GetId().ToString().Data()));
783                 }
784         }
785
786         // caching entries
787         TIter iter(result);
788         AliCDBEntry* entry=0;
789         while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
790
791                 if(!fIds->Contains(&entry->GetId())){
792                         fIds->Add(entry->GetId().Clone());
793                 }
794                 if(fCache && (query.GetFirstRun() == fRun)){
795                         CacheEntry(entry->GetId().GetPath(), entry);
796                 }
797         }
798
799
800         return result;
801 }
802
803 //_____________________________________________________________________________
804 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData, DataType type){
805 // store an AliCDBEntry object into the database
806
807         if (object==0x0) {
808                 AliError("Null Entry! No storage will be done!");
809                 return kFALSE;
810         } 
811
812         AliCDBEntry anEntry(object, id, metaData);
813         return Put(&anEntry, type);
814
815 }
816
817
818 //_____________________________________________________________________________
819 Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
820 // store an AliCDBEntry object into the database
821
822         if(type == kPrivate && !fDefaultStorage) {
823                 AliError("No storage set!");
824                 return kFALSE;
825         }
826
827         if (entry->GetObject()==0x0){
828                 AliError("No valid object in CDB entry!");
829                 return kFALSE;
830         }
831
832         if (!entry){
833                 AliError("No entry!");
834                 return kFALSE;
835         }
836
837         if (!entry->GetId().IsValid()) {
838                 AliError(Form("Invalid entry ID: %s", 
839                         entry->GetId().ToString().Data()));
840                 return kFALSE;
841         }       
842
843         if (!entry->GetId().IsSpecified()) {
844                 AliError(Form("Unspecified entry ID: %s", 
845                         entry->GetId().ToString().Data()));
846                 return kFALSE;
847         }
848
849         AliCDBId id = entry->GetId();
850         AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
851
852         AliCDBStorage *aStorage=0;
853         
854         if(aPar) {
855                 aStorage=GetStorage(aPar);
856         } else {
857                 switch(type){
858                         case kCondition:
859                                 aStorage = GetStorage(fCondParam);
860                                 break;
861                         case kReference:
862                                 aStorage = GetStorage(fRefParam);
863                                 break;
864                         case kPrivate:
865                                 aStorage = GetDefaultStorage();
866                                 break;
867                 }
868         }
869
870         AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
871
872         Bool_t result = aStorage->Put(entry, type);
873
874         if(fRun >= 0) QueryCDB();
875
876         return result;
877
878
879 }
880
881 //_____________________________________________________________________________
882 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
883 {
884 // cache AliCDBEntry. Cache is valid until run number is changed.
885
886         AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
887
888         if(chkEntry) {
889                 AliDebug(2, Form("Object %s already in cache !!", path));
890                 return;
891         } else {
892                 AliDebug(2,Form("Caching entry %s", path));
893         }
894
895         fEntryCache.Add(new TObjString(path), entry);
896         AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
897
898 }
899
900 //_____________________________________________________________________________
901 void AliCDBManager::Print(Option_t* /*option*/) const
902 {
903 // Print list of active storages and their URIs
904
905         TString output=Form("Run number = %d; ",fRun);
906         output += "Cache is ";
907         if(!fCache) output += "NOT ";
908         output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
909
910         if(fDefaultStorage) {
911                 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
912 //              AliInfo(output.Data());
913         }
914         if(fSpecificStorages.GetEntries()>0) {
915                 TIter iter(fSpecificStorages.GetTable());
916                 TPair *aPair=0;
917                 Int_t i=1;
918                 while((aPair = (TPair*) iter.Next())){
919                         output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
920                                 i++, ((TObjString*) aPair->Key())->GetName(),
921                                 ((AliCDBParam*) aPair->Value())->GetURI().Data());
922                 }
923         }
924         if(fDrainStorage) {
925                 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
926         }
927         AliInfo(output.Data());
928 }
929
930 //_____________________________________________________________________________
931 void AliCDBManager::SetRun(Int_t run)
932 {
933 // Sets current run number.
934 // When the run number changes the caching is cleared.
935         
936         if (fRun == run)
937                 return;
938   
939         if(fLock && fRun >= 0) {
940                 AliFatal("Lock is ON, cannot reset run number!");
941         }       
942                 
943         fRun = run;
944         ClearCache();
945         QueryCDB();
946 }
947
948 //_____________________________________________________________________________
949 void AliCDBManager::ClearCache(){
950 // clear AliCDBEntry cache
951
952         AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
953         
954         /*
955         // To clean entries one by one
956         TIter iter(fEntryCache.GetTable());
957         TPair* pair=0;
958         while((pair= dynamic_cast<TPair*> (iter.Next()))){
959         
960                 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
961                 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
962                 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
963                 if (entry) delete entry;
964                 delete fEntryCache.Remove(key);
965         }
966         */
967         fEntryCache.DeleteAll();
968         AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
969 }
970
971 //_____________________________________________________________________________
972 void AliCDBManager::UnloadFromCache(const char* path){
973 // unload cached object
974
975         AliCDBPath queryPath(path);
976         if(!queryPath.IsValid()) return;
977
978         if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
979                 if(fEntryCache.Contains(path)){
980                         AliInfo(Form("Unloading object \"%s\" from cache", path));
981                         TObjString pathStr(path);
982                         AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
983                         if(entry) delete entry;
984                         delete fEntryCache.Remove(&pathStr);
985                 } else {
986                         AliError(Form("Cache does not contain object \"%s\"!", path))
987                 }
988                 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
989                 return;
990         }
991
992         // path is wildcard: loop on the cache and unload all comprised objects!
993         TIter iter(fEntryCache.GetTable());
994         TPair* pair = 0;
995
996         while((pair = dynamic_cast<TPair*> (iter.Next()))){
997                 AliCDBPath entryPath = pair->Key()->GetName();
998                 if(queryPath.Comprises(entryPath)) {
999                         AliInfo(Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
1000                         TObjString pathStr(entryPath.GetPath().Data());
1001                         AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
1002                         if(entry) delete entry;
1003                         delete fEntryCache.Remove(&pathStr);
1004                 }
1005         }
1006         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
1007 }
1008
1009 //_____________________________________________________________________________
1010 void AliCDBManager::DestroyActiveStorages() {
1011 // delete list of active storages
1012
1013         fActiveStorages.DeleteAll();
1014         fSpecificStorages.DeleteAll();
1015 }
1016
1017 //_____________________________________________________________________________
1018 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
1019 // destroys active storage
1020
1021 /*
1022         TIter iter(fActiveStorages.GetTable());
1023         TPair* aPair;
1024         while ((aPair = (TPair*) iter.Next())) {
1025                 if(storage == (AliCDBStorage*) aPair->Value())
1026                         delete fActiveStorages.Remove(aPair->Key());
1027                         storage->Delete(); storage=0x0;
1028         }
1029 */
1030
1031 }
1032
1033 //_____________________________________________________________________________
1034 void AliCDBManager::QueryCDB() {
1035 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1036
1037         if (fRun < 0){
1038                 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1039         return;
1040         }
1041         if (!fDefaultStorage){
1042                 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1043         return;
1044         }
1045         if(fDefaultStorage->GetType() == "alien"){
1046                 fDefaultStorage->QueryCDB(fRun);
1047         } else {
1048                 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1049         }
1050
1051         TIter iter(&fSpecificStorages);
1052         TObjString *aCalibType=0;
1053         AliCDBParam* aPar=0;
1054         while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1055                 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1056                 if(aPar) {
1057                         AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1058                         AliCDBStorage *aStorage = GetStorage(aPar);
1059                         if(aStorage->GetType() == "alien"){
1060                                 aStorage->QueryCDB(fRun,aCalibType->GetName());
1061                         } else {
1062                                 AliDebug(2,
1063                                         "Skipping query for valid files, it is used only in grid...");
1064                         }
1065                 }
1066         }
1067 }
1068
1069 //______________________________________________________________________________________________
1070 const char* AliCDBManager::GetDataTypeName(DataType type)
1071 {
1072   // returns the name (string) of the data type
1073
1074       switch (type){
1075             case kCondition: return "Conditions";
1076             case kReference: return "Reference";
1077             case kPrivate: return "Private";
1078      }
1079      return 0;
1080
1081 }
1082
1083 //______________________________________________________________________________________________
1084 void AliCDBManager::InitShortLived()
1085 {
1086   // Init the list of short-lived objects
1087   // currently disabled
1088
1089         fShortLived=0x0;
1090
1091 //      fShortLived = new TList();
1092 //      fShortLived->SetOwner(1);
1093 //
1094 //      fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1095 // 
1096 //      fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1097 //      fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1098 // 
1099 //      fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1100 // 
1101 //      fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1102 //      fShortLived->Add(new TObjString("MUON/Calib/HV"));
1103 //      fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1104 // 
1105 //      fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1106 //      fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1107 // 
1108 //      fShortLived->Add(new TObjString("PMD/Calib/Data"));
1109 // 
1110 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1111 //      fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1112 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1113 //      fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1114 //      fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1115 //      fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1116 // 
1117 //      fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1118
1119 }
1120
1121 //______________________________________________________________________________________________
1122 Bool_t AliCDBManager::IsShortLived(const char* path)
1123 {
1124   // returns the name (string) of the data type
1125
1126         if(!fShortLived) return kFALSE;
1127
1128         AliCDBPath aPath(path);
1129         if(!aPath.IsValid()){
1130                 AliError(Form("Not a valid path: %s", path));
1131                 return kFALSE;
1132         }
1133
1134         return fShortLived->Contains(path);
1135
1136 }
1137
1138 //______________________________________________________________________________________________
1139 void AliCDBManager::SetLock(Bool_t lock){
1140
1141         if(fLock == kTRUE && lock == kFALSE) {
1142                 AliFatal("Lock is ON: cannot reset it!");
1143         }
1144         
1145         fLock=lock;
1146 }
1147
1148 ///////////////////////////////////////////////////////////
1149 // AliCDBManager Parameter class                         //
1150 // interface to specific AliCDBParameter class           //
1151 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
1152 ///////////////////////////////////////////////////////////
1153
1154 AliCDBParam::AliCDBParam():
1155   fType(),
1156   fURI()
1157 {
1158 // constructor
1159
1160 }
1161
1162 //_____________________________________________________________________________
1163 AliCDBParam::~AliCDBParam() {
1164 // destructor
1165
1166 }
1167