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