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()));
204 //_____________________________________________________________________________
205 TList* AliCDBManager::GetActiveStorages() {
206 // return list of active storages
208 TList* result = new TList();
210 TIter iter(fActiveStorages.GetTable());
212 while ((aPair = (TPair*) iter.Next())) {
213 result->Add(aPair->Value());
219 //_____________________________________________________________________________
220 void AliCDBManager::SetDrain(const char* dbString) {
221 // set drain storage from URI string
223 fDrainStorage = GetStorage(dbString);
226 //_____________________________________________________________________________
227 void AliCDBManager::SetDrain(const AliCDBParam* param) {
228 // set drain storage from AliCDBParam
230 fDrainStorage = GetStorage(param);
233 //_____________________________________________________________________________
234 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
235 // set drain storage from another active storage
237 fDrainStorage = storage;
240 //_____________________________________________________________________________
241 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
242 // drain retrieved object to drain storage
244 AliInfo("Draining into drain storage...");
245 return fDrainStorage->Put(entry);
248 //_____________________________________________________________________________
249 void AliCDBManager::SetDefaultStorage(const char* dbString) {
250 // sets default storage from URI string
252 fDefaultStorage = GetStorage(dbString);
255 //_____________________________________________________________________________
256 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
257 // set default storage from AliCDBParam object
259 fDrainStorage = GetStorage(param);
262 //_____________________________________________________________________________
263 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
264 // set default storage from another active storage
266 fDefaultStorage = storage;
269 //_____________________________________________________________________________
270 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
271 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
273 AliCDBParam *aPar = CreateParameter(dbString);
275 SetSpecificStorage(calibType, aPar);
279 //_____________________________________________________________________________
280 void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
281 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
282 // Default storage should be defined prior to any specific storages, e.g.:
283 // AliCDBManager::instance()->SetDefaultStorage("alien://");
284 // AliCDBManager::instance()->SetSpecificStorage("TPC","local://DB_TPC");
285 // AliCDBManager::instance()->SetSpecificStorage("RICH/Align","local://DB_TPCAlign");
287 if(!fDefaultStorage) {
288 AliError("Please activate a default storage first!");
292 TObjString *objCalibType = new TObjString(calibType);
293 if(fSpecificStorages.Contains(objCalibType)){
294 AliWarning(Form("%s storage already activated! It will be replaced by the new one",
296 fSpecificStorages.Remove(objCalibType);
299 fSpecificStorages.Add(objCalibType, param->CloneParam());
302 //_____________________________________________________________________________
303 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
304 // get storage specific for detector or calibration type
306 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibType);
308 AliError(Form("%s storage not found!",calibType));
311 return GetStorage(checkPar);
316 //_____________________________________________________________________________
317 AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
318 // select storage valid for path from the list of specific storages
320 TIter iter(&fSpecificStorages);
321 TObjString *aCalibType;
323 while((aCalibType = (TObjString*) iter.Next())){
324 if(path.Contains(aCalibType->String(), TString::kIgnoreCase)) {
325 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
332 //_____________________________________________________________________________
333 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
334 Int_t version, Int_t subVersion) {
335 // get an AliCDBEntry object from the database
338 // RunNumber is not specified. Try with fRun
340 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
346 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
349 //_____________________________________________________________________________
350 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
351 const AliCDBRunRange& runRange, Int_t version,
353 // get an AliCDBEntry object from the database!
355 return Get(AliCDBId(path, runRange, version, subVersion));
358 //_____________________________________________________________________________
359 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
360 // get an AliCDBEntry object from the database
362 if(!fDefaultStorage) {
363 AliError("No storage set!");
367 // check if query's path and runRange are valid
368 // query is invalid also if version is not specified and subversion is!
369 if (!query.IsValid()) {
370 AliError(Form("Invalid query: %s", query.ToString().Data()));
374 // query is not specified if path contains wildcard or run range= [-1,-1]
375 if (!query.IsSpecified()) {
376 AliError(Form("Unspecified query: %s",
377 query.ToString().Data()));
381 if(fCache && query.GetFirstRun() != fRun)
382 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
385 AliCDBEntry *entry=0;
387 // first look into map of cached objects
388 if(query.GetFirstRun() == fRun)
389 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
392 AliDebug(2,Form("Object %s retrieved from cache !!",query.GetPath().Data()));
396 // Entry is not in cache -> retrieve it from CDB and cache it!!
397 AliCDBStorage *aStorage=0;
398 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
401 aStorage=GetStorage(aPar);
402 TString str = aPar->GetURI();
403 AliDebug(2,Form("Looking into storage: %s",str.Data()));
406 aStorage=GetDefaultStorage();
407 AliDebug(2,"Looking into default storage");
410 entry = aStorage->Get(query);
411 if (!entry) return NULL;
413 if(fCache && (query.GetFirstRun() == fRun)){
414 AliDebug(2,Form("Caching entry %s !",query.GetPath().Data()));
415 CacheEntry(query.GetPath(), entry);
422 //_____________________________________________________________________________
423 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
424 Int_t version, Int_t subVersion) {
425 // get multiple AliCDBEntry objects from the database
427 return GetAll(AliCDBId(path, runNumber, runNumber, version,
431 //_____________________________________________________________________________
432 TList* AliCDBManager::GetAll(const AliCDBPath& path,
433 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
434 // get multiple AliCDBEntry objects from the database
436 return GetAll(AliCDBId(path, runRange, version, subVersion));
439 //_____________________________________________________________________________
440 TList* AliCDBManager::GetAll(const AliCDBId& query) {
441 // get multiple AliCDBEntry objects from the database
442 // Warning: this method works correctly only for queries of the type "Detector/*"
443 // and not for more specific queries e.g. "Detector/Calib/*" !
445 if(!fDefaultStorage) {
446 AliError("No storage set!");
450 if (!query.IsValid()) {
451 AliError(Form("Invalid query: %s", query.ToString().Data()));
455 if(query.GetPath().BeginsWith('*')){
456 AliError("Query too generic in this context!");
460 if (query.IsAnyRange()) {
461 AliError(Form("Unspecified run or runrange: %s",
462 query.ToString().Data()));
466 TObjString objStrLev0(query.GetLevel0());
467 AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
469 AliCDBStorage *aStorage;
471 aStorage=GetStorage(aPar);
472 TString str = aPar->GetURI();
473 AliDebug(2,Form("Looking into storage: %s",str.Data()));
476 aStorage=GetDefaultStorage();
477 AliDebug(2,"Looking into default storage");
480 TList *result = aStorage->GetAll(query);
485 //_____________________________________________________________________________
486 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData){
487 // store an AliCDBEntry object into the database
489 AliCDBEntry anEntry(object, id, metaData);
490 return Put(&anEntry);
495 //_____________________________________________________________________________
496 Bool_t AliCDBManager::Put(AliCDBEntry* entry){
497 // store an AliCDBEntry object into the database
499 if(!fDefaultStorage) {
500 AliError("No storage set!");
505 AliError("No entry!");
509 if (!entry->GetId().IsValid()) {
510 AliError(Form("Invalid entry ID: %s",
511 entry->GetId().ToString().Data()));
515 if (!entry->GetId().IsSpecified()) {
516 AliError(Form("Unspecified entry ID: %s",
517 entry->GetId().ToString().Data()));
521 AliCDBId id = entry->GetId();
522 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
524 AliCDBStorage *aStorage;
527 aStorage=GetStorage(aPar);
528 TString str = aPar->GetURI();
529 AliDebug(2,Form("Storing object into storage: %s",str.Data()));
532 aStorage=GetDefaultStorage();
533 AliDebug(2,"Storing object into default storage");
536 return aStorage->Put(entry);
541 //_____________________________________________________________________________
542 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
544 // cache AliCDBEntry. Cache is valid until run number is changed.
546 AliDebug(2,Form("Filling cache with entry %s",path));
547 fEntryCache.Add(new TObjString(path), entry);
548 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
552 //_____________________________________________________________________________
553 void AliCDBManager::SetRun(Long64_t run)
555 // Sets current run number.
556 // When the run number changes the caching is cleared.
565 //_____________________________________________________________________________
566 void AliCDBManager::ClearCache(){
567 // clear AliCDBEntry cache
569 AliDebug(2,Form("Clearing cache!"));
570 fEntryCache.DeleteAll();
571 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
575 //_____________________________________________________________________________
576 void AliCDBManager::DestroyActiveStorages() {
577 // delete list of active storages
579 fActiveStorages.DeleteAll();
580 fSpecificStorages.DeleteAll();
583 //_____________________________________________________________________________
584 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
585 // destroys active storage
588 TIter iter(fActiveStorages.GetTable());
590 while ((aPair = (TPair*) iter.Next())) {
591 if(storage == (AliCDBStorage*) aPair->Value())
592 delete fActiveStorages.Remove(aPair->Key());
593 storage->Delete(); storage=0x0;
599 ///////////////////////////////////////////////////////////
600 // AliCDBManager Parameter class //
601 // interface to specific AliCDBParameter class //
602 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
603 ///////////////////////////////////////////////////////////
605 AliCDBParam::AliCDBParam() {
610 //_____________________________________________________________________________
611 AliCDBParam::~AliCDBParam() {