]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCDBGrid.cxx
adding else statement in case the grid query returns no result
[u/mrichter/AliRoot.git] / STEER / CDB / AliCDBGrid.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /////////////////////////////////////////////////////////////////////////////////////////////////
17 //                                                                                             //
18 // AliCDBGrid                                                                                  //
19 // access class to a DataBase in an AliEn storage                                              //
20 //                                                                                             //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
22
23 #include <cstdlib>
24 #include <TGrid.h>
25 #include <TGridResult.h>
26 #include <TFile.h>
27 #include <TKey.h>
28 #include <TROOT.h>
29 #include <TList.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
32 #include <TMath.h>
33 #include <TRegexp.h>
34
35 #include "AliLog.h"
36 #include "AliCDBEntry.h"
37 #include "AliCDBGrid.h"
38 #include "AliCDBManager.h"
39
40
41 ClassImp(AliCDBGrid)
42
43 //_____________________________________________________________________________
44 AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder,
45                        const char *se, const char* cacheFolder, Bool_t operateDisconnected,
46                        Long64_t cacheSize, Long_t cleanupInterval) :
47 AliCDBStorage(),
48 fGridUrl(gridUrl),
49 fUser(user),
50 fDBFolder(dbFolder),
51 fSE(se),
52 fMirrorSEs(""),
53 fCacheFolder(cacheFolder),
54 fOperateDisconnected(operateDisconnected),
55 fCacheSize(cacheSize),
56 fCleanupInterval(cleanupInterval)
57 {
58 // constructor //
59
60         // if the same Grid is alreay active, skip connection
61         if (!gGrid || fGridUrl != gGrid->GridUrl()
62              || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
63                 // connection to the Grid
64                 AliInfo("Connection to the Grid...");
65                 if(gGrid){
66                         AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
67                         AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
68                 }
69                 TGrid::Connect(fGridUrl.Data(),fUser.Data());
70         }
71
72         if(!gGrid) {
73                 AliError("Connection failed!");
74                 return;
75         }
76
77         TString initDir(gGrid->Pwd(0));
78         if (fDBFolder[0] != '/') {
79                 fDBFolder.Prepend(initDir);
80         }
81
82         // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
83         if(!gGrid->Cd(fDBFolder.Data(),0)){
84                 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
85                 TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data()));
86                 TString result = res->GetKey(0,"__result__");
87                 if(result == "0"){
88                         AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data()));
89                         return;
90                 }
91         } else {
92                 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
93         }
94
95         // removes any '/' at the end of path, then append one '/'
96         while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/')); 
97         fDBFolder+="/";
98
99         fType="alien";
100         fBaseFolder = fDBFolder;
101
102         // Setting the cache
103
104         // Check if local cache folder is already defined
105         TString origCache(TFile::GetCacheFileDir());
106         if(fCacheFolder.Length() > 0) {
107                 if(origCache.Length() == 0) {
108                         AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data()));
109                 } else if(fCacheFolder != origCache) {
110                         AliWarning(Form("Local cache folder was already defined, changing it to: %s",
111                                         fCacheFolder.Data()));
112                 }
113
114                 // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE
115                 if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) {
116                         AliError(Form("Could not set cache folder %s !", fCacheFolder.Data()));
117                         fCacheFolder = "";
118                 } else {
119                         // reset fCacheFolder because the function may have
120                         // slightly changed the folder name (e.g. '/' added)
121                         fCacheFolder = TFile::GetCacheFileDir();
122                 }
123
124                 // default settings are: cacheSize=1GB, cleanupInterval = 0
125                 if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) {
126                         AliError(Form("Could not set following values "
127                                 "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !",
128                                 fCacheSize, fCleanupInterval));
129                 }
130         }
131
132         // return to the initial directory
133         gGrid->Cd(initDir.Data(),0);
134
135         fNretry = 3;  // default
136         fInitRetrySeconds = 5;   // default
137 }
138
139 //_____________________________________________________________________________
140 AliCDBGrid::~AliCDBGrid()
141 {
142 // destructor
143         delete gGrid; gGrid=0;
144
145 }
146
147 //_____________________________________________________________________________
148 Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
149 // build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
150
151         if(filename.Contains(fDBFolder)){
152                 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
153         }
154
155         TString idPath = filename(0,filename.Last('/'));
156         id.SetPath(idPath);
157         if(!id.IsValid()) return kFALSE;
158
159         filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
160
161         Ssiz_t mSize;
162         // valid filename: Run#firstRun_#lastRun_v#version_s0.root
163         TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
164         keyPattern.Index(filename, &mSize);
165         if (!mSize) {
166
167                 // TODO backward compatibility ... maybe remove later!
168                 Ssiz_t oldmSize;
169                 TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
170                 oldKeyPattern.Index(filename, &oldmSize);
171                 if(!oldmSize) {
172                         AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
173                         return kFALSE;
174                 } else {
175                         AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
176                         id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
177                 }
178
179         } else {
180                 id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
181         }
182
183         filename.Resize(filename.Length() - sizeof(".root") + 1);
184
185         TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
186
187         TString firstRunString(((TObjString*) strArray->At(0))->GetString());
188         id.SetFirstRun(atoi(firstRunString.Data() + 3));
189         id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
190
191         TString verString(((TObjString*) strArray->At(2))->GetString());
192         id.SetVersion(atoi(verString.Data() + 1));
193
194         delete strArray;
195
196         return kTRUE;
197 }
198
199 //_____________________________________________________________________________
200 Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
201 // build file name from AliCDBId (path, run range, version) and fDBFolder
202
203         if (!id.GetAliCDBRunRange().IsValid()) {
204                 AliDebug(2,Form("Invalid run range <%d, %d>.",
205                         id.GetFirstRun(), id.GetLastRun()));
206                 return kFALSE;
207         }
208
209         if (id.GetVersion() < 0) {
210                 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
211                 return kFALSE;
212         }
213
214         filename = Form("Run%d_%d_v%d",
215                                 id.GetFirstRun(),
216                                 id.GetLastRun(),
217                                 id.GetVersion());
218
219         if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
220         filename += ".root";
221
222         filename.Prepend(fDBFolder + id.GetPath() + '/');
223
224         return kTRUE;
225 }
226
227 //_____________________________________________________________________________
228 void AliCDBGrid::SetRetry(Int_t nretry, Int_t initsec) {
229
230         // Function to set the exponential retry for putting entries in the OCDB
231
232         AliWarning("WARNING!!! You are changing the exponential retry times and delay: this function should be used by experts!"); 
233         fNretry = nretry;
234         fInitRetrySeconds = initsec;
235         AliDebug(2,Form("fNretry = %d, fInitRetrySeconds = %d", fNretry, fInitRetrySeconds));
236
237
238
239 //_____________________________________________________________________________
240 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
241 // prepare id (version) of the object that will be stored (called by PutEntry)
242
243         TString initDir(gGrid->Pwd(0));
244
245         TString dirName(fDBFolder);
246
247         Bool_t dirExist=kFALSE;
248
249
250
251         // go to the path; if directory does not exist, create it
252         for(int i=0;i<3;i++){
253                 //TString cmd("find -d ");
254                 //cmd += Form("%s ",dirName);
255                 //cmd += 
256                 //gGrid->Command(cmd.Data());
257                 dirName+=Form("%s/",id.GetPathLevel(i).Data());
258                 dirExist=gGrid->Cd(dirName,0);
259                 if (!dirExist) {
260                         AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
261                         if(!gGrid->Mkdir(dirName,"",0)){
262                                 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
263                                 gGrid->Cd(initDir.Data());
264                         return kFALSE;
265                         }
266
267                         // if folders are new add tags to them
268                         if(i == 1) {
269                                 // TODO Currently disabled
270                                 // add short lived tag!
271                                 // AliInfo("Tagging level 1 folder with \"ShortLived\" tag");
272                                 // if(!AddTag(dirName,"ShortLived_try")){
273                                 //      AliError(Form("Could not tag folder %s !", dirName.Data()));
274                                 //      if(!gGrid->Rmdir(dirName.Data())){
275                                 //              AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
276                                 //      }
277                                 //      return 0;
278                                 //}
279
280                         } else if(i == 2) {
281                                 AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
282                                 if(!AddTag(dirName,"CDB")){
283                                         AliError(Form("Could not tag folder %s !", dirName.Data()));
284                                         if(!gGrid->Rmdir(dirName.Data())){
285                                                 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
286                                         }
287                                         return 0;
288                                 }
289                                 if(!AddTag(dirName,"CDB_MD")){
290                                         AliError(Form("Could not tag folder %s !", dirName.Data()));
291                                         if(!gGrid->Rmdir(dirName.Data())){
292                                                 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
293                                         }
294                                         return 0;
295                                 }
296
297                                 // TODO Currently disabled
298                                 // add short lived tag!
299                                 // TString path=id.GetPath();
300                                 // if(AliCDBManager::Instance()->IsShortLived(path.Data())) {
301                                 //      AliInfo(Form("Tagging %s as short lived", dirName.Data()));
302                                 //      if(!TagShortLived(dirName, kTRUE)){
303                                 //              AliError(Form("Could not tag folder %s !", dirName.Data()));
304                                 //              if(!gGrid->Rmdir(dirName.Data())){
305                                 //                      AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
306                                 //              }
307                                 //              return 0;
308                                 //      }
309                                 // } else {
310                                 //      AliInfo(Form("Tagging %s as long lived", dirName.Data()));
311                                 //      if(!TagShortLived(dirName, kFALSE)){
312                                 //              AliError(Form("Could not tag folder %s !", dirName.Data()));
313                                 //              if(!gGrid->Rmdir(dirName.Data())){
314                                 //                      AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
315                                 //              }
316                                 //              return 0;
317                                 //      }
318                                 // }
319                         }
320                 }
321         }
322         gGrid->Cd(initDir,0);
323
324         TString filename;
325         AliCDBId anId; // the id got from filename
326         AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
327         Int_t lastVersion=0; // highest version found
328
329         TGridResult *res = gGrid->Ls(dirName);
330
331         //loop on the files in the directory, look for highest version
332         for(int i=0; i < res->GetEntries(); i++){
333                 filename=res->GetFileNamePath(i);
334                 if (!FilenameToId(filename, anId)) continue;
335                 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
336                         lastVersion = anId.GetVersion();
337                         lastRunRange = anId.GetAliCDBRunRange();
338                 }
339
340         }
341         delete res;
342
343         // GRP entries with explicitly set version escape default incremental versioning
344         if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0)
345         {
346                 AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data()));
347                 return kFALSE;
348         }
349
350         id.SetVersion(lastVersion + 1);
351         id.SetSubVersion(0);
352
353         TString lastStorage = id.GetLastStorage();
354         if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
355                 AliDebug(2, Form("A NEW object is being stored with version %d",
356                                         id.GetVersion()));
357                 AliDebug(2, Form("and it will hide previously stored object with version %d!",
358                                         id.GetVersion()-1));
359         }
360
361         if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
362                 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
363                         lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
364
365         return kTRUE;
366 }
367
368 //_____________________________________________________________________________
369 AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
370 // look for the Id that matches query's requests (highest or exact version)
371
372         if(validFileIds.GetEntriesFast() < 1)
373                 return NULL;
374
375         TIter iter(&validFileIds);
376
377         AliCDBId *anIdPtr=0;
378         AliCDBId* result=0;
379
380         while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
381                 if(anIdPtr->GetPath() != query.GetPath()) continue;
382
383                 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
384
385                 if (!query.HasVersion()){ // look for highest version
386                         if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
387                         if(result && result->GetVersion() == anIdPtr->GetVersion()) {
388                                 AliError(Form("More than one object valid for run %d, version %d!",
389                                         query.GetFirstRun(), anIdPtr->GetVersion()));
390                                 return NULL;
391                         }
392                         result = anIdPtr;
393                 } else { // look for specified version
394                         if(query.GetVersion() != anIdPtr->GetVersion()) continue;
395                         if(result && result->GetVersion() == anIdPtr->GetVersion()){
396                                 AliError(Form("More than one object valid for run %d, version %d!",
397                                         query.GetFirstRun(), anIdPtr->GetVersion()));
398                                 return NULL;
399                         }
400                         result = anIdPtr;
401                 }
402
403         }
404         
405         if (!result) return NULL;
406
407         return dynamic_cast<AliCDBId*> (result->Clone());
408 }
409
410 //_____________________________________________________________________________
411 AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
412 // get AliCDBId from the database
413 // User must delete returned object
414
415         AliCDBId* dataId=0;
416
417         AliCDBId selectedId(queryId);
418         if (!selectedId.HasVersion()) {
419                 // if version is not specified, first check the selection criteria list
420                 GetSelection(&selectedId);
421         }
422
423         TObjArray validFileIds;
424         validFileIds.SetOwner(1);
425
426         // look for file matching query requests (path, runRange, version)
427         if(selectedId.GetFirstRun() == fRun && fPathFilter.Comprises(selectedId.GetAliCDBPath()) &&
428                         fVersion == selectedId.GetVersion() && !fMetaDataFilter){
429                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
430                 AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!",
431                                         selectedId.GetFirstRun(), selectedId.GetPath().Data()));
432                 dataId = GetId(fValidFileIds, selectedId);
433
434         } else {
435                 // List of files valid for reqested run was not loaded. Looking directly into CDB
436                 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!",
437                                         selectedId.GetFirstRun(), selectedId.GetVersion(), selectedId.GetPath().Data()));
438
439                 TString filter;
440                 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
441
442                 TString pattern = ".root";
443                 TString optionQuery = "-y -m";
444                 if(selectedId.GetVersion() >= 0) {
445                         pattern.Prepend(Form("_v%d_s0",selectedId.GetVersion()));
446                         optionQuery = "";
447                 }
448
449                 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),selectedId.GetPath().Data()));
450
451                 if (optionQuery.Contains("-y")){
452                         AliInfo("Only latest version will be returned");
453                 }
454
455                 AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
456                 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
457                 if (res) {
458                         AliCDBId validFileId;
459                         for(int i=0; i<res->GetEntries(); i++){
460                                 TString filename = res->GetKey(i, "lfn");
461                                 if(filename == "") continue;
462                                 if(FilenameToId(filename, validFileId))
463                                                 validFileIds.AddLast(validFileId.Clone());
464                         }
465                         delete res;
466                 }else{
467                     return 0; // this should be only in case of file catalogue glitch
468                 }
469
470                 dataId = GetId(validFileIds, selectedId);
471         }
472
473         return dataId;
474 }
475
476 //_____________________________________________________________________________
477 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
478 // get AliCDBEntry from the database
479
480         AliCDBId* dataId = GetEntryId(queryId);
481
482         if (!dataId) return NULL;
483
484         TString filename;
485         if (!IdToFilename(*dataId, filename)) {
486                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
487                 delete dataId;
488                 return NULL;
489         }
490
491         AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
492
493         delete dataId;
494         return anEntry;
495 }
496
497 //_____________________________________________________________________________
498 AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
499 // Get AliCBEntry object from file "filename"
500
501         AliDebug(2,Form("Opening file: %s",filename.Data()));
502
503         filename.Prepend("/alien");
504
505         // if option="CACHEREAD" TFile will use the local caching facility!
506         TString option="READ";
507         if(fCacheFolder != ""){
508
509                 // Check if local cache folder was changed in the meanwhile
510                 TString origCache(TFile::GetCacheFileDir());
511                 if(fCacheFolder != origCache) {
512                         AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
513                                         fCacheFolder.Data(), origCache.Data()));
514                         TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
515                         TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
516                 }
517
518                 option.Prepend("CACHE");
519         }
520
521         AliDebug(2, Form("Option: %s", option.Data()));
522
523         TFile *file = TFile::Open(filename, option);
524         if (!file) {
525                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
526                 return NULL;
527         }
528
529         // get the only AliCDBEntry object from the file
530         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
531
532         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
533
534         if (!anEntry) {
535                 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
536                 file->Close();
537                 return NULL;
538         }
539
540         // The object's Id is not reset during storage
541         // If object's Id runRange or version do not match with filename,
542         // it means that someone renamed file by hand. In this case a warning msg is issued.
543
544         if(anEntry){
545                 AliCDBId entryId = anEntry->GetId();
546                 Int_t tmpSubVersion = dataId->GetSubVersion();
547                 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
548                 if(!entryId.IsEqual(dataId)){
549                         AliWarning(Form("Mismatch between file name and object's Id!"));
550                         AliWarning(Form("File name: %s", dataId->ToString().Data()));
551                         AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
552                 }
553                 dataId->SetSubVersion(tmpSubVersion);
554         }
555
556         anEntry->SetLastStorage("grid");
557
558         // Check whether entry contains a TTree. In case load the tree in memory!
559         LoadTreeFromFile(anEntry);
560
561         // close file, return retieved entry
562         file->Close(); delete file; file=0;
563
564         return anEntry;
565 }
566
567 //_____________________________________________________________________________
568 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
569 // multiple request (AliCDBStorage::GetAll)
570
571         TList* result = new TList();
572         result->SetOwner();
573
574         TObjArray validFileIds;
575         validFileIds.SetOwner(1);
576
577         Bool_t alreadyLoaded = kFALSE;
578
579         // look for file matching query requests (path, runRange)
580         if(queryId.GetFirstRun() == fRun &&
581                         fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
582                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
583                 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
584                                         queryId.GetFirstRun(), queryId.GetPath().Data()));
585
586                 alreadyLoaded = kTRUE;
587
588         } else {
589                 // List of files valid for reqested run was not loaded. Looking directly into CDB
590                 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
591                                         queryId.GetFirstRun(), queryId.GetPath().Data()));
592
593                 TString filter;
594                 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
595
596                 TString path = queryId.GetPath();
597
598                 TString pattern = "Run*.root";
599                 TString optionQuery = "-y";
600
601                 TString addFolder = "";
602                 if (!path.Contains("*")){
603                     if (!path.BeginsWith("/")) addFolder += "/";
604                     addFolder += path;
605                 }
606                 else{
607                     if (path.BeginsWith("/")) path.Remove(0,1);
608                     if (path.EndsWith("/")) path.Remove(path.Length()-1,1);     
609                     TObjArray* tokenArr = path.Tokenize("/");
610                     if (tokenArr->GetEntries() != 3) {
611                         AliError("Not a 3 level path! Keeping old query...");
612                         pattern.Prepend(path+"/");
613                     }
614                     else{
615                         TString str0 = ((TObjString*)tokenArr->At(0))->String();
616                         TString str1 = ((TObjString*)tokenArr->At(1))->String();
617                         TString str2 = ((TObjString*)tokenArr->At(2))->String();
618                         if (str0 != "*" && str1 != "*" && str2 == "*"){
619                             // e.g. "ITS/Calib/*"
620                             addFolder = "/"+str0+"/"+str1;
621                         }
622                         else if (str0 != "*" && str1 == "*" && str2 == "*"){    
623                             // e.g. "ITS/*/*"
624                             addFolder = "/"+str0;
625                         }
626                         else if (str0 == "*" && str1 == "*" && str2 == "*"){    
627                             // e.g. "*/*/*"
628                             // do nothing: addFolder is already an empty string;
629                         }
630                         else{
631                             // e.g. "ITS/*/RecoParam"
632                             pattern.Prepend(path+"/");
633                         }
634                     }
635                     delete tokenArr; tokenArr=0;
636                 }
637
638                 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
639
640                 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
641
642                 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());  
643
644                 if (!res) {
645                     AliError("Grid query failed");
646                     return 0;
647                 }
648
649                 AliCDBId validFileId;
650                 for(int i=0; i<res->GetEntries(); i++){
651                         TString filename = res->GetKey(i, "lfn");
652                         if(filename == "") continue;
653                         if(FilenameToId(filename, validFileId))
654                                         validFileIds.AddLast(validFileId.Clone());
655                 }
656                 delete res;
657         }
658
659         TIter *iter=0;
660         if(alreadyLoaded){
661                 iter = new TIter(&fValidFileIds);
662         } else {
663                 iter = new TIter(&validFileIds);
664         }
665
666         TObjArray selectedIds;
667         selectedIds.SetOwner(1);
668
669         // loop on list of valid Ids to select the right version to get.
670         // According to query and to the selection criteria list, version can be the highest or exact
671         AliCDBPath pathCopy;
672         AliCDBId* anIdPtr=0;
673         AliCDBId* dataId=0;
674         AliCDBPath queryPath = queryId.GetAliCDBPath();
675         while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
676                 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
677                 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
678                 pathCopy = thisCDBPath;
679
680                 // check the selection criteria list for this query
681                 AliCDBId thisId(*anIdPtr);
682                 thisId.SetVersion(queryId.GetVersion());
683                 if(!thisId.HasVersion()) GetSelection(&thisId);
684
685                 if(alreadyLoaded){
686                         dataId = GetId(fValidFileIds, thisId);
687                 } else {
688                         dataId = GetId(validFileIds, thisId);
689                 }
690                 if(dataId) selectedIds.Add(dataId);
691         }
692
693         delete iter; iter=0;
694
695         // selectedIds contains the Ids of the files matching all requests of query!
696         // All the objects are now ready to be retrieved
697         iter = new TIter(&selectedIds);
698         while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
699                 TString filename;
700                 if (!IdToFilename(*anIdPtr, filename)) {
701                         AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
702                         continue;
703                 }
704
705                 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
706
707                 if(anEntry) result->Add(anEntry);
708
709         }
710         delete iter; iter=0;
711
712         return result;
713 }
714
715 //_____________________________________________________________________________
716 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry, const char* mirrors) {
717         // put an AliCDBEntry object into the database
718
719         AliCDBId& id = entry->GetId();
720
721         // set version for the entry to be stored
722         if (!PrepareId(id)) return kFALSE;
723
724         // build filename from entry's id
725         TString filename;
726         if (!IdToFilename(id, filename)) {
727                 AliError("Bad ID encountered! Subnormal error!");
728                 return kFALSE;
729         }
730
731         TString folderToTag = Form("%s%s",
732                         fDBFolder.Data(),
733                         id.GetPath().Data());
734
735         TDirectory* saveDir = gDirectory;
736
737         TString fullFilename = Form("/alien%s", filename.Data());
738         TString seMirrors(mirrors);
739         if(seMirrors.IsNull() || seMirrors.IsWhitespace()) seMirrors=GetMirrorSEs();
740         // specify SE to filename
741         // if a list of SEs was passed to this method or set via SetMirrorSEs, set the first as SE for opening the file.
742         // The other SEs will be used in cascade in case of failure in opening the file.
743         // The remaining SEs will be used to create replicas.
744         TObjArray *arraySEs = seMirrors.Tokenize(',');
745         Int_t nSEs = arraySEs->GetEntries();
746         Int_t remainingSEs = 1;
747         if(nSEs == 0){
748                 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
749         }else{
750                 remainingSEs = nSEs;
751         }
752
753         // open file
754         TFile *file=0;
755         AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
756         TString targetSE("");
757
758         Bool_t result = kFALSE;
759         Bool_t reOpenResult = kFALSE;
760         Int_t reOpenAttempts=0;
761         while( !reOpenResult && reOpenAttempts<2){ //loop to check the file after closing it, to catch the unlikely but possible case when the file
762                 // is cleaned up by alien just before closing as a consequence of a network disconnection while writing
763
764                 while( !file && remainingSEs>0){
765                         if(nSEs!=0){
766                                 TObjString *target = (TObjString*) arraySEs->At(nSEs-remainingSEs);
767                                 targetSE=target->String();
768                                 if ( !(targetSE.BeginsWith("ALICE::") && targetSE.CountChar(':')==4) ) {
769                                         AliError(Form("\"%s\" is an invalid storage element identifier.",targetSE.Data()));
770                                         continue;
771                                 }
772                                 if(fullFilename.Contains('?')) fullFilename.Remove(fullFilename.Last('?'));
773                                 fullFilename += Form("?se=%s",targetSE.Data());
774                         }
775                         Int_t remainingAttempts=fNretry;
776                         Int_t nsleep = fInitRetrySeconds; // number of seconds between attempts. We let it increase exponentially
777                         AliDebug(2, Form("Uploading file into SE #%d: %s",nSEs-remainingSEs+1,targetSE.Data()));
778                         while(remainingAttempts > 0) {
779                                 AliDebug(2, Form("Uploading file into OCDB at %s - Attempt #%d",targetSE.Data(),fNretry-remainingAttempts+1));
780                                 remainingAttempts--;
781                                 file = TFile::Open(fullFilename,"CREATE");
782                                 if(!file || !file->IsWritable()){
783                                         if(file) file->Close(); delete file; file=0; // file is not writable
784                                         TString message(TString::Format("Attempt %d failed.",fNretry-remainingAttempts));
785                                         if(remainingAttempts>0) {
786                                                 message += " Sleeping for "; message += nsleep; message += " seconds";
787                                         }else{
788                                                 if(remainingSEs>0) message += " Trying to upload at next SE";
789                                         }
790                                         AliDebug(2, message.Data());
791                                         if(remainingAttempts>0) sleep(nsleep);
792                                 }else{
793                                         remainingAttempts=0;
794                                 }
795                                 nsleep*=fInitRetrySeconds;
796                         }
797                         remainingSEs--;
798                 }
799                 if(!file){
800                         AliError(Form("All %d attempts have failed on all %d SEs. Returning...",fNretry,nSEs));
801                         return kFALSE;
802                 }
803
804                 file->cd();
805
806                 //SetTreeToFile(entry, file);
807                 entry->SetVersion(id.GetVersion());
808
809                 // write object (key name: "AliCDBEntry")
810                 result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
811                 if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
812                 file->Close();
813
814                 if(result)
815                 {
816                         AliDebug(2, Form("Reopening file %s for checking its correctness",fullFilename.Data()));
817                         TFile* ffile = TFile::Open(fullFilename.Data(),"READ");
818                         if(!ffile){
819                                 reOpenResult = kFALSE;
820                                 AliInfo(Form("The file %s was closed successfully but cannot be reopened. Trying now to regenerate it (regeneration attempt number %d)",
821                                                         fullFilename.Data(),++reOpenAttempts));
822                                 delete file; file=0;
823                                 AliDebug(2, Form("Removing file %s", filename.Data()));
824                                 if(!gGrid->Rm(filename.Data()))
825                                         AliError("Can't delete file!");
826                                 remainingSEs++;
827                         }else{
828                                 reOpenResult = kTRUE;
829                                 ffile->Close();
830                         }
831                         delete ffile; ffile=0;
832                 }
833         }
834
835         if (saveDir) saveDir->cd(); else gROOT->cd();
836         delete file; file=0;
837
838         if(result && reOpenResult) {
839
840                 if(!TagFileId(filename, &id)){
841                         AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
842                         if(!gGrid->Rm(filename.Data()))
843                                 AliError("Can't delete file!");
844                         return kFALSE;
845                 }
846
847                 TagFileMetaData(filename, entry->GetMetaData());
848         }else{
849                 AliError("The file could not be opend or the object could not be written");
850                 if(!gGrid->Rm(filename.Data()))
851                         AliError("Can't delete file!");
852                 return kFALSE;
853         }
854
855         AliInfo(Form("CDB object stored into file %s", filename.Data()));
856         if(nSEs==0)
857                 AliInfo(Form("Storage Element: %s", fSE.Data()));
858         else
859                 AliInfo(Form("Storage Element: %s", targetSE.Data()));
860
861         //In case of other SEs specified by the user, mirror the file to the remaining SEs
862         for(Int_t i=0; i<nSEs; i++){
863                 if(i==nSEs-remainingSEs-1) continue; // skip mirroring to the SE where the file was saved
864                 TString mirrorCmd("mirror ");
865                 mirrorCmd += filename;
866                 mirrorCmd += " ";
867                 TObjString *target = (TObjString*) arraySEs->At(i);
868                 TString mirrorSE(target->String());
869                 mirrorCmd += mirrorSE;
870                 AliDebug(5,Form("mirror command: \"%s\"",mirrorCmd.Data()));
871                 AliInfo(Form("Mirroring to storage element: %s", mirrorSE.Data()));
872                 gGrid->Command(mirrorCmd.Data());
873         }
874
875         return kTRUE;
876 }
877
878 //_____________________________________________________________________________
879 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
880 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
881
882         Bool_t result = kTRUE;
883         AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
884         TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
885         TGridResult *gridres = gGrid->Command(addTag.Data());
886         const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
887         if(resCode[0] != '1') {
888                 AliError(Form("Couldn't add %s tags to folder %s !",
889                                                 tagname, folderToTag.Data()));
890                 result = kFALSE;
891         }
892         delete gridres;
893         return result;
894 }
895
896 //_____________________________________________________________________________
897 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
898 // tag stored object in CDB table using object Id's parameters
899
900
901         TString dirname(filename);
902         Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
903         
904         TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
905         TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
906                                         id->GetFirstRun(),
907                                         id->GetLastRun(),
908                                         id->GetVersion());
909         TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
910                                         id->GetPathLevel(0).Data(),
911                                         id->GetPathLevel(1).Data(),
912                                         id->GetPathLevel(2).Data());
913         //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber); 
914         TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber); 
915         TString addTagValue = Form("%s%s%s%s",
916                                         addTagValue1.Data(),
917                                         addTagValue2.Data(),
918                                         addTagValue3.Data(),
919                                         addTagValue4.Data());
920
921         Bool_t result = kFALSE;
922         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
923         TGridResult* res = gGrid->Command(addTagValue.Data());
924         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
925         if(resCode[0] != '1') {
926                 AliError(Form("Couldn't add CDB tag value to file %s !",
927                                                 filename.Data()));
928                 result = kFALSE;
929         } else {
930                 AliDebug(2, "Object successfully tagged.");
931                 result = kTRUE;
932         }
933         delete res;
934         return result;
935
936 }
937
938 //_____________________________________________________________________________
939 Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
940 // tag folder with ShortLived tag
941
942         TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
943
944         Bool_t result = kFALSE;
945         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
946         TGridResult* res = gGrid->Command(addTagValue.Data());
947         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
948         if(resCode[0] != '1') {
949                 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
950                 result = kFALSE;
951         } else {
952                 AliDebug(2,"Object successfully tagged.");
953                 result = kTRUE;
954         }
955         delete res;
956         return result;
957
958 }
959
960 //_____________________________________________________________________________
961 Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
962 // tag stored object in CDB table using object Id's parameters
963
964         TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
965         TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
966                                         md->GetObjectClassName(),
967                                         md->GetResponsible(),
968                                         md->GetBeamPeriod());
969         TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
970                                         md->GetAliRootVersion(),
971                                         md->GetComment());
972         TString addTagValue = Form("%s%s%s",
973                                         addTagValue1.Data(),
974                                         addTagValue2.Data(),
975                                         addTagValue3.Data());
976
977         Bool_t result = kFALSE;
978         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
979         TGridResult* res = gGrid->Command(addTagValue.Data());
980         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
981         if(resCode[0] != '1') {
982                 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
983                                                 filename.Data()));
984                 result = kFALSE;
985         } else {
986                 AliDebug(2,"Object successfully tagged.");
987                 result = kTRUE;
988         }
989         return result;
990 }
991
992 //_____________________________________________________________________________
993 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
994
995         TString turl(fileName);
996         turl.Prepend("/alien" + fDBFolder);
997         turl += "?se="; turl += fSE.Data();
998         TFile *file = TFile::Open(turl);
999         if (!file) {
1000                 AliError(Form("Can't open selection file <%s>!", turl.Data()));
1001                 return NULL;
1002         }
1003
1004         TList *list = new TList();
1005         list->SetOwner();
1006         int i=0;
1007         TString keycycle;
1008
1009         AliCDBId *id;
1010         while(1){
1011                 i++;
1012                 keycycle = "AliCDBId;";
1013                 keycycle+=i;
1014                 
1015                 id = (AliCDBId*) file->Get(keycycle);
1016                 if(!id) break;
1017                 list->AddFirst(id);
1018         }
1019         file->Close(); delete file; file=0;
1020         
1021         return list;
1022
1023
1024 }
1025
1026 //_____________________________________________________________________________
1027 Bool_t AliCDBGrid::Contains(const char* path) const{
1028 // check for path in storage's DBFolder
1029
1030         TString initDir(gGrid->Pwd(0));
1031         TString dirName(fDBFolder);
1032         dirName += path; // dirName = fDBFolder/path
1033         Bool_t result=kFALSE;
1034         if (gGrid->Cd(dirName,0)) result=kTRUE;
1035         gGrid->Cd(initDir.Data(),0);
1036         return result;
1037 }
1038
1039 //_____________________________________________________________________________
1040 void AliCDBGrid::QueryValidFiles()
1041 {
1042 // Query the CDB for files valid for AliCDBStorage::fRun
1043 // fills list fValidFileIds with AliCDBId objects created from file name
1044
1045         TString filter;
1046         MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
1047
1048         TString path = fPathFilter.GetPath();
1049
1050         TString pattern = "Run*";
1051         TString optionQuery = "-y";
1052         if(fVersion >= 0) {
1053                 pattern += Form("_v%d_s0", fVersion);
1054                 optionQuery = "";
1055         }
1056         pattern += ".root";
1057         AliDebug(2,Form("pattern: %s", pattern.Data()));
1058
1059         TString addFolder = "";
1060         if (!path.Contains("*")){
1061                 if (!path.BeginsWith("/")) addFolder += "/";
1062                 addFolder += path;
1063         }
1064         else{
1065                 if (path.BeginsWith("/")) path.Remove(0,1);
1066                 if (path.EndsWith("/")) path.Remove(path.Length()-1,1); 
1067                 TObjArray* tokenArr = path.Tokenize("/");
1068                 if (tokenArr->GetEntries() != 3) {
1069                         AliError("Not a 3 level path! Keeping old query...");
1070                         pattern.Prepend(path+"/");
1071                 }
1072                 else{
1073                         TString str0 = ((TObjString*)tokenArr->At(0))->String();
1074                         TString str1 = ((TObjString*)tokenArr->At(1))->String();
1075                         TString str2 = ((TObjString*)tokenArr->At(2))->String();
1076                         if (str0 != "*" && str1 != "*" && str2 == "*"){
1077                                 // e.g. "ITS/Calib/*"
1078                                 addFolder = "/"+str0+"/"+str1;
1079                         }
1080                         else if (str0 != "*" && str1 == "*" && str2 == "*"){    
1081                                 // e.g. "ITS/*/*"
1082                                 addFolder = "/"+str0;
1083                         }
1084                         else if (str0 == "*" && str1 == "*" && str2 == "*"){    
1085                                 // e.g. "*/*/*"
1086                                 // do nothing: addFolder is already an empty string;
1087                         }
1088                         else{
1089                                 // e.g. "ITS/*/RecoParam"
1090                                 pattern.Prepend(path+"/");
1091                         }
1092                 }
1093                 delete tokenArr; tokenArr=0;
1094         }
1095
1096         TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1097
1098         AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1099
1100         if (optionQuery == "-y"){
1101                 AliInfo("Only latest version will be returned");
1102         } 
1103
1104         TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());  
1105
1106         if (!res) {
1107                 AliError("Grid query failed");
1108                 return;
1109         }
1110
1111         TIter next(res);
1112         TMap *map;
1113         while ((map = (TMap*)next())) {
1114           TObjString *entry;
1115           if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
1116             TString& filename = entry->String();
1117             if(filename.IsNull()) continue;
1118             AliDebug(2,Form("Found valid file: %s", filename.Data()));
1119             AliCDBId *validFileId = new AliCDBId;
1120             Bool_t result = FilenameToId(filename, *validFileId);
1121             if(result) {
1122               fValidFileIds.AddLast(validFileId);
1123             }
1124             else {
1125               delete validFileId;
1126             }
1127           }
1128         }
1129         delete res;
1130
1131 }
1132
1133 //_____________________________________________________________________________
1134 void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
1135                                         const AliCDBMetaData* md, TString& result) const
1136 {
1137 // create filter for file query
1138
1139         result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
1140
1141 //      if(version >= 0) {
1142 //              result += Form(" and CDB:version=%d", version);
1143 //      }
1144 //      if(pathFilter.GetLevel0() != "*") {
1145 //              result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
1146 //      }
1147 //      if(pathFilter.GetLevel1() != "*") {
1148 //              result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
1149 //      }
1150 //      if(pathFilter.GetLevel2() != "*") {
1151 //              result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
1152 //      }
1153
1154         if(md){
1155                 if(md->GetObjectClassName()[0] != '\0') {
1156                         result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
1157                 }
1158                 if(md->GetResponsible()[0] != '\0') {
1159                         result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
1160                 }
1161                 if(md->GetBeamPeriod() != 0) {
1162                         result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
1163                 }
1164                 if(md->GetAliRootVersion()[0] != '\0') {
1165                         result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
1166                 }
1167                 if(md->GetComment()[0] != '\0') {
1168                         result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
1169                 }
1170         }
1171         AliDebug(2, Form("filter: %s",result.Data()));
1172
1173 }
1174
1175 //_____________________________________________________________________________
1176 Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
1177 // get last version found in the database valid for run and path
1178
1179         AliCDBPath aCDBPath(path);
1180         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
1181                 AliError(Form("Invalid path in request: %s", path));
1182                 return -1;
1183         }
1184         AliCDBId query(path, run, run, -1, -1);
1185
1186         AliCDBId* dataId = 0;
1187         // look for file matching query requests (path, runRange, version)
1188         if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
1189                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
1190                 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
1191                                         run, path));
1192                 dataId = GetId(fValidFileIds, query);
1193                 if (!dataId) return -1;
1194                 Int_t version = dataId->GetVersion();
1195                 delete dataId;
1196                 return version;
1197
1198         }
1199         // List of files valid for reqested run was not loaded. Looking directly into CDB
1200         AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
1201                                 run, path));
1202
1203         TObjArray validFileIds;
1204         validFileIds.SetOwner(1);
1205
1206         TString filter;
1207         MakeQueryFilter(run, run, 0, filter);
1208
1209         TString pattern = ".root";
1210
1211         TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),path));
1212
1213         AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1214         TGridResult *res = gGrid->Query(folderCopy, pattern, filter, "-y -m");
1215
1216         AliCDBId validFileId;
1217         if (res->GetEntries()>1){
1218                 AliWarning("Number of found entries >1, even if option -y was used");
1219                 for(int i=0; i<res->GetEntries(); i++){
1220                         TString filename = res->GetKey(i, "lfn");
1221                         if(filename == "") continue;
1222                         if(FilenameToId(filename, validFileId))
1223                                 validFileIds.AddLast(validFileId.Clone());
1224                 }
1225                 dataId = GetId(validFileIds, query);
1226                 if (!dataId) return -1;
1227                 
1228                 Int_t version = dataId->GetVersion();
1229                 delete dataId;
1230                 return version;
1231         }
1232         else if (res->GetEntries()==1){
1233                 TString filename = res->GetKey(0, "lfn");
1234                 if(filename == "") {
1235                         AliError("The only entry found has filename empty");
1236                         return -1;
1237                 }
1238                 if(FilenameToId(filename, validFileId)) return validFileId.GetVersion();
1239                 else{
1240                         AliError("Impossible to get FileId from filename");
1241                         return -1;
1242                 }
1243         }
1244         else {
1245                 AliError("No entries found");
1246                 return -1;
1247         }
1248
1249         delete res;
1250
1251
1252 }
1253
1254 //_____________________________________________________________________________
1255 Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
1256 // get last subversion found in the database valid for run and path
1257         AliError("Objects in GRID storage have no sub version!");
1258         return -1;
1259 }
1260
1261
1262 /////////////////////////////////////////////////////////////////////////////////////////////////
1263 //                                                                                             //
1264 // AliCDBGrid factory                                                                          //
1265 //                                                                                             //
1266 /////////////////////////////////////////////////////////////////////////////////////////////////
1267
1268 ClassImp(AliCDBGridFactory)
1269
1270 //_____________________________________________________________________________
1271 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1272 // check if the string is valid Grid URI
1273
1274         TRegexp gridPattern("^alien://.+$");
1275
1276         return TString(gridString).Contains(gridPattern);
1277 }
1278
1279 //_____________________________________________________________________________
1280 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1281 // create AliCDBGridParam class from the URI string
1282
1283         if (!Validate(gridString)) {
1284                 return NULL;
1285         }
1286
1287         TString buffer(gridString);
1288
1289         TString gridUrl         = "alien://";
1290         TString user            = "";
1291         TString dbFolder        = "";
1292         TString se              = "default";
1293         TString cacheFolder     = "";
1294         Bool_t  operateDisconnected = kTRUE;
1295         Long64_t cacheSize          = (UInt_t) 1024*1024*1024; // 1GB
1296         Long_t cleanupInterval      = 0;
1297
1298         TObjArray *arr = buffer.Tokenize('?');
1299         TIter iter(arr);
1300         TObjString *str = 0;
1301
1302         while((str = (TObjString*) iter.Next())){
1303                 TString entry(str->String());
1304                 Int_t indeq = entry.Index('=');
1305                 if(indeq == -1) {
1306                         if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1307                                 gridUrl = entry;
1308                                 continue;
1309                         } else {
1310                                 AliError(Form("Invalid entry! %s",entry.Data()));
1311                                 continue;
1312                         }
1313                 }
1314                 
1315                 TString key = entry(0,indeq);
1316                 TString value = entry(indeq+1,entry.Length()-indeq);
1317
1318                 if(key.Contains("grid",TString::kIgnoreCase)) {
1319                         gridUrl += value;
1320                 }
1321                 else if (key.Contains("user",TString::kIgnoreCase)){
1322                         user = value;
1323                 }
1324                 else if (key.Contains("se",TString::kIgnoreCase)){
1325                         se = value;
1326                 }
1327                 else if (key.Contains("cacheF",TString::kIgnoreCase)){
1328                         cacheFolder = value;
1329                         if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
1330                                 cacheFolder += "/";
1331                 }
1332                 else if (key.Contains("folder",TString::kIgnoreCase)){
1333                         dbFolder = value;
1334                 }
1335                 else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1336                         if(value == "kTRUE") {
1337                                 operateDisconnected = kTRUE;
1338                         } else if (value == "kFALSE") {
1339                                 operateDisconnected = kFALSE;
1340                         } else if (value == "0" || value == "1") {
1341                                 operateDisconnected = (Bool_t) value.Atoi();
1342                         } else {
1343                                 AliError(Form("Invalid entry! %s",entry.Data()));
1344                                 return NULL;
1345                         }
1346                 }
1347                 else if (key.Contains("cacheS",TString::kIgnoreCase)){
1348                         if(value.IsDigit()) {
1349                                 cacheSize = value.Atoi();
1350                         } else {
1351                                 AliError(Form("Invalid entry! %s",entry.Data()));
1352                                 return NULL;
1353                         }
1354                 }
1355                 else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1356                         if(value.IsDigit()) {
1357                                 cleanupInterval = value.Atoi();
1358                         } else {
1359                                 AliError(Form("Invalid entry! %s",entry.Data()));
1360                                 return NULL;
1361                         }
1362                 }
1363                 else{
1364                         AliError(Form("Invalid entry! %s",entry.Data()));
1365                         return NULL;
1366                 }
1367         }
1368         delete arr; arr=0;
1369
1370         AliDebug(2, Form("gridUrl:      %s", gridUrl.Data()));
1371         AliDebug(2, Form("user: %s", user.Data()));
1372         AliDebug(2, Form("dbFolder:     %s", dbFolder.Data()));
1373         AliDebug(2, Form("s.e.: %s", se.Data()));
1374         AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1375         AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
1376         AliDebug(2, Form("local cache size: %lld", cacheSize));
1377         AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
1378
1379         if(dbFolder == ""){
1380                 AliError("Base folder must be specified!");
1381                 return NULL;
1382         }
1383
1384         return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1385                           dbFolder.Data(), se.Data(), cacheFolder.Data(),
1386                           operateDisconnected, cacheSize, cleanupInterval);
1387 }
1388
1389 //_____________________________________________________________________________
1390 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1391 // create AliCDBGrid storage instance from parameters
1392         
1393         AliCDBGrid *grid = 0;
1394         if (AliCDBGridParam::Class() == param->IsA()) {
1395
1396                 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1397                 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1398                                       gridParam->GetUser().Data(),
1399                                       gridParam->GetDBFolder().Data(),
1400                                       gridParam->GetSE().Data(),
1401                                       gridParam->GetCacheFolder().Data(),
1402                                       gridParam->GetOperateDisconnected(),
1403                                       gridParam->GetCacheSize(),
1404                                       gridParam->GetCleanupInterval());
1405
1406         }
1407
1408         if(!gGrid && grid) {
1409                 delete grid; grid=0;
1410         }
1411
1412         return grid;
1413 }
1414
1415 /////////////////////////////////////////////////////////////////////////////////////////////////
1416 //                                                                                             //
1417 // AliCDBGrid Parameter class                                                                  //                                         //
1418 //                                                                                             //
1419 /////////////////////////////////////////////////////////////////////////////////////////////////
1420
1421 ClassImp(AliCDBGridParam)
1422
1423 //_____________________________________________________________________________
1424 AliCDBGridParam::AliCDBGridParam():
1425  AliCDBParam(),
1426  fGridUrl(),
1427  fUser(),
1428  fDBFolder(),
1429  fSE(),
1430  fCacheFolder(),
1431  fOperateDisconnected(),
1432  fCacheSize(),
1433  fCleanupInterval()
1434
1435  {
1436 // default constructor
1437
1438 }
1439
1440 //_____________________________________________________________________________
1441 AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
1442                                 const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1443                                 Long64_t cacheSize, Long_t cleanupInterval):
1444  AliCDBParam(),
1445  fGridUrl(gridUrl),
1446  fUser(user),
1447  fDBFolder(dbFolder),
1448  fSE(se),
1449  fCacheFolder(cacheFolder),
1450  fOperateDisconnected(operateDisconnected),
1451  fCacheSize(cacheSize),
1452  fCleanupInterval(cleanupInterval)
1453 {
1454 // constructor
1455
1456         SetType("alien");
1457
1458         TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
1459                         "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
1460                         fGridUrl.Data(), fUser.Data(),
1461                         fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1462                         fOperateDisconnected, fCacheSize, fCleanupInterval);
1463
1464         SetURI(uri.Data());
1465 }
1466
1467 //_____________________________________________________________________________
1468 AliCDBGridParam::~AliCDBGridParam() {
1469 // destructor
1470
1471 }
1472
1473 //_____________________________________________________________________________
1474 AliCDBParam* AliCDBGridParam::CloneParam() const {
1475 // clone parameter
1476
1477         return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1478                                         fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1479                                         fOperateDisconnected, fCacheSize, fCleanupInterval);
1480 }
1481
1482 //_____________________________________________________________________________
1483 ULong_t AliCDBGridParam::Hash() const {
1484 // return Hash function
1485
1486         return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
1487 }
1488
1489 //_____________________________________________________________________________
1490 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1491 // check if this object is equal to AliCDBParam obj
1492
1493         if (this == obj) {
1494                 return kTRUE;
1495         }
1496
1497         if (AliCDBGridParam::Class() != obj->IsA()) {
1498                 return kFALSE;
1499         }
1500
1501         AliCDBGridParam* other = (AliCDBGridParam*) obj;
1502
1503         if(fGridUrl != other->fGridUrl) return kFALSE;
1504         if(fUser != other->fUser) return kFALSE;
1505         if(fDBFolder != other->fDBFolder) return kFALSE;
1506         if(fSE != other->fSE) return kFALSE;
1507         if(fCacheFolder != other->fCacheFolder) return kFALSE;
1508         if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1509         if(fCacheSize != other->fCacheSize) return kFALSE;
1510         if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
1511         return kTRUE;
1512 }
1513