+//_____________________________________________________________________________
+void AliCDBGrid::QueryValidFiles()
+{
+// Query the CDB for files valid for AliCDBStorage::fRun
+// fills list fValidFileIds with AliCDBId objects created from file name
+
+ TString filter;
+ MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
+
+ TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data());
+ if(fVersion >= 0) pattern += Form("_v%d*", fVersion);
+ 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;
+ AliDebug(2,Form("Found valid file: %s", filename.Data()));
+ Bool_t result = FilenameToId(filename, validFileId);
+ if(result) {
+ fValidFileIds.AddLast(validFileId.Clone());
+ }
+ }
+ delete res;
+
+}
+
+//_____________________________________________________________________________
+void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
+ const AliCDBMetaData* md, TString& result) const
+{
+// create filter for file query
+
+ result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
+
+// if(version >= 0) {
+// result += Form(" and CDB:version=%d", version);
+// }
+// if(pathFilter.GetLevel0() != "*") {
+// result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
+// }
+// if(pathFilter.GetLevel1() != "*") {
+// result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
+// }
+// if(pathFilter.GetLevel2() != "*") {
+// result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
+// }
+
+ if(md){
+ if(md->GetObjectClassName()[0] != '\0') {
+ result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
+ }
+ if(md->GetResponsible()[0] != '\0') {
+ result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
+ }
+ if(md->GetBeamPeriod() != 0) {
+ result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
+ }
+ if(md->GetAliRootVersion()[0] != '\0') {
+ result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
+ }
+ if(md->GetComment()[0] != '\0') {
+ result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
+ }
+ }
+ AliDebug(2, Form("filter: %s",result.Data()));
+
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBGrid::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 = 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()
+ AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
+ run, path));
+ dataId = GetId(fValidFileIds, query);
+ if (!dataId) return -1;
+ 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, "");
+ 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, query);
+ if (!dataId) return -1;
+
+ Int_t version = dataId->GetVersion();
+ delete dataId;
+ return version;
+
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
+// get last subversion found in the database valid for run and path
+ AliError("Objects in GRID storage have no sub version!");
+ return -1;
+}
+
+