]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCDBManager.cxx
detector name to build the lhcPeriod_DET is to be looked in "detector" column, not...
[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
9e1ceb13 127//_____________________________________________________________________________
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
62032124 240//_____________________________________________________________________________
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
9e1ceb13 247//_____________________________________________________________________________
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
9e1ceb13 292//_____________________________________________________________________________
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);
4954c5cd 300
301 if(bckStorage && (fDefaultStorage != bckStorage)){
302 AliWarning("Existing default storage replaced: clearing cache!");
303 ClearCache();
304 }
b21e3194 305
306 if (fStorageMap->Contains("default")) {
307 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
308 }
309 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 310}
311
312//_____________________________________________________________________________
313void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
314// set default storage from AliCDBParam object
1f341624 315
4954c5cd 316 AliCDBStorage* bckStorage = fDefaultStorage;
317
b8ec52f6 318 fDefaultStorage = GetStorage(param);
4954c5cd 319
320 if(bckStorage && (fDefaultStorage != bckStorage)){
321 AliWarning("Existing default storage replaced: clearing cache!");
322 ClearCache();
323 }
b21e3194 324
325 if (fStorageMap->Contains("default")) {
326 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
327 }
328 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 329}
330
331//_____________________________________________________________________________
332void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
333// set default storage from another active storage
1f341624 334
335 // if lock is ON, cannot activate more storages!
336 if(fLock) {
337 if (fDefaultStorage) {
338 AliFatal("Lock is ON, and default storage is already set: "
339 "cannot reset it or activate more storages!");
340 }
341 }
342
343 if (!storage) {
344 UnsetDefaultStorage();
345 return;
346 }
347
4954c5cd 348 AliCDBStorage* bckStorage = fDefaultStorage;
349
9e1ceb13 350 fDefaultStorage = storage;
4954c5cd 351
352 if(bckStorage && (fDefaultStorage != bckStorage)){
353 AliWarning("Existing default storage replaced: clearing cache!");
354 ClearCache();
355 }
b21e3194 356
357 if (fStorageMap->Contains("default")) {
358 delete fStorageMap->Remove(fStorageMap->GetValue("default"));
359 }
360 fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
9e1ceb13 361}
4667c116 362
363//_____________________________________________________________________________
1f341624 364void AliCDBManager::UnsetDefaultStorage() {
365// Unset default storage
366
367 // if lock is ON, action is forbidden!
368 if(fLock) {
369 if (fDefaultStorage) {
370 AliFatal("Lock is ON: cannot unset default storage!");
371 }
372 }
373
374 if (fDefaultStorage) {
375 AliWarning("Clearing cache!");
376 ClearCache();
377 }
378
379 fDefaultStorage = 0x0;
4667c116 380}
9e1ceb13 381
02c4845e 382//_____________________________________________________________________________
024cf675 383void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString) {
384// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
02c4845e 385
386 AliCDBParam *aPar = CreateParameter(dbString);
387 if(!aPar) return;
024cf675 388 SetSpecificStorage(calibType, aPar);
02c4845e 389 delete aPar;
390}
391
392//_____________________________________________________________________________
024cf675 393void AliCDBManager::SetSpecificStorage(const char* calibType, AliCDBParam* param) {
394// sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
395// Default storage should be defined prior to any specific storages, e.g.:
396// AliCDBManager::instance()->SetDefaultStorage("alien://");
c3a7b59a 397// AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
398// AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
399// calibType must be a valid CDB path! (3 level folder structure)
02c4845e 400
401 if(!fDefaultStorage) {
c3a7b59a 402 AliError("Please activate a default storage first!");
02c4845e 403 return;
404 }
4005d0b5 405
62032124 406 AliCDBPath aPath(calibType);
407 if(!aPath.IsValid()){
408 AliError(Form("Not a valid path: %s", calibType));
409 return;
410 }
411
412 TObjString *objCalibType = new TObjString(aPath.GetPath());
024cf675 413 if(fSpecificStorages.Contains(objCalibType)){
62032124 414 AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
024cf675 415 calibType));
4005d0b5 416 AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
417 if(checkPar) delete checkPar;
8e245d15 418 delete fSpecificStorages.Remove(objCalibType);
02c4845e 419 }
420 GetStorage(param);
96b7636d 421
422 fSpecificStorages.Add(objCalibType, param->CloneParam());
b21e3194 423
424 if(fStorageMap->Contains(objCalibType)){
425 delete fStorageMap->Remove(objCalibType);
426 }
427 fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
428
02c4845e 429}
430
431//_____________________________________________________________________________
024cf675 432AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* calibType) {
433// get storage specific for detector or calibration type
4b5e0dce 434
9c7fb557 435 AliCDBPath calibPath(calibType);
436 if(!calibPath.IsValid()) return NULL;
96b7636d 437
9c7fb557 438 AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
02c4845e 439 if(!checkPar){
b8ec52f6 440 AliError(Form("%s storage not found!", calibType));
02c4845e 441 return NULL;
442 } else {
443 return GetStorage(checkPar);
444 }
96b7636d 445
02c4845e 446}
447
024cf675 448//_____________________________________________________________________________
449AliCDBParam* AliCDBManager::SelectSpecificStorage(const TString& path) {
82096dfc 450// select storage valid for path from the list of specific storages
024cf675 451
62032124 452 AliCDBPath aPath(path);
4005d0b5 453 if(!aPath.IsValid()) return NULL;
62032124 454
024cf675 455 TIter iter(&fSpecificStorages);
62032124 456 TObjString *aCalibType=0;
96b7636d 457 AliCDBPath tmpPath("null/null/null");
024cf675 458 AliCDBParam* aPar=0;
459 while((aCalibType = (TObjString*) iter.Next())){
62032124 460 AliCDBPath calibTypePath(aCalibType->GetName());
461 if(calibTypePath.Comprises(aPath)) {
96b7636d 462 if(calibTypePath.Comprises(tmpPath)) continue;
024cf675 463 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
96b7636d 464 tmpPath.SetPath(calibTypePath.GetPath());
024cf675 465 }
466 }
467 return aPar;
468}
469
02c4845e 470//_____________________________________________________________________________
4667c116 471AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber,
02c4845e 472 Int_t version, Int_t subVersion) {
473// get an AliCDBEntry object from the database
474
024cf675 475 if(runNumber < 0){
476 // RunNumber is not specified. Try with fRun
477 if (fRun < 0){
478 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
479 return NULL;
480 }
481 runNumber = fRun;
482 }
483
02c4845e 484 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
485}
486
487//_____________________________________________________________________________
4667c116 488AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path,
02c4845e 489 const AliCDBRunRange& runRange, Int_t version,
490 Int_t subVersion) {
491// get an AliCDBEntry object from the database!
492
493 return Get(AliCDBId(path, runRange, version, subVersion));
494}
495
496//_____________________________________________________________________________
4667c116 497AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {
02c4845e 498// get an AliCDBEntry object from the database
499
500 if(!fDefaultStorage) {
501 AliError("No storage set!");
502 return NULL;
503 }
504
505 // check if query's path and runRange are valid
506 // query is invalid also if version is not specified and subversion is!
507 if (!query.IsValid()) {
508 AliError(Form("Invalid query: %s", query.ToString().Data()));
509 return NULL;
510 }
024cf675 511
512 // query is not specified if path contains wildcard or run range= [-1,-1]
513 if (!query.IsSpecified()) {
4667c116 514 AliError(Form("Unspecified query: %s",
02c4845e 515 query.ToString().Data()));
516 return NULL;
517 }
518
1f341624 519 if(fLock && query.GetFirstRun() != fRun)
520 AliFatal("Lock is ON: cannot use different run number than the internal one!");
521
522 if(fCache && query.GetFirstRun() != fRun)
024cf675 523 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
524
024cf675 525 AliCDBEntry *entry=0;
8e245d15 526
024cf675 527 // first look into map of cached objects
62032124 528 if(fCache && query.GetFirstRun() == fRun)
024cf675 529 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
530
531 if(entry) {
b8ec52f6 532 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
024cf675 533 return entry;
534 }
96b7636d 535
536 // Entry is not in cache -> retrieve it from CDB and cache it!!
024cf675 537 AliCDBStorage *aStorage=0;
538 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1f341624 539// Bool_t usedDefStorage=kTRUE;
62032124 540
02c4845e 541 if(aPar) {
542 aStorage=GetStorage(aPar);
543 TString str = aPar->GetURI();
544 AliDebug(2,Form("Looking into storage: %s",str.Data()));
1f341624 545// usedDefStorage=kFALSE;
4667c116 546
02c4845e 547 } else {
548 aStorage=GetDefaultStorage();
96b7636d 549 AliDebug(2,"Looking into default storage");
02c4845e 550 }
4667c116 551
024cf675 552 entry = aStorage->Get(query);
024cf675 553
4667c116 554 if(entry && fCache && (query.GetFirstRun() == fRun)){
024cf675 555 CacheEntry(query.GetPath(), entry);
556 }
8e245d15 557
b21e3194 558 if(entry && !fIds->Contains(&entry->GetId())){
559 fIds->Add(entry->GetId().Clone());
560 }
561
562
024cf675 563 return entry;
4667c116 564
565}
566
0f40a831 567//_____________________________________________________________________________
568const char* AliCDBManager::GetURI(const char* path) {
569// return the URI of the storage where to look for path
570
571 if(!IsDefaultStorageSet()) return 0;
572
573 AliCDBParam *aPar=SelectSpecificStorage(path);
574
575 if(aPar) {
576 return aPar->GetURI().Data();
577
578 } else {
579 return GetDefaultStorage()->GetURI().Data();
580 }
581
582 return 0;
583}
584
4667c116 585//_____________________________________________________________________________
586AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber,
587 Int_t version, Int_t subVersion) {
588// get the AliCDBId of the valid object from the database (does not retrieve the object)
589// User must delete returned object!
590
591 if(runNumber < 0){
592 // RunNumber is not specified. Try with fRun
593 if (fRun < 0){
594 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
595 return NULL;
596 }
597 runNumber = fRun;
598 }
599
600 return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
601}
602
603//_____________________________________________________________________________
604AliCDBId* AliCDBManager::GetId(const AliCDBPath& path,
605 const AliCDBRunRange& runRange, Int_t version,
606 Int_t subVersion) {
607// get the AliCDBId of the valid object from the database (does not retrieve the object)
608// User must delete returned object!
609
610 return GetId(AliCDBId(path, runRange, version, subVersion));
611}
612
613//_____________________________________________________________________________
614AliCDBId* AliCDBManager::GetId(const AliCDBId& query) {
615// get the AliCDBId of the valid object from the database (does not retrieve the object)
616// User must delete returned object!
617
618 if(!fDefaultStorage) {
619 AliError("No storage set!");
620 return NULL;
621 }
622
623 // check if query's path and runRange are valid
624 // query is invalid also if version is not specified and subversion is!
625 if (!query.IsValid()) {
626 AliError(Form("Invalid query: %s", query.ToString().Data()));
627 return NULL;
628 }
629
630 // query is not specified if path contains wildcard or run range= [-1,-1]
631 if (!query.IsSpecified()) {
632 AliError(Form("Unspecified query: %s",
633 query.ToString().Data()));
634 return NULL;
635 }
636
637 if(fCache && query.GetFirstRun() != fRun)
638 AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
639
640 AliCDBEntry* entry = 0;
641
642 // first look into map of cached objects
643 if(fCache && query.GetFirstRun() == fRun)
644 entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
645
646 if(entry) {
647 AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
648 return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
649 }
650
651 // Entry is not in cache -> retrieve it from CDB and cache it!!
652 AliCDBStorage *aStorage=0;
653 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
654
655 if(aPar) {
656 aStorage=GetStorage(aPar);
657 TString str = aPar->GetURI();
658 AliDebug(2,Form("Looking into storage: %s",str.Data()));
024cf675 659
4667c116 660 } else {
661 aStorage=GetDefaultStorage();
662 AliDebug(2,"Looking into default storage");
663 }
664
665 return aStorage->GetId(query);
666
02c4845e 667}
668
669//_____________________________________________________________________________
62032124 670TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber,
02c4845e 671 Int_t version, Int_t subVersion) {
672// get multiple AliCDBEntry objects from the database
673
afd8dbf4 674 if(runNumber < 0){
675 // RunNumber is not specified. Try with fRun
676 if (fRun < 0){
677 AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
678 return NULL;
679 }
680 runNumber = fRun;
681 }
682
02c4845e 683 return GetAll(AliCDBId(path, runNumber, runNumber, version,
684 subVersion));
685}
686
687//_____________________________________________________________________________
8e245d15 688TList* AliCDBManager::GetAll(const AliCDBPath& path,
02c4845e 689 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
690// get multiple AliCDBEntry objects from the database
691
692 return GetAll(AliCDBId(path, runRange, version, subVersion));
693}
694
695//_____________________________________________________________________________
696TList* AliCDBManager::GetAll(const AliCDBId& query) {
697// get multiple AliCDBEntry objects from the database
024cf675 698// Warning: this method works correctly only for queries of the type "Detector/*"
699// and not for more specific queries e.g. "Detector/Calib/*" !
8e245d15 700// Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
701// To get an object from cache use Get() function
02c4845e 702
703 if(!fDefaultStorage) {
704 AliError("No storage set!");
705 return NULL;
706 }
707
708 if (!query.IsValid()) {
709 AliError(Form("Invalid query: %s", query.ToString().Data()));
710 return NULL;
711 }
712
afd8dbf4 713 if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
714 // if specific storages are active a query with "*" is ambiguous
715 AliError("Query too generic in this context!");
8e245d15 716 return NULL;
02c4845e 717 }
718
719 if (query.IsAnyRange()) {
720 AliError(Form("Unspecified run or runrange: %s",
8e245d15 721 query.ToString().Data()));
02c4845e 722 return NULL;
8e245d15 723 }
724
1f341624 725 if(fLock && query.GetFirstRun() != fRun)
726 AliFatal("Lock is ON: cannot use different run number than the internal one!");
727
6dc56e97 728 AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
024cf675 729
8e245d15 730 AliCDBStorage *aStorage;
02c4845e 731 if(aPar) {
732 aStorage=GetStorage(aPar);
96b7636d 733 AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
8e245d15 734
02c4845e 735 } else {
736 aStorage=GetDefaultStorage();
8e245d15 737 AliDebug(2,"Looking into default storage");
02c4845e 738 }
739
96b7636d 740 TList *result = 0;
741 if(aStorage) result = aStorage->GetAll(query);
8e245d15 742 if(!result) return 0;
743
96b7636d 744 // loop on result to check whether entries should be re-queried with specific storages
745 if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
746 AliInfo("Now look into all other specific storages...");
747
748 TIter iter(result);
749 AliCDBEntry* chkEntry=0;
750
751 while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
752 AliCDBId& chkId = chkEntry->GetId();
753 AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
754 AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
755 if (!chkPar || aPar == chkPar) continue;
756 AliCDBStorage *chkStorage = GetStorage(chkPar);
757 AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
758
759 AliCDBEntry *newEntry=0;
760 chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
761 chkId.SetVersion(query.GetVersion());
762 chkId.SetSubVersion(query.GetSubVersion());
763
764 if(chkStorage) newEntry = chkStorage->Get(chkId);
765 if(!newEntry) continue;
766
767 // object is found in specific storage: replace entry in the result list!
768 chkEntry->SetOwner(1);
769 delete result->Remove(chkEntry);
770 result->AddFirst(newEntry);
771 }
772
773 Int_t nEntries = result->GetEntries();
774 AliInfo("After look into other specific storages, result list is:");
775 for(int i=0; i<nEntries;i++){
776 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
777 AliInfo(Form("%s",entry->GetId().ToString().Data()));
778 }
779 }
780
8e245d15 781 // caching entries
b21e3194 782 TIter iter(result);
783 AliCDBEntry* entry=0;
784 while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
02c4845e 785
b21e3194 786 if(!fIds->Contains(&entry->GetId())){
787 fIds->Add(entry->GetId().Clone());
788 }
789 if(fCache && (query.GetFirstRun() == fRun)){
96b7636d 790 CacheEntry(entry->GetId().GetPath(), entry);
8e245d15 791 }
792 }
793
b21e3194 794
8e245d15 795 return result;
02c4845e 796}
797
798//_____________________________________________________________________________
b8ec52f6 799Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, DataType type){
02c4845e 800// store an AliCDBEntry object into the database
801
802 AliCDBEntry anEntry(object, id, metaData);
b8ec52f6 803 return Put(&anEntry, type);
02c4845e 804
805}
806
807
808//_____________________________________________________________________________
b8ec52f6 809Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){
02c4845e 810// store an AliCDBEntry object into the database
811
b8ec52f6 812 if(type == kPrivate && !fDefaultStorage) {
02c4845e 813 AliError("No storage set!");
814 return kFALSE;
815 }
816
817 if (!entry){
818 AliError("No entry!");
819 return kFALSE;
820 }
821
822 if (!entry->GetId().IsValid()) {
823 AliError(Form("Invalid entry ID: %s",
824 entry->GetId().ToString().Data()));
825 return kFALSE;
826 }
827
828 if (!entry->GetId().IsSpecified()) {
829 AliError(Form("Unspecified entry ID: %s",
830 entry->GetId().ToString().Data()));
831 return kFALSE;
832 }
833
834 AliCDBId id = entry->GetId();
024cf675 835 AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
836
4005d0b5 837 AliCDBStorage *aStorage=0;
02c4845e 838
839 if(aPar) {
840 aStorage=GetStorage(aPar);
02c4845e 841 } else {
b8ec52f6 842 switch(type){
843 case kCondition:
844 aStorage = GetStorage(fCondParam);
845 break;
846 case kReference:
847 aStorage = GetStorage(fRefParam);
848 break;
849 case kPrivate:
850 aStorage = GetDefaultStorage();
851 break;
852 }
02c4845e 853 }
854
b8ec52f6 855 AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
856
4005d0b5 857 Bool_t result = aStorage->Put(entry, type);
858
859 if(fRun >= 0) QueryCDB();
860
861 return result;
02c4845e 862
863
864}
9e1ceb13 865
4b5e0dce 866//_____________________________________________________________________________
867void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
868{
869// cache AliCDBEntry. Cache is valid until run number is changed.
870
96b7636d 871 AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
872
873 if(chkEntry) {
874 AliDebug(2, Form("Object %s already in cache !!", path));
875 return;
876 } else {
877 AliDebug(2,Form("Caching entry %s", path));
878 }
879
4b5e0dce 880 fEntryCache.Add(new TObjString(path), entry);
96b7636d 881 AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
4b5e0dce 882
883}
884
917a098b 885//_____________________________________________________________________________
886void AliCDBManager::Print(Option_t* /*option*/) const
887{
888// Print list of active storages and their URIs
917a098b 889
b8ec52f6 890 TString output=Form("Run number = %d; ",fRun);
891 output += "Cache is ";
917a098b 892 if(!fCache) output += "NOT ";
b8ec52f6 893 output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
917a098b 894
895 if(fDefaultStorage) {
b8ec52f6 896 output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
897// AliInfo(output.Data());
917a098b 898 }
899 if(fSpecificStorages.GetEntries()>0) {
917a098b 900 TIter iter(fSpecificStorages.GetTable());
62032124 901 TPair *aPair=0;
b8ec52f6 902 Int_t i=1;
917a098b 903 while((aPair = (TPair*) iter.Next())){
b8ec52f6 904 output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
905 i++, ((TObjString*) aPair->Key())->GetName(),
917a098b 906 ((AliCDBParam*) aPair->Value())->GetURI().Data());
917a098b 907 }
917a098b 908 }
909 if(fDrainStorage) {
b8ec52f6 910 output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
917a098b 911 }
b8ec52f6 912 AliInfo(output.Data());
917a098b 913}
914
4b5e0dce 915//_____________________________________________________________________________
c3a7b59a 916void AliCDBManager::SetRun(Int_t run)
4b5e0dce 917{
917a098b 918// Sets current run number.
4b5e0dce 919// When the run number changes the caching is cleared.
1f341624 920
4b5e0dce 921 if (fRun == run)
922 return;
923
1f341624 924 if(fLock && fRun >= 0) {
925 AliFatal("Lock is ON, cannot reset run number!");
926 }
927
4b5e0dce 928 fRun = run;
929 ClearCache();
62032124 930 QueryCDB();
4b5e0dce 931}
932
933//_____________________________________________________________________________
934void AliCDBManager::ClearCache(){
935// clear AliCDBEntry cache
936
1f341624 937 AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
938
939 /*
940 // To clean entries one by one
941 TIter iter(fEntryCache.GetTable());
942 TPair* pair=0;
943 while((pair= dynamic_cast<TPair*> (iter.Next()))){
944
945 TObjString* key = dynamic_cast<TObjString*> (pair->Key());
946 AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
947 AliDebug(2, Form("Deleting entry: %s", key->GetName()));
948 if (entry) delete entry;
949 delete fEntryCache.Remove(key);
950 }
951 */
4b5e0dce 952 fEntryCache.DeleteAll();
1f341624 953 AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
4b5e0dce 954}
955
4005d0b5 956//_____________________________________________________________________________
957void AliCDBManager::UnloadFromCache(const char* path){
958// unload cached object
959
960 AliCDBPath queryPath(path);
961 if(!queryPath.IsValid()) return;
962
963 if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
964 if(fEntryCache.Contains(path)){
965 AliInfo(Form("Unloading object \"%s\" from cache", path));
966 TObjString pathStr(path);
967 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
968 if(entry) delete entry;
969 delete fEntryCache.Remove(&pathStr);
970 } else {
971 AliError(Form("Cache does not contain object \"%s\"!", path))
972 }
973 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
974 return;
975 }
976
977 // path is wildcard: loop on the cache and unload all comprised objects!
978 TIter iter(fEntryCache.GetTable());
979 TPair* pair = 0;
980
981 while((pair = dynamic_cast<TPair*> (iter.Next()))){
982 AliCDBPath entryPath = pair->Key()->GetName();
983 if(queryPath.Comprises(entryPath)) {
984 AliInfo(Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data()));
985 TObjString pathStr(entryPath.GetPath().Data());
986 AliCDBEntry *entry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(&pathStr));
987 if(entry) delete entry;
988 delete fEntryCache.Remove(&pathStr);
989 }
990 }
991 AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
992}
993
9e1ceb13 994//_____________________________________________________________________________
995void AliCDBManager::DestroyActiveStorages() {
996// delete list of active storages
997
998 fActiveStorages.DeleteAll();
02c4845e 999 fSpecificStorages.DeleteAll();
9e1ceb13 1000}
1001
1002//_____________________________________________________________________________
1003void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
b05400be 1004// destroys active storage
1005
1006/*
917a098b 1007 TIter iter(fActiveStorages.GetTable());
b05400be 1008 TPair* aPair;
1009 while ((aPair = (TPair*) iter.Next())) {
1010 if(storage == (AliCDBStorage*) aPair->Value())
1011 delete fActiveStorages.Remove(aPair->Key());
1012 storage->Delete(); storage=0x0;
1013 }
6dc56e97 1014*/
9e1ceb13 1015
1016}
1017
62032124 1018//_____________________________________________________________________________
1019void AliCDBManager::QueryCDB() {
1020// query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1021
1022 if (fRun < 0){
1023 AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1024 return;
1025 }
6dc56e97 1026 if (!fDefaultStorage){
1027 AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1028 return;
1029 }
1030 if(fDefaultStorage->GetType() == "alien"){
1031 fDefaultStorage->QueryCDB(fRun);
1032 } else {
1033 AliDebug(2,"Skipping query for valid files, it used only in grid...");
1034 }
62032124 1035
1036 TIter iter(&fSpecificStorages);
1037 TObjString *aCalibType=0;
1038 AliCDBParam* aPar=0;
1039 while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1040 aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1041 if(aPar) {
c3a7b59a 1042 AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
6dc56e97 1043 AliCDBStorage *aStorage = GetStorage(aPar);
1044 if(aStorage->GetType() == "alien"){
1045 aStorage->QueryCDB(fRun,aCalibType->GetName());
1046 } else {
1047 AliDebug(2,
1048 "Skipping query for valid files, it is used only in grid...");
1049 }
62032124 1050 }
1051 }
62032124 1052}
1053
b8ec52f6 1054//______________________________________________________________________________________________
1055const char* AliCDBManager::GetDataTypeName(DataType type)
1056{
1057 // returns the name (string) of the data type
1058
1059 switch (type){
1060 case kCondition: return "Conditions";
1061 case kReference: return "Reference";
1062 case kPrivate: return "Private";
1063 }
1064 return 0;
1065
1066}
62032124 1067
4667c116 1068//______________________________________________________________________________________________
1069void AliCDBManager::InitShortLived()
1070{
1071 // Init the list of short-lived objects
1072 // currently disabled
1073
1074 fShortLived=0x0;
1075
1076// fShortLived = new TList();
1077// fShortLived->SetOwner(1);
1078//
1079// fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1080//
1081// fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1082// fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1083//
1084// fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1085//
1086// fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1087// fShortLived->Add(new TObjString("MUON/Calib/HV"));
1088// fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1089//
1090// fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1091// fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1092//
1093// fShortLived->Add(new TObjString("PMD/Calib/Data"));
1094//
1095// fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1096// fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1097// fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1098// fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1099// fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1100// fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1101//
1102// fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1103
1104}
1105
1106//______________________________________________________________________________________________
1107Bool_t AliCDBManager::IsShortLived(const char* path)
1108{
1109 // returns the name (string) of the data type
1110
1111 if(!fShortLived) return kFALSE;
1112
1113 AliCDBPath aPath(path);
1114 if(!aPath.IsValid()){
1115 AliError(Form("Not a valid path: %s", path));
1116 return kFALSE;
1117 }
1118
1119 return fShortLived->Contains(path);
1120
1121}
1122
1f341624 1123//______________________________________________________________________________________________
1124void AliCDBManager::SetLock(Bool_t lock){
1125
1126 if(fLock == kTRUE && lock == kFALSE) {
1127 AliFatal("Lock is ON: cannot reset it!");
1128 }
1129
1130 fLock=lock;
1131}
1132
9e1ceb13 1133///////////////////////////////////////////////////////////
1134// AliCDBManager Parameter class //
1135// interface to specific AliCDBParameter class //
1136// (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1137///////////////////////////////////////////////////////////
1138
62032124 1139AliCDBParam::AliCDBParam():
fe12e09c 1140 fType(),
1141 fURI()
62032124 1142{
9e1ceb13 1143// constructor
1144
1145}
1146
1147//_____________________________________________________________________________
1148AliCDBParam::~AliCDBParam() {
1149// destructor
1150
1151}
1152