]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBManager.cxx
New version of the CDB classes. Run number added (A.Colla).
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
index 22802be21f581b05a1eec01b2987f0f830eac16a..f469c7f55518dcc69e5c6820c33b93a983bc041e 100644 (file)
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
+//-------------------------------------------------------------------------
+//   Implementation of AliCDBManager and AliCDBParam classe
+//   Author: Alberto Colla 
+//   e-mail: Alberto.Colla@cern.ch
+//-------------------------------------------------------------------------
 
 #include "AliCDBManager.h"
 #include "AliCDBStorage.h"
@@ -19,6 +24,8 @@
 #include "AliCDBDump.h"
 #include "AliCDBLocal.h"
 #include "AliCDBGrid.h"
+#include "AliCDBEntry.h"
+#include "AliCDBMetaData.h"
 
 #include <TObjString.h>
 #include <TSystem.h>
@@ -58,6 +65,7 @@ void AliCDBManager::Destroy() {
 // delete ALCDBManager instance and active storages
 
        if (fgInstance) {
+               //fgInstance->Delete();
                delete fgInstance;
                fgInstance = 0x0;
        }
@@ -66,16 +74,22 @@ void AliCDBManager::Destroy() {
 //_____________________________________________________________________________
 AliCDBManager::AliCDBManager():
        fDefaultStorage(NULL),
-       fDrainStorage(NULL)
+       fDrainStorage(NULL),
+       fCache(kTRUE),
+       fRun(0)
 {
 // default constuctor
-       fFactories.SetOwner();
+       fFactories.SetOwner(1);
+       fEntryCache.SetOwner(1);
 }
 
 //_____________________________________________________________________________
 AliCDBManager::~AliCDBManager() {
 // destructor
+       ClearCache();
        DestroyActiveStorages();
+       fDrainStorage = 0x0;
+       fDefaultStorage = 0x0;
 }
 
 //_____________________________________________________________________________
@@ -103,7 +117,7 @@ void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
 }
 
 //_____________________________________________________________________________
-Bool_t AliCDBManager::HasStorage(const char* dbString) {
+Bool_t AliCDBManager::HasStorage(const char* dbString) const {
 // check if dbString is a URI valid for one of the registered factories 
 
        TIter iter(&fFactories);
@@ -120,7 +134,7 @@ Bool_t AliCDBManager::HasStorage(const char* dbString) {
 }
 
 //_____________________________________________________________________________
-AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) {
+AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
 // create AliCDBParam object from URI string
 
        TIter iter(&fFactories);
@@ -231,18 +245,10 @@ Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
        return fDrainStorage->Put(entry);
 }
 
-//_____________________________________________________________________________
-void AliCDBManager::RemoveDrain() {
-// remove drain storage
-
-       fDrainStorage=0;
-}
-
 //_____________________________________________________________________________
 void AliCDBManager::SetDefaultStorage(const char* dbString) {
 // sets default storage from URI string
 
-       if(fDefaultStorage) fDefaultStorage = 0;
        fDefaultStorage = GetStorage(dbString); 
 }
 
@@ -250,7 +256,6 @@ void AliCDBManager::SetDefaultStorage(const char* dbString) {
 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
 // set default storage from AliCDBParam object
        
-       if(fDefaultStorage) fDefaultStorage = 0;
        fDrainStorage = GetStorage(param);
 }
 
@@ -258,15 +263,288 @@ void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
 // set default storage from another active storage
        
-       if(fDefaultStorage) fDefaultStorage = 0;
        fDefaultStorage = storage;
 }
 
 //_____________________________________________________________________________
-void AliCDBManager::RemoveDefaultStorage() {
-// remove default storage
+void AliCDBManager::SetSpecificStorage(const char* detName, const char* dbString) {
+// sets storage specific for detector (works with AliCDBManager::Get(...))
+
+       AliCDBParam *aPar = CreateParameter(dbString);
+       if(!aPar) return;
+       SetSpecificStorage(detName, aPar);
+       delete aPar;
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetSpecificStorage(const char* detName, AliCDBParam* param) {
+// sets storage specific for detector (works with AliCDBManager::Get(...))
+
+       if(!fDefaultStorage) {
+               AliError("Please activate a default storage first!");   
+               return;
+       }
+       
+       TObjString *objDetName = new TObjString(detName);
+       AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(objDetName);
+       if(checkPar){
+               AliWarning(Form("%s storage already activated! It will be replaced by the new one",objDetName->String().Data()));
+               fSpecificStorages.Remove(objDetName);   
+               delete checkPar;
+       }
+       GetStorage(param);
+       fSpecificStorages.Add(objDetName, param->CloneParam());
+}
+
+//_____________________________________________________________________________
+AliCDBStorage* AliCDBManager::GetSpecificStorage(const char* detName) {
+// get storage specific for detector 
+
+       AliCDBParam *checkPar = (AliCDBParam*) fSpecificStorages.GetValue(detName);
+       if(!checkPar){
+               AliError(Form("%s storage not found!",detName));
+               return NULL;
+       } else {
+               return GetStorage(checkPar);
+       }
+       
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, Int_t runNumber, 
+       Int_t version, Int_t subVersion) {
+// get an AliCDBEntry object from the database
+
+       return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, 
+       const AliCDBRunRange& runRange, Int_t version,
+       Int_t subVersion) {
+// get an AliCDBEntry object from the database!
+
+       return Get(AliCDBId(path, runRange, version, subVersion));
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) {       
+// get an AliCDBEntry object from the database
+       
+       if(!fDefaultStorage) {
+               AliError("No storage set!");
+               return NULL;
+       }
+
+       // check if query's path and runRange are valid
+       // query is invalid also if version is not specified and subversion is!
+       if (!query.IsValid()) {
+               AliError(Form("Invalid query: %s", query.ToString().Data()));
+               return NULL;
+       }
+
+       // query is not specified if path contains wildcard or runrange = [-1,-1] 
+       if (!query.IsSpecified()) {
+               AliError(Form("Unspecified query: %s", 
+                               query.ToString().Data()));
+                return NULL;
+       }
+
+       TObjString objStrLev0(query.GetLevel0());
+       AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
+       AliCDBStorage *aStorage;
+       
+       if(aPar) {
+               aStorage=GetStorage(aPar);
+               TString str = aPar->GetURI();
+               AliDebug(2,Form("Looking into storage: %s",str.Data()));
+               
+       } else {
+               aStorage=GetDefaultStorage();
+               AliDebug(2,"Looking into default storage");     
+       }
+                       
+       return aStorage->Get(query);
+}
+
+//_____________________________________________________________________________
+TList* AliCDBManager::GetAll(const AliCDBPath& path, Int_t runNumber, 
+       Int_t version, Int_t subVersion) {
+// get multiple AliCDBEntry objects from the database
+
+       return GetAll(AliCDBId(path, runNumber, runNumber, version,     
+                       subVersion));
+}
+
+//_____________________________________________________________________________
+TList* AliCDBManager::GetAll(const AliCDBPath& path, 
+       const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
+// get multiple AliCDBEntry objects from the database
+
+       return GetAll(AliCDBId(path, runRange, version, subVersion));
+}
+
+//_____________________________________________________________________________
+TList* AliCDBManager::GetAll(const AliCDBId& query) {
+// get multiple AliCDBEntry objects from the database
+
+       if(!fDefaultStorage) {
+               AliError("No storage set!");
+               return NULL;
+       }
+
+       if (!query.IsValid()) {
+                AliError(Form("Invalid query: %s", query.ToString().Data()));
+                return NULL;
+        }
+
+       if(query.GetPath().BeginsWith('*')){
+                AliError("Query too generic in this context!");
+                return NULL;           
+       }
+
+       if (query.IsAnyRange()) {
+               AliError(Form("Unspecified run or runrange: %s",
+                               query.ToString().Data()));      
+               return NULL;
+       }       
+        
+       TObjString objStrLev0(query.GetLevel0());
+       AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
+       AliCDBStorage *aStorage;
+       
+       if(aPar) {
+               aStorage=GetStorage(aPar);
+               TString str = aPar->GetURI();
+               AliDebug(2,Form("Looking into storage: %s",str.Data()));
+               
+       } else {
+               aStorage=GetDefaultStorage();
+               AliDebug(2,"Looking into default storage");     
+       }
+
+       TList *result = aStorage->GetAll(query);
+
+        return result;
+}
+
+//_____________________________________________________________________________
+Bool_t AliCDBManager::Put(TObject* object, AliCDBId& id,  AliCDBMetaData* metaData){
+// store an AliCDBEntry object into the database
+
+       AliCDBEntry anEntry(object, id, metaData);
+       return Put(&anEntry);
+
+}
+
+
+//_____________________________________________________________________________
+Bool_t AliCDBManager::Put(AliCDBEntry* entry){
+// store an AliCDBEntry object into the database
+
+       if(!fDefaultStorage) {
+               AliError("No storage set!");
+               return kFALSE;
+       }
+
+       if (!entry){
+               AliError("No entry!");
+               return kFALSE;
+       }
+
+       if (!entry->GetId().IsValid()) {
+               AliError(Form("Invalid entry ID: %s", 
+                       entry->GetId().ToString().Data()));
+               return kFALSE;
+       }       
+
+       if (!entry->GetId().IsSpecified()) {
+               AliError(Form("Unspecified entry ID: %s", 
+                       entry->GetId().ToString().Data()));
+               return kFALSE;
+       }
+
+       AliCDBId id = entry->GetId();
+       TObjString objStrLev0(id.GetLevel0());
+       AliCDBParam *aPar = (AliCDBParam*) fSpecificStorages.GetValue(&objStrLev0);
+       AliCDBStorage *aStorage;
+       
+       if(aPar) {
+               aStorage=GetStorage(aPar);
+               TString str = aPar->GetURI();
+               AliDebug(2,Form("Storing object into storage: %s",str.Data()));
+               
+       } else {
+               aStorage=GetDefaultStorage();
+               AliDebug(2,"Storing object into default storage");      
+       }
+
+       return aStorage->Put(entry);
+
+
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBManager::Get(const char* path)
+{
+// get an AliCDBEntry object from the database, using fRun as run number
+    
+       if (fRun < 0)
+       {
+               AliError("Run number not set! Use AliCDBManager::SetRun.");
+               return 0;
+       }
+       
+       AliCDBEntry *entry;
+  
+       // first look into map of cached objects
+       entry = (AliCDBEntry*) fEntryCache.GetValue(path);
+       if(entry) return entry;
+
+       // Entry is not in cache -> retrieve it from CDB and cache it!!
+       entry = Get(path, fRun); 
+       if (!entry) return 0;
+   
+       if(fCache) CacheEntry(path, entry);
+  
+       return entry;
+
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry)
+{
+// cache AliCDBEntry. Cache is valid until run number is changed.
+
+       AliDebug(2,Form("Filling cache with entry %s",path));
+       fEntryCache.Add(new TObjString(path), entry);
+       AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
+
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::SetRun(Long64_t run)
+{
+//
+// Sets current run number.  
+// When the run number changes the caching is cleared.
+//
+  
+       if (fRun == run)
+               return;
+  
+       fRun = run;
+       ClearCache();
+}
+
+//_____________________________________________________________________________
+void AliCDBManager::ClearCache(){
+// clear AliCDBEntry cache
+
+       AliDebug(2,Form("Clearing cache!"));
+       fEntryCache.DeleteAll();
+       AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries()));
 
-       fDefaultStorage = 0;
 }
 
 //_____________________________________________________________________________
@@ -274,11 +552,22 @@ void AliCDBManager::DestroyActiveStorages() {
 // delete list of active storages
 
        fActiveStorages.DeleteAll();
+       fSpecificStorages.DeleteAll();
 }
 
 //_____________________________________________________________________________
 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
-// destroys active storage (not implemented)
+// destroys active storage
+
+/*
+       TIter iter(fActiveStorages.GetTable()); 
+       TPair* aPair;
+       while ((aPair = (TPair*) iter.Next())) {
+               if(storage == (AliCDBStorage*) aPair->Value())
+                       delete fActiveStorages.Remove(aPair->Key());
+                       storage->Delete(); storage=0x0;
+       }
+*/     
 
 }