1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 //-------------------------------------------------------------------------
21 #include "AliCDBManager.h"
22 #include "AliCDBStorage.h"
24 #include "AliCDBDump.h"
25 #include "AliCDBLocal.h"
26 #include "AliCDBGrid.h"
27 #include "AliCDBEntry.h"
28 #include "AliCDBMetaData.h"
30 #include <TObjString.h>
35 ClassImp(AliCDBManager)
37 AliCDBManager* AliCDBManager::fgInstance = 0x0;
39 //_____________________________________________________________________________
40 AliCDBManager* AliCDBManager::Instance() {
41 // returns AliCDBManager instance (singleton)
44 fgInstance = new AliCDBManager();
51 //_____________________________________________________________________________
52 void AliCDBManager::Init() {
53 // factory registering
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());
63 //_____________________________________________________________________________
64 void AliCDBManager::Destroy() {
65 // delete ALCDBManager instance and active storages
68 //fgInstance->Delete();
74 //_____________________________________________________________________________
75 AliCDBManager::AliCDBManager():
76 fDefaultStorage(NULL),
82 fFactories.SetOwner(1);
83 fEntryCache.SetOwner(1);
86 //_____________________________________________________________________________
87 AliCDBManager::~AliCDBManager() {
90 DestroyActiveStorages();
92 fDefaultStorage = 0x0;
95 //_____________________________________________________________________________
96 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
97 // get a storage object from the list of active storages
99 return (AliCDBStorage*) fActiveStorages.GetValue(param);
102 //_____________________________________________________________________________
103 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
104 // put a storage object into the list of active storages
106 fActiveStorages.Add(param, storage);
107 AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
110 //_____________________________________________________________________________
111 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
112 // add a storage factory to the list of registerd factories
114 if (!fFactories.Contains(factory)) {
115 fFactories.Add(factory);
119 //_____________________________________________________________________________
120 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
121 // check if dbString is a URI valid for one of the registered factories
123 TIter iter(&fFactories);
125 AliCDBStorageFactory* factory;
126 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
128 if (factory->Validate(dbString)) {
136 //_____________________________________________________________________________
137 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
138 // create AliCDBParam object from URI string
140 TIter iter(&fFactories);
142 AliCDBStorageFactory* factory;
143 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
145 AliCDBParam* param = factory->CreateParameter(dbString);
154 //_____________________________________________________________________________
155 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
156 // get storage object from URI string
158 AliCDBParam* param = CreateParameter(dbString);
163 AliCDBStorage* aStorage = GetStorage(param);
170 //_____________________________________________________________________________
171 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
172 // get storage object from AliCDBParam object
174 // if the list of active storages already contains
175 // the requested storage, return it
176 AliCDBStorage* aStorage = GetActiveStorage(param);
181 TIter iter(&fFactories);
183 AliCDBStorageFactory* factory;
185 // loop on the list of registered factories
186 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
188 // each factory tries to create its storage from the parameter
189 aStorage = factory->Create(param);
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()));
197 aStorage->SetURI(param->GetURI());
205 //_____________________________________________________________________________
206 TList* AliCDBManager::GetActiveStorages() {
207 // return list of active storages
209 TList* result = new TList();
211 TIter iter(fActiveStorages.GetTable());
213 while ((aPair = (TPair*) iter.Next())) {
214 result->Add(aPair->Value());
220 //_____________________________________________________________________________
221 void AliCDBManager::SetDrain(const char* dbString) {
222 // set drain storage from URI string
224 fDrainStorage = GetStorage(dbString);
227 //_____________________________________________________________________________
228 void AliCDBManager::SetDrain(const AliCDBParam* param) {
229 // set drain storage from AliCDBParam
231 fDrainStorage = GetStorage(param);
234 //_____________________________________________________________________________
235 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
236 // set drain storage from another active storage
238 fDrainStorage = storage;
241 //_____________________________________________________________________________
242 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
243 // drain retrieved object to drain storage
245 AliInfo("Draining into drain storage...");
246 return fDrainStorage->Put(entry);
249 //_____________________________________________________________________________
250 void AliCDBManager::SetDefaultStorage(const char* dbString) {
251 // sets default storage from URI string
253 fDefaultStorage = GetStorage(dbString);
256 //_____________________________________________________________________________
257 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
258 // set default storage from AliCDBParam object
260 fDrainStorage = GetStorage(param);
263 //_____________________________________________________________________________
264 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
265 // set default storage from another active storage
267 fDefaultStorage = storage;
270 //_____________________________________________________________________________
271 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
272 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
274 AliCDBParam *aPar = CreateParameter(dbString);
276 SetSpecificStorage(calibType, aPar);
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");
288 if(!fDefaultStorage) {
289 AliError("Please activate a default storage first!");
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",
297 fSpecificStorages.Remove(objCalibType);
300 fSpecificStorages.Add(objCalibType, param->CloneParam());
303 //_____________________________________________________________________________
304 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
305 // get storage specific for detector or calibration type
307 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibType);
309 AliError(Form("%s storage not found!",calibType));
312 return GetStorage(checkPar);
317 //_____________________________________________________________________________
318 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
319 // select storage valid for path from the list of specific storages
321 TIter iter(&fSpecificStorages);
322 TObjString *aCalibType;
324 while((aCalibType = (TObjString*) iter.Next())){
325 if(path.Contains(aCalibType->String(), TString::kIgnoreCase)) {
326 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
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
339 // RunNumber is not specified. Try with fRun
341 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
347 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
350 //_____________________________________________________________________________
351 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
352 const AliCDBRunRange& runRange, Int_t version,
354 // get an AliCDBEntry object from the database!
356 return Get(AliCDBId(path, runRange, version, subVersion));
359 //_____________________________________________________________________________
360 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
361 // get an AliCDBEntry object from the database
363 if(!fDefaultStorage) {
364 AliError("No storage set!");
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()));
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()));
382 if(fCache && query.GetFirstRun() != fRun)
383 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
386 AliCDBEntry *entry=0;
388 // first look into map of cached objects
389 if(query.GetFirstRun() == fRun)
390 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
393 AliDebug(2,Form("Object %s retrieved from cache !!",query.GetPath().Data()));
397 // Entry is not in cache -> retrieve it from CDB and cache it!!
398 AliCDBStorage *aStorage=0;
399 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
402 aStorage=GetStorage(aPar);
403 TString str = aPar->GetURI();
404 AliDebug(2,Form("Looking into storage: %s",str.Data()));
407 aStorage=GetDefaultStorage();
408 AliDebug(2,"Looking into default storage");
411 entry = aStorage->Get(query);
412 if (!entry) return NULL;
414 if(fCache && (query.GetFirstRun() == fRun)){
415 AliDebug(2,Form("Caching entry %s !",query.GetPath().Data()));
416 CacheEntry(query.GetPath(), entry);
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
429 // RunNumber is not specified. Try with fRun
431 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
437 return GetAll(AliCDBId(path, runNumber, runNumber, version,
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
446 return GetAll(AliCDBId(path, runRange, version, subVersion));
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/*" !
455 if(!fDefaultStorage) {
456 AliError("No storage set!");
460 if (!query.IsValid()) {
461 AliError(Form("Invalid query: %s", query.ToString().Data()));
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!");
471 if (query.IsAnyRange()) {
472 AliError(Form("Unspecified run or runrange: %s",
473 query.ToString().Data()));
477 TObjString objStrLev0(query.GetLevel0());
478 AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
480 AliCDBStorage *aStorage;
482 aStorage=GetStorage(aPar);
483 TString str = aPar->GetURI();
484 AliDebug(2,Form("Looking into storage: %s",str.Data()));
487 aStorage=GetDefaultStorage();
488 AliDebug(2,"Looking into default storage");
491 TList *result = aStorage->GetAll(query);
496 //_____________________________________________________________________________
497 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData){
498 // store an AliCDBEntry object into the database
500 AliCDBEntry anEntry(object, id, metaData);
501 return Put(&anEntry);
506 //_____________________________________________________________________________
507 Bool_t AliCDBManager::Put(AliCDBEntry* entry){
508 // store an AliCDBEntry object into the database
510 if(!fDefaultStorage) {
511 AliError("No storage set!");
516 AliError("No entry!");
520 if (!entry->GetId().IsValid()) {
521 AliError(Form("Invalid entry ID: %s",
522 entry->GetId().ToString().Data()));
526 if (!entry->GetId().IsSpecified()) {
527 AliError(Form("Unspecified entry ID: %s",
528 entry->GetId().ToString().Data()));
532 AliCDBId id = entry->GetId();
533 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
535 AliCDBStorage *aStorage;
538 aStorage=GetStorage(aPar);
539 TString str = aPar->GetURI();
540 AliDebug(2,Form("Storing object into storage: %s",str.Data()));
543 aStorage=GetDefaultStorage();
544 AliDebug(2,"Storing object into default storage");
547 return aStorage->Put(entry);
552 //_____________________________________________________________________________
553 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
555 // cache AliCDBEntry. Cache is valid until run number is changed.
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()));
563 //_____________________________________________________________________________
564 void AliCDBManager::Print(Option_t* /*option*/) const
566 // Print list of active storages and their URIs
567 AliInfo(Form("Run number: %d\n",fRun));
570 output = "Cache is ";
571 if(!fCache) output += "NOT ";
572 output += "ACTIVE\n";
573 AliInfo(output.Data());
575 if(fDefaultStorage) {
576 AliInfo("*** Default Storage: ***");
577 output = Form("%s\n",fDefaultStorage->GetURI().Data());
578 AliInfo(output.Data());
580 if(fSpecificStorages.GetEntries()>0) {
581 AliInfo("*** Specific Storages: ***");
582 TIter iter(fSpecificStorages.GetTable());
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());
593 AliInfo("*** Drain Storage: ***");
594 output = Form("%s\n",fDrainStorage->GetURI().Data());
595 AliInfo(output.Data());
597 AliInfo(Form("Total number of active storages: %d",fActiveStorages.GetEntries()));
601 //_____________________________________________________________________________
602 void AliCDBManager::SetRun(Long64_t run)
604 // Sets current run number.
605 // When the run number changes the caching is cleared.
614 //_____________________________________________________________________________
615 void AliCDBManager::ClearCache(){
616 // clear AliCDBEntry cache
618 AliDebug(2,Form("Clearing cache!"));
619 fEntryCache.DeleteAll();
620 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
624 //_____________________________________________________________________________
625 void AliCDBManager::DestroyActiveStorages() {
626 // delete list of active storages
628 fActiveStorages.DeleteAll();
629 fSpecificStorages.DeleteAll();
632 //_____________________________________________________________________________
633 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
634 // destroys active storage
637 TIter iter(fActiveStorages.GetTable());
639 while ((aPair = (TPair*) iter.Next())) {
640 if(storage == (AliCDBStorage*) aPair->Value())
641 delete fActiveStorages.Remove(aPair->Key());
642 storage->Delete(); storage=0x0;
648 ///////////////////////////////////////////////////////////
649 // AliCDBManager Parameter class //
650 // interface to specific AliCDBParameter class //
651 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
652 ///////////////////////////////////////////////////////////
654 AliCDBParam::AliCDBParam() {
659 //_____________________________________________________________________________
660 AliCDBParam::~AliCDBParam() {