Implementation of the local caching of the OCDB set required some upgrade of the...
[u/mrichter/AliRoot.git] / STEER / AliCDBDump.cxx
index c90dffe..49b97af 100644 (file)
@@ -46,6 +46,9 @@ fFile(NULL), fReadOnly(readOnly) {
                AliDebug(2,Form("File <%s> opened",dbFile));
                if(fReadOnly) AliDebug(2,Form("in read-only mode"));
        }
+
+       fType="dump";
+       fBaseFolder = dbFile;
 }
 
 //_____________________________________________________________________________
@@ -233,9 +236,9 @@ Bool_t AliCDBDump::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));
        }
 
@@ -248,9 +251,119 @@ Bool_t AliCDBDump::PrepareId(AliCDBId& id) {
 
 }
 
+// //_____________________________________________________________________________
+// Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
+// // look for filename matching query (called by GetEntry)
+// 
+// 
+//         AliCDBRunRange aRunRange; // the runRange got from filename
+//         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
+// 
+//     TIter iter(gDirectory->GetListOfKeys());
+//     TKey* key;
+// 
+//     if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
+// 
+//                 while ((key = (TKey*) iter.Next())) { // loop on the keys
+// 
+//                     if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
+//                         // aRunRange, aVersion, aSubVersion filled from filename
+// 
+//                     if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
+//                     // aRunRange contains requested run!
+// 
+//                     if (result.GetVersion() < aVersion) {
+//                             result.SetVersion(aVersion);
+//                             result.SetSubVersion(aSubVersion);
+// 
+//                             result.SetFirstRun(
+//                                     aRunRange.GetFirstRun());
+//                             result.SetLastRun(
+//                                     aRunRange.GetLastRun());
+// 
+//                     } else if (result.GetVersion() == aVersion
+//                             && result.GetSubVersion()
+//                                     < aSubVersion) {
+// 
+//                             result.SetSubVersion(aSubVersion);
+// 
+//                             result.SetFirstRun(
+//                                     aRunRange.GetFirstRun());
+//                             result.SetLastRun(
+//                                     aRunRange.GetLastRun());
+//                     } else if (result.GetVersion() == aVersion
+//                             && result.GetSubVersion() == aSubVersion){
+//                                     AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
+//                                     query.GetFirstRun(), aVersion, aSubVersion));
+//                             result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
+//                             return kFALSE;
+//                             }
+//             }
+// 
+//     } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
+// 
+//             result.SetVersion(query.GetVersion());
+// 
+//                 while ((key = (TKey*) iter.Next())) { // loop on the keys
+// 
+//                     if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
+//                         // aRunRange, aVersion, aSubVersion filled from filename
+// 
+//                        if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
+//                     // aRunRange contains requested run!
+//                     
+//                     if(query.GetVersion() != aVersion) continue;
+//                     // aVersion is requested version!
+// 
+//                     if(result.GetSubVersion() == aSubVersion){
+//                                     AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
+//                                     query.GetFirstRun(), aVersion, aSubVersion));
+//                             result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
+//                             return kFALSE;
+//                     }
+//                     if( result.GetSubVersion() < aSubVersion) {
+// 
+//                                 result.SetSubVersion(aSubVersion);
+// 
+//                                 result.SetFirstRun(
+//                                     aRunRange.GetFirstRun());
+//                                 result.SetLastRun(
+//                                     aRunRange.GetLastRun());
+//                     }
+//                 }
+// 
+//     } else { // both version and subversion specified
+// 
+//                 while ((key = (TKey*) iter.Next())) { // loop on the keys
+// 
+//                     if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
+//                         // aRunRange, aVersion, aSubVersion filled from filename
+// 
+//                     if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
+//                     // aRunRange contains requested run!
+// 
+//                     if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
+//                     // aVersion and aSubVersion are requested version and subVersion!
+// 
+//                     if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
+//                                     AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
+//                                     query.GetFirstRun(), aVersion, aSubVersion));
+//                             result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
+//                             return kFALSE; 
+//                     }
+//                     result.SetVersion(aVersion);
+//                     result.SetSubVersion(aSubVersion);
+//                     result.SetFirstRun(aRunRange.GetFirstRun());
+//                     result.SetLastRun(aRunRange.GetLastRun());
+// 
+//             }
+//     }
+// 
+//     return kTRUE;
+// }
 
 //_____________________________________________________________________________
-Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
+AliCDBId* AliCDBDump::GetId(const AliCDBId& query) {
 // look for filename matching query (called by GetEntry)
 
 
@@ -260,8 +373,11 @@ Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
        TIter iter(gDirectory->GetListOfKeys());
        TKey* key;
 
+       AliCDBId* result = new AliCDBId();
+       result->SetPath(query.GetPath());
+
        if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
-               
+
                 while ((key = (TKey*) iter.Next())) { // loop on the keys
                        
                        if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
@@ -269,41 +385,41 @@ Bool_t AliCDBDump::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);
 
-                               result.SetFirstRun(
+                       if (result->GetVersion() < aVersion) {
+                               result->SetVersion(aVersion);
+                               result->SetSubVersion(aSubVersion);
+
+                               result->SetFirstRun(
                                        aRunRange.GetFirstRun());
-                               result.SetLastRun(
+                               result->SetLastRun(
                                        aRunRange.GetLastRun());
 
-                       } else if (result.GetVersion() == aVersion 
-                               && result.GetSubVersion() 
+                       } else if (result->GetVersion() == aVersion
+                               && result->GetSubVersion()
                                        < aSubVersion) {
 
-                               result.SetSubVersion(aSubVersion);
+                               result->SetSubVersion(aSubVersion);
 
-                               result.SetFirstRun(
+                               result->SetFirstRun(
                                        aRunRange.GetFirstRun());
-                               result.SetLastRun(
+                               result->SetLastRun(
                                        aRunRange.GetLastRun());
-                       } else if (result.GetVersion() == aVersion
-                               && result.GetSubVersion() == aSubVersion){
-                               AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
+                       } else if (result->GetVersion() == aVersion
+                               && result->GetSubVersion() == aSubVersion){
+                               AliError(Form("More than one object valid for run %d, version %d_%d!",
                                        query.GetFirstRun(), aVersion, aSubVersion));
-                               result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
-                               return kFALSE; 
+                               delete result;
+                               return NULL;
                                }
                }
-               
+
        } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
 
-               result.SetVersion(query.GetVersion());
+               result->SetVersion(query.GetVersion());
 
                 while ((key = (TKey*) iter.Next())) { // loop on the keys
-                       
+
                        if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
                         // aRunRange, aVersion, aSubVersion filled from filename
 
@@ -312,20 +428,20 @@ Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
                        
                        if(query.GetVersion() != aVersion) continue;
                        // aVersion is requested version!
-                       
-                       if(result.GetSubVersion() == aSubVersion){
-                               AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
+
+                       if(result->GetSubVersion() == aSubVersion){
+                               AliError(Form("More than one object valid for run %d, version %d_%d!",
                                        query.GetFirstRun(), aVersion, aSubVersion));
-                               result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
-                               return kFALSE; 
+                               delete result;
+                               return NULL;
                        }
-                       if( result.GetSubVersion() < aSubVersion) {
+                       if( result->GetSubVersion() < aSubVersion) {
 
-                                result.SetSubVersion(aSubVersion);
+                                result->SetSubVersion(aSubVersion);
 
-                                result.SetFirstRun(
+                                result->SetFirstRun(
                                        aRunRange.GetFirstRun());
-                                result.SetLastRun(
+                                result->SetLastRun(
                                        aRunRange.GetLastRun());
                        } 
                 }
@@ -336,28 +452,28 @@ Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
                        
                        if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
                         // aRunRange, aVersion, aSubVersion filled from filename
-                       
+
                        if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
                        // aRunRange contains requested run!
 
-                       if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue; 
+                       if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
                        // aVersion and aSubVersion are requested version and subVersion!
 
-                       if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
-                               AliDebug(2,Form("More than one object valid for run %d, version %d_%d!", 
+                       if(result->GetVersion() == aVersion && result->GetSubVersion() == aSubVersion){
+                               AliError(Form("More than one object valid for run %d, version %d_%d!",
                                        query.GetFirstRun(), aVersion, aSubVersion));
-                               result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
-                               return kFALSE; 
+                               delete result;
+                               return NULL;
                        }
-                       result.SetVersion(aVersion);
-                       result.SetSubVersion(aSubVersion);
-                       result.SetFirstRun(aRunRange.GetFirstRun());
-                       result.SetLastRun(aRunRange.GetLastRun());
+                       result->SetVersion(aVersion);
+                       result->SetSubVersion(aSubVersion);
+                       result->SetFirstRun(aRunRange.GetFirstRun());
+                       result->SetLastRun(aRunRange.GetLastRun());
                        
                }
        }
 
-       return kTRUE;
+       return result;
 }
 
 //_____________________________________________________________________________
@@ -375,51 +491,80 @@ AliCDBEntry* AliCDBDump::GetEntry(const AliCDBId& queryId) {
                 return NULL;
         }
 
-       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
-               AliCDBId selectedId(queryId);
-               GetSelection(&selectedId);
-               result = GetId(selectedId, dataId);
-       } else {
-               result = GetId(queryId, dataId);
-       }
+       AliCDBId *dataId = GetEntryId(queryId);
 
-       if (!result || !dataId.IsSpecified()) {
+       if (!dataId || !dataId->IsSpecified()) {
+               if(dataId) delete dataId;
                return NULL;
        }
-       
+
        TString keyname;
-       if (!IdToKeyName(dataId.GetAliCDBRunRange(), dataId.GetVersion(), 
-               dataId.GetSubVersion(), keyname)) {
+       if (!IdToKeyName(dataId->GetAliCDBRunRange(), dataId->GetVersion(),
+               dataId->GetSubVersion(), keyname)) {
                AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
+               delete dataId;
                return NULL;
        }
 
        // get the only AliCDBEntry object from the file
-       // the object in the file is an AliCDBEntry entry named keyname 
+       // the object in the file is an AliCDBEntry entry named keyname
        // keyName = Run#firstRun_#lastRun_v#version_s#subVersion
 
        TObject* anObject = gDirectory->Get(keyname);
        if (!anObject) {
                AliDebug(2,Form("Bad storage data: NULL entry object!"));
+               delete dataId;
                return NULL;
        } 
 
        if (AliCDBEntry::Class() != anObject->IsA()) {
                AliDebug(2,Form("Bad storage data: Invalid entry object!"));
+               delete dataId;
                return NULL;
        }
 
        ((AliCDBEntry*) anObject)->SetLastStorage("dump");
-       
+
+       delete dataId;
        return (AliCDBEntry*) anObject;
 }
 
 //_____________________________________________________________________________
+AliCDBId* AliCDBDump::GetEntryId(const AliCDBId& queryId) {
+// get AliCDBEntry from the database
+
+       TDirectory::TContext context(gDirectory, fFile);
+
+       if (!(fFile && fFile->IsOpen())) {
+                AliError("AliCDBDump storage is not initialized properly");
+                return NULL;
+        }
+
+        if (!gDirectory->cd(queryId.GetPath())) {
+                return NULL;
+        }
+
+       AliCDBId* dataId = 0;
+
+       // 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
+               AliCDBId selectedId(queryId);
+               GetSelection(&selectedId);
+               dataId = GetId(queryId);
+       } else {
+               dataId = GetId(queryId);
+       }
+
+       if (dataId && !dataId->IsSpecified()) {
+               delete dataId;
+               return NULL;
+       }
+
+       return dataId;
+}
+
+//_____________________________________________________________________________
 void AliCDBDump::GetEntriesForLevel0(const AliCDBId& queryId, TList* result) {
 // multiple request (AliCDBStorage::GetAll)
 
@@ -429,7 +574,7 @@ void AliCDBDump::GetEntriesForLevel0(const AliCDBId& queryId, TList* result) {
        TKey* key;
 
        while ((key = (TKey*) iter.Next())) {
-               
+
                TString keyNameStr(key->GetName());
                if (queryId.GetAliCDBPath().Level1Comprises(keyNameStr)) {
                        gDirectory->cd(keyNameStr);
@@ -542,7 +687,7 @@ Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry) {
        // write object (key name: Run#firstRun_#lastRun_v#version_s#subVersion)
        Bool_t result = gDirectory->WriteTObject(entry, keyname);
        if (!result) {
-               AliError(Form("Can't write entry to file: %s", 
+               AliError(Form("Can't write entry to file: %s",
                                fFile->GetName()));
        }
 
@@ -600,6 +745,66 @@ Bool_t AliCDBDump::Contains(const char* path) const{
 
 }
 
+//_____________________________________________________________________________
+void AliCDBDump::QueryValidFiles()
+{
+// Query the CDB for files valid for AliCDBStorage::fRun
+// fills list fValidFileIds with AliCDBId objects created from file name
+
+       AliError("Not yet (and maybe never) implemented");
+}
+
+//_____________________________________________________________________________
+Bool_t AliCDBDump::IdToFilename(const AliCDBId& /*id*/, TString& /*filename*/) const {
+// build file name from AliCDBId (path, run range, version) and fDBFolder
+
+       AliError("Not implemented");
+        return kFALSE;
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBDump::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);
+
+       if(!dataId) return -1;
+       Int_t version = dataId->GetVersion();
+
+       delete dataId;
+       return version;
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBDump::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);
+
+       if(!dataId) return -1;
+
+       Int_t subVersion = dataId->GetSubVersion();
+
+       delete dataId;
+       return subVersion;
+
+}
+
+
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                                             //
 // AliCDBDump factory                                                                                 //
@@ -654,7 +859,7 @@ AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
                 const AliCDBDumpParam* dumpParam = 
                        (const AliCDBDumpParam*) param;
 
-                return new AliCDBDump(dumpParam->GetPath(), 
+                return new AliCDBDump(dumpParam->GetPath(),
                                dumpParam->IsReadOnly());
         }
 
@@ -670,7 +875,9 @@ AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
 ClassImp(AliCDBDumpParam)
 
 //_____________________________________________________________________________
-AliCDBDumpParam::AliCDBDumpParam() {
+AliCDBDumpParam::AliCDBDumpParam():
+fDBPath(), fReadOnly(kFALSE)
+{
 // default constructor
 
 }