]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBLocal.cxx
updated commment
[u/mrichter/AliRoot.git] / STEER / AliCDBLocal.cxx
index 5f0fa08f6da4b18c8b4a65146f152c932d568b8d..aae166900d9fa6ab5c16c206a01cc40d98741cf6 100644 (file)
@@ -49,6 +49,8 @@ fBaseDirectory(baseDir)
                AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data()));
                gSystem->FreeDirectory(dir);
        }
+       fType="local";
+       fBaseFolder = fBaseDirectory;
 }
 
 //_____________________________________________________________________________
@@ -96,35 +98,29 @@ Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
 
 
 //_____________________________________________________________________________
-Bool_t AliCDBLocal::IdToFilename(const AliCDBRunRange& runRange, Int_t version,
-       Int_t subVersion, TString& filename) {
+Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
 // build file name from AliCDBId data (run range, version, subVersion)
 
-       if (!runRange.IsValid()) {
+       if (!id.GetAliCDBRunRange().IsValid()) {
                AliDebug(2,Form("Invalid run range <%d, %d>.", 
-                       runRange.GetFirstRun(), runRange.GetLastRun()));
+                       id.GetFirstRun(), id.GetLastRun()));
                return kFALSE;
        }
 
-       if (version < 0) {
-               AliDebug(2,Form("Invalid version <%d>.", version));
+       if (id.GetVersion() < 0) {
+               AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
                 return kFALSE;
        }
 
-       if (subVersion < 0) {
-               AliDebug(2,Form("Invalid subversion <%s>.", subVersion));
+       if (id.GetSubVersion() < 0) {
+               AliDebug(2,Form("Invalid subversion <%s>.", id.GetSubVersion()));
                return kFALSE;
        }
  
-        filename += "Run";
-       filename += runRange.GetFirstRun();
-        filename += "_";
-        filename += runRange.GetLastRun();
-       filename += "_v";
-       filename += version;
-       filename += "_s";
-       filename += subVersion;
-       filename += ".root";
+       filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(),
+                                                id.GetVersion(), id.GetSubVersion());
+
+       filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
 
         return kTRUE;
 }
@@ -133,10 +129,7 @@ Bool_t AliCDBLocal::IdToFilename(const AliCDBRunRange& runRange, Int_t version,
 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
 
-       TString dirName;
-       dirName += fBaseDirectory;
-       dirName += '/';
-       dirName += id.GetPath();
+       TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
 
        // go to the path; if directory does not exist, create it
        void* dirPtr = gSystem->OpenDirectory(dirName);
@@ -225,9 +218,9 @@ Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
 
        if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
           id.GetSubVersion() > 0 ){
-               AliWarning(Form("A NEW object is being stored with version v%d_s%d",
+               AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
                                        id.GetVersion(),id.GetSubVersion()));
-               AliWarning(Form("and it will hide previously stored object with v%d_s%d!",
+               AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
                                        id.GetVersion(),id.GetSubVersion()-1));
        }
 
@@ -243,27 +236,24 @@ Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
 Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
 // look for filename matching query (called by GetEntry)
 
-       TString dirName;
-       dirName += fBaseDirectory;
-       dirName += '/';
-       dirName += query.GetPath(); // dirName = fDBPath/idPath
+       TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
 
-       void* dirPtr = gSystem->OpenDirectory(dirName); 
+       void* dirPtr = gSystem->OpenDirectory(dirName);
        if (!dirPtr) {
-               AliError(Form("Directory <%s> not found", (query.GetPath()).Data()));
-               AliError(Form("in DB folder %s", fBaseDirectory.Data()));
+               AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
+               AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
                return kFALSE;
        }
 
-       const char* filename;   
-       
+       const char* filename;
+
        AliCDBRunRange aRunRange; // the runRange got from filename
        Int_t aVersion, aSubVersion; // the version and subVersion got from filename
 
        if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
-               
+
                while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
-                       
+
                        TString aString(filename);
                        if (aString == "." || aString == "..") continue;
 
@@ -272,7 +262,7 @@ Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
 
                        if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
                        // aRunRange contains requested run!
-                       
+
                        if (result.GetVersion() < aVersion) {
                                result.SetVersion(aVersion);
                                result.SetSubVersion(aSubVersion);
@@ -294,7 +284,7 @@ Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
                                        aRunRange.GetLastRun());
                        } else if (result.GetVersion() == aVersion
                                && result.GetSubVersion() == aSubVersion){
-                               AliError(Form("More than one object valid for run %d, version %d_%d!", 
+                               AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
                                        query.GetFirstRun(), aVersion, aSubVersion));
                                gSystem->FreeDirectory(dirPtr);
                                return kFALSE; 
@@ -320,7 +310,7 @@ Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
                        // aVersion is requested version!
                        
                        if(result.GetSubVersion() == aSubVersion){
-                               AliError(Form("More than one object valid for run %d, version %d_%d!", 
+                               AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
                                        query.GetFirstRun(), aVersion, aSubVersion));
                                gSystem->FreeDirectory(dirPtr);
                                return kFALSE; 
@@ -353,7 +343,7 @@ Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
                        // aVersion and aSubVersion are requested version and subVersion!
                        
                        if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
-                               AliError(Form("More than one object valid for run %d, version %d_%d!", 
+                               AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
                                        query.GetFirstRun(), aVersion, aSubVersion));
                                gSystem->FreeDirectory(dirPtr);
                                return kFALSE; 
@@ -377,7 +367,7 @@ AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
 
        AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
         Bool_t result;
-               
+
        // look for a filename matching query requests (path, runRange, version, subVersion)
        if (!queryId.HasVersion()) {
                // if version is not specified, first check the selection criteria list
@@ -391,52 +381,45 @@ AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
        if (!result || !dataId.IsSpecified()) return NULL;
 
        TString filename;
-       if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),
-                dataId.GetSubVersion(), filename)) {
+       if (!IdToFilename(dataId, filename)) {
 
-               AliError("Bad data ID encountered! Subnormal error!");
+               AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
                 return NULL;
        }
-       
-       filename.Prepend((fBaseDirectory +'/' + queryId.GetPath() + '/'));
 
        TFile file(filename, "READ"); // open file
        if (!file.IsOpen()) {
-               AliError(Form("Can't open file <%s>!", filename.Data()));
+               AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
                 return NULL;
        }
 
        // get the only AliCDBEntry object from the file
        // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
        
-       TObject* anObject = file.Get("AliCDBEntry");
-       if (!anObject) {
-               AliError("Bad storage data: NULL entry object!");
+       AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
+       if (!anEntry) {
+               AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
+               file.Close();
                return NULL;
        }
 
-       if (AliCDBEntry::Class() != anObject->IsA()) {
-               AliError("Bad storage data: Invalid entry object!");
-               return NULL;
-       }
-       
-       AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
+       AliCDBId entryId = anEntry->GetId();
  
        // The object's Id are not reset during storage
        // If object's Id runRange or version do not match with filename,
        // it means that someone renamed file by hand. In this case a warning msg is issued.
-       
-       ((AliCDBEntry*) anObject)-> SetLastStorage("local");
+
+       anEntry-> SetLastStorage("local");
  
-       if(!((entryId.GetAliCDBRunRange()).IsEqual(& dataId.GetAliCDBRunRange())) || 
-               (entryId.GetVersion() != dataId.GetVersion()) || (entryId.GetSubVersion() != dataId.GetSubVersion())){
-               AliWarning(Form("Either object Id's RunRange or version do noth match with file name:"));
-               AliWarning("someone renamed file by hand!");
+       if(!entryId.IsEqual(&dataId)){
+               AliWarning(Form("Mismatch between file name and object's Id!"));
+               AliWarning(Form("File name: %s", dataId.ToString().Data()));
+               AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
         }
-       
+
        // close file, return retieved entry
        file.Close();
-       return (AliCDBEntry*) anObject;
+       return anEntry;
 }
 
 //_____________________________________________________________________________
@@ -444,17 +427,14 @@ void AliCDBLocal::GetEntriesForLevel0(const char* level0,
        const AliCDBId& queryId, TList* result) {
 // multiple request (AliCDBStorage::GetAll)
 
-       TString level0Dir;
-       level0Dir += fBaseDirectory;
-       level0Dir += '/';
-       level0Dir += level0;
-       
+       TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
+
        void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
        if (!level0DirPtr) {
-               AliError(Form("Can't open level0 directory <%s>!", 
+               AliDebug(2,Form("Can't open level0 directory <%s>!",
                        level0Dir.Data()));
                 return;
-       } 
+       }
 
        const char* level1;
        while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
@@ -477,16 +457,11 @@ void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
        const AliCDBId& queryId, TList* result) {
 // multiple request (AliCDBStorage::GetAll)
 
-       TString level1Dir;
-       level1Dir += fBaseDirectory;
-       level1Dir += '/';
-       level1Dir += level0;
-       level1Dir += '/';
-       level1Dir += level1;
+       TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
 
        void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
        if (!level1DirPtr) {
-               AliError(Form("Can't open level1 directory <%s>!", 
+               AliDebug(2,Form("Can't open level1 directory <%s>!",
                        level1Dir.Data()));
                 return;
        }
@@ -500,7 +475,7 @@ void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
                }
 
                if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
-                       
+
                        AliCDBPath entryPath(level0, level1, level2);
                        AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
                                queryId.GetVersion(), queryId.GetSubVersion());
@@ -521,27 +496,27 @@ TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
 
        void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
        if (!storageDirPtr) {
-               AliError(Form("Can't open storage directory <%s>",
+               AliDebug(2,Form("Can't open storage directory <%s>",
                        fBaseDirectory.Data()));
                return NULL;
-       }       
+       }
 
        TList* result = new TList();
        result->SetOwner();
 
        const char* level0;
        while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
-               
+
                TString level0Str(level0);
                if (level0Str == "." || level0Str == "..") {
                        continue;
                }
 
                if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
-                       GetEntriesForLevel0(level0, queryId, result);   
+                       GetEntriesForLevel0(level0, queryId, result);
                }
-       }       
-       
+       }
+
        gSystem->FreeDirectory(storageDirPtr);
 
        return result;  
@@ -554,20 +529,17 @@ Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
        AliCDBId& id = entry->GetId();
 
        // set version and subVersion for the entry to be stored
-       if (!PrepareId(id)) return kFALSE;              
+       if (!PrepareId(id)) return kFALSE;
 
        
        // build filename from entry's id
-       TString filename;
-       if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(),
-               id.GetSubVersion(), filename)) {
+       TString filename="";
+       if (!IdToFilename(id, filename)) {
 
-               AliError("Bad ID encountered! Subnormal error!");
+               AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
                return kFALSE;
        }
-       
-       filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
-       
+
        // open file
        TFile file(filename, "CREATE");
        if (!file.IsOpen()) {
@@ -577,13 +549,16 @@ Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
        
        entry->SetVersion(id.GetVersion());
        entry->SetSubVersion(id.GetSubVersion());
-       
+
        // write object (key name: "AliCDBEntry")
        Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
-       if (!result) AliError(Form("Can't write entry to file: %s", filename.Data()));
+       if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
 
        file.Close();
-        if(result) AliInfo(Form("CDB object stored into file %s",filename.Data()));
+        if(result) {
+               if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
+                       AliInfo(Form("CDB object stored into file %s",filename.Data()));
+       }
 
        return result;
 }
@@ -623,10 +598,7 @@ TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
 Bool_t AliCDBLocal::Contains(const char* path) const{
 // check for path in storage's fBaseDirectory
 
-       TString dirName;
-       dirName += fBaseDirectory;
-       dirName += '/';
-       dirName += path; // dirName = fDBPath/path
+       TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
        Bool_t result=kFALSE;
 
        void* dirPtr = gSystem->OpenDirectory(dirName); 
@@ -636,6 +608,127 @@ Bool_t AliCDBLocal::Contains(const char* path) const{
        return result;
 }
 
+//_____________________________________________________________________________
+void AliCDBLocal::QueryValidFiles()
+{
+// Query the CDB for files valid for AliCDBStorage::fRun
+// fills list fValidFileIds with AliCDBId objects created from file name
+
+       if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
+       if(fMetaDataFilter) {
+               AliWarning ("CDB meta data parameters are not used by local storage query!");
+               delete fMetaDataFilter; fMetaDataFilter=0;
+       }
+
+       void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
+
+       const char* level0;
+       while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
+
+               TString level0Str(level0);
+               if (level0Str == "." || level0Str == "..") {
+                       continue;
+               }
+
+               if (fPathFilter.Level0Comprises(level0)) {
+                       TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
+                       void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
+                       const char* level1;
+                       while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
+
+                               TString level1Str(level1);
+                               if (level1Str == "." || level1Str == "..") {
+                                       continue;
+                               }
+
+                               if (fPathFilter.Level1Comprises(level1)) {
+                                       TString level1Dir = Form("%s/%s/%s",
+                                                       fBaseDirectory.Data(),level0,level1);
+
+                                       void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
+                                       const char* level2;
+                                       while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
+
+                                               TString level2Str(level2);
+                                               if (level2Str == "." || level2Str == "..") {
+                                                       continue;
+                                               }
+
+                                               if (fPathFilter.Level2Comprises(level2)) {
+                                                       TString dirName = Form("%s/%s/%s/%s",
+                                                                       fBaseDirectory.Data(),level0,level1,level2);
+
+                                                       void* dirPtr = gSystem->OpenDirectory(dirName);
+
+                                                       const char* filename;
+
+                                                       AliCDBRunRange aRunRange; // the runRange got from filename
+                                                       Int_t aVersion, aSubVersion; // the version and subVersion got from filename
+
+                                                       while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
+
+                                                               TString aString(filename);
+                                                               if (aString == "." || aString == "..") continue;
+
+                                                               if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
+                                                               AliCDBRunRange runrg(fRun,fRun);
+                                                               if (!aRunRange.Comprises(runrg)) continue;
+                                                               // aRunRange contains requested run!
+                                                               AliCDBPath validPath(level0,level1,level2);
+                                                               AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
+                                                               fValidFileIds.AddLast(validId.Clone());
+                                                       }
+                                                       gSystem->FreeDirectory(dirPtr);
+                                               }
+                                       }
+                                       gSystem->FreeDirectory(level1DirPtr);
+                               }
+                       }
+                       gSystem->FreeDirectory(level0DirPtr);
+               }
+       }
+       gSystem->FreeDirectory(storageDirPtr);
+
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
+// get last version found in the database valid for run and path
+
+       AliCDBPath aCDBPath(path);
+       if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
+               AliError(Form("Invalid path in request: %s", path));
+               return -1;
+       }
+
+       AliCDBId query(path, run, run, -1, -1);
+       AliCDBId dataId;
+
+       GetId(query,dataId);
+
+       return dataId.GetVersion();
+
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
+// get last version found in the database valid for run and path
+
+       AliCDBPath aCDBPath(path);
+       if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
+               AliError(Form("Invalid path in request: %s", path));
+               return -1;
+       }
+
+       AliCDBId query(path, run, run, version, -1);
+       AliCDBId dataId;
+
+       GetId(query,dataId);
+
+       return dataId.GetSubVersion();
+
+}
+
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                                             //
 // AliCDBLocal factory                                                                        //
@@ -696,15 +789,19 @@ AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
 ClassImp(AliCDBLocalParam)
 
 //_____________________________________________________________________________
-AliCDBLocalParam::AliCDBLocalParam() {
+AliCDBLocalParam::AliCDBLocalParam():
+ AliCDBParam(),
+ fDBPath()
+ {
 // default constructor
 
 }
 
 //_____________________________________________________________________________
 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
-fDBPath(dbPath)
-{      
+ AliCDBParam(),
+ fDBPath(dbPath)
+{
 // constructor
 
        SetType("local");