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