]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBGrid.cxx
Add DetectoreInReco and DetectorsInDAQ to AliDetectorTag class (and all dependencies)
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
index 1fcd7719e9a8d1be1d938bf273e576c86b411d3f..2ee6056d146dce8a249dc37f7695277a1627742f 100644 (file)
 //                                                                                             //
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
-
+#include <cstdlib>
 #include <TGrid.h>
 #include <TGridResult.h>
 #include <TFile.h>
 #include <TKey.h>
 #include <TROOT.h>
-//#include <TSystem.h>
+#include <TList.h>
 #include <TObjArray.h>
 #include <TObjString.h>
 #include <TRegexp.h>
 #include "AliLog.h"
 #include "AliCDBEntry.h"
 #include "AliCDBGrid.h"
+#include "AliCDBManager.h"
 
 
 ClassImp(AliCDBGrid)
 
 //_____________________________________________________________________________
-AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder, const char *se) :
+AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder,
+                       const char *se, const char* cacheFolder, Bool_t operateDisconnected,
+                      Long64_t cacheSize, Long_t cleanupInterval) :
 AliCDBStorage(),
 fGridUrl(gridUrl),
 fUser(user),
 fDBFolder(dbFolder),
-fSE(se)
+fSE(se),
+fCacheFolder(cacheFolder),
+fOperateDisconnected(operateDisconnected),
+fCacheSize(cacheSize),
+fCleanupInterval(cleanupInterval)
 {
 // constructor //
 
        // if the same Grid is alreay active, skip connection
-       if (!gGrid || fGridUrl != gGrid->GridUrl()  
+       if (!gGrid || fGridUrl != gGrid->GridUrl()
             || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
                // connection to the Grid
                AliInfo("Connection to the Grid...");
@@ -73,8 +80,11 @@ fSE(se)
        // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
        if(!gGrid->Cd(fDBFolder.Data(),0)){
                AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
-               if(!gGrid->Mkdir(fDBFolder.Data(),"",0)){
-                       AliError(Form("Cannot create folder <%s> !",fDBFolder.Data())); 
+               TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data()));
+               TString result = res->GetKey(0,"__result__");
+               if(result == "0"){
+                       AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data()));
+                       return;
                }
        } else {
                AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
@@ -87,6 +97,36 @@ fSE(se)
        fType="alien";
        fBaseFolder = fDBFolder;
 
+       // Setting the cache
+
+       // Check if local cache folder is already defined
+       TString origCache(TFile::GetCacheFileDir());
+       if(fCacheFolder.Length() > 0) {
+               if(origCache.Length() == 0) {
+                       AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data()));
+               } else if(fCacheFolder != origCache) {
+                       AliWarning(Form("Local cache folder was already defined, changing it to: %s",
+                                       fCacheFolder.Data()));
+               }
+
+               // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE
+               if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) {
+                       AliError(Form("Could not set cache folder %s !", fCacheFolder.Data()));
+                       fCacheFolder = "";
+               } else {
+                       // reset fCacheFolder because the function may have
+                       // slightly changed the folder name (e.g. '/' added)
+                       fCacheFolder = TFile::GetCacheFileDir();
+               }
+
+               // 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 !",
+                               fCacheSize, fCleanupInterval));
+               }
+       }
+
        // return to the initial directory
        gGrid->Cd(initDir.Data(),0);
 }
@@ -184,17 +224,20 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
 // prepare id (version) of the object that will be stored (called by PutEntry)
 
        TString initDir(gGrid->Pwd(0));
-       TString pathName= id.GetPath();
 
        TString dirName(fDBFolder);
 
        Bool_t dirExist=kFALSE;
 
+
+
        // go to the path; if directory does not exist, create it
-       TObjArray *arrName=pathName.Tokenize("/");
-       for(int i=0;i<arrName->GetEntries();i++){
-               TString buffer((arrName->At(i))->GetName());
-               dirName+=buffer; dirName+="/";
+       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) {
                        AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
@@ -203,9 +246,62 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
                                gGrid->Cd(initDir.Data());
                        return kFALSE;
                        }
+
+                       // if folders are new add tags to them
+                       if(i == 1) {
+                               // TODO Currently disabled
+                               // add short lived tag!
+                               // AliInfo("Tagging level 1 folder with \"ShortLived\" tag");
+                               // if(!AddTag(dirName,"ShortLived_try")){
+                               //      AliError(Form("Could not tag folder %s !", dirName.Data()));
+                               //      if(!gGrid->Rmdir(dirName.Data())){
+                               //              AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
+                               //      }
+                               //      return 0;
+                               //}
+
+                       } else if(i == 2) {
+                               AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
+                               if(!AddTag(dirName,"CDB")){
+                                       AliError(Form("Could not tag folder %s !", dirName.Data()));
+                                       if(!gGrid->Rmdir(dirName.Data())){
+                                               AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
+                                       }
+                                       return 0;
+                               }
+                               if(!AddTag(dirName,"CDB_MD")){
+                                       AliError(Form("Could not tag folder %s !", dirName.Data()));
+                                       if(!gGrid->Rmdir(dirName.Data())){
+                                               AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
+                                       }
+                                       return 0;
+                               }
+
+                               // TODO Currently disabled
+                               // add short lived tag!
+                               // TString path=id.GetPath();
+                               // if(AliCDBManager::Instance()->IsShortLived(path.Data())) {
+                               //      AliInfo(Form("Tagging %s as short lived", dirName.Data()));
+                               //      if(!TagShortLived(dirName, kTRUE)){
+                               //              AliError(Form("Could not tag folder %s !", dirName.Data()));
+                               //              if(!gGrid->Rmdir(dirName.Data())){
+                               //                      AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
+                               //              }
+                               //              return 0;
+                               //      }
+                               // } else {
+                               //      AliInfo(Form("Tagging %s as long lived", dirName.Data()));
+                               //      if(!TagShortLived(dirName, kFALSE)){
+                               //              AliError(Form("Could not tag folder %s !", dirName.Data()));
+                               //              if(!gGrid->Rmdir(dirName.Data())){
+                               //                      AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
+                               //              }
+                               //              return 0;
+                               //      }
+                               // }
+                       }
                }
        }
-       delete arrName;
        gGrid->Cd(initDir,0);
 
        TString filename;
@@ -227,6 +323,13 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
        }
        delete res;
 
+       // GRP entries with explicitly set version escape default incremental versioning
+       if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0)
+       {
+               AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data()));
+               return kFALSE;
+       }
+
        id.SetVersion(lastVersion + 1);
        id.SetSubVersion(0);
 
@@ -249,11 +352,8 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
 AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
 // look for the Id that matches query's requests (highest or exact version)
 
-       if(validFileIds.GetEntriesFast() < 1) {
+       if(validFileIds.GetEntriesFast() < 1)
                return NULL;
-       } else if (validFileIds.GetEntriesFast() == 1) {
-               return dynamic_cast<AliCDBId*> (validFileIds.At(0));
-       }
 
        TIter iter(&validFileIds);
 
@@ -268,7 +368,7 @@ AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query
                if (!query.HasVersion()){ // look for highest version
                        if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
                        if(result && result->GetVersion() == anIdPtr->GetVersion()) {
-                               AliDebug(2,Form("More than one object valid for run %d, version %d!",
+                               AliError(Form("More than one object valid for run %d, version %d!",
                                        query.GetFirstRun(), anIdPtr->GetVersion()));
                                return NULL;
                        }
@@ -276,7 +376,7 @@ AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query
                } else { // look for specified version
                        if(query.GetVersion() != anIdPtr->GetVersion()) continue;
                        if(result && result->GetVersion() == anIdPtr->GetVersion()){
-                               AliDebug(2,Form("More than one object valid for run %d, version %d!",
+                               AliError(Form("More than one object valid for run %d, version %d!",
                                        query.GetFirstRun(), anIdPtr->GetVersion()));
                                return NULL;
                        }
@@ -284,14 +384,16 @@ AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query
                }
 
        }
+       
+       if (!result) return NULL;
 
-
-       return result;
+       return dynamic_cast<AliCDBId*> (result->Clone());
 }
 
 //_____________________________________________________________________________
-AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
-// get AliCDBEntry from the database
+AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
+// get AliCDBId from the database
+// User must delete returned object
 
        AliCDBId* dataId=0;
 
@@ -308,45 +410,67 @@ AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
        if(selectedId.GetFirstRun() == fRun &&
                        fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
                // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
-               AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
+               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()));
                dataId = GetId(fValidFileIds, selectedId);
 
        } 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 and for path %s 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()));
 
                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());
+               TString optionQuery = "-y";
+               if(selectedId.GetVersion() >= 0) {
+                       pattern += Form("_v%d*",selectedId.GetVersion());
+                       optionQuery = "";
+               }
+
                pattern += ".root";
                AliDebug(2,Form("pattern: %s", pattern.Data()));
 
-               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
-               AliCDBId validFileId;
-               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());
+               if (optionQuery == "-y"){
+                       AliInfo("Only latest version will be returned");
                }
-               delete res;
+
+               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, optionQuery.Data());
+               if (res) {
+                       AliCDBId validFileId;
+                       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());
+                       }
+                       delete res;
+               }       
                dataId = GetId(validFileIds, selectedId);
        }
 
+       return dataId;
+}
+
+//_____________________________________________________________________________
+AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
+// get AliCDBEntry from the database
+
+       AliCDBId* dataId = GetEntryId(queryId);
+
        if (!dataId) return NULL;
 
        TString filename;
        if (!IdToFilename(*dataId, filename)) {
                AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
+               delete dataId;
                return NULL;
        }
 
        AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
 
+       delete dataId;
        return anEntry;
 }
 
@@ -357,7 +481,26 @@ AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
        AliDebug(2,Form("Opening file: %s",filename.Data()));
 
        filename.Prepend("/alien");
-       TFile *file = TFile::Open(filename);
+
+       // if option="CACHEREAD" TFile will use the local caching facility!
+       TString option="READ";
+       if(fCacheFolder != ""){
+
+               // Check if local cache folder was changed in the meanwhile
+               TString origCache(TFile::GetCacheFileDir());
+               if(fCacheFolder != origCache) {
+                       AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
+                                       fCacheFolder.Data(), origCache.Data()));
+                       TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
+                       TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
+               }
+
+               option.Prepend("CACHE");
+        }
+
+       AliDebug(2, Form("Option: %s", option.Data()));
+
+       TFile *file = TFile::Open(filename, option);
        if (!file) {
                AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
                return NULL;
@@ -433,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++){
@@ -476,7 +626,7 @@ TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
                } else {
                        dataId = GetId(validFileIds, thisId);
                }
-               if(dataId) selectedIds.Add(dataId->Clone());
+               if(dataId) selectedIds.Add(dataId);
        }
 
        delete iter; iter=0;
@@ -521,11 +671,6 @@ Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
                                        fDBFolder.Data(),
                                        id.GetPath().Data());
 
-       // add CDB and CDB_MD tag to folder
-       // TODO how to check that folder has already tags?
-       AddTag(folderToTag,"CDB");
-       AddTag(folderToTag,"CDB_MD");
-
        TDirectory* saveDir = gDirectory;
 
        TString fullFilename = Form("/alien%s", filename.Data());
@@ -555,9 +700,7 @@ Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
        file->Close(); delete file; file=0;
 
        if(result) {
-               AliInfo(Form("CDB object stored into file %s", filename.Data()));
-               AliInfo(Form("using S.E.: %s", fSE.Data()));
-
+       
                if(!TagFileId(filename, &id)){
                        AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
                        if(!gGrid->Rm(filename.Data()))
@@ -568,6 +711,8 @@ Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
                TagFileMetaData(filename, entry->GetMetaData());
        }
 
+       AliInfo(Form("CDB object stored into file %s", filename.Data()));
+       AliInfo(Form("Storage Element: %s", fSE.Data()));
        return result;
 }
 //_____________________________________________________________________________
@@ -592,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\"",
-                                       id->GetLevel0().Data(),
-                                       id->GetLevel1().Data(),
-                                       id->GetLevel2().Data());
-       TString addTagValue = Form("%s%s%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 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()));
@@ -615,7 +767,29 @@ Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
                                                filename.Data()));
                result = kFALSE;
        } else {
-               AliInfo("Object successfully tagged.");
+               AliDebug(2, "Object successfully tagged.");
+               result = kTRUE;
+       }
+       delete res;
+       return result;
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
+// tag folder with ShortLived tag
+
+       TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
+
+       Bool_t result = kFALSE;
+       AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
+       TGridResult* res = gGrid->Command(addTagValue.Data());
+       const char* resCode = res->GetKey(0,"__result__"); // '1' if success
+       if(resCode[0] != '1') {
+               AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
+               result = kFALSE;
+       } else {
+               AliDebug(2,"Object successfully tagged.");
                result = kTRUE;
        }
        delete res;
@@ -649,7 +823,7 @@ Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
                                                filename.Data()));
                result = kFALSE;
        } else {
-               AliInfo("Object successfully tagged.");
+               AliDebug(2,"Object successfully tagged.");
                result = kTRUE;
        }
        return result;
@@ -712,21 +886,44 @@ void AliCDBGrid::QueryValidFiles()
        MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
 
        TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data());
-       if(fVersion >= 0) pattern += Form("_v%d*", fVersion);
+       TString optionQuery = "-y";
+       if(fVersion >= 0) {
+               pattern += Form("_v%d*", fVersion);
+               optionQuery = "";
+       }
        pattern += ".root";
        AliDebug(2,Form("pattern: %s", pattern.Data()));
 
-       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
+       AliInfo(Form("fDBFolder = %s, pattern = %s, filter = %s",fDBFolder.Data(), pattern.Data(), filter.Data()));
 
-       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());
-               }
+       if (optionQuery == "-y"){
+               AliInfo("Only latest version will be returned");
+       } 
+
+       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, optionQuery.Data());  
+
+       if (!res) {
+               AliError("Grid query failed");
+               return;
+       }
+
+       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;
 
@@ -778,17 +975,14 @@ void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
 Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
 // get last version found in the database valid for run and path
 
-       TObjArray validFileIds;
-       validFileIds.SetOwner(1);
-
        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 = 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()
@@ -796,33 +990,60 @@ Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
                                        run, path));
                dataId = GetId(fValidFileIds, query);
                if (!dataId) return -1;
-               return dataId->GetVersion();
+               Int_t version = dataId->GetVersion();
+               delete dataId;
+               return version;
 
        }
        // List of files valid for reqested run was not loaded. Looking directly into CDB
        AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
                                run, path));
 
+       TObjArray validFileIds;
+       validFileIds.SetOwner(1);
+
        TString filter;
        MakeQueryFilter(run, run, 0, filter);
 
        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;
 
-       return dataId->GetVersion();
 
 }
 
@@ -846,10 +1067,6 @@ ClassImp(AliCDBGridFactory)
 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
 // check if the string is valid Grid URI
 
-       // pattern: alien://hostName:Port;user;dbPath;SE
-       // example of a valid pattern:
-       // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
-//        TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
         TRegexp gridPattern("^alien://.+$");
 
         return TString(gridString).Contains(gridPattern);
@@ -862,18 +1079,22 @@ AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
        if (!Validate(gridString)) {
                return NULL;
        }
-       //TString buffer(gridString + sizeof("alien://") - 1);
+
        TString buffer(gridString);
 
        TString gridUrl         = "alien://";
        TString user            = "";
-       TString dbFolder        = "DBGrid";
+       TString dbFolder        = "";
        TString se              = "default";
+       TString cacheFolder     = "";
+       Bool_t  operateDisconnected = kTRUE;
+       Long64_t cacheSize          = (UInt_t) 1024*1024*1024; // 1GB
+       Long_t cleanupInterval      = 0;
 
        TObjArray *arr = buffer.Tokenize('?');
        TIter iter(arr);
        TObjString *str = 0;
-       
+
        while((str = (TObjString*) iter.Next())){
                TString entry(str->String());
                Int_t indeq = entry.Index('=');
@@ -892,28 +1113,73 @@ AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
 
                if(key.Contains("grid",TString::kIgnoreCase)) {
                        gridUrl += value;
-               } 
+               }
                else if (key.Contains("user",TString::kIgnoreCase)){
                        user = value;
                }
+               else if (key.Contains("se",TString::kIgnoreCase)){
+                       se = value;
+               }
+               else if (key.Contains("cacheF",TString::kIgnoreCase)){
+                       cacheFolder = value;
+                       if (!cacheFolder.EndsWith("/"))
+                               cacheFolder += "/";
+               }
                else if (key.Contains("folder",TString::kIgnoreCase)){
                        dbFolder = value;
                }
-               else if (key.Contains("se",TString::kIgnoreCase)){
-                       se = value;
+               else if (key.Contains("operateDisc",TString::kIgnoreCase)){
+                       if(value == "kTRUE") {
+                               operateDisconnected = kTRUE;
+                       } else if (value == "kFALSE") {
+                               operateDisconnected = kFALSE;
+                       } else if (value == "0" || value == "1") {
+                               operateDisconnected = (Bool_t) value.Atoi();
+                       } else {
+                               AliError(Form("Invalid entry! %s",entry.Data()));
+                               return NULL;
+                       }
+               }
+               else if (key.Contains("cacheS",TString::kIgnoreCase)){
+                       if(value.IsDigit()) {
+                               cacheSize = value.Atoi();
+                       } else {
+                               AliError(Form("Invalid entry! %s",entry.Data()));
+                               return NULL;
+                       }
+               }
+               else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
+                       if(value.IsDigit()) {
+                               cleanupInterval = value.Atoi();
+                       } else {
+                               AliError(Form("Invalid entry! %s",entry.Data()));
+                               return NULL;
+                       }
                }
                else{
                        AliError(Form("Invalid entry! %s",entry.Data()));
+                       return NULL;
                }
        }
        delete arr; arr=0;
-               
-       AliDebug(2, Form("gridUrl:      %s",gridUrl.Data()));
-       AliDebug(2, Form("user: %s",user.Data()));
-       AliDebug(2, Form("dbFolder:     %s",dbFolder.Data()));
-       AliDebug(2, Form("s.e.: %s",se.Data()));
 
-       return new AliCDBGridParam(gridUrl.Data(), user.Data(), dbFolder.Data(), se.Data());
+       AliDebug(2, Form("gridUrl:      %s", gridUrl.Data()));
+       AliDebug(2, Form("user: %s", user.Data()));
+       AliDebug(2, Form("dbFolder:     %s", dbFolder.Data()));
+       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));
+
+       if(dbFolder == ""){
+               AliError("Base folder must be specified!");
+               return NULL;
+       }
+
+       return new AliCDBGridParam(gridUrl.Data(), user.Data(),
+                         dbFolder.Data(), se.Data(), cacheFolder.Data(),
+                         operateDisconnected, cacheSize, cleanupInterval);
 }
 
 //_____________________________________________________________________________
@@ -927,7 +1193,11 @@ AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
                grid = new AliCDBGrid(gridParam->GridUrl().Data(),
                                      gridParam->GetUser().Data(),
                                      gridParam->GetDBFolder().Data(),
-                                     gridParam->GetSE().Data());
+                                     gridParam->GetSE().Data(),
+                                     gridParam->GetCacheFolder().Data(),
+                                     gridParam->GetOperateDisconnected(),
+                                     gridParam->GetCacheSize(),
+                                     gridParam->GetCleanupInterval());
 
        }
 
@@ -940,7 +1210,7 @@ AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                                             //
-// AliCDBGrid Parameter class                                                                         //                                          //
+// AliCDBGrid Parameter class                                                                         //                                         //
 //                                                                                             //
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -952,30 +1222,40 @@ AliCDBGridParam::AliCDBGridParam():
  fGridUrl(),
  fUser(),
  fDBFolder(),
- fSE()
+ fSE(),
+ fCacheFolder(),
+ fOperateDisconnected(),
+ fCacheSize(),
+ fCleanupInterval()
+
  {
 // default constructor
 
 }
 
 //_____________________________________________________________________________
-AliCDBGridParam::AliCDBGridParam(const char* gridUrl, 
-                               const char* user,
-                               const char* dbFolder, 
-                               const char* se):
+AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
+                               const char* se, const char* cacheFolder, Bool_t operateDisconnected,
+                               Long64_t cacheSize, Long_t cleanupInterval):
  AliCDBParam(),
  fGridUrl(gridUrl),
  fUser(user),
  fDBFolder(dbFolder),
- fSE(se)
+ fSE(se),
+ fCacheFolder(cacheFolder),
+ fOperateDisconnected(operateDisconnected),
+ fCacheSize(cacheSize),
+ fCleanupInterval(cleanupInterval)
 {
 // constructor
-       
+
        SetType("alien");
 
-       TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s",
+       TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
+                       "?OperateDisconnected=%d?CacheSize=%d?CleanupInterval=%d",
                        fGridUrl.Data(), fUser.Data(),
-                       fDBFolder.Data(), fSE.Data());
+                       fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
+                       fOperateDisconnected, fCacheSize, fCleanupInterval);
 
        SetURI(uri.Data());
 }
@@ -991,14 +1271,15 @@ AliCDBParam* AliCDBGridParam::CloneParam() const {
 // clone parameter
 
         return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
-                                       fDBFolder.Data(), fSE.Data());
+                                       fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
+                                       fOperateDisconnected, fCacheSize, fCleanupInterval);
 }
 
 //_____________________________________________________________________________
 ULong_t AliCDBGridParam::Hash() const {
 // return Hash function
 
-        return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
+        return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
 }
 
 //_____________________________________________________________________________
@@ -1019,6 +1300,10 @@ Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
         if(fUser != other->fUser) return kFALSE;
         if(fDBFolder != other->fDBFolder) return kFALSE;
         if(fSE != other->fSE) return kFALSE;
+        if(fCacheFolder != other->fCacheFolder) return kFALSE;
+        if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
+        if(fCacheSize != other->fCacheSize) return kFALSE;
+        if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
        return kTRUE;
 }