Added methods for finding a given module in the DDL map (F. Prino)
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
CommitLineData
9e1ceb13 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 **************************************************************************/
fdf65bb5 15//-------------------------------------------------------------------------
16// Implementation of AliCDBManager and AliCDBParam classe
17// Author: Alberto Colla
18// e-mail: Alberto.Colla@cern.ch
19//-------------------------------------------------------------------------
9e1ceb13 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"
02c4845e 27#include "AliCDBEntry.h"
28#include "AliCDBMetaData.h"
9e1ceb13 29
02c4845e 30#include <TObjString.h>
9e1ceb13 31#include <TSystem.h>
32
33ClassImp(AliCDBParam)
34
35ClassImp(AliCDBManager)
36
b8ec52f6 37//TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
38TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
39TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
9e1ceb13 40AliCDBManager* AliCDBManager::fgInstance = 0x0;
41
42//_____________________________________________________________________________
b8ec52f6 43AliCDBManager* AliCDBManager::Instance()
44{
9e1ceb13 45// returns AliCDBManager instance (singleton)
46
47 if (!fgInstance) {
48 fgInstance = new AliCDBManager();
49 fgInstance->Init();
50 }
51
52 return fgInstance;
53}
54
55//_____________________________________________________________________________
56void 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());
b8ec52f6 65 fCondParam = CreateParameter(fgkCondUri);
66 fRefParam = CreateParameter(fgkRefUri);
9e1ceb13 67 }
4667c116 68
69 InitShortLived();
9e1ceb13 70}
71//_____________________________________________________________________________
72void AliCDBManager::Destroy() {
73// delete ALCDBManager instance and active storages
74
75 if (fgInstance) {
4b5e0dce 76 //fgInstance->Delete();
9e1ceb13 77 delete fgInstance;
78 fgInstance = 0x0;
79 }
80}
81
82//_____________________________________________________________________________
83AliCDBManager::AliCDBManager():
fe12e09c 84 TObject(),
85 fFactories(),
86 fActiveStorages(),
87 fSpecificStorages(),
b21e3194 88 fEntryCache(),
89 fIds(0),
90 fStorageMap(0),
91 fShortLived(0),
fe12e09c 92 fDefaultStorage(NULL),
93 fDrainStorage(NULL),
b21e3194 94 fCondParam(0),
95 fRefParam(0),
4667c116 96 fRun(-1),
1f341624 97 fCache(kTRUE),
98 fLock(kFALSE)
9e1ceb13 99{
100// default constuctor
4b5e0dce 101 fFactories.SetOwner(1);
62032124 102 fActiveStorages.SetOwner(1);
103 fSpecificStorages.SetOwner(1);
104 fEntryCache.SetOwner(1);
b21e3194 105
106 fStorageMap = new TMap();
107 fStorageMap->SetOwner(1);
108 fIds = new TList();
109 fIds->SetOwner(1);
9e1ceb13 110}
111
112//_____________________________________________________________________________
113AliCDBManager::~AliCDBManager() {
114// destructor
4b5e0dce 115 ClearCache();
9e1ceb13 116 DestroyActiveStorages();
62032124 117 fFactories.Delete();
b05400be 118 fDrainStorage = 0x0;
119 fDefaultStorage = 0x0;
b21e3194 120 delete fStorageMap; fStorageMap = 0;
121 delete fIds; fIds = 0;
b8ec52f6 122 delete fCondParam;
123 delete fRefParam;
4667c116 124 delete fShortLived; fShortLived = 0x0;
9e1ceb13 125}
126
127//_____________________________________________________________________________
9e1ceb13 128void 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//_____________________________________________________________________________
136void 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//_____________________________________________________________________________
fdf65bb5 145Bool_t AliCDBManager::HasStorage(const char* dbString) const {
b21e3194 146// check if dbString is a URI valid for one of the registered factories
9e1ceb13 147
148 TIter iter(&fFactories);
149
62032124 150 AliCDBStorageFactory* factory=0;
9e1ceb13 151 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
152
153 if (factory->Validate(dbString)) {
154 return kTRUE;
4005d0b5 155 }
9e1ceb13 156 }
157
158 return kFALSE;
159}
160
161//_____________________________________________________________________________
fdf65bb5 162AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
9e1ceb13 163// create AliCDBParam object from URI string
164
165 TIter iter(&fFactories);
166
62032124 167 AliCDBStorageFactory* factory=0;
9e1ceb13 168 while ((factory = (AliCDBStorageFactory*) iter.Next())) {
9e1ceb13 169 AliCDBParam* param = factory->CreateParameter(dbString);
62032124 170 if(param) return param;
9e1ceb13 171 }
172
173 return NULL;
174}
175
176//_____________________________________________________________________________
177AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
178// get storage object from URI string
1f341624 179
9e1ceb13 180 AliCDBParam* param = CreateParameter(dbString);
181 if (!param) {
c3a7b59a 182 AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
9e1ceb13 183 return NULL;
82096dfc 184 }
9e1ceb13 185
186 AliCDBStorage* aStorage = GetStorage(param);
187
188 delete param;
9e1ceb13 189 return aStorage;
190}
191
192//_____________________________________________________________________________
193AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
194// get storage object from AliCDBParam object
1f341624 195
82096dfc 196 // if the list of active storages already contains
9e1ceb13 197 // the requested storage, return it
198 AliCDBStorage* aStorage = GetActiveStorage(param);
199 if (aStorage) {
200 return aStorage;
201 }
202
1f341624 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
9e1ceb13 211 TIter iter(&fFactories);
212
62032124 213 AliCDBStorageFactory* factory=0;
917a098b 214
9e1ceb13 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);
917a098b 222 aStorage->SetURI(param->GetURI());
6dc56e97 223 if(fRun >= 0) {
96b7636d 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 }
c3a7b59a 230 }
9e1ceb13 231 return aStorage;
232 }
233 }
234
c3a7b59a 235 AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
236
9e1ceb13 237 return NULL;
238}
239
240//_____________________________________________________________________________
62032124 241AliCDBStorage* 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//_____________________________________________________________________________
9e1ceb13 248TList* AliCDBManager::GetActiveStorages() {
249// return list of active storages
62032124 250// user has responsibility to delete returned object
9e1ceb13 251
252 TList* result = new TList();
253
917a098b 254 TIter iter(fActiveStorages.GetTable());
62032124 255 TPair* aPair=0;
9e1ceb13 256 while ((aPair = (TPair*) iter.Next())) {
257 result->Add(aPair->Value());
258 }
259
260 return result;
261}
262
263//_____________________________________________________________________________
264void AliCDBManager::SetDrain(const char* dbString) {
265// set drain storage from URI string
266
267 fDrainStorage = GetStorage(dbString);
268}
269
270//_____________________________________________________________________________
271void AliCDBManager::SetDrain(const AliCDBParam* param) {
272// set drain storage from AliCDBParam
b8ec52f6 273
9e1ceb13 274 fDrainStorage = GetStorage(param);
275}
276
277//_____________________________________________________________________________
278void AliCDBManager::SetDrain(AliCDBStorage* storage) {
279// set drain storage from another active storage
280
281 fDrainStorage = storage;
282}
283
284//_____________________________________________________________________________
285Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
286// drain retrieved object to drain storage
287
b8ec52f6 288 AliDebug(2, "Draining into drain storage...");
9e1ceb13 289 return fDrainStorage->Put(entry);
290}
291
292//_____________________________________________________________________________
9e1ceb13 293void AliCDBManager::SetDefaultStorage(const char* dbString) {
294// sets default storage from URI string
1f341624 295
82096dfc 296 AliInfo(Form("Setting Default storage to: %s",dbString));
4954c5cd 297 AliCDBStorage* bckStorage = fDefaultStorage;
298
82096dfc 299 fDefaultStorage = GetStorage(dbString);
95c0e132 300
301 if(!fDefaultStorage) return;
4954c5cd 302
303 if(bckStorage && (fDefaultStorage != bckStorage)){
304 AliWarning("Existing default storage replaced: clearing cache!");
305 ClearCache();
306 }
b21e3194 307
308 if (fStorageMap->Contains("default")) {
309 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
310 }
311 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 312}
313
314//_____________________________________________________________________________
315void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
316// set default storage from AliCDBParam object
1f341624 317
4954c5cd 318 AliCDBStorage* bckStorage = fDefaultStorage;
319
b8ec52f6 320 fDefaultStorage = GetStorage(param);
4954c5cd 321
95c0e132 322 if(!fDefaultStorage) return;
323
4954c5cd 324 if(bckStorage && (fDefaultStorage != bckStorage)){
325 AliWarning("Existing default storage replaced: clearing cache!");
326 ClearCache();
327 }
b21e3194 328
329 if (fStorageMap->Contains("default")) {
330 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
331 }
332 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 333}
334
335//_____________________________________________________________________________
336void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
337// set default storage from another active storage
1f341624 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
4954c5cd 352 AliCDBStorage* bckStorage = fDefaultStorage;
353
9e1ceb13 354 fDefaultStorage = storage;
4954c5cd 355
356 if(bckStorage && (fDefaultStorage != bckStorage)){
357 AliWarning("Existing default storage replaced: clearing cache!");
358 ClearCache();
359 }
b21e3194 360
361 if (fStorageMap->Contains("default")) {
362 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
363 }
364 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 365}
4667c116 366
367//_____________________________________________________________________________
1f341624 368void 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;
4667c116 384}
9e1ceb13 385
02c4845e 386//_____________________________________________________________________________
024cf675 387void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
388// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
02c4845e 389
390 AliCDBParam *aPar = CreateParameter(dbString);
391 if(!aPar) return;
024cf675 392 SetSpecificStorage(calibType, aPar);
02c4845e 393 delete aPar;
394}
395
396//_____________________________________________________________________________
024cf675 397void 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://");
c3a7b59a 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)
02c4845e 404
405 if(!fDefaultStorage) {
c3a7b59a 406 AliError("Please activate a default storage first!");
02c4845e 407 return;
408 }
4005d0b5 409
62032124 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());
024cf675 417 if(fSpecificStorages.Contains(objCalibType)){
62032124 418 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
024cf675 419 calibType));
4005d0b5 420 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
421 if(checkPar) delete checkPar;
8e245d15 422 delete fSpecificStorages.Remove(objCalibType);
02c4845e 423 }
95c0e132 424 AliCDBStorage *aStorage = GetStorage(param);
425 if(!aStorage) return;
96b7636d 426
427 fSpecificStorages.Add(objCalibType, param->CloneParam());
b21e3194 428
429 if(fStorageMap->Contains(objCalibType)){
430 delete fStorageMap->Remove(objCalibType);
431 }
432 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
433
02c4845e 434}
435
436//_____________________________________________________________________________
024cf675 437AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
438// get storage specific for detector or calibration type
4b5e0dce 439
9c7fb557 440 AliCDBPath calibPath(calibType);
441 if(!calibPath.IsValid()) return NULL;
96b7636d 442
9c7fb557 443 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
02c4845e 444 if(!checkPar){
b8ec52f6 445 AliError(Form("%s storage not found!", calibType));
02c4845e 446 return NULL;
447 } else {
448 return GetStorage(checkPar);
449 }
96b7636d 450
02c4845e 451}
452
453//_____________________________________________________________________________
024cf675 454AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
82096dfc 455// select storage valid for path from the list of specific storages
024cf675 456
62032124 457 AliCDBPath aPath(path);
4005d0b5 458 if(!aPath.IsValid()) return NULL;
62032124 459
024cf675 460 TIter iter(&fSpecificStorages);
62032124 461 TObjString *aCalibType=0;
96b7636d 462 AliCDBPath tmpPath("null/null/null");
024cf675 463 AliCDBParam* aPar=0;
464 while((aCalibType = (TObjString*) iter.Next())){
62032124 465 AliCDBPath calibTypePath(aCalibType->GetName());
466 if(calibTypePath.Comprises(aPath)) {
96b7636d 467 if(calibTypePath.Comprises(tmpPath)) continue;
024cf675 468 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
96b7636d 469 tmpPath.SetPath(calibTypePath.GetPath());
024cf675 470 }
471 }
472 return aPar;
473}
474
475//_____________________________________________________________________________
4667c116 476AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
02c4845e 477 Int_t version, Int_t subVersion) {
478// get an AliCDBEntry object from the database
479
024cf675 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
02c4845e 489 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
490}
491
492//_____________________________________________________________________________
4667c116 493AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
02c4845e 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//_____________________________________________________________________________
4667c116 502AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
02c4845e 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 }
024cf675 516
517 // query is not specified if path contains wildcard or run range= [-1,-1]
518 if (!query.IsSpecified()) {
4667c116 519 AliError(Form("Unspecified query: %s",
02c4845e 520 query.ToString().Data()));
521 return NULL;
522 }
523
1f341624 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)
024cf675 528 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
529
024cf675 530 AliCDBEntry *entry=0;
8e245d15 531
024cf675 532 // first look into map of cached objects
62032124 533 if(fCache && query.GetFirstRun() == fRun)
024cf675 534 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
535
536 if(entry) {
b8ec52f6 537 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
024cf675 538 return entry;
539 }
96b7636d 540
541 // Entry is not in cache -> retrieve it from CDB and cache it!!
024cf675 542 AliCDBStorage *aStorage=0;
543 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1f341624 544// Bool_t usedDefStorage=kTRUE;
62032124 545
02c4845e 546 if(aPar) {
547 aStorage=GetStorage(aPar);
548 TString str = aPar->GetURI();
549 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1f341624 550// usedDefStorage=kFALSE;
4667c116 551
02c4845e 552 } else {
553 aStorage=GetDefaultStorage();
96b7636d 554 AliDebug(2,"Looking into default storage");
02c4845e 555 }
4667c116 556
024cf675 557 entry = aStorage->Get(query);
024cf675 558
4667c116 559 if(entry && fCache && (query.GetFirstRun() == fRun)){
024cf675 560 CacheEntry(query.GetPath(), entry);
561 }
8e245d15 562
b21e3194 563 if(entry && !fIds->Contains(&entry->GetId())){
564 fIds->Add(entry->GetId().Clone());
565 }
566
567
024cf675 568 return entry;
4667c116 569
570}
571
572//_____________________________________________________________________________
0f40a831 573const 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//_____________________________________________________________________________
4667c116 591AliCDBId* 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//_____________________________________________________________________________
609AliCDBId* 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//_____________________________________________________________________________
619AliCDBId* 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()));
024cf675 664
4667c116 665 } else {
666 aStorage=GetDefaultStorage();
667 AliDebug(2,"Looking into default storage");
668 }
669
670 return aStorage->GetId(query);
671
02c4845e 672}
673
674//_____________________________________________________________________________
62032124 675TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
02c4845e 676 Int_t version, Int_t subVersion) {
677// get multiple AliCDBEntry objects from the database
678
afd8dbf4 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
02c4845e 688 return GetAll(AliCDBId(path, runNumber, runNumber, version,
689 subVersion));
690}
691
692//_____________________________________________________________________________
8e245d15 693TList* AliCDBManager::GetAll(const AliCDBPath& path,
02c4845e 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//_____________________________________________________________________________
701TList* AliCDBManager::GetAll(const AliCDBId& query) {
702// get multiple AliCDBEntry objects from the database
024cf675 703// Warning: this method works correctly only for queries of the type "Detector/*"
704// and not for more specific queries e.g. "Detector/Calib/*" !
8e245d15 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
02c4845e 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
afd8dbf4 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!");
8e245d15 721 return NULL;
02c4845e 722 }
723
724 if (query.IsAnyRange()) {
725 AliError(Form("Unspecified run or runrange: %s",
8e245d15 726 query.ToString().Data()));
02c4845e 727 return NULL;
8e245d15 728 }
729
1f341624 730 if(fLock && query.GetFirstRun() != fRun)
731 AliFatal("Lock is ON: cannot use different run number than the internal one!");
732
6dc56e97 733 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
024cf675 734
8e245d15 735 AliCDBStorage *aStorage;
02c4845e 736 if(aPar) {
737 aStorage=GetStorage(aPar);
96b7636d 738 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
8e245d15 739
02c4845e 740 } else {
741 aStorage=GetDefaultStorage();
8e245d15 742 AliDebug(2,"Looking into default storage");
02c4845e 743 }
744
96b7636d 745 TList *result = 0;
746 if(aStorage) result = aStorage->GetAll(query);
8e245d15 747 if(!result) return 0;
748
96b7636d 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
8e245d15 786 // caching entries
b21e3194 787 TIter iter(result);
788 AliCDBEntry* entry=0;
789 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
02c4845e 790
b21e3194 791 if(!fIds->Contains(&entry->GetId())){
792 fIds->Add(entry->GetId().Clone());
793 }
794 if(fCache && (query.GetFirstRun() == fRun)){
96b7636d 795 CacheEntry(entry->GetId().GetPath(), entry);
8e245d15 796 }
797 }
798
b21e3194 799
8e245d15 800 return result;
02c4845e 801}
802
803//_____________________________________________________________________________
b8ec52f6 804Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, DataType type){
02c4845e 805// store an AliCDBEntry object into the database
806
968cfa08 807 if (object==0x0) {
808 AliError("Null Entry! No storage will be done!");
809 return kFALSE;
810 }
811
02c4845e 812 AliCDBEntry anEntry(object, id, metaData);
b8ec52f6 813 return Put(&anEntry, type);
02c4845e 814
815}
816
817
818//_____________________________________________________________________________
b8ec52f6 819Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
02c4845e 820// store an AliCDBEntry object into the database
821
b8ec52f6 822 if(type == kPrivate && !fDefaultStorage) {
02c4845e 823 AliError("No storage set!");
824 return kFALSE;
825 }
826
968cfa08 827 if (entry->GetObject()==0x0){
828 AliError("No valid object in CDB entry!");
829 return kFALSE;
830 }
831
02c4845e 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();
024cf675 850 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
851
4005d0b5 852 AliCDBStorage *aStorage=0;
02c4845e 853
854 if(aPar) {
855 aStorage=GetStorage(aPar);
02c4845e 856 } else {
b8ec52f6 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 }
02c4845e 868 }
869
b8ec52f6 870 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
871
4005d0b5 872 Bool_t result = aStorage->Put(entry, type);
873
874 if(fRun >= 0) QueryCDB();
875
876 return result;
02c4845e 877
878
879}
9e1ceb13 880
881//_____________________________________________________________________________
4b5e0dce 882void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
883{
884// cache AliCDBEntry. Cache is valid until run number is changed.
885
96b7636d 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
4b5e0dce 895 fEntryCache.Add(new TObjString(path), entry);
96b7636d 896 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
4b5e0dce 897
898}
899
900//_____________________________________________________________________________
917a098b 901void AliCDBManager::Print(Option_t* /*option*/) const
902{
903// Print list of active storages and their URIs
917a098b 904
b8ec52f6 905 TString output=Form("Run number = %d; ",fRun);
906 output += "Cache is ";
917a098b 907 if(!fCache) output += "NOT ";
b8ec52f6 908 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
917a098b 909
910 if(fDefaultStorage) {
b8ec52f6 911 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
912// AliInfo(output.Data());
917a098b 913 }
914 if(fSpecificStorages.GetEntries()>0) {
917a098b 915 TIter iter(fSpecificStorages.GetTable());
62032124 916 TPair *aPair=0;
b8ec52f6 917 Int_t i=1;
917a098b 918 while((aPair = (TPair*) iter.Next())){
b8ec52f6 919 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
920 i++, ((TObjString*) aPair->Key())->GetName(),
917a098b 921 ((AliCDBParam*) aPair->Value())->GetURI().Data());
917a098b 922 }
917a098b 923 }
924 if(fDrainStorage) {
b8ec52f6 925 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
917a098b 926 }
b8ec52f6 927 AliInfo(output.Data());
917a098b 928}
929
930//_____________________________________________________________________________
c3a7b59a 931void AliCDBManager::SetRun(Int_t run)
4b5e0dce 932{
917a098b 933// Sets current run number.
4b5e0dce 934// When the run number changes the caching is cleared.
1f341624 935
4b5e0dce 936 if (fRun == run)
937 return;
938
1f341624 939 if(fLock && fRun >= 0) {
940 AliFatal("Lock is ON, cannot reset run number!");
941 }
942
4b5e0dce 943 fRun = run;
944 ClearCache();
62032124 945 QueryCDB();
4b5e0dce 946}
947
948//_____________________________________________________________________________
949void AliCDBManager::ClearCache(){
950// clear AliCDBEntry cache
951
1f341624 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 */
4b5e0dce 967 fEntryCache.DeleteAll();
1f341624 968 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
4b5e0dce 969}
970
971//_____________________________________________________________________________
4005d0b5 972void 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//_____________________________________________________________________________
9e1ceb13 1010void AliCDBManager::DestroyActiveStorages() {
1011// delete list of active storages
1012
1013 fActiveStorages.DeleteAll();
02c4845e 1014 fSpecificStorages.DeleteAll();
9e1ceb13 1015}
1016
1017//_____________________________________________________________________________
1018void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
b05400be 1019// destroys active storage
1020
1021/*
917a098b 1022 TIter iter(fActiveStorages.GetTable());
b05400be 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 }
6dc56e97 1029*/
9e1ceb13 1030
1031}
1032
62032124 1033//_____________________________________________________________________________
1034void 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 }
6dc56e97 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 }
62032124 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) {
c3a7b59a 1057 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
6dc56e97 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 }
62032124 1065 }
1066 }
62032124 1067}
1068
b8ec52f6 1069//______________________________________________________________________________________________
1070const 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}
62032124 1082
4667c116 1083//______________________________________________________________________________________________
1084void 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//______________________________________________________________________________________________
1122Bool_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
1f341624 1138//______________________________________________________________________________________________
1139void 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
9e1ceb13 1148///////////////////////////////////////////////////////////
1149// AliCDBManager Parameter class //
1150// interface to specific AliCDBParameter class //
1151// (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1152///////////////////////////////////////////////////////////
1153
62032124 1154AliCDBParam::AliCDBParam():
fe12e09c 1155 fType(),
1156 fURI()
62032124 1157{
9e1ceb13 1158// constructor
1159
1160}
1161
1162//_____________________________________________________________________________
1163AliCDBParam::~AliCDBParam() {
1164// destructor
1165
1166}
1167