]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCDBGrid.cxx
Merge remote-tracking branch 'origin/master' into TPCdev
[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 // 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     }else{
466       return 0; // this should be only in case of file catalogue glitch
467     }
468
469     dataId = GetId(validFileIds, selectedId);
470   }
471
472   return dataId;
473 }
474
475 //_____________________________________________________________________________
476 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
477 // get AliCDBEntry from the database
478
479   AliCDBId* dataId = GetEntryId(queryId);
480
481   if (!dataId){
482     AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
483     return NULL;
484   }
485
486   TString filename;
487   if (!IdToFilename(*dataId, filename)) {
488     AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
489     delete dataId;
490     AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
491   }
492
493   AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
494
495   delete dataId;
496   if(!anEntry)
497     AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
498
499   return anEntry;
500 }
501
502 //_____________________________________________________________________________
503 AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
504 // Get AliCBEntry object from file "filename"
505
506   AliDebug(2,Form("Opening file: %s",filename.Data()));
507
508   filename.Prepend("/alien");
509
510   // if option="CACHEREAD" TFile will use the local caching facility!
511   TString option="READ";
512   if(fCacheFolder != ""){
513
514     // Check if local cache folder was changed in the meanwhile
515     TString origCache(TFile::GetCacheFileDir());
516     if(fCacheFolder != origCache) {
517       AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
518             fCacheFolder.Data(), origCache.Data()));
519       TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
520       TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
521     }
522
523     option.Prepend("CACHE");
524   }
525
526   AliDebug(2, Form("Option: %s", option.Data()));
527
528   TFile *file = TFile::Open(filename, option);
529   if (!file) {
530     AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
531     return NULL;
532   }
533
534   // get the only AliCDBEntry object from the file
535   // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
536
537   AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
538
539   if (!anEntry) {
540     AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
541     file->Close();
542     return NULL;
543   }
544
545   // The object's Id is not reset during storage
546   // If object's Id runRange or version do not match with filename,
547   // it means that someone renamed file by hand. In this case a warning msg is issued.
548
549   if(anEntry){
550     AliCDBId entryId = anEntry->GetId();
551     Int_t tmpSubVersion = dataId->GetSubVersion();
552     dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
553     if(!entryId.IsEqual(dataId)){
554       AliWarning(Form("Mismatch between file name and object's Id!"));
555       AliWarning(Form("File name: %s", dataId->ToString().Data()));
556       AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
557     }
558     dataId->SetSubVersion(tmpSubVersion);
559   }
560
561   anEntry->SetLastStorage("grid");
562
563   // Check whether entry contains a TTree. In case load the tree in memory!
564   LoadTreeFromFile(anEntry);
565
566   // close file, return retieved entry
567   file->Close(); delete file; file=0;
568
569   return anEntry;
570 }
571
572 //_____________________________________________________________________________
573 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
574 // multiple request (AliCDBStorage::GetAll)
575
576   TList* result = new TList();
577   result->SetOwner();
578
579   TObjArray validFileIds;
580   validFileIds.SetOwner(1);
581
582   Bool_t alreadyLoaded = kFALSE;
583
584   // look for file matching query requests (path, runRange)
585   if(queryId.GetFirstRun() == fRun &&
586       fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
587     // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
588     AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
589           queryId.GetFirstRun(), queryId.GetPath().Data()));
590
591     alreadyLoaded = kTRUE;
592
593   } else {
594     // List of files valid for reqested run was not loaded. Looking directly into CDB
595     AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
596           queryId.GetFirstRun(), queryId.GetPath().Data()));
597
598     TString filter;
599     MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
600
601     TString path = queryId.GetPath();
602
603     TString pattern = "Run*.root";
604     TString optionQuery = "-y";
605
606     TString addFolder = "";
607     if (!path.Contains("*")){
608       if (!path.BeginsWith("/")) addFolder += "/";
609       addFolder += path;
610     }
611     else{
612       if (path.BeginsWith("/")) path.Remove(0,1);
613       if (path.EndsWith("/")) path.Remove(path.Length()-1,1);   
614       TObjArray* tokenArr = path.Tokenize("/");
615       if (tokenArr->GetEntries() != 3) {
616         AliError("Not a 3 level path! Keeping old query...");
617         pattern.Prepend(path+"/");
618       }
619       else{
620         TString str0 = ((TObjString*)tokenArr->At(0))->String();
621         TString str1 = ((TObjString*)tokenArr->At(1))->String();
622         TString str2 = ((TObjString*)tokenArr->At(2))->String();
623         if (str0 != "*" && str1 != "*" && str2 == "*"){
624           // e.g. "ITS/Calib/*"
625           addFolder = "/"+str0+"/"+str1;
626         }
627         else if (str0 != "*" && str1 == "*" && str2 == "*"){    
628           // e.g. "ITS/*/*"
629           addFolder = "/"+str0;
630         }
631         else if (str0 == "*" && str1 == "*" && str2 == "*"){    
632           // e.g. "*/*/*"
633           // do nothing: addFolder is already an empty string;
634         }
635         else{
636           // e.g. "ITS/*/RecoParam"
637           pattern.Prepend(path+"/");
638         }
639       }
640       delete tokenArr; tokenArr=0;
641     }
642
643     TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
644
645     AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
646
647     TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());  
648
649     if (!res) {
650       AliError("Grid query failed");
651       return 0;
652     }
653
654     AliCDBId validFileId;
655     for(int i=0; i<res->GetEntries(); i++){
656       TString filename = res->GetKey(i, "lfn");
657       if(filename == "") continue;
658       if(FilenameToId(filename, validFileId))
659         validFileIds.AddLast(validFileId.Clone());
660     }
661     delete res;
662   }
663
664   TIter *iter=0;
665   if(alreadyLoaded){
666     iter = new TIter(&fValidFileIds);
667   } else {
668     iter = new TIter(&validFileIds);
669   }
670
671   TObjArray selectedIds;
672   selectedIds.SetOwner(1);
673
674   // loop on list of valid Ids to select the right version to get.
675   // According to query and to the selection criteria list, version can be the highest or exact
676   AliCDBPath pathCopy;
677   AliCDBId* anIdPtr=0;
678   AliCDBId* dataId=0;
679   AliCDBPath queryPath = queryId.GetAliCDBPath();
680   while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
681     AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
682     if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
683     pathCopy = thisCDBPath;
684
685     // check the selection criteria list for this query
686     AliCDBId thisId(*anIdPtr);
687     thisId.SetVersion(queryId.GetVersion());
688     if(!thisId.HasVersion()) GetSelection(&thisId);
689
690     if(alreadyLoaded){
691       dataId = GetId(fValidFileIds, thisId);
692     } else {
693       dataId = GetId(validFileIds, thisId);
694     }
695     if(dataId) selectedIds.Add(dataId);
696   }
697
698   delete iter; iter=0;
699
700   // selectedIds contains the Ids of the files matching all requests of query!
701   // All the objects are now ready to be retrieved
702   iter = new TIter(&selectedIds);
703   while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
704     TString filename;
705     if (!IdToFilename(*anIdPtr, filename)) {
706       AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
707       continue;
708     }
709
710     AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
711
712     if(anEntry) result->Add(anEntry);
713
714   }
715   delete iter; iter=0;
716
717   return result;
718 }
719
720 //_____________________________________________________________________________
721 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry, const char* mirrors) {
722 // put an AliCDBEntry object into the database
723
724   AliCDBId& id = entry->GetId();
725
726   // set version for the entry to be stored
727   if (!PrepareId(id)) return kFALSE;
728
729   // build filename from entry's id
730   TString filename;
731   if (!IdToFilename(id, filename)) {
732     AliError("Bad ID encountered! Subnormal error!");
733     return kFALSE;
734   }
735
736   TString folderToTag = Form("%s%s",
737       fDBFolder.Data(),
738       id.GetPath().Data());
739
740   TDirectory* saveDir = gDirectory;
741
742   TString fullFilename = Form("/alien%s", filename.Data());
743   TString seMirrors(mirrors);
744   if(seMirrors.IsNull() || seMirrors.IsWhitespace()) seMirrors=GetMirrorSEs();
745   // specify SE to filename
746   // if a list of SEs was passed to this method or set via SetMirrorSEs, set the first as SE for opening the file.
747   // The other SEs will be used in cascade in case of failure in opening the file.
748   // The remaining SEs will be used to create replicas.
749   TObjArray *arraySEs = seMirrors.Tokenize(',');
750   Int_t nSEs = arraySEs->GetEntries();
751   Int_t remainingSEs = 1;
752   if(nSEs == 0){
753     if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
754   }else{
755     remainingSEs = nSEs;
756   }
757
758   // open file
759   TFile *file=0;
760   AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
761   TString targetSE("");
762
763   Bool_t result = kFALSE;
764   Bool_t reOpenResult = kFALSE;
765   Int_t reOpenAttempts=0;
766   while( !reOpenResult && reOpenAttempts<2){ //loop to check the file after closing it, to catch the unlikely but possible case when the file
767     // is cleaned up by alien just before closing as a consequence of a network disconnection while writing
768
769     while( !file && remainingSEs>0){
770       if(nSEs!=0){
771         TObjString *target = (TObjString*) arraySEs->At(nSEs-remainingSEs);
772         targetSE=target->String();
773         if ( !(targetSE.BeginsWith("ALICE::") && targetSE.CountChar(':')==4) ) {
774           AliError(Form("\"%s\" is an invalid storage element identifier.",targetSE.Data()));
775           continue;
776         }
777         if(fullFilename.Contains('?')) fullFilename.Remove(fullFilename.Last('?'));
778         fullFilename += Form("?se=%s",targetSE.Data());
779       }
780       Int_t remainingAttempts=fNretry;
781       Int_t nsleep = fInitRetrySeconds; // number of seconds between attempts. We let it increase exponentially
782       AliDebug(2, Form("Uploading file into SE #%d: %s",nSEs-remainingSEs+1,targetSE.Data()));
783       while(remainingAttempts > 0) {
784         AliDebug(2, Form("Uploading file into OCDB at %s - Attempt #%d",targetSE.Data(),fNretry-remainingAttempts+1));
785         remainingAttempts--;
786         file = TFile::Open(fullFilename,"CREATE");
787         if(!file || !file->IsWritable()){
788           if(file) file->Close(); delete file; file=0; // file is not writable
789           TString message(TString::Format("Attempt %d failed.",fNretry-remainingAttempts));
790           if(remainingAttempts>0) {
791             message += " Sleeping for "; message += nsleep; message += " seconds";
792           }else{
793             if(remainingSEs>0) message += " Trying to upload at next SE";
794           }
795           AliDebug(2, message.Data());
796           if(remainingAttempts>0) sleep(nsleep);
797         }else{
798           remainingAttempts=0;
799         }
800         nsleep*=fInitRetrySeconds;
801       }
802       remainingSEs--;
803     }
804     if(!file){
805       AliError(Form("All %d attempts have failed on all %d SEs. Returning...",fNretry,nSEs));
806       return kFALSE;
807     }
808
809     file->cd();
810
811     //SetTreeToFile(entry, file);
812     entry->SetVersion(id.GetVersion());
813
814     // write object (key name: "AliCDBEntry")
815     result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
816     if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
817     file->Close();
818
819     if(result)
820     {
821       AliDebug(2, Form("Reopening file %s for checking its correctness",fullFilename.Data()));
822       TFile* ffile = TFile::Open(fullFilename.Data(),"READ");
823       if(!ffile){
824         reOpenResult = kFALSE;
825         AliInfo(Form("The file %s was closed successfully but cannot be reopened. Trying now to regenerate it (regeneration attempt number %d)",
826               fullFilename.Data(),++reOpenAttempts));
827         delete file; file=0;
828         AliDebug(2, Form("Removing file %s", filename.Data()));
829         if(!gGrid->Rm(filename.Data()))
830           AliError("Can't delete file!");
831         remainingSEs++;
832       }else{
833         reOpenResult = kTRUE;
834         ffile->Close();
835       }
836       delete ffile; ffile=0;
837     }
838   }
839
840   if (saveDir) saveDir->cd(); else gROOT->cd();
841   delete file; file=0;
842
843   if(result && reOpenResult) {
844
845     if(!TagFileId(filename, &id)){
846       AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
847       if(!gGrid->Rm(filename.Data()))
848         AliError("Can't delete file!");
849       return kFALSE;
850     }
851
852     TagFileMetaData(filename, entry->GetMetaData());
853   }else{
854     AliError("The file could not be opend or the object could not be written");
855     if(!gGrid->Rm(filename.Data()))
856       AliError("Can't delete file!");
857     return kFALSE;
858   }
859
860   AliInfo(Form("CDB object stored into file %s", filename.Data()));
861   if(nSEs==0)
862     AliInfo(Form("Storage Element: %s", fSE.Data()));
863   else
864     AliInfo(Form("Storage Element: %s", targetSE.Data()));
865
866   //In case of other SEs specified by the user, mirror the file to the remaining SEs
867   for(Int_t i=0; i<nSEs; i++){
868     if(i==nSEs-remainingSEs-1) continue; // skip mirroring to the SE where the file was saved
869     TString mirrorCmd("mirror ");
870     mirrorCmd += filename;
871     mirrorCmd += " ";
872     TObjString *target = (TObjString*) arraySEs->At(i);
873     TString mirrorSE(target->String());
874     mirrorCmd += mirrorSE;
875     AliDebug(5,Form("mirror command: \"%s\"",mirrorCmd.Data()));
876     AliInfo(Form("Mirroring to storage element: %s", mirrorSE.Data()));
877     gGrid->Command(mirrorCmd.Data());
878   }
879   arraySEs->Delete(); arraySEs=0;
880
881   return kTRUE;
882 }
883
884 //_____________________________________________________________________________
885 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
886 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
887
888   Bool_t result = kTRUE;
889   AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
890   TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
891   TGridResult *gridres = gGrid->Command(addTag.Data());
892   const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
893   if(resCode[0] != '1') {
894     AliError(Form("Couldn't add %s tags to folder %s !",
895           tagname, folderToTag.Data()));
896     result = kFALSE;
897   }
898   delete gridres;
899   return result;
900 }
901
902 //_____________________________________________________________________________
903 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
904 // tag stored object in CDB table using object Id's parameters
905
906
907   TString dirname(filename);
908   Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
909
910   TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
911   TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
912       id->GetFirstRun(),
913       id->GetLastRun(),
914       id->GetVersion());
915   TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
916       id->GetPathLevel(0).Data(),
917       id->GetPathLevel(1).Data(),
918       id->GetPathLevel(2).Data());
919   //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber); 
920   TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber); 
921   TString addTagValue = Form("%s%s%s%s",
922       addTagValue1.Data(),
923       addTagValue2.Data(),
924       addTagValue3.Data(),
925       addTagValue4.Data());
926
927   Bool_t result = kFALSE;
928   AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
929   TGridResult* res = gGrid->Command(addTagValue.Data());
930   const char* resCode = res->GetKey(0,"__result__"); // '1' if success
931   if(resCode[0] != '1') {
932     AliError(Form("Couldn't add CDB tag value to file %s !",
933           filename.Data()));
934     result = kFALSE;
935   } else {
936     AliDebug(2, "Object successfully tagged.");
937     result = kTRUE;
938   }
939   delete res;
940   return result;
941
942 }
943
944 //_____________________________________________________________________________
945 Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
946 // tag folder with ShortLived tag
947
948   TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
949
950   Bool_t result = kFALSE;
951   AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
952   TGridResult* res = gGrid->Command(addTagValue.Data());
953   const char* resCode = res->GetKey(0,"__result__"); // '1' if success
954   if(resCode[0] != '1') {
955     AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
956     result = kFALSE;
957   } else {
958     AliDebug(2,"Object successfully tagged.");
959     result = kTRUE;
960   }
961   delete res;
962   return result;
963
964 }
965
966 //_____________________________________________________________________________
967 Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
968 // tag stored object in CDB table using object Id's parameters
969
970   TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
971   TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
972       md->GetObjectClassName(),
973       md->GetResponsible(),
974       md->GetBeamPeriod());
975   TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
976       md->GetAliRootVersion(),
977       md->GetComment());
978   TString addTagValue = Form("%s%s%s",
979       addTagValue1.Data(),
980       addTagValue2.Data(),
981       addTagValue3.Data());
982
983   Bool_t result = kFALSE;
984   AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
985   TGridResult* res = gGrid->Command(addTagValue.Data());
986   const char* resCode = res->GetKey(0,"__result__"); // '1' if success
987   if(resCode[0] != '1') {
988     AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
989           filename.Data()));
990     result = kFALSE;
991   } else {
992     AliDebug(2,"Object successfully tagged.");
993     result = kTRUE;
994   }
995   return result;
996 }
997
998 //_____________________________________________________________________________
999 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
1000
1001   TString turl(fileName);
1002   turl.Prepend("/alien" + fDBFolder);
1003   turl += "?se="; turl += fSE.Data();
1004   TFile *file = TFile::Open(turl);
1005   if (!file) {
1006     AliError(Form("Can't open selection file <%s>!", turl.Data()));
1007     return NULL;
1008   }
1009
1010   TList *list = new TList();
1011   list->SetOwner();
1012   int i=0;
1013   TString keycycle;
1014
1015   AliCDBId *id;
1016   while(1){
1017     i++;
1018     keycycle = "AliCDBId;";
1019     keycycle+=i;
1020
1021     id = (AliCDBId*) file->Get(keycycle);
1022     if(!id) break;
1023     list->AddFirst(id);
1024   }
1025   file->Close(); delete file; file=0;
1026
1027   return list;
1028
1029
1030 }
1031
1032 //_____________________________________________________________________________
1033 Bool_t AliCDBGrid::Contains(const char* path) const{
1034 // check for path in storage's DBFolder
1035
1036   TString initDir(gGrid->Pwd(0));
1037   TString dirName(fDBFolder);
1038   dirName += path; // dirName = fDBFolder/path
1039   Bool_t result=kFALSE;
1040   if (gGrid->Cd(dirName,0)) result=kTRUE;
1041   gGrid->Cd(initDir.Data(),0);
1042   return result;
1043 }
1044
1045 //_____________________________________________________________________________
1046 void AliCDBGrid::QueryValidFiles()
1047 {
1048   // Query the CDB for files valid for AliCDBStorage::fRun
1049   // Fills list fValidFileIds with AliCDBId objects extracted from CDB files
1050   // selected from AliEn metadata.
1051   // If fVersion was not set, fValidFileIds is filled with highest versions. 
1052
1053   TString filter;
1054   MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
1055
1056   TString path = fPathFilter.GetPath();
1057
1058   TString pattern = "Run*";
1059   TString optionQuery = "-y";
1060   if(fVersion >= 0) {
1061     pattern += Form("_v%d_s0", fVersion);
1062     optionQuery = "";
1063   }
1064   pattern += ".root";
1065   AliDebug(2,Form("pattern: %s", pattern.Data()));
1066
1067   TString addFolder = "";
1068   if (!path.Contains("*")){
1069     if (!path.BeginsWith("/")) addFolder += "/";
1070     addFolder += path;
1071   }
1072   else{
1073     if (path.BeginsWith("/")) path.Remove(0,1);
1074     if (path.EndsWith("/")) path.Remove(path.Length()-1,1);     
1075     TObjArray* tokenArr = path.Tokenize("/");
1076     if (tokenArr->GetEntries() != 3) {
1077       AliError("Not a 3 level path! Keeping old query...");
1078       pattern.Prepend(path+"/");
1079     }
1080     else{
1081       TString str0 = ((TObjString*)tokenArr->At(0))->String();
1082       TString str1 = ((TObjString*)tokenArr->At(1))->String();
1083       TString str2 = ((TObjString*)tokenArr->At(2))->String();
1084       if (str0 != "*" && str1 != "*" && str2 == "*"){
1085         // e.g. "ITS/Calib/*"
1086         addFolder = "/"+str0+"/"+str1;
1087       }
1088       else if (str0 != "*" && str1 == "*" && str2 == "*"){      
1089         // e.g. "ITS/*/*"
1090         addFolder = "/"+str0;
1091       }
1092       else if (str0 == "*" && str1 == "*" && str2 == "*"){      
1093         // e.g. "*/*/*"
1094         // do nothing: addFolder is already an empty string;
1095       }
1096       else{
1097         // e.g. "ITS/*/RecoParam"
1098         pattern.Prepend(path+"/");
1099       }
1100     }
1101     delete tokenArr; tokenArr=0;
1102   }
1103
1104   TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1105
1106   AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1107
1108   if (optionQuery == "-y"){
1109     AliInfo("Only latest version will be returned");
1110   } 
1111
1112   TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());  
1113
1114   if (!res) {
1115     AliError("Grid query failed");
1116     return;
1117   }
1118
1119   TIter next(res);
1120   TMap *map;
1121   while ((map = (TMap*)next())) {
1122     TObjString *entry;
1123     if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
1124       TString& filename = entry->String();
1125       if(filename.IsNull()) continue;
1126       AliDebug(2,Form("Found valid file: %s", filename.Data()));
1127       AliCDBId *validFileId = new AliCDBId;
1128       Bool_t result = FilenameToId(filename, *validFileId);
1129       if(result) {
1130         fValidFileIds.AddLast(validFileId);
1131       }
1132       else {
1133         delete validFileId;
1134       }
1135     }
1136   }
1137   delete res;
1138
1139 }
1140
1141 //_____________________________________________________________________________
1142 void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
1143     const AliCDBMetaData* md, TString& result) const
1144 {
1145   // create filter for file query
1146
1147   result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
1148
1149   //    if(version >= 0) {
1150   //            result += Form(" and CDB:version=%d", version);
1151   //    }
1152   //    if(pathFilter.GetLevel0() != "*") {
1153   //            result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
1154   //    }
1155   //    if(pathFilter.GetLevel1() != "*") {
1156   //            result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
1157   //    }
1158   //    if(pathFilter.GetLevel2() != "*") {
1159   //            result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
1160   //    }
1161
1162   if(md){
1163     if(md->GetObjectClassName()[0] != '\0') {
1164       result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
1165     }
1166     if(md->GetResponsible()[0] != '\0') {
1167       result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
1168     }
1169     if(md->GetBeamPeriod() != 0) {
1170       result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
1171     }
1172     if(md->GetAliRootVersion()[0] != '\0') {
1173       result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
1174     }
1175     if(md->GetComment()[0] != '\0') {
1176       result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
1177     }
1178   }
1179   AliDebug(2, Form("filter: %s",result.Data()));
1180
1181 }
1182
1183
1184 /////////////////////////////////////////////////////////////////////////////////////////////////
1185 //                                                                                             //
1186 // AliCDBGrid factory                                                                          //
1187 //                                                                                             //
1188 /////////////////////////////////////////////////////////////////////////////////////////////////
1189
1190 ClassImp(AliCDBGridFactory)
1191
1192   //_____________________________________________________________________________
1193   Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1194     // check if the string is valid Grid URI
1195
1196     TRegexp gridPattern("^alien://.+$");
1197
1198     return TString(gridString).Contains(gridPattern);
1199   }
1200
1201 //_____________________________________________________________________________
1202 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1203   // create AliCDBGridParam class from the URI string
1204
1205   if (!Validate(gridString)) {
1206     return NULL;
1207   }
1208
1209   TString buffer(gridString);
1210
1211   TString gridUrl       = "alien://";
1212   TString user          = "";
1213   TString dbFolder      = "";
1214   TString se            = "default";
1215   TString cacheFolder   = "";
1216   Bool_t  operateDisconnected = kTRUE;
1217   Long64_t cacheSize          = (UInt_t) 1024*1024*1024; // 1GB
1218   Long_t cleanupInterval      = 0;
1219
1220   TObjArray *arr = buffer.Tokenize('?');
1221   TIter iter(arr);
1222   TObjString *str = 0;
1223
1224   while((str = (TObjString*) iter.Next())){
1225     TString entry(str->String());
1226     Int_t indeq = entry.Index('=');
1227     if(indeq == -1) {
1228       if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1229         gridUrl = entry;
1230         continue;
1231       } else {
1232         AliError(Form("Invalid entry! %s",entry.Data()));
1233         continue;
1234       }
1235     }
1236
1237     TString key = entry(0,indeq);
1238     TString value = entry(indeq+1,entry.Length()-indeq);
1239
1240     if(key.Contains("grid",TString::kIgnoreCase)) {
1241       gridUrl += value;
1242     }
1243     else if (key.Contains("user",TString::kIgnoreCase)){
1244       user = value;
1245     }
1246     else if (key.Contains("se",TString::kIgnoreCase)){
1247       se = value;
1248     }
1249     else if (key.Contains("cacheF",TString::kIgnoreCase)){
1250       cacheFolder = value;
1251       if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
1252         cacheFolder += "/";
1253     }
1254     else if (key.Contains("folder",TString::kIgnoreCase)){
1255       dbFolder = value;
1256     }
1257     else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1258       if(value == "kTRUE") {
1259         operateDisconnected = kTRUE;
1260       } else if (value == "kFALSE") {
1261         operateDisconnected = kFALSE;
1262       } else if (value == "0" || value == "1") {
1263         operateDisconnected = (Bool_t) value.Atoi();
1264       } else {
1265         AliError(Form("Invalid entry! %s",entry.Data()));
1266         return NULL;
1267       }
1268     }
1269     else if (key.Contains("cacheS",TString::kIgnoreCase)){
1270       if(value.IsDigit()) {
1271         cacheSize = value.Atoi();
1272       } else {
1273         AliError(Form("Invalid entry! %s",entry.Data()));
1274         return NULL;
1275       }
1276     }
1277     else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1278       if(value.IsDigit()) {
1279         cleanupInterval = value.Atoi();
1280       } else {
1281         AliError(Form("Invalid entry! %s",entry.Data()));
1282         return NULL;
1283       }
1284     }
1285     else{
1286       AliError(Form("Invalid entry! %s",entry.Data()));
1287       return NULL;
1288     }
1289   }
1290   delete arr; arr=0;
1291
1292   AliDebug(2, Form("gridUrl:    %s", gridUrl.Data()));
1293   AliDebug(2, Form("user:       %s", user.Data()));
1294   AliDebug(2, Form("dbFolder:   %s", dbFolder.Data()));
1295   AliDebug(2, Form("s.e.:       %s", se.Data()));
1296   AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1297   AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
1298   AliDebug(2, Form("local cache size: %lld", cacheSize));
1299   AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
1300
1301   if(dbFolder == ""){
1302     AliError("Base folder must be specified!");
1303     return NULL;
1304   }
1305
1306   return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1307       dbFolder.Data(), se.Data(), cacheFolder.Data(),
1308       operateDisconnected, cacheSize, cleanupInterval);
1309 }
1310
1311 //_____________________________________________________________________________
1312 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1313   // create AliCDBGrid storage instance from parameters
1314
1315   AliCDBGrid *grid = 0;
1316   if (AliCDBGridParam::Class() == param->IsA()) {
1317
1318     const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1319     grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1320         gridParam->GetUser().Data(),
1321         gridParam->GetDBFolder().Data(),
1322         gridParam->GetSE().Data(),
1323         gridParam->GetCacheFolder().Data(),
1324         gridParam->GetOperateDisconnected(),
1325         gridParam->GetCacheSize(),
1326         gridParam->GetCleanupInterval());
1327
1328   }
1329
1330   if(!gGrid && grid) {
1331     delete grid; grid=0;
1332   }
1333
1334   return grid;
1335 }
1336
1337 /////////////////////////////////////////////////////////////////////////////////////////////////
1338 //                                                                                             //
1339 // AliCDBGrid Parameter class                                                                  //                                         //
1340 //                                                                                             //
1341 /////////////////////////////////////////////////////////////////////////////////////////////////
1342
1343 ClassImp(AliCDBGridParam)
1344
1345   //_____________________________________________________________________________
1346   AliCDBGridParam::AliCDBGridParam():
1347     AliCDBParam(),
1348     fGridUrl(),
1349     fUser(),
1350     fDBFolder(),
1351     fSE(),
1352     fCacheFolder(),
1353     fOperateDisconnected(),
1354     fCacheSize(),
1355     fCleanupInterval()
1356
1357 {
1358   // default constructor
1359
1360 }
1361
1362 //_____________________________________________________________________________
1363 AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
1364     const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1365     Long64_t cacheSize, Long_t cleanupInterval):
1366   AliCDBParam(),
1367   fGridUrl(gridUrl),
1368   fUser(user),
1369   fDBFolder(dbFolder),
1370   fSE(se),
1371   fCacheFolder(cacheFolder),
1372   fOperateDisconnected(operateDisconnected),
1373   fCacheSize(cacheSize),
1374   fCleanupInterval(cleanupInterval)
1375 {
1376   // constructor
1377
1378   SetType("alien");
1379
1380   TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
1381       "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
1382       fGridUrl.Data(), fUser.Data(),
1383       fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1384       fOperateDisconnected, fCacheSize, fCleanupInterval);
1385
1386   SetURI(uri.Data());
1387 }
1388
1389 //_____________________________________________________________________________
1390 AliCDBGridParam::~AliCDBGridParam() {
1391   // destructor
1392
1393 }
1394
1395 //_____________________________________________________________________________
1396 AliCDBParam* AliCDBGridParam::CloneParam() const {
1397   // clone parameter
1398
1399   return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1400       fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1401       fOperateDisconnected, fCacheSize, fCleanupInterval);
1402 }
1403
1404 //_____________________________________________________________________________
1405 ULong_t AliCDBGridParam::Hash() const {
1406   // return Hash function
1407
1408   return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
1409 }
1410
1411 //_____________________________________________________________________________
1412 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1413   // check if this object is equal to AliCDBParam obj
1414
1415   if (this == obj) {
1416     return kTRUE;
1417   }
1418
1419   if (AliCDBGridParam::Class() != obj->IsA()) {
1420     return kFALSE;
1421   }
1422
1423   AliCDBGridParam* other = (AliCDBGridParam*) obj;
1424
1425   if(fGridUrl != other->fGridUrl) return kFALSE;
1426   if(fUser != other->fUser) return kFALSE;
1427   if(fDBFolder != other->fDBFolder) return kFALSE;
1428   if(fSE != other->fSE) return kFALSE;
1429   if(fCacheFolder != other->fCacheFolder) return kFALSE;
1430   if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1431   if(fCacheSize != other->fCacheSize) return kFALSE;
1432   if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
1433   return kTRUE;
1434 }
1435