]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBGrid.cxx
coverity 10161
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
index c91ca2abc9e2e5459a45c3e0e4be2e5820782e2a..ac11594a14ef6639f27987c3b0a841b5ca51fadf 100644 (file)
@@ -61,7 +61,7 @@ fCleanupInterval(cleanupInterval)
                // connection to the Grid
                AliInfo("Connection to the Grid...");
                if(gGrid){
-                       AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
+                       AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
                        AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
                }
                TGrid::Connect(fGridUrl.Data(),fUser.Data());
@@ -122,7 +122,7 @@ fCleanupInterval(cleanupInterval)
                // default settings are: cacheSize=1GB, cleanupInterval = 0
                if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) {
                        AliError(Form("Could not set following values "
-                               "to ShrinkCacheFileDir: cacheSize = %d, cleanupInterval = %d !",
+                               "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !",
                                fCacheSize, fCleanupInterval));
                }
        }
@@ -233,6 +233,10 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
 
        // go to the path; if directory does not exist, create it
        for(int i=0;i<3;i++){
+               //TString cmd("find -d ");
+               //cmd += Form("%s ",dirName);
+               //cmd += 
+               //gGrid->Command(cmd.Data());
                dirName+=Form("%s/",id.GetPathLevel(i).Data());
                dirExist=gGrid->Cd(dirName,0);
                if (!dirExist) {
@@ -403,8 +407,8 @@ AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
        validFileIds.SetOwner(1);
 
        // look for file matching query requests (path, runRange, version)
-       if(selectedId.GetFirstRun() == fRun &&
-                       fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
+       if(selectedId.GetFirstRun() == fRun && fPathFilter.Comprises(selectedId.GetAliCDBPath()) &&
+                       fVersion == selectedId.GetVersion() && !fMetaDataFilter){
                // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
                AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!",
                                        selectedId.GetFirstRun(), selectedId.GetPath().Data()));
@@ -412,18 +416,27 @@ AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
 
        } else {
                // List of files valid for reqested run was not loaded. Looking directly into CDB
-               AliDebug(2, Form("List of files valid for run %d was not loaded. Looking directly into CDB for fileids valid for path %s!",
-                                       selectedId.GetFirstRun(), selectedId.GetPath().Data()));
+               AliDebug(2, Form("List of files valid for run %d and version %d was not loaded. Looking directly into CDB for fileids valid for path %s!",
+                                       selectedId.GetFirstRun(), selectedId.GetVersion(), selectedId.GetPath().Data()));
 
                TString filter;
                MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
 
-               TString pattern = Form("%s/Run*", selectedId.GetPath().Data());
-               if(selectedId.GetVersion() >= 0) pattern += Form("_v%d*",selectedId.GetVersion());
-               pattern += ".root";
-               AliDebug(2,Form("pattern: %s", pattern.Data()));
+               TString pattern = ".root";
+               TString optionQuery = "-y -m";
+               if(selectedId.GetVersion() >= 0) {
+                       pattern.Prepend(Form("_v%d_s0",selectedId.GetVersion()));
+                       optionQuery = "";
+               }
+
+               TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),selectedId.GetPath().Data()));
 
-               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
+               if (optionQuery.Contains("-y")){
+                       AliInfo("Only latest version will be returned");
+               }
+
+               AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
+               TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
                if (res) {
                        AliCDBId validFileId;
                        for(int i=0; i<res->GetEntries(); i++){
@@ -563,7 +576,14 @@ TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
                TString pattern = Form("%s/Run*.root", queryId.GetPath().Data());
                AliDebug(2,Form("pattern: %s", pattern.Data()));
 
-               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
+               TString optionQuery("-y");
+               if(queryId.GetVersion() >= 0) optionQuery = "";
+
+               if (optionQuery == "-y"){
+                       AliInfo("Only latest version will be returned");
+               }
+
+               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, optionQuery.Data());
 
                AliCDBId validFileId;
                for(int i=0; i<res->GetEntries(); i++){
@@ -717,19 +737,26 @@ Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
 // tag stored object in CDB table using object Id's parameters
 
+
+        TString dirname(filename);
+       Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
+       
        TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
        TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
                                        id->GetFirstRun(),
                                        id->GetLastRun(),
                                        id->GetVersion());
-       TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
+       TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
                                        id->GetPathLevel(0).Data(),
                                        id->GetPathLevel(1).Data(),
                                        id->GetPathLevel(2).Data());
-       TString addTagValue = Form("%s%s%s",
+       //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber); 
+       TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber); 
+       TString addTagValue = Form("%s%s%s%s",
                                        addTagValue1.Data(),
                                        addTagValue2.Data(),
-                                       addTagValue3.Data());
+                                       addTagValue3.Data(),
+                                       addTagValue4.Data());
 
        Bool_t result = kFALSE;
        AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
@@ -858,27 +885,87 @@ void AliCDBGrid::QueryValidFiles()
        TString filter;
        MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
 
-       TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data());
-       if(fVersion >= 0) pattern += Form("_v%d*", fVersion);
+       TString path = fPathFilter.GetPath();
+
+       TString pattern = "Run*";
+       TString optionQuery = "-y";
+       if(fVersion >= 0) {
+               pattern += Form("_v%d_s0", fVersion);
+               optionQuery = "";
+       }
        pattern += ".root";
        AliDebug(2,Form("pattern: %s", pattern.Data()));
 
-       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
+       TString tempPattern = pattern;
+       TString addFolder = "";
+       if (!path.Contains("*")){
+               if (!path.BeginsWith("/")) addFolder += "/";
+               addFolder += path;
+       }
+       else{
+               if (path.BeginsWith("/")) path.Remove(0,1);
+               if (path.EndsWith("/")) path.Remove(path.Length()-1,1); 
+               TObjArray* tokenArr = path.Tokenize("/");
+               if (tokenArr->GetEntries() != 3) {
+                       AliError("Not a 3 level path! Keeping old query...");
+                       tempPattern = pattern.Prepend(path+"/");
+               }
+               else{
+                       TString str0 = ((TObjString*)tokenArr->At(0))->String();
+                       TString str1 = ((TObjString*)tokenArr->At(1))->String();
+                       TString str2 = ((TObjString*)tokenArr->At(2))->String();
+                       if (str0 != "*" && str1 != "*" && str2 == "*"){
+                               // e.g. "ITS/Calib/*"
+                               addFolder = "/"+str0+"/"+str1;
+                       }
+                       else if (str0 != "*" && str1 == "*" && str2 == "*"){    
+                               // e.g. "ITS/*/*"
+                               addFolder = "/"+str0;
+                       }
+                       else if (str0 == "*" && str1 == "*" && str2 == "*"){    
+                               // e.g. "*/*/*"
+                               // do nothing: addFolder is already an empty string;
+                       }
+                       else{
+                               // e.g. "ITS/*/RecoParam"
+                               tempPattern = pattern.Prepend(path+"/");
+                       }
+               }
+               delete tokenArr; tokenArr=0;
+       }
+
+       TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
+
+       AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), tempPattern.Data(), filter.Data()));
+
+       if (optionQuery == "-y"){
+               AliInfo("Only latest version will be returned");
+       } 
+
+       TGridResult *res = gGrid->Query(folderCopy, tempPattern, filter, optionQuery.Data());  
 
        if (!res) {
                AliError("Grid query failed");
                return;
        }
 
-       AliCDBId validFileId;
-       for(int i=0; i<res->GetEntries(); i++){
-               TString filename = res->GetKey(i, "lfn");
-               if(filename == "") continue;
-               AliDebug(2,Form("Found valid file: %s", filename.Data()));
-               Bool_t result = FilenameToId(filename, validFileId);
-               if(result) {
-                       fValidFileIds.AddLast(validFileId.Clone());
-               }
+       TIter next(res);
+       TMap *map;
+       while ((map = (TMap*)next())) {
+         TObjString *entry;
+         if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
+           TString& filename = entry->String();
+           if(filename.IsNull()) continue;
+           AliDebug(2,Form("Found valid file: %s", filename.Data()));
+           AliCDBId *validFileId = new AliCDBId;
+           Bool_t result = FilenameToId(filename, *validFileId);
+           if(result) {
+             fValidFileIds.AddLast(validFileId);
+           }
+           else {
+             delete validFileId;
+           }
+         }
        }
        delete res;
 
@@ -936,8 +1023,8 @@ Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
                return -1;
        }
        AliCDBId query(path, run, run, -1, -1);
-       AliCDBId* dataId = 0;
 
+       AliCDBId* dataId = 0;
        // look for file matching query requests (path, runRange, version)
        if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
                // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
@@ -963,22 +1050,42 @@ Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
        TString pattern = Form("%s/Run*.root", path);
        AliDebug(2,Form("pattern: %s", pattern.Data()));
 
-       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
+       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "-y");
        AliCDBId validFileId;
-       for(int i=0; i<res->GetEntries(); i++){
-               TString filename = res->GetKey(i, "lfn");
-               if(filename == "") continue;
-               if(FilenameToId(filename, validFileId))
+       if (res->GetEntries()>1){
+               AliWarning("Number of found entries >1, even if option -y was used");
+               for(int i=0; i<res->GetEntries(); i++){
+                       TString filename = res->GetKey(i, "lfn");
+                       if(filename == "") continue;
+                       if(FilenameToId(filename, validFileId))
                                validFileIds.AddLast(validFileId.Clone());
+               }
+               dataId = GetId(validFileIds, query);
+               if (!dataId) return -1;
+               
+               Int_t version = dataId->GetVersion();
+               delete dataId;
+               return version;
+       }
+       else if (res->GetEntries()==1){
+               TString filename = res->GetKey(0, "lfn");
+               if(filename == "") {
+                       AliError("The only entry found has filename empty");
+                       return -1;
+               }
+               if(FilenameToId(filename, validFileId)) return validFileId.GetVersion();
+               else{
+                       AliError("Impossible to get FileId from filename");
+                       return -1;
+               }
+       }
+       else {
+               AliError("No entries found");
+               return -1;
        }
-       delete res;
 
-       dataId = GetId(validFileIds, query);
-       if (!dataId) return -1;
+       delete res;
 
-       Int_t version = dataId->GetVersion();
-       delete dataId;
-       return version;
 
 }
 
@@ -1057,7 +1164,7 @@ AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
                }
                else if (key.Contains("cacheF",TString::kIgnoreCase)){
                        cacheFolder = value;
-                       if (!cacheFolder.EndsWith("/"))
+                       if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
                                cacheFolder += "/";
                }
                else if (key.Contains("folder",TString::kIgnoreCase)){
@@ -1104,8 +1211,8 @@ AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
        AliDebug(2, Form("s.e.: %s", se.Data()));
        AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
        AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
-       AliDebug(2, Form("local cache size: %d", cacheSize));
-       AliDebug(2, Form("local cache cleanup interval: %d", cleanupInterval));
+       AliDebug(2, Form("local cache size: %lld", cacheSize));
+       AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
 
        if(dbFolder == ""){
                AliError("Base folder must be specified!");
@@ -1187,7 +1294,7 @@ AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const ch
        SetType("alien");
 
        TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
-                       "?OperateDisconnected=%d?CacheSize=%d?CleanupInterval=%d",
+                       "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
                        fGridUrl.Data(), fUser.Data(),
                        fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
                        fOperateDisconnected, fCacheSize, fCleanupInterval);