]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBManager.cxx
Updated version of the CDB classes. Bug fix in AliCDBGrid. New AliCDBStorage function...
[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 AliCDBManager* AliCDBManager::fgInstance = 0x0;
38
39 //_____________________________________________________________________________
40 AliCDBManager* AliCDBManager::Instance() {
41 // returns AliCDBManager instance (singleton)
42
43         if (!fgInstance) {
44                 fgInstance = new AliCDBManager();
45                 fgInstance->Init();
46         }
47
48         return fgInstance;
49 }
50
51 //_____________________________________________________________________________
52 void AliCDBManager::Init() {
53 // factory registering
54
55         RegisterFactory(new AliCDBDumpFactory());
56         RegisterFactory(new AliCDBLocalFactory()); 
57         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
58         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
59                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
60                 RegisterFactory(new AliCDBGridFactory());
61         }
62 }
63 //_____________________________________________________________________________
64 void AliCDBManager::Destroy() {
65 // delete ALCDBManager instance and active storages
66
67         if (fgInstance) {
68                 //fgInstance->Delete();
69                 delete fgInstance;
70                 fgInstance = 0x0;
71         }
72 }
73
74 //_____________________________________________________________________________
75 AliCDBManager::AliCDBManager():
76   TObject(),
77   fFactories(),
78   fActiveStorages(),
79   fSpecificStorages(),
80   fDefaultStorage(NULL),
81   fDrainStorage(NULL),
82   fEntryCache(),
83   fCache(kTRUE),
84   fRun(-1)
85 {
86 // default constuctor
87         fFactories.SetOwner(1);
88         fActiveStorages.SetOwner(1);
89         fSpecificStorages.SetOwner(1);
90         fEntryCache.SetOwner(1);
91 }
92
93 //_____________________________________________________________________________
94 AliCDBManager::~AliCDBManager() {
95 // destructor
96         ClearCache();
97         DestroyActiveStorages();
98         fFactories.Delete();
99         fDrainStorage = 0x0;
100         fDefaultStorage = 0x0;
101 }
102
103 //_____________________________________________________________________________
104 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
105 // put a storage object into the list of active storages
106
107         fActiveStorages.Add(param, storage);
108         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
109 }
110
111 //_____________________________________________________________________________
112 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
113 // add a storage factory to the list of registerd factories
114  
115         if (!fFactories.Contains(factory)) {
116                 fFactories.Add(factory);
117         }
118 }
119
120 //_____________________________________________________________________________
121 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
122 // check if dbString is a URI valid for one of the registered factories 
123
124         TIter iter(&fFactories);
125
126         AliCDBStorageFactory* factory=0;
127         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
128
129                 if (factory->Validate(dbString)) {
130                         return kTRUE;
131                 }       
132         }
133
134         return kFALSE;
135 }
136
137 //_____________________________________________________________________________
138 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
139 // create AliCDBParam object from URI string
140
141         TIter iter(&fFactories);
142
143         AliCDBStorageFactory* factory=0;
144         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
145                 AliCDBParam* param = factory->CreateParameter(dbString);
146                 if(param) return param;
147         }
148
149         return NULL;
150 }
151
152 //_____________________________________________________________________________
153 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
154 // get storage object from URI string
155         
156         AliCDBParam* param = CreateParameter(dbString);
157         if (!param) {
158                 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
159                 return NULL;
160         }
161
162         AliCDBStorage* aStorage = GetStorage(param);
163
164         delete param;
165         return aStorage;
166 }
167
168 //_____________________________________________________________________________
169 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
170 // get storage object from AliCDBParam object
171
172         // if the list of active storages already contains
173         // the requested storage, return it
174         AliCDBStorage* aStorage = GetActiveStorage(param);
175         if (aStorage) {
176                 return aStorage;
177         }
178
179         TIter iter(&fFactories);
180
181         AliCDBStorageFactory* factory=0;
182
183         // loop on the list of registered factories
184         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
185
186                 // each factory tries to create its storage from the parameter
187                 aStorage = factory->Create(param);
188                 if (aStorage) {
189                         PutActiveStorage(param->CloneParam(), aStorage);
190                         aStorage->SetURI(param->GetURI());
191                         if(fRun > 0) {
192                                 aStorage->QueryCDB(fRun);
193                         }
194                         return aStorage;
195                 }
196         }
197
198         AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
199
200         return NULL;
201 }
202
203 //_____________________________________________________________________________
204 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
205 // get a storage object from the list of active storages
206
207         return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
208 }
209
210 //_____________________________________________________________________________
211 TList* AliCDBManager::GetActiveStorages() {
212 // return list of active storages
213 // user has responsibility to delete returned object
214
215         TList* result = new TList();
216
217         TIter iter(fActiveStorages.GetTable());
218         TPair* aPair=0;
219         while ((aPair = (TPair*) iter.Next())) {
220                 result->Add(aPair->Value());
221         }
222
223         return result;
224 }
225
226 //_____________________________________________________________________________
227 void AliCDBManager::SetDrain(const char* dbString) {
228 // set drain storage from URI string
229
230         fDrainStorage = GetStorage(dbString);   
231 }
232
233 //_____________________________________________________________________________
234 void AliCDBManager::SetDrain(const AliCDBParam* param) {
235 // set drain storage from AliCDBParam
236         
237         fDrainStorage = GetStorage(param);
238 }
239
240 //_____________________________________________________________________________
241 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
242 // set drain storage from another active storage
243         
244         fDrainStorage = storage;
245 }
246
247 //_____________________________________________________________________________
248 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
249 // drain retrieved object to drain storage
250
251         AliInfo("Draining into drain storage...");
252         return fDrainStorage->Put(entry);
253 }
254
255 //_____________________________________________________________________________
256 void AliCDBManager::SetDefaultStorage(const char* dbString) {
257 // sets default storage from URI string
258
259         AliInfo(Form("Setting Default storage to: %s",dbString));
260         fDefaultStorage = GetStorage(dbString);
261 }
262
263 //_____________________________________________________________________________
264 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
265 // set default storage from AliCDBParam object
266         
267         fDrainStorage = GetStorage(param);
268 }
269
270 //_____________________________________________________________________________
271 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
272 // set default storage from another active storage
273         
274         fDefaultStorage = storage;
275 }
276
277 //_____________________________________________________________________________
278 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
279 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
280
281         AliCDBParam *aPar = CreateParameter(dbString);
282         if(!aPar) return;
283         SetSpecificStorage(calibType, aPar);
284         delete aPar;
285 }
286
287 //_____________________________________________________________________________
288 void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
289 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
290 // Default storage should be defined prior to any specific storages, e.g.:
291 // AliCDBManager::instance()->SetDefaultStorage("alien://");
292 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
293 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
294 // calibType must be a valid CDB path! (3 level folder structure)
295
296         if(!fDefaultStorage) {
297                 AliError("Please activate a default storage first!");
298                 return;
299         }
300         
301         AliCDBPath aPath(calibType);
302         if(!aPath.IsValid()){
303                 AliError(Form("Not a valid path: %s", calibType));
304                 return;
305         }
306
307         TObjString *objCalibType = new TObjString(aPath.GetPath());
308         if(fSpecificStorages.Contains(objCalibType)){
309                 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
310                                         calibType));
311                 fSpecificStorages.Remove(objCalibType);
312         }
313         GetStorage(param);
314         fSpecificStorages.Add(objCalibType, param->CloneParam());
315 }
316
317 //_____________________________________________________________________________
318 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
319 // get storage specific for detector or calibration type 
320
321         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibType);
322         if(!checkPar){
323                 AliError(Form("%s storage not found!",calibType));
324                 return NULL;
325         } else {
326                 return GetStorage(checkPar);
327         }
328         
329 }
330
331 //_____________________________________________________________________________
332 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
333 // select storage valid for path from the list of specific storages
334
335         AliCDBPath aPath(path);
336         if(!aPath.IsValid()){
337                 AliError(Form("Not a valid path: %s", path.Data()));
338                 return NULL;
339         }
340
341         TIter iter(&fSpecificStorages);
342         TObjString *aCalibType=0;
343         AliCDBParam* aPar=0;
344         while((aCalibType = (TObjString*) iter.Next())){
345                 AliCDBPath calibTypePath(aCalibType->GetName());
346                 if(calibTypePath.Comprises(aPath)) {
347                         aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
348                         break;
349                 }
350         }
351         return aPar;
352 }
353
354 //_____________________________________________________________________________
355 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber, 
356         Int_t version, Int_t subVersion) {
357 // get an AliCDBEntry object from the database
358
359         if(runNumber < 0){
360                 // RunNumber is not specified. Try with fRun
361                 if (fRun < 0){
362                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
363                         return NULL;
364                 }
365                 runNumber = fRun;
366         }
367
368         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
369 }
370
371 //_____________________________________________________________________________
372 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, 
373         const AliCDBRunRange& runRange, Int_t version,
374         Int_t subVersion) {
375 // get an AliCDBEntry object from the database!
376
377         return Get(AliCDBId(path, runRange, version, subVersion));
378 }
379
380 //_____________________________________________________________________________
381 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {        
382 // get an AliCDBEntry object from the database
383         
384         if(!fDefaultStorage) {
385                 AliError("No storage set!");
386                 return NULL;
387         }
388
389         // check if query's path and runRange are valid
390         // query is invalid also if version is not specified and subversion is!
391         if (!query.IsValid()) {
392                 AliError(Form("Invalid query: %s", query.ToString().Data()));
393                 return NULL;
394         }
395         
396         // query is not specified if path contains wildcard or run range= [-1,-1]
397         if (!query.IsSpecified()) {
398                 AliError(Form("Unspecified query: %s", 
399                                 query.ToString().Data()));
400                 return NULL;
401         }
402
403         if(fCache && query.GetFirstRun() != fRun) 
404                 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
405
406         
407         AliCDBEntry *entry=0;
408         
409         // first look into map of cached objects
410         if(fCache && query.GetFirstRun() == fRun)
411                 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
412
413         if(entry) {
414                 AliInfo(Form("Object %s retrieved from cache !!",query.GetPath().Data()));
415                 return entry;
416         }
417         
418         // Entry is not in cache -> retrieve it from CDB and cache it!!   
419         AliCDBStorage *aStorage=0;
420         AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
421
422         if(aPar) {
423                 aStorage=GetStorage(aPar);
424                 TString str = aPar->GetURI();
425                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
426                 
427         } else {
428                 aStorage=GetDefaultStorage();
429                 AliDebug(2,"Looking into default storage");     
430         }
431                         
432         entry = aStorage->Get(query);
433         if (!entry) return NULL;
434
435         if(fCache && (query.GetFirstRun() == fRun)){
436                 AliDebug(2,Form("Caching entry %s !",query.GetPath().Data()));
437                 CacheEntry(query.GetPath(), entry);
438         }
439   
440         return entry;
441                 
442 }
443
444 //_____________________________________________________________________________
445 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
446         Int_t version, Int_t subVersion) {
447 // get multiple AliCDBEntry objects from the database
448
449         if(runNumber < 0){
450                 // RunNumber is not specified. Try with fRun
451                 if (fRun < 0){
452                         AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
453                         return NULL;
454                 }
455                 runNumber = fRun;
456         }
457
458         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
459                         subVersion));
460 }
461
462 //_____________________________________________________________________________
463 TList* AliCDBManager::GetAll(const AliCDBPath& path, 
464         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
465 // get multiple AliCDBEntry objects from the database
466
467         return GetAll(AliCDBId(path, runRange, version, subVersion));
468 }
469
470 //_____________________________________________________________________________
471 TList* AliCDBManager::GetAll(const AliCDBId& query) {
472 // get multiple AliCDBEntry objects from the database
473 // Warning: this method works correctly only for queries of the type "Detector/*"
474 //              and not for more specific queries e.g. "Detector/Calib/*" !
475
476         if(!fDefaultStorage) {
477                 AliError("No storage set!");
478                 return NULL;
479         }
480
481         if (!query.IsValid()) {
482                 AliError(Form("Invalid query: %s", query.ToString().Data()));
483                 return NULL;
484         }
485
486         if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
487                 // if specific storages are active a query with "*" is ambiguous
488                 AliError("Query too generic in this context!");
489                 return NULL;            
490         }
491
492         if (query.IsAnyRange()) {
493                 AliError(Form("Unspecified run or runrange: %s",
494                                 query.ToString().Data()));      
495                 return NULL;
496         }       
497         
498         TObjString objStrLev0(query.GetLevel0());
499         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
500
501         AliCDBStorage *aStorage;        
502         if(aPar) {
503                 aStorage=GetStorage(aPar);
504                 TString str = aPar->GetURI();
505                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
506                 
507         } else {
508                 aStorage=GetDefaultStorage();
509                 AliDebug(2,"Looking into default storage");     
510         }
511
512         TList *result = aStorage->GetAll(query);
513
514         return result;
515 }
516
517 //_____________________________________________________________________________
518 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData){
519 // store an AliCDBEntry object into the database
520
521         AliCDBEntry anEntry(object, id, metaData);
522         return Put(&anEntry);
523
524 }
525
526
527 //_____________________________________________________________________________
528 Bool_t AliCDBManager::Put(AliCDBEntry* entry){
529 // store an AliCDBEntry object into the database
530
531         if(!fDefaultStorage) {
532                 AliError("No storage set!");
533                 return kFALSE;
534         }
535
536         if (!entry){
537                 AliError("No entry!");
538                 return kFALSE;
539         }
540
541         if (!entry->GetId().IsValid()) {
542                 AliError(Form("Invalid entry ID: %s", 
543                         entry->GetId().ToString().Data()));
544                 return kFALSE;
545         }       
546
547         if (!entry->GetId().IsSpecified()) {
548                 AliError(Form("Unspecified entry ID: %s", 
549                         entry->GetId().ToString().Data()));
550                 return kFALSE;
551         }
552
553         AliCDBId id = entry->GetId();
554         AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
555
556         AliCDBStorage *aStorage;
557         
558         if(aPar) {
559                 aStorage=GetStorage(aPar);
560                 TString str = aPar->GetURI();
561                 AliDebug(2,Form("Storing object into storage: %s",str.Data()));
562                 
563         } else {
564                 aStorage=GetDefaultStorage();
565                 AliDebug(2,"Storing object into default storage");      
566         }
567
568         return aStorage->Put(entry);
569
570
571 }
572
573 //_____________________________________________________________________________
574 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
575 {
576 // cache AliCDBEntry. Cache is valid until run number is changed.
577
578         AliDebug(2,Form("Filling cache with entry %s",path));
579         fEntryCache.Add(new TObjString(path), entry);
580         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
581
582 }
583
584 //_____________________________________________________________________________
585 void AliCDBManager::Print(Option_t* /*option*/) const
586 {
587 // Print list of active storages and their URIs
588         AliInfo(Form("Run number: %d\n",fRun));
589
590         TString output;
591         output = "Cache is ";
592         if(!fCache) output += "NOT ";
593         output += "ACTIVE\n";
594         AliInfo(output.Data());
595
596         if(fDefaultStorage) {
597                 AliInfo("*** Default Storage: ***");
598                 output = Form("%s\n",fDefaultStorage->GetURI().Data());
599                 AliInfo(output.Data());
600         }
601         if(fSpecificStorages.GetEntries()>0) {
602                 AliInfo("*** Specific Storages: ***");
603                 TIter iter(fSpecificStorages.GetTable());
604                 TPair *aPair=0;
605                 while((aPair = (TPair*) iter.Next())){
606                         output = Form("Key: %s - Storage: %s",
607                                 ((TObjString*) aPair->Key())->GetName(),
608                                 ((AliCDBParam*) aPair->Value())->GetURI().Data());
609                         AliInfo(output.Data());
610                 }
611                 printf("\n");
612         }
613         if(fDrainStorage) {
614                 AliInfo("*** Drain Storage: ***");
615                 output = Form("%s\n",fDrainStorage->GetURI().Data());
616                 AliInfo(output.Data());
617         }
618         AliInfo(Form("Total number of active storages: %d",fActiveStorages.GetEntries()));
619
620 }
621
622 //_____________________________________________________________________________
623 void AliCDBManager::SetRun(Int_t run)
624 {
625 // Sets current run number.
626 // When the run number changes the caching is cleared.
627   
628         if (fRun == run)
629                 return;
630   
631         fRun = run;
632         ClearCache();
633         QueryCDB();
634 }
635
636 //_____________________________________________________________________________
637 void AliCDBManager::ClearCache(){
638 // clear AliCDBEntry cache
639
640         AliDebug(2,Form("Clearing cache!"));
641         fEntryCache.DeleteAll();
642         AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
643
644 }
645
646 //_____________________________________________________________________________
647 void AliCDBManager::DestroyActiveStorages() {
648 // delete list of active storages
649
650         fActiveStorages.DeleteAll();
651         fSpecificStorages.DeleteAll();
652 }
653
654 //_____________________________________________________________________________
655 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
656 // destroys active storage
657
658 /*
659         TIter iter(fActiveStorages.GetTable());
660         TPair* aPair;
661         while ((aPair = (TPair*) iter.Next())) {
662                 if(storage == (AliCDBStorage*) aPair->Value())
663                         delete fActiveStorages.Remove(aPair->Key());
664                         storage->Delete(); storage=0x0;
665         }
666 */      
667
668 }
669
670 //_____________________________________________________________________________
671 void AliCDBManager::QueryCDB() {
672 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
673
674         if (fRun < 0){
675                 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
676         return;
677         }
678
679         AliInfo(Form("Querying default and specific storages for files valid for run %d", fRun));
680         fDefaultStorage->QueryCDB(fRun);
681
682         TIter iter(&fSpecificStorages);
683         TObjString *aCalibType=0;
684         AliCDBParam* aPar=0;
685         while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
686                 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
687                 if(aPar) {
688                         AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
689                         GetStorage(aPar)->QueryCDB(fRun,aCalibType->GetName());
690
691                 }
692         }
693
694 }
695
696
697 ///////////////////////////////////////////////////////////
698 // AliCDBManager Parameter class                         //
699 // interface to specific AliCDBParameter class           //
700 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
701 ///////////////////////////////////////////////////////////
702
703 AliCDBParam::AliCDBParam():
704   fType(),
705   fURI()
706 {
707 // constructor
708
709 }
710
711 //_____________________________________________________________________________
712 AliCDBParam::~AliCDBParam() {
713 // destructor
714
715 }
716