AliRoot Core  3dc7879 (3dc7879)
AliCDBManager.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: The ALICE Off-line Project. *
5  * Contributors are mentioned in the code where appropriate. *
6  * *
7  * Permission to use, copy, modify and distribute this software and its *
8  * documentation strictly for non-commercial purposes is hereby granted *
9  * without fee, provided that the above copyright notice appears in all *
10  * copies and that both the copyright notice and this permission notice *
11  * appear in the supporting documentation. The authors make no claims *
12  * about the suitability of this software for any purpose. It is *
13  * provided "as is" without express or implied warranty. *
14  **************************************************************************/
15 //-------------------------------------------------------------------------
16 // Implementation of AliCDBManager and AliCDBParam classe
17 // Author: Alberto Colla
18 // e-mail: Alberto.Colla@cern.ch
19 //-------------------------------------------------------------------------
20 
21 #include <fstream>
22 
23 #include "AliCDBManager.h"
24 #include "AliCDBStorage.h"
25 #include "AliLog.h"
26 #include "AliCDBDump.h"
27 #include "AliCDBLocal.h"
28 #include "AliCDBGrid.h"
29 #include "AliCDBEntry.h"
30 #include "AliCDBHandler.h"
31 
32 #include <TObjString.h>
33 #include <TSAXParser.h>
34 #include <TFile.h>
35 #include <TKey.h>
36 #include <TUUID.h>
37 #include <TGrid.h>
38 #include "TMessage.h"
39 #include "TObject.h"
40 #include "TRegexp.h"
41 
42 ClassImp(AliCDBParam)
43 
44 ClassImp(AliCDBManager)
45 
46 //TODO OCDB and Reference folder should not be fully hardcoded but built from run number (or year/LHC period)
47 TString AliCDBManager::fgkCondUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/CDB?user=aliprod");
48 TString AliCDBManager::fgkRefUri("alien://folder=/alice/cern.ch/user/a/aliprod/testCDB/Reference?user=aliprod");
49 TString AliCDBManager::fgkMCIdealStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Ideal");
50 TString AliCDBManager::fgkMCFullStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Full");
51 TString AliCDBManager::fgkMCResidualStorage("alien://folder=/alice/simulation/2008/v4-15-Release/Residual");
52 TString AliCDBManager::fgkOCDBFolderXMLfile("alien:
53 AliCDBManager* AliCDBManager::fgInstance = 0x0;
54 
55 //_____________________________________________________________________________
56 AliCDBManager* AliCDBManager::Instance(TMap *entryCache, Int_t run) {
57 // returns AliCDBManager instance (singleton)
58 
59  if (!fgInstance) {
60  fgInstance = new AliCDBManager();
61  if (!entryCache)
62  fgInstance->Init();
63  else
64  fgInstance->InitFromCache(entryCache,run);
65  }
66 
67  return fgInstance;
68 }
69 
70 //_____________________________________________________________________________
72 // factory registering
73 
76  // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
77  static int hltOnlineMode = getenv("HLT_ONLINE_MODE") && strcmp(getenv("HLT_ONLINE_MODE"), "on") == 0;
78  if(!hltOnlineMode && !gSystem->Exec("root-config --has-alien 2>/dev/null |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
79  AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
83  }
84 
86 }
87 
88 //_____________________________________________________________________________
89 void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) {
90 // initialize manager from existing cache
91 // used on the slaves in case of parallel reconstruction
92  SetRun(run);
93 
94  TIter iter(entryCache->GetTable());
95  TPair* pair = 0;
96 
97  while((pair = dynamic_cast<TPair*> (iter.Next()))){
98  fEntryCache.Add(pair->Key(),pair->Value());
99  }
100  // fEntry is the new owner of the cache
101  fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
102  entryCache->SetOwnerKeyValue(kFALSE,kFALSE);
103  AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries()));
104 }
105 
106 //_____________________________________________________________________________
107 void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys) const {
108 //
109 // If singleKeys is true, dump the entries map and the ids list to the snapshot file
110 // (provided mostly for historical reasons, the file is then read with InitFromSnapshot),
111 // otherwise write to file each AliCDBEntry separately (the is the preferred way, the file
112 // is then read with SetSnapshotMode).
113 
114  // open the file
115  TFile *f = TFile::Open(snapshotFileName,"RECREATE");
116  if (!f || f->IsZombie()){
117  AliError(Form("Cannot open file %s",snapshotFileName));
118  return;
119  }
120 
121  AliInfo(Form("Dumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries()));
122  AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
123 
124  f->cd();
125  //
126  if(singleKeys){
127  f->WriteObject(&fEntryCache,"CDBentriesMap");
128  f->WriteObject(fIds,"CDBidsList");
129  }else{
130  // We write the entries one by one named by their calibration path
131  TIter iter(fEntryCache.GetTable());
132  TPair* pair = 0;
133  while((pair = dynamic_cast<TPair*> (iter.Next()))){
134  TObjString *os = dynamic_cast<TObjString*>(pair->Key());
135  if (!os) continue;
136  TString path = os->GetString();
137  AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(pair->Value());
138  if (!entry) continue;
139  path.ReplaceAll("/","*");
140  entry->Write(path.Data());
141  }
142  // RS: always store the CDBMap/CDBList
143  f->WriteObject(fStorageMap,"CDBSnapshotEntriesMap");
144  f->WriteObject(fIds,"CDBSnapshotIdsList");
145  }
146  f->Close();
147  delete f;
148 }
149 
150 //_____________________________________________________________________________
151 void AliCDBManager::DumpToLightSnapshotFile(const char* lightSnapshotFileName) const {
152 // The light snapshot does not contain the CDB objects (AliCDBEntries) but
153 // only the information identifying them, that is the map of storages and
154 // the list of AliCDBIds, as in the UserInfo of AliESDs.root
155 
156  // open the file
157  TFile *f = TFile::Open(lightSnapshotFileName,"RECREATE");
158  if (!f || f->IsZombie()){
159  AliError(Form("Cannot open file %s",lightSnapshotFileName));
160  return;
161  }
162 
163  AliInfo(Form("Dumping map of storages with %d entries!\n", fStorageMap->GetEntries()));
164  AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries()));
165  f->WriteObject(fStorageMap,"cdbStoragesMap");
166  f->WriteObject(fIds,"CDBidsList");
167 
168  f->Close();
169  delete f;
170 }
171 
172 //_____________________________________________________________________________
173 Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){
174 // initialize manager from a CDB snapshot, that is add the entries
175 // to the entries map and the ids to the ids list taking them from
176 // the map and the list found in the input file
177 
178  // if the manager is locked it cannot initialize from a snapshot
179  if(fLock) {
180  AliError("Being locked I cannot initialize from the snapshot!");
181  return kFALSE;
182  }
183 
184  // open the file
185  TString snapshotFile(snapshotFileName);
186  if(snapshotFile.BeginsWith("alien://")){
187  if(!gGrid) {
188  TGrid::Connect("alien://","");
189  if(!gGrid) {
190  AliError("Connection to alien failed!");
191  return kFALSE;
192  }
193  }
194  }
195 
196  TFile *f = TFile::Open(snapshotFileName);
197  if (!f || f->IsZombie()){
198  AliError(Form("Cannot open file %s",snapshotFileName));
199  return kFALSE;
200  }
201 
202  // retrieve entries' map from snapshot file
203  TMap *entriesMap = 0;
204  TIter next(f->GetListOfKeys());
205  TKey *key;
206  while ((key = (TKey*)next())) {
207  if (strcmp(key->GetClassName(),"TMap") != 0) continue;
208  entriesMap = (TMap*)key->ReadObj();
209  break;
210  }
211  if (!entriesMap || entriesMap->GetEntries()==0){
212  AliError("Cannot get valid map of CDB entries from snapshot file");
213  return kFALSE;
214  }
215 
216  // retrieve ids' list from snapshot file
217  TList *idsList = 0;
218  TIter nextKey(f->GetListOfKeys());
219  TKey *keyN;
220  while ((keyN = (TKey*)nextKey())) {
221  if (strcmp(keyN->GetClassName(),"TList") != 0) continue;
222  idsList = (TList*)keyN->ReadObj();
223  break;
224  }
225  if (!idsList || idsList->GetEntries()==0){
226  AliError("Cannot get valid list of CDB entries from snapshot file");
227  return kFALSE;
228  }
229 
230  // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids.
231  // If "overwrite" is false: add the entry to the cache and its id to the list of ids
232  // only if neither of them is already there.
233  // If "overwrite" is true: write the snapshot entry,id in any case. If something
234  // was already there for that calibration type, remove it and issue a warning
235  TIter iterObj(entriesMap->GetTable());
236  TPair* pair = 0;
237  Int_t nAdded=0;
238  while((pair = dynamic_cast<TPair*> (iterObj.Next()))){
239  TObjString* os = (TObjString*) pair->Key();
240  TString path = os->GetString();
241  TIter iterId(idsList);
242  AliCDBId* id=0;
243  AliCDBId* correspondingId=0;
244  while((id = dynamic_cast<AliCDBId*> (iterId.Next()))){
245  TString idpath(id->GetPath());
246  if(idpath==path){
247  correspondingId=id;
248  break;
249  }
250  }
251  if(!correspondingId){
252  AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data()));
253  break;
254  }
255  Bool_t cached = fEntryCache.Contains(path.Data());
256  Bool_t registeredId = kFALSE;
257  TIter iter(fIds);
258  AliCDBId *idT = 0;
259  while((idT = dynamic_cast<AliCDBId*> (iter.Next()))){
260  if(idT->GetPath()==path){
261  registeredId = kTRUE;
262  break;
263  }
264  }
265 
266  if(overwrite){
267  if(cached || registeredId){
268  AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data()));
269  UnloadFromCache(path.Data());
270  }
271  fEntryCache.Add(pair->Key(),pair->Value());
272  fIds->Add(id);
273  nAdded++;
274  }else{
275  if(cached || registeredId){
276  AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data()));
277  }else{
278  fEntryCache.Add(pair->Key(),pair->Value());
279  fIds->Add(id);
280  nAdded++;
281  }
282  }
283  }
284 
285  // fEntry is the new owner of the cache
286  fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
287  entriesMap->SetOwnerKeyValue(kFALSE,kFALSE);
288  fIds->SetOwner(kTRUE);
289  idsList->SetOwner(kFALSE);
290  AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries()));
291 
292  f->Close();
293  delete f;
294 
295  return kTRUE;
296 }
297 
298 //_____________________________________________________________________________
300 // delete ALCDBManager instance and active storages
301 
302  if (fgInstance) {
303  //fgInstance->Delete();
304  delete fgInstance;
305  fgInstance = 0x0;
306  }
307 }
308 
309 //_____________________________________________________________________________
311  TObject(),
312  fFactories(),
313  fActiveStorages(),
315  fEntryCache(),
317  fIds(0),
318  fStorageMap(0),
319  fShortLived(0),
320  fDefaultStorage(NULL),
321  fDrainStorage(NULL),
322  fCondParam(0),
323  fRefParam(0),
324  fRun(-1),
325  fCache(kTRUE),
326  fPromptCache(kFALSE),
327  fLock(kFALSE),
328  fSnapshotMode(kFALSE),
329  fSnapshotFile(0),
330  fOCDBUploadMode(kFALSE),
331  fRaw(kFALSE),
332  fCvmfsOcdb(""),
333  fStartRunLHCPeriod(-1),
334  fEndRunLHCPeriod(-1),
335  fLHCPeriod(""),
336  fMaxDate(0),
337  fKey(0)
338 {
339  // default constuctor
340  fFactories.SetOwner(1);
341  fActiveStorages.SetOwner(1);
342  fSpecificStorages.SetOwner(1);
343  fEntryCache.SetName("CDBEntryCache");
344  fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
345  fPromptEntryCache.SetOwnerKeyValue(kTRUE,kTRUE);
346 
347  fStorageMap = new TMap();
348  fStorageMap->SetOwner(1);
349  fIds = new TList();
350  fIds->SetOwner(1);
351 }
352 
353 //_____________________________________________________________________________
355 // destructor
356  ClearCache();
359  fFactories.Delete();
360  fDrainStorage = 0x0;
361  fDefaultStorage = 0x0;
362  delete fStorageMap; fStorageMap = 0;
363  delete fIds; fIds = 0;
364  delete fCondParam;
365  delete fRefParam;
366  delete fShortLived; fShortLived = 0x0;
367  //fSnapshotCache = 0;
368  //fSnapshotIdsList = 0;
369  if(fSnapshotMode){
370  fSnapshotFile->Close();
371  fSnapshotFile = 0;
372  }
373 }
374 
375 //_____________________________________________________________________________
377 // put a storage object into the list of active storages
378 
379  fActiveStorages.Add(param, storage);
380  AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
381 }
382 
383 //_____________________________________________________________________________
385 // add a storage factory to the list of registerd factories
386 
387  if (!fFactories.Contains(factory)) {
388  fFactories.Add(factory);
389  }
390 }
391 
392 //_____________________________________________________________________________
393 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
394 // check if dbString is a URI valid for one of the registered factories
395 
396  TIter iter(&fFactories);
397 
398  AliCDBStorageFactory* factory=0;
399  while ((factory = (AliCDBStorageFactory*) iter.Next())) {
400 
401  if (factory->Validate(dbString)) {
402  return kTRUE;
403  }
404  }
405 
406  return kFALSE;
407 }
408 
409 //_____________________________________________________________________________
410 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
411 // create AliCDBParam object from URI string
412 
413  TString uriString(dbString);
414 
415  if ( !fCvmfsOcdb.IsNull() && uriString.BeginsWith("alien://")) {
416  AlienToCvmfsUri(uriString);
417  }
418 
419  TIter iter(&fFactories);
420 
421  AliCDBStorageFactory* factory=0;
422  while ((factory = (AliCDBStorageFactory*) iter.Next())) {
423  AliCDBParam* param = factory->CreateParameter(uriString);
424  if(param) return param;
425  }
426 
427  return NULL;
428 }
429 
430 //_____________________________________________________________________________
431 void AliCDBManager::AlienToCvmfsUri(TString& uriString) const {
432 // convert alien storage uri to local:///cvmfs storage uri (called when OCDB_PATH is set)
433 
434  TObjArray *arr = uriString.Tokenize('?');
435  TIter iter(arr);
436  TObjString *str = 0;
437  TString entryKey = "";
438  TString entryValue = "";
439  TString newUriString = "";
440  while((str = (TObjString*) iter.Next())){
441  TString entry(str->String());
442  Int_t indeq = entry.Index('=');
443  entryKey = entry(0, indeq+1);
444  entryValue = entry(indeq+1, entry.Length()-indeq);
445 
446  if ( entryKey.Contains("folder", TString::kIgnoreCase) )
447  {
448  TRegexp re_RawFolder("^/alice/data/20[0-9]+/OCDB");
449  TRegexp re_MCFolder("^/alice/simulation/2008/v4-15-Release");
450  TString rawFolder = entryValue(re_RawFolder);
451  TString mcFolder = entryValue(re_MCFolder);
452  TString cvmfsUri(fCvmfsOcdb);
453  gSystem->ExpandPathName(cvmfsUri);
454  cvmfsUri = cvmfsUri.Strip(TString::kTrailing, '/');
455  cvmfsUri.Append("/calibration");
456  if ( !rawFolder.IsNull() ){
457  entryValue.Replace(0, 6, cvmfsUri);
458  //entryValue.Replace(entryValue.Length()-4, entryValue.Length(), "");
459  } else if ( !mcFolder.IsNull() ){
460  cvmfsUri.Append("/MC");
461  entryValue.Replace(0, 36, cvmfsUri);
462  } else {
463  AliFatal(Form("Environment variable for cvmfs OCDB folder set for an invalid OCDB storage:\n %s", entryValue.Data()));
464  }
465  } else {
466  newUriString += entryKey;
467  }
468  newUriString += entryValue;
469  newUriString += '?';
470  }
471  newUriString.Prepend("local://");
472  newUriString.Remove(TString::kTrailing, '?');
473  uriString = newUriString;
474 }
475 
476 //_____________________________________________________________________________
477 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
478 // Get the CDB storage corresponding to the URI string passed as argument
479 // If "raw://" is passed, get the storage for the raw OCDB for the current run (fRun)
480 
481  TString uriString(dbString);
482  if (uriString.EqualTo("raw://")) {
483  if (!fLHCPeriod.IsNull() && !fLHCPeriod.IsWhitespace()) {
484  return GetDefaultStorage();
485  } else {
486  TString lhcPeriod("");
487  Int_t startRun = -1, endRun = -1;
488  TString cvmfsOcdb(gSystem->Getenv("OCDB_PATH"));
489  if (! cvmfsOcdb.IsNull()){
490  GetLHCPeriodAgainstCvmfsFile(fRun, lhcPeriod, startRun, endRun);
491  } else {
492  GetLHCPeriodAgainstAlienFile(fRun, lhcPeriod, startRun, endRun);
493  }
494  return GetStorage(lhcPeriod.Data());
495  }
496  }
497 
498  AliCDBParam* param = CreateParameter(dbString);
499  if (!param) {
500  AliError(Form("Failed to activate requested storage! Check URI: %s", dbString));
501  return NULL;
502  }
503 
504  AliCDBStorage* aStorage = GetStorage(param);
505 
506  delete param;
507  return aStorage;
508 }
509 
510 //_____________________________________________________________________________
512 // get storage object from AliCDBParam object
513 
514  // if the list of active storages already contains
515  // the requested storage, return it
516  AliCDBStorage* aStorage = GetActiveStorage(param);
517  if (aStorage) {
518  return aStorage;
519  }
520 
521  // if lock is ON, cannot activate more storages!
522  if(fLock) {
523  if (fDefaultStorage) {
524  AliFatal("Lock is ON, and default storage is already set: "
525  "cannot reset it or activate more storages!");
526  }
527  }
528 
529  // loop on the list of registered factories
530  TIter iter(&fFactories);
531  AliCDBStorageFactory* factory=0;
532  while ((factory = (AliCDBStorageFactory*) iter.Next())) {
533 
534  // each factory tries to create its storage from the parameter
535  aStorage = factory->Create(param);
536  if (aStorage) {
537  PutActiveStorage(param->CloneParam(), aStorage);
538  aStorage->SetURI(param->GetURI());
539  if(fRun >= 0) {
540  if( aStorage->GetType() == "alien" || aStorage->GetType() == "local" )
541  aStorage->SetMaxDate(fMaxDate);
542  aStorage->QueryCDB(fRun);
543  }
544  return aStorage;
545  }
546  }
547 
548  AliError(Form("Failed to activate requested storage! Check URI: %s", param->GetURI().Data()));
549 
550  return NULL;
551 }
552 
553 //_____________________________________________________________________________
555 // get a storage object from the list of active storages
556 
557  return dynamic_cast<AliCDBStorage*> (fActiveStorages.GetValue(param));
558 }
559 
560 //_____________________________________________________________________________
562 // return list of active storages
563 // user has responsibility to delete returned object
564 
565  TList* result = new TList();
566 
567  TIter iter(fActiveStorages.GetTable());
568  TPair* aPair=0;
569  while ((aPair = (TPair*) iter.Next())) {
570  result->Add(aPair->Value());
571  }
572 
573  return result;
574 }
575 
576 //_____________________________________________________________________________
577 void AliCDBManager::SetDrain(const char* dbString) {
578 // set drain storage from URI string
579 
580  fDrainStorage = GetStorage(dbString);
581 }
582 
583 //_____________________________________________________________________________
585 // set drain storage from AliCDBParam
586 
587  fDrainStorage = GetStorage(param);
588 }
589 
590 //_____________________________________________________________________________
592 // set drain storage from another active storage
593 
594  fDrainStorage = storage;
595 }
596 
597 //_____________________________________________________________________________
599 // drain retrieved object to drain storage
600 
601  AliDebug(2, "Draining into drain storage...");
602  return fDrainStorage->Put(entry);
603 }
604 
605 //____________________________________________________________________________
607 // Set the framework in official upload mode. This tells the framework to upload
608 // objects to cvmfs after they have been uploaded to AliEn OCDBs.
609 // It return false if the executable to upload to cvmfs is not found.
610 
611  TString cvmfsUploadExecutable("$HOME/bin/ocdb-cvmfs");
612  gSystem->ExpandPathName(cvmfsUploadExecutable);
613  if ( gSystem->AccessPathName(cvmfsUploadExecutable) )
614  return kFALSE;
615  fOCDBUploadMode = kTRUE;
616  return kTRUE;
617 }
618 
619 //____________________________________________________________________________
620 void AliCDBManager::SetDefaultStorage(const char* storageUri) {
621 // sets default storage from URI string
622 
623  // If in the CVMFS case (triggered by environment variable) check for path
624  // validity and modify URI dynamically if it is "raw://"
625  TString cvmfsOcdb(gSystem->Getenv("OCDB_PATH"));
626  if (! cvmfsOcdb.IsNull()){
627  fCvmfsOcdb = cvmfsOcdb;
629  }
630 
631  // checking whether we are in the raw case
632  TString uriTemp(storageUri);
633  if (uriTemp == "raw://") {
634  fRaw = kTRUE; // read then by SetRun to check if the method has to be called again with expanded uri
635  AliInfo("Setting the run-number will set the corresponding OCDB for raw data reconstruction.");
636  return;
637  }
638 
639  AliCDBStorage* bckStorage = fDefaultStorage;
640 
641  fDefaultStorage = GetStorage(storageUri);
642 
643  if(!fDefaultStorage) return;
644 
645  if(bckStorage && (fDefaultStorage != bckStorage)){
646  AliWarning("Existing default storage replaced: clearing cache!");
647  ClearCache();
648  }
649 
650  if (fStorageMap->Contains("default")) {
651  delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
652  }
653  fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
654 }
655 
656 //_____________________________________________________________________________
658 // set default storage from AliCDBParam object
659 
660  AliCDBStorage* bckStorage = fDefaultStorage;
661 
662  fDefaultStorage = GetStorage(param);
663 
664  if(!fDefaultStorage) return;
665 
666  if(bckStorage && (fDefaultStorage != bckStorage)){
667  AliWarning("Existing default storage replaced: clearing cache!");
668  ClearCache();
669  }
670 
671  if (fStorageMap->Contains("default")) {
672  delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
673  }
674  fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
675 }
676 
677 //_____________________________________________________________________________
679 // set default storage from another active storage
680 
681  // if lock is ON, cannot activate more storages!
682  if(fLock) {
683  if (fDefaultStorage) {
684  AliFatal("Lock is ON, and default storage is already set: "
685  "cannot reset it or activate more storages!");
686  }
687  }
688 
689  if (!storage) {
691  return;
692  }
693 
694  AliCDBStorage* bckStorage = fDefaultStorage;
695 
696  fDefaultStorage = storage;
697 
698  if(bckStorage && (fDefaultStorage != bckStorage)){
699  AliWarning("Existing default storage replaced: clearing cache!");
700  ClearCache();
701  }
702 
703  if (fStorageMap->Contains("default")) {
704  delete fStorageMap->Remove(((TPair*)fStorageMap->FindObject("default"))->Key());
705  }
706  fStorageMap->Add(new TObjString("default"), new TObjString(fDefaultStorage->GetURI()));
707 }
708 
709 //_____________________________________________________________________________
710 void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) {
711 // sets default storage for MC data
712 // mcString MUST be "MC",
713 // simType can be "Ideal","Residual","Full"
714 
715  TString strmcString(mcString);
716  TString strsimType(simType);
717  TString dbString;
718  if (strmcString != "MC"){
719  AliFatal("Method requires first string to be MC!");
720  }
721  else {
722  if (strsimType == "Ideal"){
723  dbString = fgkMCIdealStorage;
724  }
725  else if (strsimType == "Full"){
726  dbString = fgkMCFullStorage;
727  }
728  else if (strsimType == "Residual"){
729  dbString = fgkMCResidualStorage;
730  }
731  else {
732  AliFatal("Error in setting the storage for MC data, second argument MUST be either \"Ideal\" or \"Full\" or \"Residual\".");
733  }
734 
735  SetDefaultStorage(dbString.Data());
738  if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",dbString.Data()));
739  }
740 }
741 
742 //_____________________________________________________________________________
744  TString cvmfsPath(fCvmfsOcdb);
745  gSystem->ExpandPathName(cvmfsPath);
746  if (gSystem->AccessPathName(cvmfsPath))
747  AliFatal(Form("OCDB_PATH set to an invalid path: %s", cvmfsPath.Data()));
748 
749  AliDebug(3, "OCDB_PATH envvar is set. Changing OCDB storage from alien:// to local:///cvmfs type.");
750  cvmfsPath = cvmfsPath.Strip(TString::kTrailing, '/');
751  cvmfsPath.Append("/calibration/data/OCDBFoldervsRunRange.xml");
752  if (gSystem->AccessPathName(cvmfsPath))
753  AliFatal(Form("Cannot find valid file OCDBFoldervsRunRange.xml in: %s", cvmfsPath.Data()));
754 }
755 
756 //_____________________________________________________________________________
758 // set default storage from the run number - to be used only with raw data
759 
760  // if lock is ON, cannot activate more storages!
761  if(fLock) {
762  if (fDefaultStorage) {
763  AliFatal("Lock is ON, and default storage is already set: "
764  "cannot activate default storage from run number");
765  }
766  }
767 
768  TString lhcPeriod("");
769  Int_t startRun = 0, endRun = 0;
770  if (! fCvmfsOcdb.IsNull()) { // fRaw and cvmfs case: set LHC period from cvmfs file
771  GetLHCPeriodAgainstCvmfsFile(run, lhcPeriod, startRun, endRun);
772  } else { // fRaw: set LHC period from AliEn XML file
773  GetLHCPeriodAgainstAlienFile(run, lhcPeriod, startRun, endRun);
774  }
775 
776  fLHCPeriod = lhcPeriod;
777  fStartRunLHCPeriod = startRun;
778  fEndRunLHCPeriod = endRun;
779 
781  if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data()));
782 
783 }
784 
785 //_____________________________________________________________________________
786 void AliCDBManager::GetLHCPeriodAgainstAlienFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
787 // set LHC period (year + first, last run) comparing run number and AliEn XML file
788 
789 // retrieve XML file from alien
790  if(!gGrid) {
791  TGrid::Connect("alien://","");
792  if(!gGrid) {
793  AliError("Connection to alien failed!");
794  return;
795  }
796  }
797  TUUID uuid;
798  TString rndname = gSystem->TempDirectory(); // "/tmp/";
799  rndname += "/";
800  rndname += "OCDBFolderXML.";
801  rndname += uuid.AsString();
802  rndname += ".xml";
803  AliDebug(2, Form("file to be copied = %s", fgkOCDBFolderXMLfile.Data()));
804  if (!TFile::Cp(fgkOCDBFolderXMLfile.Data(), rndname.Data())) {
805  AliFatal(Form("Cannot make a local copy of OCDBFolder xml file in %s",rndname.Data()));
806  }
807  AliCDBHandler* saxcdb = new AliCDBHandler();
808  saxcdb->SetRun(run);
809  TSAXParser *saxParser = new TSAXParser();
810  saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
811  saxParser->ParseFile(rndname.Data());
812  AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
813  AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
814  AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
815  lhcPeriod = saxcdb->GetOCDBFolder();
816  startRun = saxcdb->GetStartRunRange();
817  endRun = saxcdb->GetEndRunRange();
818 }
819 
820 //_____________________________________________________________________________
821 void AliCDBManager::GetLHCPeriodAgainstCvmfsFile(Int_t run, TString& lhcPeriod, Int_t& startRun, Int_t& endRun) {
822 // set LHC period (year + first, last run) comparing run number and CVMFS file
823 
824  AliCDBHandler* saxcdb = new AliCDBHandler();
825  saxcdb->SetRun(run);
826  TSAXParser *saxParser = new TSAXParser();
827  saxParser->ConnectToHandler("AliCDBHandler", saxcdb);
828  TString cvmfsUri(fCvmfsOcdb);
829  gSystem->ExpandPathName(cvmfsUri);
830  cvmfsUri = cvmfsUri.Strip(TString::kTrailing, '/');
831  cvmfsUri.Append("/calibration/data/OCDBFoldervsRunRange.xml");
832  saxParser->ParseFile(cvmfsUri);
833  AliInfo(Form(" LHC folder = %s", saxcdb->GetOCDBFolder().Data()));
834  AliInfo(Form(" LHC period start run = %d", saxcdb->GetStartRunRange()));
835  AliInfo(Form(" LHC period end run = %d", saxcdb->GetEndRunRange()));
836  lhcPeriod = saxcdb->GetOCDBFolder();
837  startRun = saxcdb->GetStartRunRange();
838  endRun = saxcdb->GetEndRunRange();
839 }
840 
841 //_____________________________________________________________________________
843 // Unset default storage
844 
845  // if lock is ON, action is forbidden!
846  if(fLock) {
847  if (fDefaultStorage) {
848  AliFatal("Lock is ON: cannot unset default storage!");
849  }
850  }
851 
852  if (fDefaultStorage) {
853  AliWarning("Clearing cache!");
854  ClearCache();
855  }
856 
858  fRaw = kFALSE;
859 
860  fDefaultStorage = 0x0;
861 }
862 
863 //_____________________________________________________________________________
864 void AliCDBManager::SetSpecificStorage(const char* calibType, const char* dbString, Int_t version, Int_t subVersion) {
865 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
866 
867  AliCDBParam *aPar = CreateParameter(dbString);
868  if(!aPar) return;
869  SetSpecificStorage(calibType, aPar, version, subVersion);
870  delete aPar;
871 }
872 
873 //_____________________________________________________________________________
874 void AliCDBManager::SetSpecificStorage(const char* calibType, const AliCDBParam* param, Int_t version, Int_t subVersion) {
875 // sets storage specific for detector or calibration type (works with AliCDBManager::Get(...))
876 // Default storage should be defined prior to any specific storages, e.g.:
877 // AliCDBManager::instance()->SetDefaultStorage("alien://");
878 // AliCDBManager::instance()->SetSpecificStorage("TPC/*","local://DB_TPC");
879 // AliCDBManager::instance()->SetSpecificStorage("*/Align/*","local://DB_TPCAlign");
880 // calibType must be a valid CDB path! (3 level folder structure)
881 // Specific version/subversion is set in the uniqueid of the AliCDBParam value stored in the
882 // specific storages map
883 
884  if(!fDefaultStorage && !fRaw) {
885  AliError("Please activate a default storage first!");
886  return;
887  }
888 
889  AliCDBPath aPath(calibType);
890  if(!aPath.IsValid()){
891  AliError(Form("Not a valid path: %s", calibType));
892  return;
893  }
894 
895  TObjString *objCalibType = new TObjString(aPath.GetPath());
896  if(fSpecificStorages.Contains(objCalibType)){
897  AliWarning(Form("Storage \"%s\" already activated! It will be replaced by the new one",
898  calibType));
899  AliCDBParam *checkPar = dynamic_cast<AliCDBParam*> (fSpecificStorages.GetValue(calibType));
900  if(checkPar) delete checkPar;
901  delete fSpecificStorages.Remove(objCalibType);
902  }
903  AliCDBStorage *aStorage = GetStorage(param);
904  if(!aStorage) return;
905 
906  // Set the unique id of the AliCDBParam stored in the map to store specific version/subversion
907  UInt_t uId = ((subVersion+1)<<16) + (version+1);
908  AliCDBParam *specificParam = param->CloneParam();
909  specificParam->SetUniqueID(uId);
910  fSpecificStorages.Add(objCalibType, specificParam);
911 
912  if(fStorageMap->Contains(objCalibType)){
913  delete fStorageMap->Remove(objCalibType);
914  }
915  fStorageMap->Add(objCalibType->Clone(), new TObjString(param->GetURI()));
916 
917 }
918 
919 //_____________________________________________________________________________
921 // get storage specific for detector or calibration type
922 
923  AliCDBPath calibPath(calibType);
924  if(!calibPath.IsValid()) return NULL;
925 
926  AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(calibPath.GetPath());
927  if(!checkPar){
928  AliError(Form("%s storage not found!", calibType));
929  return NULL;
930  } else {
931  return GetStorage(checkPar);
932  }
933 
934 }
935 
936 //_____________________________________________________________________________
938 // select storage valid for path from the list of specific storages
939 
940  AliCDBPath aPath(path);
941  if(!aPath.IsValid()) return NULL;
942 
943  TIter iter(&fSpecificStorages);
944  TObjString *aCalibType=0;
945  AliCDBPath tmpPath("null/null/null");
946  AliCDBParam* aPar=0;
947  while((aCalibType = (TObjString*) iter.Next())){
948  AliCDBPath calibTypePath(aCalibType->GetName());
949  if(calibTypePath.Comprises(aPath)) {
950  if(calibTypePath.Comprises(tmpPath)) continue;
951  aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
952  tmpPath.SetPath(calibTypePath.GetPath());
953  }
954  }
955  return aPar;
956 }
957 
958 //_____________________________________________________________________________
960  Int_t version, Int_t subVersion) {
961  // get an AliCDBEntry object from the database
962 
963  if(runNumber < 0){
964  // RunNumber is not specified. Try with fRun
965  if (fRun < 0){
966  AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
967  return NULL;
968  }
969  runNumber = fRun;
970  }
971 
972  return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
973 }
974 
975 //_____________________________________________________________________________
977  const AliCDBRunRange& runRange, Int_t version,
978  Int_t subVersion) {
979  // get an AliCDBEntry object from the database!
980 
981  return Get(AliCDBId(path, runRange, version, subVersion));
982 }
983 
984 //_____________________________________________________________________________
985 AliCDBEntry* AliCDBManager::Get(const AliCDBId& queryId, Bool_t forceCaching) {
986 // get an AliCDBEntry object from the database
987 
988  // check if queryId's path and runRange are valid
989  // queryId is invalid also if version is not specified and subversion is!
990  if (!queryId.IsValid()) {
991  AliError(Form("Invalid query: %s", queryId.ToString().Data()));
992  return NULL;
993  }
994 
995  // queryId is not specified if path contains wildcard or run range= [-1,-1]
996  if (!queryId.IsSpecified()) {
997  AliError(Form("Unspecified query: %s",
998  queryId.ToString().Data()));
999  return NULL;
1000  }
1001 
1002  if(fLock && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
1003  AliFatal("Lock is ON: cannot use different run number than the internal one!");
1004 
1005  if(fCache && !(fRun >= queryId.GetFirstRun() && fRun <= queryId.GetLastRun()))
1006  AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1007 
1008  AliCDBEntry *entry=0;
1009 
1010  //first of all, look in the prompt cache, for online overrides
1011  if(fPromptCache && queryId.GetFirstRun() == fRun)
1012  entry = (AliCDBEntry*) fPromptEntryCache.GetValue(queryId.GetPath());
1013  if(entry) {
1014  AliDebug(2, Form("Object %s retrieved from PROMPT cache !!",queryId.GetPath().Data()));
1015  return entry;
1016  }
1017 
1018  // first look into map of cached objects
1019  if(fCache && queryId.GetFirstRun() == fRun)
1020  entry = (AliCDBEntry*) fEntryCache.GetValue(queryId.GetPath());
1021  if(entry) {
1022  AliDebug(2, Form("Object %s retrieved from cache !!",queryId.GetPath().Data()));
1023  return entry;
1024  }
1025 
1026  // if snapshot flag is set, try getting from the snapshot
1027  // but in the case a specific storage is specified for this path
1028  AliCDBParam *aPar=SelectSpecificStorage(queryId.GetPath());
1029  if(!aPar){
1030  if(fSnapshotMode && queryId.GetFirstRun() == fRun)
1031  {
1032  entry = GetEntryFromSnapshot(queryId.GetPath());
1033  if(entry) {
1034  AliInfo(Form("Object \"%s\" retrieved from the snapshot.",queryId.GetPath().Data()));
1035  if(queryId.GetFirstRun() == fRun) // no need to check fCache, fSnapshotMode not possible otherwise
1036  CacheEntry(queryId.GetPath(), entry);
1037 
1038  if(!fIds->Contains(&entry->GetId()))
1039  fIds->Add(entry->GetId().Clone());
1040 
1041  return entry;
1042  }
1043  }
1044  }
1045 
1046  // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either)
1047  // => retrieve it from the storage and cache it!!
1048  if(!fDefaultStorage) {
1049  AliError("No storage set!");
1050  return NULL;
1051  }
1052 
1053  Int_t version = -1, subVersion = -1;
1054  AliCDBStorage *aStorage=0;
1055  if(aPar) {
1056  aStorage=GetStorage(aPar);
1057  TString str = aPar->GetURI();
1058  UInt_t uId = aPar->GetUniqueID();
1059  version = Int_t(uId&0xffff) - 1;
1060  subVersion = Int_t(uId>>16) - 1;
1061  AliDebug(2,Form("Looking into storage: %s",str.Data()));
1062  } else {
1063  aStorage=GetDefaultStorage();
1064  AliDebug(2,"Looking into default storage");
1065  }
1066 
1067  AliCDBId finalQueryId(queryId);
1068  if(version >= 0) {
1069  AliDebug(2,Form("Specific version set to: %d", version));
1070  finalQueryId.SetVersion(version);
1071  }
1072  if(subVersion >= 0) {
1073  AliDebug(2,Form("Specific subversion set to: %d", subVersion));
1074  finalQueryId.SetSubVersion(subVersion);
1075  }
1076  entry = aStorage->Get(finalQueryId);
1077 
1078  if(entry && fCache && (queryId.GetFirstRun()==fRun || forceCaching)){
1079  CacheEntry(queryId.GetPath(), entry);
1080  }
1081 
1082  if(entry && !fIds->Contains(&entry->GetId())){
1083  fIds->Add(entry->GetId().Clone());
1084  }
1085 
1086  return entry;
1087 }
1088 
1089 //_____________________________________________________________________________
1091 {
1092  // get the map used for snapshot
1093  if (!fSnapshotFile) return 0;
1094  return dynamic_cast<TMap*>(fSnapshotFile->Get("CDBSnapshotEntriesMap"));
1095  //
1096 }
1097 
1098 //_____________________________________________________________________________
1100 {
1101  // get the list retrieved for snapshot
1102  if (!fSnapshotFile) return 0;
1103  return dynamic_cast<TList*>(fSnapshotFile->Get("CDBSnapshotIdsList"));
1104  //
1105 }
1106 
1107 //_____________________________________________________________________________
1109 // get the entry from the open snapshot file
1110 
1111  TString sPath(path);
1112  sPath.ReplaceAll("/","*");
1113  if(!fSnapshotFile){
1114  AliError("No snapshot file is open!");
1115  return 0;
1116  }
1117  AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(fSnapshotFile->Get(sPath.Data()));
1118  if(!entry){
1119  AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path));
1120  return 0;
1121  }
1122 
1123  return entry;
1124 }
1125 
1126 //_____________________________________________________________________________
1127 Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) {
1128 // set the manager in snapshot mode
1129 
1130  if(!fCache){
1131  AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!");
1132  return kFALSE;
1133  }
1134 
1135  //open snapshot file
1136  TString snapshotFile(snapshotFileName);
1137  if(snapshotFile.BeginsWith("alien://")){
1138  if(!gGrid) {
1139  TGrid::Connect("alien://","");
1140  if(!gGrid) {
1141  AliError("Connection to alien failed!");
1142  return kFALSE;
1143  }
1144  }
1145  }
1146 
1147  fSnapshotFile = TFile::Open(snapshotFileName);
1148  if (!fSnapshotFile || fSnapshotFile->IsZombie()){
1149  AliError(Form("Cannot open file %s",snapshotFileName));
1150  return kFALSE;
1151  }
1152 
1153  AliInfo("The CDB manager is set in snapshot mode!");
1154  fSnapshotMode = kTRUE;
1155  return kTRUE;
1156 
1157 }
1158 
1159 //_____________________________________________________________________________
1160 const char* AliCDBManager::GetURI(const char* path) {
1161 // return the URI of the storage where to look for path
1162 
1163  if(!IsDefaultStorageSet()) return 0;
1164 
1165  AliCDBParam *aPar=SelectSpecificStorage(path);
1166 
1167  if(aPar) {
1168  return aPar->GetURI().Data();
1169 
1170  } else {
1171  return GetDefaultStorage()->GetURI().Data();
1172  }
1173 
1174  return 0;
1175 }
1176 
1177 //_____________________________________________________________________________
1179 // get the first run of validity
1180 // for the current period
1181 // if set
1182  if(fStartRunLHCPeriod==-1)
1183  AliWarning("Run-range not yet set for the current LHC period.");
1184  return fStartRunLHCPeriod;
1185 }
1186 
1187 //_____________________________________________________________________________
1189 // get the last run of validity
1190 // for the current period
1191 // if set
1192  if(fEndRunLHCPeriod==-1)
1193  AliWarning("Run-range not yet set for the current LHC period.");
1194  return fEndRunLHCPeriod;
1195 }
1196 
1197 //_____________________________________________________________________________
1199 // get the current LHC period string
1200 //
1201  if(fLHCPeriod.IsWhitespace() || fLHCPeriod.IsNull())
1202  AliWarning("LHC period (OCDB folder) not yet set");
1203  return fLHCPeriod;
1204 }
1205 
1206 //_____________________________________________________________________________
1208  Int_t version, Int_t subVersion) {
1209  // get the AliCDBId of the valid object from the database (does not retrieve the object)
1210  // User must delete returned object!
1211 
1212  if(runNumber < 0){
1213  // RunNumber is not specified. Try with fRun
1214  if (fRun < 0){
1215  AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1216  return NULL;
1217  }
1218  runNumber = fRun;
1219  }
1220 
1221  return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
1222 }
1223 
1224 //_____________________________________________________________________________
1226  const AliCDBRunRange& runRange, Int_t version,
1227  Int_t subVersion) {
1228  // get the AliCDBId of the valid object from the database (does not retrieve the object)
1229  // User must delete returned object!
1230 
1231  return GetId(AliCDBId(path, runRange, version, subVersion));
1232 }
1233 
1234 //_____________________________________________________________________________
1236 // get the AliCDBId of the valid object from the database (does not retrieve the object)
1237 // User must delete returned object!
1238 
1239  if(!fDefaultStorage) {
1240  AliError("No storage set!");
1241  return NULL;
1242  }
1243 
1244  // check if query's path and runRange are valid
1245  // query is invalid also if version is not specified and subversion is!
1246  if (!query.IsValid()) {
1247  AliError(Form("Invalid query: %s", query.ToString().Data()));
1248  return NULL;
1249  }
1250 
1251  // query is not specified if path contains wildcard or run range= [-1,-1]
1252  if (!query.IsSpecified()) {
1253  AliError(Form("Unspecified query: %s",
1254  query.ToString().Data()));
1255  return NULL;
1256  }
1257 
1258  if(fCache && query.GetFirstRun() != fRun)
1259  AliWarning("Run number explicitly set in query: CDB cache temporarily disabled!");
1260 
1261  AliCDBEntry* entry = 0;
1262 
1263  // first look into map of cached objects
1264  if(fCache && query.GetFirstRun() == fRun)
1265  entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath());
1266 
1267  if(entry) {
1268  AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data()));
1269  return dynamic_cast<AliCDBId*> (entry->GetId().Clone());
1270  }
1271 
1272  // Entry is not in cache -> retrieve it from CDB and cache it!!
1273  AliCDBStorage *aStorage=0;
1274  AliCDBParam *aPar=SelectSpecificStorage(query.GetPath());
1275 
1276  if(aPar) {
1277  aStorage=GetStorage(aPar);
1278  TString str = aPar->GetURI();
1279  AliDebug(2,Form("Looking into storage: %s",str.Data()));
1280 
1281  } else {
1282  aStorage=GetDefaultStorage();
1283  AliDebug(2,"Looking into default storage");
1284  }
1285 
1286  return aStorage->GetId(query);
1287 
1288 }
1289 
1290 //_____________________________________________________________________________
1292  Int_t version, Int_t subVersion) {
1293  // get multiple AliCDBEntry objects from the database
1294 
1295  if(runNumber < 0){
1296  // RunNumber is not specified. Try with fRun
1297  if (fRun < 0){
1298  AliError("Run number neither specified in query nor set in AliCDBManager! Use AliCDBManager::SetRun.");
1299  return NULL;
1300  }
1301  runNumber = fRun;
1302  }
1303 
1304  return GetAll(AliCDBId(path, runNumber, runNumber, version,
1305  subVersion));
1306 }
1307 
1308 //_____________________________________________________________________________
1310  const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
1311  // get multiple AliCDBEntry objects from the database
1312 
1313  return GetAll(AliCDBId(path, runRange, version, subVersion));
1314 }
1315 
1316 //_____________________________________________________________________________
1317 TList* AliCDBManager::GetAll(const AliCDBId& query) {
1318 // get multiple AliCDBEntry objects from the database
1319 // Warning: this method works correctly only for queries of the type "Detector/*"
1320 // and not for more specific queries e.g. "Detector/Calib/*" !
1321 // Warning #2: Entries are cached, but GetAll will keep on retrieving objects from OCDB!
1322 // To get an object from cache use Get() function
1323 
1324  if(!fDefaultStorage) {
1325  AliError("No storage set!");
1326  return NULL;
1327  }
1328 
1329  if (!query.IsValid()) {
1330  AliError(Form("Invalid query: %s", query.ToString().Data()));
1331  return NULL;
1332  }
1333 
1334  if((fSpecificStorages.GetEntries()>0) && query.GetPath().BeginsWith('*')){
1335  // if specific storages are active a query with "*" is ambiguous
1336  AliError("Query too generic in this context!");
1337  return NULL;
1338  }
1339 
1340  if (query.IsAnyRange()) {
1341  AliError(Form("Unspecified run or runrange: %s",
1342  query.ToString().Data()));
1343  return NULL;
1344  }
1345 
1346  if(fLock && query.GetFirstRun() != fRun)
1347  AliFatal("Lock is ON: cannot use different run number than the internal one!");
1348 
1349  AliCDBParam *aPar = SelectSpecificStorage(query.GetPath());
1350 
1351  AliCDBStorage *aStorage;
1352  if(aPar) {
1353  aStorage=GetStorage(aPar);
1354  AliDebug(2,Form("Looking into storage: %s", aPar->GetURI().Data()));
1355 
1356  } else {
1357  aStorage=GetDefaultStorage();
1358  AliDebug(2,Form("Looking into default storage: %s", aStorage->GetURI().Data()));
1359  }
1360 
1361  TList *result = 0;
1362  if(aStorage) result = aStorage->GetAll(query);
1363  if(!result) return 0;
1364 
1365  // loop on result to check whether entries should be re-queried with specific storages
1366  if(fSpecificStorages.GetEntries()>0 && ! (fSpecificStorages.GetEntries() == 1 && aPar)) {
1367  AliInfo("Now look into all other specific storages...");
1368 
1369  TIter iter(result);
1370  AliCDBEntry* chkEntry=0;
1371 
1372  while((chkEntry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1373  AliCDBId& chkId = chkEntry->GetId();
1374  AliDebug(2, Form("Checking id %s ", chkId.GetPath().Data()));
1375  AliCDBParam *chkPar=SelectSpecificStorage(chkId.GetPath());
1376  if (!chkPar || aPar == chkPar) continue;
1377  AliCDBStorage *chkStorage = GetStorage(chkPar);
1378  AliDebug(2, Form("Found specific storage! %s", chkPar->GetURI().Data()));
1379 
1380  chkId.SetRunRange(query.GetFirstRun(), query.GetLastRun());
1381  UInt_t uId = chkPar->GetUniqueID();
1382  Int_t version = -1, subVersion = -1;
1383  version = Int_t(uId&0xffff) - 1;
1384  subVersion = Int_t(uId>>16) - 1;
1385  if(version!=-1){
1386  chkId.SetVersion(version);
1387  }else{
1388  chkId.SetVersion(query.GetVersion());
1389  }
1390  if(subVersion!=-1){
1391  chkId.SetSubVersion(subVersion);
1392  }else{
1393  chkId.SetSubVersion(query.GetSubVersion());
1394  }
1395 
1396  AliCDBEntry *newEntry=0;
1397  if(chkStorage) newEntry = chkStorage->Get(chkId);
1398  if(!newEntry) continue;
1399 
1400  // object is found in specific storage: replace entry in the result list!
1401  chkEntry->SetOwner(1);
1402  delete result->Remove(chkEntry);
1403  result->AddFirst(newEntry);
1404  }
1405 
1406  Int_t nEntries = result->GetEntries();
1407  AliInfo("After look into other specific storages, result list is:");
1408  for(int i=0; i<nEntries;i++){
1409  AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
1410  AliInfo(Form("%s",entry->GetId().ToString().Data()));
1411  }
1412  }
1413 
1414  // caching entries
1415  TIter iter(result);
1416  AliCDBEntry* entry=0;
1417  while((entry = dynamic_cast<AliCDBEntry*> (iter.Next()))){
1418 
1419  if(!fIds->Contains(&entry->GetId())){
1420  fIds->Add(entry->GetId().Clone());
1421  }
1422  if(fCache && (query.GetFirstRun() == fRun)){
1423  CacheEntry(entry->GetId().GetPath(), entry);
1424  }
1425  }
1426 
1427 
1428  return result;
1429 }
1430 
1431 //_____________________________________________________________________________
1432 Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, DataType type){
1433 // store an AliCDBEntry object into the database
1434 
1435  if (object==0x0) {
1436  AliError("Null Entry! No storage will be done!");
1437  return kFALSE;
1438  }
1439 
1440  AliCDBEntry anEntry(object, id, metaData);
1441  return Put(&anEntry, mirrors, type);
1442 
1443 }
1444 
1445 
1446 //_____________________________________________________________________________
1447 Bool_t AliCDBManager::Put(AliCDBEntry* entry, const char* mirrors, DataType type){
1448 // store an AliCDBEntry object into the database
1449 
1450  if(type == kPrivate && !fDefaultStorage) {
1451  AliError("No storage set!");
1452  return kFALSE;
1453  }
1454 
1455  if (!entry){
1456  AliError("No entry!");
1457  return kFALSE;
1458  }
1459 
1460  if (entry->GetObject()==0x0){
1461  AliError("No valid object in CDB entry!");
1462  return kFALSE;
1463  }
1464 
1465  if (!entry->GetId().IsValid()) {
1466  AliError(Form("Invalid entry ID: %s",
1467  entry->GetId().ToString().Data()));
1468  return kFALSE;
1469  }
1470 
1471  if (!entry->GetId().IsSpecified()) {
1472  AliError(Form("Unspecified entry ID: %s",
1473  entry->GetId().ToString().Data()));
1474  return kFALSE;
1475  }
1476 
1477  AliCDBId id = entry->GetId();
1478  AliCDBParam *aPar = SelectSpecificStorage(id.GetPath());
1479 
1480  AliCDBStorage *aStorage=0;
1481 
1482  if(aPar) {
1483  aStorage=GetStorage(aPar);
1484  } else {
1485  switch(type){
1486  case kCondition:
1487  aStorage = GetStorage(fCondParam);
1488  break;
1489  case kReference:
1490  aStorage = GetStorage(fRefParam);
1491  break;
1492  case kPrivate:
1493  aStorage = GetDefaultStorage();
1494  break;
1495  }
1496  }
1497 
1498  AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data()));
1499 
1500  TString strMirrors(mirrors);
1501  Bool_t result = kFALSE;
1502  if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
1503  result = aStorage->Put(entry, mirrors, type);
1504  else
1505  result = aStorage->Put(entry, "", type);
1506 
1507  if(fRun >= 0) QueryCDB();
1508 
1509  return result;
1510 
1511 
1512 }
1513 
1514 //_____________________________________________________________________________
1515 void AliCDBManager::SetMirrorSEs(const char* mirrors) {
1516 // set mirror Storage Elements for the default storage, if it is of type "alien"
1517  if(fDefaultStorage->GetType() != "alien"){
1518  AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1519  return;
1520  }
1521  fDefaultStorage->SetMirrorSEs(mirrors);
1522 }
1523 
1524 //_____________________________________________________________________________
1525 const char* AliCDBManager::GetMirrorSEs() const {
1526 // get mirror Storage Elements for the default storage, if it is of type "alien"
1527  if(fDefaultStorage->GetType() != "alien"){
1528  AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!");
1529  return "";
1530  }
1531  return fDefaultStorage->GetMirrorSEs();
1532 }
1533 
1534 //_____________________________________________________________________________
1535 void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry) {
1536 // cache AliCDBEntry. Cache is valid until run number is changed.
1537 
1538  AliCDBEntry *chkEntry = dynamic_cast<AliCDBEntry*> (fEntryCache.GetValue(path));
1539 
1540  if(chkEntry) {
1541  AliDebug(2, Form("Object %s already in cache !!", path));
1542  return;
1543  } else {
1544  AliDebug(2,Form("Caching entry %s", path));
1545  }
1546 
1547  fEntryCache.Add(new TObjString(path), entry);
1548  AliDebug(2,Form("Cache entries: %d", fEntryCache.GetEntries()));
1549 
1550 }
1551 
1552 //_____________________________________________________________________________
1553 void AliCDBManager::Print(Option_t* /*option*/) const {
1554 // Print list of active storages and their URIs
1555 
1556  TString output=Form("Run number = %d; ",fRun);
1557  output += "Cache is ";
1558  if(!fCache) output += "NOT ";
1559  output += Form("ACTIVE; Number of active storages: %d\n",fActiveStorages.GetEntries());
1560 
1561  if(fDefaultStorage) {
1562  output += Form("\t*** Default Storage URI: \"%s\"\n",fDefaultStorage->GetURI().Data());
1563  // AliInfo(output.Data());
1564  }
1565  if(fSpecificStorages.GetEntries()>0) {
1566  TIter iter(fSpecificStorages.GetTable());
1567  TPair *aPair=0;
1568  Int_t i=1;
1569  while((aPair = (TPair*) iter.Next())){
1570  output += Form("\t*** Specific storage %d: Path \"%s\" -> URI \"%s\"\n",
1571  i++, ((TObjString*) aPair->Key())->GetName(),
1572  ((AliCDBParam*) aPair->Value())->GetURI().Data());
1573  }
1574  }
1575  if(fDrainStorage) {
1576  output += Form("*** Drain Storage URI: %s\n",fDrainStorage->GetURI().Data());
1577  }
1578  AliInfo(output.Data());
1579 }
1580 
1581 //_____________________________________________________________________________
1582 void AliCDBManager::SetRun(Int_t run) {
1583 // Sets current run number.
1584 // When the run number changes the caching is cleared.
1585 
1586  if(fRun == run)
1587  return;
1588 
1589  if(fLock && fRun >= 0) {
1590  AliFatal("Lock is ON, cannot reset run number!");
1591  }
1592 
1593  fRun = run;
1594 
1595  if (fRaw) {
1596  // here the LHCPeriod xml file is parsed; the string containing the correct period is returned; the default storage is set
1597  if (fStartRunLHCPeriod <= run && fEndRunLHCPeriod >= run){
1598  AliInfo("LHCPeriod alien folder for current run already in memory");
1599  }else{
1601  if(fEntryCache.GetEntries()!=0) ClearCache();
1602  return;
1603  }
1604  }
1605  ClearCache();
1606  QueryCDB();
1607 }
1608 
1609 //_____________________________________________________________________________
1610 void AliCDBManager::SetMaxDate(time_t maxDate) {
1611  if (maxDate == fMaxDate) return;
1612  if (fRun > -1) AliFatal("Cannot call AliCDBManager::SetMaxDate() after run was set!");
1613  fMaxDate = maxDate;
1614 }
1615 
1616 //_____________________________________________________________________________
1618 // clear AliCDBEntry prompt cache
1619 
1620  AliDebug(2, Form("PROMPT cache entries to be deleted: %d",fPromptEntryCache.GetEntries()));
1621 
1622  fPromptEntryCache.DeleteAll();
1623 
1624  AliDebug(2, Form("After deleting - PROMPT cache entries: %d",fPromptEntryCache.GetEntries()));
1625 }
1626 
1627 //_____________________________________________________________________________
1629 // clear AliCDBEntry cache
1630 
1631  AliDebug(2, Form("Cache entries to be deleted: %d",fEntryCache.GetEntries()));
1632 
1633  /*
1634  // To clean entries one by one
1635  TIter iter(fEntryCache.GetTable());
1636  TPair* pair=0;
1637  while((pair= dynamic_cast<TPair*> (iter.Next()))){
1638 
1639  TObjString* key = dynamic_cast<TObjString*> (pair->Key());
1640  AliCDBEntry* entry = dynamic_cast<AliCDBEntry*> (pair->Value());
1641  AliDebug(2, Form("Deleting entry: %s", key->GetName()));
1642  if (entry) delete entry;
1643  delete fEntryCache.Remove(key);
1644  }
1645  */
1646  fEntryCache.DeleteAll();
1647  AliDebug(2, Form("After deleting - Cache entries: %d",fEntryCache.GetEntries()));
1648 }
1649 
1650 //_____________________________________________________________________________
1652 // unload cached object
1653 // that is remove the entry from the cache and the id from the list of ids
1654 //
1655  if(!fActiveStorages.GetEntries()) {
1656  AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path));
1657  return;
1658  }
1659 
1660  AliCDBPath queryPath(path);
1661  if(!queryPath.IsValid()) return;
1662 
1663  if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it!
1664  if(fEntryCache.Contains(path)){
1665  AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path));
1666  TObjString pathStr(path);
1667  delete fEntryCache.Remove(&pathStr);
1668  // we do not remove from the list of Id's (it's not very coherent but we leave the
1669  // id for the benefit of the userinfo)
1670  /*
1671  TIter iter(fIds);
1672  AliCDBId *id = 0;
1673  while((id = dynamic_cast<AliCDBId*> (iter.Next()))){
1674  if(queryPath.Comprises(id->GetPath()))
1675  delete fIds->Remove(id);
1676  }*/
1677  } else {
1678  AliWarning(Form("Cache does not contain object \"%s\"!", path));
1679  }
1680  AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries()));
1681  return;
1682  }
1683 
1684  // path is wildcard: loop on the cache and unload all comprised objects!
1685  TIter iter(fEntryCache.GetTable());
1686  TPair* pair = 0;
1687  Int_t removed=0;
1688 
1689  while((pair = dynamic_cast<TPair*> (iter.Next()))){
1690  AliCDBPath entryPath = pair->Key()->GetName();
1691  if(queryPath.Comprises(entryPath)) {
1692  AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data()));
1693  TObjString pathStr(entryPath.GetPath());
1694  delete fEntryCache.Remove(&pathStr);
1695  removed++;
1696 
1697  // we do not remove from the list of Id's (it's not very coherent but we leave the
1698  // id for the benefit of the userinfo)
1699  /*
1700  TIter iterids(fIds);
1701  AliCDBId *anId = 0;
1702  while((anId = dynamic_cast<AliCDBId*> (iterids.Next()))){
1703  AliCDBPath aPath = anId->GetPath();
1704  TString aPathStr = aPath.GetPath();
1705  if(queryPath.Comprises(aPath)) {
1706  delete fIds->Remove(anId);
1707  }
1708  }*/
1709  }
1710  }
1711  AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries()));
1712 }
1713 
1714 //_____________________________________________________________________________
1716 // delete list of active storages
1717 
1718  fActiveStorages.DeleteAll();
1719  fSpecificStorages.DeleteAll();
1720 }
1721 
1722 //_____________________________________________________________________________
1724 // destroys active storage
1725 
1726  /*
1727  TIter iter(fActiveStorages.GetTable());
1728  TPair* aPair;
1729  while ((aPair = (TPair*) iter.Next())) {
1730  if(storage == (AliCDBStorage*) aPair->Value())
1731  delete fActiveStorages.Remove(aPair->Key());
1732  storage->Delete(); storage=0x0;
1733  }
1734  */
1735 
1736 }
1737 
1738 //_____________________________________________________________________________
1740 // query default and specific storages for files valid for fRun. Every storage loads the Ids into its list.
1741 
1742  if (fRun < 0){
1743  AliError("Run number not yet set! Use AliCDBManager::SetRun.");
1744  return;
1745  }
1746  if (!fDefaultStorage){
1747  AliError("Default storage is not set! Use AliCDBManager::SetDefaultStorage");
1748  return;
1749  }
1750  if(fDefaultStorage->GetType() == "alien" || fDefaultStorage->GetType() == "local"){
1751  if (!fDefaultStorage->GetMaxDate()) fDefaultStorage->SetMaxDate(fMaxDate); // no override if set per storage
1753  //} else {
1754  // AliDebug(2,"Skipping query for valid files, it used only in grid...");
1755  }
1756 
1757  TIter iter(&fSpecificStorages);
1758  TObjString *aCalibType=0;
1759  AliCDBParam* aPar=0;
1760  while((aCalibType = dynamic_cast<TObjString*> (iter.Next()))){
1761  aPar = (AliCDBParam*) fSpecificStorages.GetValue(aCalibType);
1762  if(aPar) {
1763  AliDebug(2,Form("Querying specific storage %s",aCalibType->GetName()));
1764  AliCDBStorage *aStorage = GetStorage(aPar);
1765  if(aStorage->GetType() == "alien" || aStorage->GetType() == "local"){
1766  if (!aStorage->GetMaxDate()) aStorage->SetMaxDate(fMaxDate); // no override if set per storage
1767  aStorage->QueryCDB(fRun, aCalibType->GetName());
1768  } else {
1769  AliDebug(2,
1770  "Skipping query for valid files, it is used only in grid...");
1771  }
1772  }
1773  }
1774 }
1775 
1776 //______________________________________________________________________________________________
1778 // returns the name (string) of the data type
1779 
1780  switch (type){
1781  case kCondition: return "Conditions";
1782  case kReference: return "Reference";
1783  case kPrivate: return "Private";
1784  }
1785  return 0;
1786 
1787 }
1788 
1789 //______________________________________________________________________________________________
1790 Bool_t AliCDBManager::DiffObjects(const char *cdbFile1, const char *cdbFile2) const {
1791 // Compare byte-by-byte the objects contained in the CDB entry in two different files,
1792 // whose name is passed as input
1793 // Return value:
1794 // kTRUE - in case the content of the OCDB object (persistent part) is exactly the same
1795 // kFALSE - otherwise
1796 
1797  TString f1Str(cdbFile1);
1798  TString f2Str(cdbFile2);
1799  if (!gGrid && ( f1Str.BeginsWith("alien://") || f2Str.BeginsWith("alien://") ))
1800  TGrid::Connect("alien://");
1801 
1802  TFile * f1 = TFile::Open(cdbFile1);
1803  if (!f1){
1804  Printf("Cannot open file \"%s\"",cdbFile1);
1805  return kFALSE;
1806  }
1807  TFile * f2 = TFile::Open(cdbFile2);
1808  if (!f2){
1809  Printf("Cannot open file \"%s\"",cdbFile2);
1810  return kFALSE;
1811  }
1812 
1813  AliCDBEntry * entry1 = (AliCDBEntry*)f1->Get("AliCDBEntry");
1814  if (!entry1){
1815  Printf("Cannot get CDB entry from file \"%s\"",cdbFile1);
1816  return kFALSE;
1817  }
1818  AliCDBEntry * entry2 = (AliCDBEntry*)f2->Get("AliCDBEntry");
1819  if (!entry2){
1820  Printf("Cannot get CDB entry from file \"%s\"",cdbFile2);
1821  return kFALSE;
1822  }
1823 
1824  // stream the two objects in the buffer of two TMessages
1825  TObject* object1 = entry1->GetObject();
1826  TObject* object2 = entry2->GetObject();
1827  TMessage * file1 = new TMessage(TBuffer::kWrite);
1828  file1->WriteObject(object1);
1829  Int_t size1 = file1->Length();
1830  TMessage * file2 = new TMessage(TBuffer::kWrite);
1831  file2->WriteObject(object2);
1832  Int_t size2 = file2->Length();
1833  if (size1!=size2){
1834  Printf("Problem 2: OCDB entry of different size (%d,%d)",size1,size2);
1835  return kFALSE;
1836  }
1837 
1838  // if the two buffers have the same size, check that they are the same byte-by-byte
1839  Int_t countDiff=0;
1840  char* buf1 = file1->Buffer();
1841  char* buf2 = file2->Buffer();
1842  //for (Int_t i=0; i<size1; i++) if (file1->Buffer()[i]!=file2->Buffer()[i]) countDiff++;
1843  for(Int_t i=0; i<size1; i++)
1844  if (buf1[i]!=buf2[i]) countDiff++;
1845 
1846  if (countDiff>0){
1847  Printf("The CDB objects differ by %d bytes.", countDiff);
1848  return kFALSE;
1849  }
1850 
1851  Printf("The CDB objects are the same in the two files.");
1852  return kTRUE;
1853 }
1854 
1855 //______________________________________________________________________________________________
1857 // Init the list of short-lived objects
1858 // currently disabled
1859 
1860  fShortLived=0x0;
1861 
1862  // fShortLived = new TList();
1863  // fShortLived->SetOwner(1);
1864  //
1865  // fShortLived->Add(new TObjString("EMCAL/Calib/Data"));
1866  //
1867  // fShortLived->Add(new TObjString("HMPID/Calib/Nmean"));
1868  // fShortLived->Add(new TObjString("HMPID/Calib/Qthre"));
1869  //
1870  // fShortLived->Add(new TObjString("ITS/Calib/CalibSPD"));
1871  //
1872  // fShortLived->Add(new TObjString("MUON/Calib/Gains"));
1873  // fShortLived->Add(new TObjString("MUON/Calib/HV"));
1874  // fShortLived->Add(new TObjString("MUON/Calib/Pedestals"));
1875  //
1876  // fShortLived->Add(new TObjString("PHOS/Calib/CpvGainPedestals"));
1877  // fShortLived->Add(new TObjString("PHOS/Calib/EmcGainPedestals"));
1878  //
1879  // fShortLived->Add(new TObjString("PMD/Calib/Data"));
1880  //
1881  // fShortLived->Add(new TObjString("TRD/Calib/ChamberGainFactor"));
1882  // fShortLived->Add(new TObjString("TRD/Calib/LocalGainFactor"));
1883  // fShortLived->Add(new TObjString("TRD/Calib/ChamberT0"));
1884  // fShortLived->Add(new TObjString("TRD/Calib/LocalT0"));
1885  // fShortLived->Add(new TObjString("TRD/Calib/ChamberVdrift"));
1886  // fShortLived->Add(new TObjString("TRD/Calib/LocalVdrift"));
1887  //
1888  // fShortLived->Add(new TObjString("ZDC/Calib/Data"));
1889 
1890 }
1891 
1892 //______________________________________________________________________________________________
1893 Bool_t AliCDBManager::IsShortLived(const char* path) {
1894 // returns the name (string) of the data type
1895 
1896  if(!fShortLived) return kFALSE;
1897 
1898  AliCDBPath aPath(path);
1899  if(!aPath.IsValid()){
1900  AliError(Form("Not a valid path: %s", path));
1901  return kFALSE;
1902  }
1903 
1904  return fShortLived->Contains(path);
1905 
1906 }
1907 
1908 //______________________________________________________________________________________________
1909 ULong64_t AliCDBManager::SetLock(Bool_t lock, ULong64_t key){
1910 // To lock/unlock user must provide the key. A new key is provided after
1911 // each successful lock. User should always backup the returned key and
1912 // use it on next access.
1913  ULong64_t msk=0x0000000ffffffff;
1914  if (fLock == lock) return 0; // nothing to be done
1915  if (lock) {
1916  // User wants to lock - check his identity
1917  if (fKey) {
1918  // Lock has a user - check his key
1919  if (fKey != key) {
1920  AliFatal("Wrong key provided to lock CDB. Please remove CDB lock access from your code !");
1921  return 0;
1922  }
1923  }
1924  // Provide new key
1925  fKey = gSystem->Now();
1926  fLock = kTRUE;
1927  // give to experts possibility to unlock the CDB
1928  SetUniqueID(UInt_t(fKey&msk));
1929  return fKey;
1930  }
1931  // User wants to unlock - check the provided key
1932  if (key != fKey) {
1933  AliFatal("Lock is ON: wrong key provided");
1934  return 0;
1935  }
1936  fLock = kFALSE;
1937  return key;
1938 }
1939 
1941 // AliCDBManager Parameter class //
1942 // interface to specific AliCDBParameter class //
1943 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam) //
1945 
1947  fType(),
1948  fURI()
1949 {
1950  // constructor
1951 
1952 }
1953 
1954 //_____________________________________________________________________________
1956  // destructor
1957 
1958 }
1959 
1961 {
1962  // TBD RS
1963  // remove everything but the url -
1964  // Exact copy of the AliReconstuction::Rectify.... (to be removed)
1965  //
1966  //
1967  TString sbs;
1968  if (!(sbs=url("\\?User=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1969  if (!(sbs=url("\\?DBFolder=[^?]*")).IsNull()) url.ReplaceAll("?DB","");
1970  if (!(sbs=url("\\?SE=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1971  if (!(sbs=url("\\?CacheFolder=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1972  if (!(sbs=url("\\?OperateDisconnected=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1973  if (!(sbs=url("\\?CacheSize=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1974  if (!(sbs=url("\\?CleanupInterval=[^?]*")).IsNull()) url.ReplaceAll(sbs,"");
1975  Bool_t slash=kFALSE,space=kFALSE;
1976  while ( (slash=url.EndsWith("/")) || (space=url.EndsWith(" ")) ) {
1977  if (slash) url = url.Strip(TString::kTrailing,'/');
1978  if (space) url = url.Strip(TString::kTrailing,' ');
1979  }
1980  //url.ToLower();
1981  //
1982 }
1983 
1984 //_____________________________________________________________________________
1986 {
1987  //put (or replace) a CDB entry in the prompt cache, and enable the cache
1988  //this is owner of object, so old entry is destroyed
1989 
1990  TPair* pair = static_cast<TPair*>(fEntryCache.FindObject(path));
1991  if (pair)
1992  {
1993  AliDebug(2, Form("Object %s already in cache, replacing!!", path));
1994  TObject* key = pair->Key();
1995  TObject* value = pair->Value();
1996  fEntryCache.Remove(key);
1997  delete key;
1998  //delete value; //We must not delete this here. Actually, the CDB Manager CANNOT take ownership. If some function has querried that Object, it might still have a pointer to it and we DO NOT know.
1999  }
2000  AliDebug(2,Form("Caching entry %s", path));
2001  fPromptEntryCache.Add(new TObjString(path), entry);
2002  SetPromptCacheFlag(kTRUE);
2003 }
2004 
2005 //_______________________________________
2006 void AliCDBManager::CreateMapListCopy(TMap& cdbMapCopy, TList& cdbListCopy) const
2007 {
2008  // populate used provided map and list with storages and retrieved object
2009  // names, combining information from cdb, snapshot
2010  cdbMapCopy.Clear();
2011  cdbListCopy.Clear();
2012  cdbMapCopy.SetOwner(1);
2013  cdbListCopy.SetOwner(1);
2014  const TMap* mapCDB = GetStorageMap(), *mapSNP = GetSnapshotMap();
2015  const TList* lstCDB = GetRetrievedIds(), *lstSNP = GetSnapshotRetrievedIds();
2016  //
2017  if (!mapCDB || !lstCDB) return;
2018  //
2019  TPair* pair = 0;
2020  // default storage of the snapshot (if any) becomes the new default
2021  TString defSNP,defCDB;
2022  //
2023  // add only specific storages of CDBmap since they have priority over everything else
2024  TIter iterCDBMap(mapCDB->GetTable());
2025  while((pair = dynamic_cast<TPair*> (iterCDBMap.Next()))) {
2026  TObjString* keyStr = dynamic_cast<TObjString*> (pair->Key());
2027  TObjString* valStr = dynamic_cast<TObjString*> (pair->Value());
2028  if (!keyStr || !valStr) continue;
2029  TString nms = keyStr->String();
2030  if (nms=="default") {
2031  defCDB = valStr->String();
2032  if (mapSNP) continue; // skip it since the snapshot was provided!
2033  }
2034  cdbMapCopy.Add(new TObjString(keyStr->GetName()), new TObjString(valStr->GetName()));
2035  }
2036  //
2037  // now add defaul and specific storages of the snapshot unless they were overridden
2038  if (mapSNP) {
2039  TIter iterSNPMap(mapSNP->GetTable());
2040  while((pair = dynamic_cast<TPair*> (iterSNPMap.Next()))) {
2041  TObjString* keyStr = dynamic_cast<TObjString*> (pair->Key());
2042  TObjString* valStr = dynamic_cast<TObjString*> (pair->Value());
2043  if (!keyStr || !valStr) continue;
2044  TString nms = keyStr->String();
2045  if (nms=="default") {
2046  defSNP = valStr->String();
2047  }
2048  else { // check if it was not overridden by the cdbMap
2049  if (mapCDB->FindObject(nms.Data())) continue; // skip it!
2050  }
2051  cdbMapCopy.Add(new TObjString(keyStr->GetName()), new TObjString(valStr->GetName()));
2052  }
2053  }
2054  //
2055  // now add retrieved entries: those which were not found in the snapshot or cdbManager specific
2056  // storages were obtained from cdbManager default, which will be added as new specific storage...
2057  //
2058  TIter iterCDBLst(lstCDB);
2059  AliCDBId* id=0;
2060  while( (id = dynamic_cast<AliCDBId*>(iterCDBLst.Next())) ) {
2061  TString path = id->GetPath();
2062  if (!mapCDB->FindObject(path.Data()) && (lstSNP && !lstSNP->FindObject(path.Data())) ) {
2063  cdbMapCopy.Add(new TObjString(path.Data()), new TObjString(defCDB.Data()));
2064  // printf("%s: adding CDB default as specific storage %s\n",path.Data(),defCDB.Data());
2065  }
2066  cdbListCopy.Add(new TObjString(id->ToString().Data()));
2067  }
2068 }
Bool_t IsAnyRange() const
Definition: AliCDBId.h:56
AliCDBEntry * Get(const AliCDBId &query)
const TString & GetURI() const
Definition: AliCDBStorage.h:34
void SetRun(Int_t run)
Definition: AliCDBHandler.h:44
TFile * Open(const char *filename, Long64_t &nevents)
AliCDBEntry * GetEntryFromSnapshot(const char *path)
void SetDefaultStorageFromRun(Int_t run)
static TString fgkCondUri
const TString & GetPath() const
Definition: AliCDBPath.h:38
void SetPath(const char *path)
Definition: AliCDBPath.h:39
Bool_t Comprises(const AliCDBPath &other) const
Definition: AliCDBPath.cxx:250
Bool_t Put(TObject *object, AliCDBId &id, AliCDBMetaData *metaData, const char *mirrors="", AliCDBManager::DataType type=AliCDBManager::kPrivate)
void RegisterFactory(AliCDBStorageFactory *factory)
AliCDBStorage * GetSpecificStorage(const char *calibType)
ULong64_t fKey
max UNIX timestamp for OCDB objects
void CreateMapListCopy(TMap &mapCopy, TList &listCopy) const
static AliCDBManager * fgInstance
#define TObjArray
virtual AliCDBParam * CloneParam() const =0
void ExtractBaseFolder(TString &url)
time_t GetMaxDate() const
Bool_t fCache
The run number.
void SetRunRange(Int_t firstRun, Int_t lastRun)
Definition: AliCDBId.h:52
Bool_t fPromptCache
The cache flag.
Int_t GetEndRunRange() const
Definition: AliCDBHandler.h:42
const TString & GetType() const
Definition: AliCDBStorage.h:35
const char * path
Bool_t fSnapshotMode
Lock flag, if ON default storage and run number cannot be reset.
const char * GetMirrorSEs() const
AliCDBParam * CreateParameter(const char *dbString) const
void DumpToSnapshotFile(const char *snapshotFileName, Bool_t singleKeys) const
void SetMaxDate(time_t maxDate)
void PutActiveStorage(AliCDBParam *param, AliCDBStorage *storage)
Bool_t fOCDBUploadMode
AliCDBParam * fCondParam
pointer to drain storage
void SetSubVersion(Int_t subVersion)
Definition: AliCDBId.h:62
void SetURI(const TString &uri)
Definition: AliCDBStorage.h:33
static TString fgkMCFullStorage
AliCDBStorage * fDrainStorage
pointer to default storage
static void Destroy()
Int_t GetVersion() const
Definition: AliCDBId.h:59
TString GetOCDBFolder() const
Definition: AliCDBHandler.h:43
Int_t GetLastRun() const
Definition: AliCDBId.h:49
Int_t GetEndRunLHCPeriod()
virtual AliCDBStorage * Create(const AliCDBParam *param)=0
const TString & GetURI() const
AliCDBId * GetId(const AliCDBId &query)
virtual const char * GetMirrorSEs() const
void InitFromCache(TMap *entryCache, Int_t run)
TList * fShortLived
list of storages (to be streamed to file)
void PromptCacheEntry(const char *calibType, AliCDBEntry *entry)
void DestroyActiveStorages()
#define AliWarning(message)
Definition: AliLog.h:541
Bool_t Drain(AliCDBEntry *entry)
Bool_t IsShortLived(const char *path)
void CacheEntry(const char *path, AliCDBEntry *entry)
AliCDBEntry * Get(const AliCDBId &query, Bool_t forceCaching=kFALSE)
static TString fgkMCResidualStorage
virtual ~AliCDBParam()
Int_t fStartRunLHCPeriod
static const char * GetDataTypeName(DataType type)
static TString fgkRefUri
TObject * GetObject()
Definition: AliCDBEntry.h:56
TMap fActiveStorages
list of registered storage factories
TList * GetActiveStorages()
void SetMaxDate(time_t maxDate)
void SetVersion(Int_t version)
Definition: AliCDBId.h:61
AliCDBStorage * GetStorage(const char *dbString)
void SetSpecificStorage(const char *calibType, const char *dbString, Int_t version=-1, Int_t subVersion=-1)
TMap * fStorageMap
List of the retrieved object Id&#39;s (to be streamed to file)
TFile * fSnapshotFile
flag saying if we are in snapshot mode
void UnsetDefaultStorage()
#define AliInfo(message)
Definition: AliLog.h:484
TList * GetAll(const AliCDBId &query)
Bool_t HasStorage(const char *dbString) const
TString fCvmfsOcdb
TList * fIds
cache for in-memory objects to override objects on storage (to be used online)
TMap fSpecificStorages
list of active storages
Bool_t SetOCDBUploadMode()
AliCDBId * GetId(const AliCDBId &query)
Bool_t IsValid() const
Definition: AliCDBId.cxx:135
Bool_t fRaw
flag for uploads to Official CDBs (upload to cvmfs must follow upload to AliEn)
Bool_t Put(TObject *object, const AliCDBId &id, AliCDBMetaData *metaData, const char *mirrors="", DataType type=kPrivate)
void QueryCDB(Int_t run, const char *pathFilter="*", Int_t version=-1, AliCDBMetaData *mdFilter=0)
void GetLHCPeriodAgainstAlienFile(Int_t run, TString &lhcPeriod, Int_t &startRun, Int_t &endRun)
virtual void SetMirrorSEs(const char *mirrors)
void SetRun(Int_t run)
Definition: AliCDBEntry.h:18
void SetDrain(const char *dbString)
#define AliFatal(message)
Definition: AliLog.h:640
TString ToString() const
Definition: AliCDBId.cxx:163
TString fLHCPeriod
TF1 * f
Definition: interpolTest.C:21
void SetDefaultStorage(const char *dbString)
void UnloadFromCache(const char *path)
void Print(Option_t *option="") const
Bool_t DiffObjects(const char *cdbFile1, const char *cdbFile2) const
virtual AliCDBParam * CreateParameter(const char *dbString)=0
#define AliDebug(logLevel, message)
Definition: AliLog.h:300
Int_t GetStartRunLHCPeriod()
void DumpToLightSnapshotFile(const char *lightSnapshotFileName) const
void AlienToCvmfsUri(TString &uriString) const
Int_t GetStartRunRange() const
Definition: AliCDBHandler.h:41
void DestroyActiveStorage(AliCDBStorage *storage)
const TMap * GetSnapshotMap() const
const TString & GetPath() const
Definition: AliCDBId.h:40
AliCDBStorage * GetDefaultStorage() const
Definition: AliCDBManager.h:61
static Int_t runNumber
Definition: pdc06_config.C:126
static TString fgkMCIdealStorage
Bool_t InitFromSnapshot(const char *snapshotFileName, Bool_t overwrite=kTRUE)
ULong64_t SetLock(Bool_t lockFlag=kTRUE, ULong64_t key=0)
Bool_t IsSpecified() const
Definition: AliCDBId.h:68
Bool_t IsWildcard() const
Definition: AliCDBPath.h:45
void SetOwner(Bool_t owner)
Definition: AliCDBEntry.h:64
#define AliError(message)
Definition: AliLog.h:591
virtual Bool_t Validate(const char *dbString)=0
const char * GetURI(const char *path)
AliCDBId & GetId()
Definition: AliCDBEntry.h:51
Bool_t IsValid() const
Definition: AliCDBPath.h:43
Bool_t IsDefaultStorageSet() const
Definition: AliCDBManager.h:60
void SetMirrorSEs(const char *mirrors)
AliCDBStorage * GetActiveStorage(const AliCDBParam *param)
AliCDBParam * fRefParam
Int_t fEndRunLHCPeriod
TMap fEntryCache
list of detector-specific storages
Int_t GetFirstRun() const
Definition: AliCDBId.h:48
TString GetLHCPeriod()
TMap fPromptEntryCache
cache of the retrieved objects
static Int_t Infinity()
Bool_t SetSnapshotMode(const char *snapshotFileName="OCDB.root")
AliCDBParam * SelectSpecificStorage(const TString &path)
void GetLHCPeriodAgainstCvmfsFile(Int_t run, TString &lhcPeriod, Int_t &startRun, Int_t &endRun)
void ValidateCvmfsCase() const
Bool_t fLock
The prompt cache flag.
static TString fgkOCDBFolderXMLfile
AliCDBStorage * fDefaultStorage
List of short lived objects.
const TList * GetSnapshotRetrievedIds() const
Int_t GetSubVersion() const
Definition: AliCDBId.h:60