3de69d059ead1a0161ce3e58f74a84ec869fc1b1
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
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 "AliCDBManager.h"
22 #include "AliCDBStorage.h"
23 #include "AliLog.h"
24 #include "AliCDBDump.h"
25 #include "AliCDBLocal.h"
26 #include "AliCDBGrid.h"
27 #include "AliCDBEntry.h"
28 #include "AliCDBMetaData.h"
29
30 #include <TObjString.h>
31 #include <TSystem.h>
32
33 ClassImp(AliCDBParam)
34
35 ClassImp(AliCDBManager)
36
37 AliCDBManager* AliCDBManager::fgInstance = 0x0;
38
39 //_____________________________________________________________________________
40 AliCDBManager* AliCDBManager::Instance() {
41 // returns AliCDBManager instance (singleton)
42
43         if (!fgInstance) {
44                 fgInstance = new AliCDBManager();
45                 fgInstance->Init();
46         }
47
48         return fgInstance;
49 }
50
51 //_____________________________________________________________________________
52 void AliCDBManager::Init() {
53 // factory registering
54
55         RegisterFactory(new AliCDBDumpFactory());
56         RegisterFactory(new AliCDBLocalFactory()); 
57         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
58         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
59                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
60                 RegisterFactory(new AliCDBGridFactory());
61         }
62 }
63 //_____________________________________________________________________________
64 void AliCDBManager::Destroy() {
65 // delete ALCDBManager instance and active storages
66
67         if (fgInstance) {
68                 
69                 delete fgInstance;
70                 fgInstance = 0x0;
71         }
72 }
73
74 //_____________________________________________________________________________
75 AliCDBManager::AliCDBManager():
76         fDefaultStorage(NULL),
77         fDrainStorage(NULL)
78 {
79 // default constuctor
80         fFactories.SetOwner();
81 }
82
83 //_____________________________________________________________________________
84 AliCDBManager::~AliCDBManager() {
85 // destructor
86         DestroyActiveStorages();
87         fDrainStorage = 0x0;
88         fDefaultStorage = 0x0;
89 }
90
91 //_____________________________________________________________________________
92 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
93 // get a storage object from the list of active storages 
94
95         return (AliCDBStorage*) fActiveStorages.GetValue(param);
96 }
97
98 //_____________________________________________________________________________
99 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
100 // put a storage object into the list of active storages
101
102         fActiveStorages.Add(param, storage);
103         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
104 }
105
106 //_____________________________________________________________________________
107 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
108 // add a storage factory to the list of registerd factories
109  
110         if (!fFactories.Contains(factory)) {
111                 fFactories.Add(factory);
112         }
113 }
114
115 //_____________________________________________________________________________
116 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
117 // check if dbString is a URI valid for one of the registered factories 
118
119         TIter iter(&fFactories);
120
121         AliCDBStorageFactory* factory;
122         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
123
124                 if (factory->Validate(dbString)) {
125                         return kTRUE;
126                 }       
127         }
128
129         return kFALSE;
130 }
131
132 //_____________________________________________________________________________
133 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
134 // create AliCDBParam object from URI string
135
136         TIter iter(&fFactories);
137
138         AliCDBStorageFactory* factory;
139         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
140
141                 AliCDBParam* param = factory->CreateParameter(dbString);
142                 if (param) {
143                         return param;
144                 }
145         }
146
147         return NULL;
148 }
149
150 //_____________________________________________________________________________
151 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
152 // get storage object from URI string
153         
154         AliCDBParam* param = CreateParameter(dbString);
155         if (!param) {
156                 return NULL;
157         }       
158
159         AliCDBStorage* aStorage = GetStorage(param);
160
161         delete param;
162         
163         return aStorage;
164 }
165
166 //_____________________________________________________________________________
167 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
168 // get storage object from AliCDBParam object
169
170         // if the list of active storages already contains 
171         // the requested storage, return it
172         AliCDBStorage* aStorage = GetActiveStorage(param);
173         if (aStorage) {
174                 return aStorage;
175         }
176
177         TIter iter(&fFactories);
178
179         AliCDBStorageFactory* factory;
180         
181         // loop on the list of registered factories
182         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
183
184                 // each factory tries to create its storage from the parameter
185                 aStorage = factory->Create(param);
186                 if (aStorage) {
187                         PutActiveStorage(param->CloneParam(), aStorage);
188                         // if default storage is not set, set to this storage
189                         if(!fDefaultStorage){
190                                 fDefaultStorage=aStorage;
191                                 AliInfo(Form("Default storage set to: %s",(param->GetURI()).Data()));
192                         }
193                         return aStorage;
194                 }
195         }
196
197         return NULL;
198 }
199
200 //_____________________________________________________________________________
201 TList* AliCDBManager::GetActiveStorages() {
202 // return list of active storages
203
204         TList* result = new TList();
205
206         TIter iter(fActiveStorages.GetTable()); 
207         TPair* aPair;
208         while ((aPair = (TPair*) iter.Next())) {
209                 result->Add(aPair->Value());
210         }
211
212         return result;
213 }
214
215 //_____________________________________________________________________________
216 void AliCDBManager::SetDrain(const char* dbString) {
217 // set drain storage from URI string
218
219         fDrainStorage = GetStorage(dbString);   
220 }
221
222 //_____________________________________________________________________________
223 void AliCDBManager::SetDrain(const AliCDBParam* param) {
224 // set drain storage from AliCDBParam
225         
226         fDrainStorage = GetStorage(param);
227 }
228
229 //_____________________________________________________________________________
230 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
231 // set drain storage from another active storage
232         
233         fDrainStorage = storage;
234 }
235
236 //_____________________________________________________________________________
237 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
238 // drain retrieved object to drain storage
239
240         AliInfo("Draining into drain storage...");
241         return fDrainStorage->Put(entry);
242 }
243
244 //_____________________________________________________________________________
245 void AliCDBManager::SetDefaultStorage(const char* dbString) {
246 // sets default storage from URI string
247
248         fDefaultStorage = GetStorage(dbString); 
249 }
250
251 //_____________________________________________________________________________
252 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
253 // set default storage from AliCDBParam object
254         
255         fDrainStorage = GetStorage(param);
256 }
257
258 //_____________________________________________________________________________
259 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
260 // set default storage from another active storage
261         
262         fDefaultStorage = storage;
263 }
264
265 //_____________________________________________________________________________
266 void AliCDBManager::SetSpecificStorage(const char* detName, const char* dbString) {
267 // sets storage specific for detector (works with AliCDBManager::Get(...))
268
269         AliCDBParam *aPar = CreateParameter(dbString);
270         if(!aPar) return;
271         SetSpecificStorage(detName, aPar);
272         delete aPar;
273 }
274
275 //_____________________________________________________________________________
276 void AliCDBManager::SetSpecificStorage(const char* detName, AliCDBParam* param) {
277 // sets storage specific for detector (works with AliCDBManager::Get(...))
278
279         if(!fDefaultStorage) {
280                 AliError("Please activate a default storage first!");   
281                 return;
282         }
283         
284         TObjString *objDetName = new TObjString(detName);
285         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(objDetName);
286         if(checkPar){
287                 AliWarning(Form("%s storage already activated! It will be replaced by the new one",objDetName->String().Data()));
288                 fSpecificStorages.Remove(objDetName);   
289                 delete checkPar;
290         }
291         GetStorage(param);
292         fSpecificStorages.Add(objDetName, param->CloneParam());
293 }
294
295 //_____________________________________________________________________________
296 AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* detName) {
297 // get storage specific for detector 
298         TObjString objDetName(detName);
299         AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(&objDetName);
300         if(!checkPar){
301                 AliError(Form("%s storage not found!",objDetName.String().Data()));
302                 return NULL;
303         } else {
304                 return GetStorage(checkPar);
305         }
306         
307 }
308
309 //_____________________________________________________________________________
310 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber, 
311         Int_t version, Int_t subVersion) {
312 // get an AliCDBEntry object from the database
313
314         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
315 }
316
317 //_____________________________________________________________________________
318 AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, 
319         const AliCDBRunRange& runRange, Int_t version,
320         Int_t subVersion) {
321 // get an AliCDBEntry object from the database!
322
323         return Get(AliCDBId(path, runRange, version, subVersion));
324 }
325
326 //_____________________________________________________________________________
327 AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {        
328 // get an AliCDBEntry object from the database
329         
330         if(!fDefaultStorage) {
331                 AliError("No storage set!");
332                 return NULL;
333         }
334
335         // check if query's path and runRange are valid
336         // query is invalid also if version is not specified and subversion is!
337         if (!query.IsValid()) {
338                 AliError(Form("Invalid query: %s", query.ToString().Data()));
339                 return NULL;
340         }
341
342         // query is not specified if path contains wildcard or runrange = [-1,-1] 
343         if (!query.IsSpecified()) {
344                 AliError(Form("Unspecified query: %s", 
345                                 query.ToString().Data()));
346                 return NULL;
347         }
348
349         TObjString objStrLev0(query.GetLevel0());
350         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
351         AliCDBStorage *aStorage;
352         
353         if(aPar) {
354                 aStorage=GetStorage(aPar);
355                 TString str = aPar->GetURI();
356                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
357                 
358         } else {
359                 aStorage=GetDefaultStorage();
360                 AliDebug(2,"Looking into default storage");     
361         }
362                         
363         return aStorage->Get(query);
364 }
365
366 //_____________________________________________________________________________
367 TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber, 
368         Int_t version, Int_t subVersion) {
369 // get multiple AliCDBEntry objects from the database
370
371         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
372                         subVersion));
373 }
374
375 //_____________________________________________________________________________
376 TList* AliCDBManager::GetAll(const AliCDBPath& path, 
377         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
378 // get multiple AliCDBEntry objects from the database
379
380         return GetAll(AliCDBId(path, runRange, version, subVersion));
381 }
382
383 //_____________________________________________________________________________
384 TList* AliCDBManager::GetAll(const AliCDBId& query) {
385 // get multiple AliCDBEntry objects from the database
386
387         if(!fDefaultStorage) {
388                 AliError("No storage set!");
389                 return NULL;
390         }
391
392         if (!query.IsValid()) {
393                 AliError(Form("Invalid query: %s", query.ToString().Data()));
394                 return NULL;
395         }
396
397         if(query.GetPath().BeginsWith('*')){
398                 AliError("Query too generic in this context!");
399                 return NULL;            
400         }
401
402         if (query.IsAnyRange()) {
403                 AliError(Form("Unspecified run or runrange: %s",
404                                 query.ToString().Data()));      
405                 return NULL;
406         }       
407         
408         TObjString objStrLev0(query.GetLevel0());
409         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
410         AliCDBStorage *aStorage;
411         
412         if(aPar) {
413                 aStorage=GetStorage(aPar);
414                 TString str = aPar->GetURI();
415                 AliDebug(2,Form("Looking into storage: %s",str.Data()));
416                 
417         } else {
418                 aStorage=GetDefaultStorage();
419                 AliDebug(2,"Looking into default storage");     
420         }
421
422         TList *result = aStorage->GetAll(query);
423
424         return result;
425 }
426
427 //_____________________________________________________________________________
428 Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData){
429 // store an AliCDBEntry object into the database
430
431         AliCDBEntry anEntry(object, id, metaData);
432         return Put(&anEntry);
433
434 }
435
436
437 //_____________________________________________________________________________
438 Bool_t AliCDBManager::Put(AliCDBEntry* entry){
439 // store an AliCDBEntry object into the database
440
441         if(!fDefaultStorage) {
442                 AliError("No storage set!");
443                 return kFALSE;
444         }
445
446         if (!entry){
447                 AliError("No entry!");
448                 return kFALSE;
449         }
450
451         if (!entry->GetId().IsValid()) {
452                 AliError(Form("Invalid entry ID: %s", 
453                         entry->GetId().ToString().Data()));
454                 return kFALSE;
455         }       
456
457         if (!entry->GetId().IsSpecified()) {
458                 AliError(Form("Unspecified entry ID: %s", 
459                         entry->GetId().ToString().Data()));
460                 return kFALSE;
461         }
462
463         AliCDBId id = entry->GetId();
464         TObjString objStrLev0(id.GetLevel0());
465         AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
466         AliCDBStorage *aStorage;
467         
468         if(aPar) {
469                 aStorage=GetStorage(aPar);
470                 TString str = aPar->GetURI();
471                 AliDebug(2,Form("Storing object into storage: %s",str.Data()));
472                 
473         } else {
474                 aStorage=GetDefaultStorage();
475                 AliDebug(2,"Storing object into default storage");      
476         }
477
478         return aStorage->Put(entry);
479
480
481 }
482
483 //_____________________________________________________________________________
484 void AliCDBManager::DestroyActiveStorages() {
485 // delete list of active storages
486
487         fActiveStorages.DeleteAll();
488         fSpecificStorages.DeleteAll();
489 }
490
491 //_____________________________________________________________________________
492 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
493 // destroys active storage
494
495 /*
496         TIter iter(fActiveStorages.GetTable()); 
497         TPair* aPair;
498         while ((aPair = (TPair*) iter.Next())) {
499                 if(storage == (AliCDBStorage*) aPair->Value())
500                         delete fActiveStorages.Remove(aPair->Key());
501                         storage->Delete(); storage=0x0;
502         }
503 */      
504
505 }
506
507 ///////////////////////////////////////////////////////////
508 // AliCDBManager Parameter class                         //
509 // interface to specific AliCDBParameter class           //
510 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
511 ///////////////////////////////////////////////////////////
512
513 AliCDBParam::AliCDBParam() {
514 // constructor
515
516 }
517
518 //_____________________________________________________________________________
519 AliCDBParam::~AliCDBParam() {
520 // destructor
521
522 }
523