]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCDBGrid.cxx
Default values for AliCDBGrid of number of retries (=3) and sleeping time(=5, exponen...
[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) {
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         // specify SE to filename
735         if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
736
737         Int_t nsleep = fInitRetrySeconds;
738         // open file
739         TFile *file=0;
740         AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
741         for(Int_t i=0; i<=fNretry; ++i) {
742                 AliDebug(2, Form("Putting the file in the OCDB: Retry n. %d",i));
743                 file = TFile::Open(fullFilename,"CREATE");
744                 if(!file || !file->IsWritable()){
745                         AliError(Form("Can't open file <%s>!", filename.Data()));
746                         if(file && !file->IsWritable()) file->Close(); delete file; file=0;
747                         if(i==fNretry) {
748                                 AliError(Form("After %d retries, failing putting the object in the OCDB - returning...",i));
749                                 return kFALSE;
750                         }
751                         else {
752                                 AliDebug(2,Form("Retry %d failed, sleeping for %d seconds",i,nsleep));
753                                 sleep(nsleep);
754                         }
755                 } 
756                 else {
757                         AliDebug(2, " Successful!");
758                         break;
759                 }
760                 nsleep*=fInitRetrySeconds;
761         }
762
763         file->cd();
764
765         //SetTreeToFile(entry, file);
766
767         entry->SetVersion(id.GetVersion());
768
769         // write object (key name: "AliCDBEntry")
770         Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
771         if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
772
773
774         if (saveDir) saveDir->cd(); else gROOT->cd();
775         file->Close(); delete file; file=0;
776
777         if(result) {
778         
779                 if(!TagFileId(filename, &id)){
780                         AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
781                         if(!gGrid->Rm(filename.Data()))
782                                 AliError("Can't delete file!");
783                         return kFALSE;
784                 }
785
786                 TagFileMetaData(filename, entry->GetMetaData());
787         }
788
789         AliInfo(Form("CDB object stored into file %s", filename.Data()));
790         AliInfo(Form("Storage Element: %s", fSE.Data()));
791         return result;
792 }
793 //_____________________________________________________________________________
794 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
795 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
796
797         Bool_t result = kTRUE;
798         AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
799         TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
800         TGridResult *gridres = gGrid->Command(addTag.Data());
801         const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
802         if(resCode[0] != '1') {
803                 AliError(Form("Couldn't add %s tags to folder %s !",
804                                                 tagname, folderToTag.Data()));
805                 result = kFALSE;
806         }
807         delete gridres;
808         return result;
809 }
810
811 //_____________________________________________________________________________
812 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
813 // tag stored object in CDB table using object Id's parameters
814
815
816         TString dirname(filename);
817         Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
818         
819         TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
820         TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
821                                         id->GetFirstRun(),
822                                         id->GetLastRun(),
823                                         id->GetVersion());
824         TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
825                                         id->GetPathLevel(0).Data(),
826                                         id->GetPathLevel(1).Data(),
827                                         id->GetPathLevel(2).Data());
828         //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber); 
829         TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber); 
830         TString addTagValue = Form("%s%s%s%s",
831                                         addTagValue1.Data(),
832                                         addTagValue2.Data(),
833                                         addTagValue3.Data(),
834                                         addTagValue4.Data());
835
836         Bool_t result = kFALSE;
837         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
838         TGridResult* res = gGrid->Command(addTagValue.Data());
839         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
840         if(resCode[0] != '1') {
841                 AliError(Form("Couldn't add CDB tag value to file %s !",
842                                                 filename.Data()));
843                 result = kFALSE;
844         } else {
845                 AliDebug(2, "Object successfully tagged.");
846                 result = kTRUE;
847         }
848         delete res;
849         return result;
850
851 }
852
853 //_____________________________________________________________________________
854 Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
855 // tag folder with ShortLived tag
856
857         TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
858
859         Bool_t result = kFALSE;
860         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
861         TGridResult* res = gGrid->Command(addTagValue.Data());
862         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
863         if(resCode[0] != '1') {
864                 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
865                 result = kFALSE;
866         } else {
867                 AliDebug(2,"Object successfully tagged.");
868                 result = kTRUE;
869         }
870         delete res;
871         return result;
872
873 }
874
875 //_____________________________________________________________________________
876 Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
877 // tag stored object in CDB table using object Id's parameters
878
879         TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
880         TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
881                                         md->GetObjectClassName(),
882                                         md->GetResponsible(),
883                                         md->GetBeamPeriod());
884         TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
885                                         md->GetAliRootVersion(),
886                                         md->GetComment());
887         TString addTagValue = Form("%s%s%s",
888                                         addTagValue1.Data(),
889                                         addTagValue2.Data(),
890                                         addTagValue3.Data());
891
892         Bool_t result = kFALSE;
893         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
894         TGridResult* res = gGrid->Command(addTagValue.Data());
895         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
896         if(resCode[0] != '1') {
897                 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
898                                                 filename.Data()));
899                 result = kFALSE;
900         } else {
901                 AliDebug(2,"Object successfully tagged.");
902                 result = kTRUE;
903         }
904         return result;
905 }
906
907 //_____________________________________________________________________________
908 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
909
910         TString turl(fileName);
911         turl.Prepend("/alien" + fDBFolder);
912         turl += "?se="; turl += fSE.Data();
913         TFile *file = TFile::Open(turl);
914         if (!file) {
915                 AliError(Form("Can't open selection file <%s>!", turl.Data()));
916                 return NULL;
917         }
918
919         TList *list = new TList();
920         list->SetOwner();
921         int i=0;
922         TString keycycle;
923
924         AliCDBId *id;
925         while(1){
926                 i++;
927                 keycycle = "AliCDBId;";
928                 keycycle+=i;
929                 
930                 id = (AliCDBId*) file->Get(keycycle);
931                 if(!id) break;
932                 list->AddFirst(id);
933         }
934         file->Close(); delete file; file=0;
935         
936         return list;
937
938
939 }
940
941 //_____________________________________________________________________________
942 Bool_t AliCDBGrid::Contains(const char* path) const{
943 // check for path in storage's DBFolder
944
945         TString initDir(gGrid->Pwd(0));
946         TString dirName(fDBFolder);
947         dirName += path; // dirName = fDBFolder/path
948         Bool_t result=kFALSE;
949         if (gGrid->Cd(dirName,0)) result=kTRUE;
950         gGrid->Cd(initDir.Data(),0);
951         return result;
952 }
953
954 //_____________________________________________________________________________
955 void AliCDBGrid::QueryValidFiles()
956 {
957 // Query the CDB for files valid for AliCDBStorage::fRun
958 // fills list fValidFileIds with AliCDBId objects created from file name
959
960         TString filter;
961         MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
962
963         TString path = fPathFilter.GetPath();
964
965         TString pattern = "Run*";
966         TString optionQuery = "-y";
967         if(fVersion >= 0) {
968                 pattern += Form("_v%d_s0", fVersion);
969                 optionQuery = "";
970         }
971         pattern += ".root";
972         AliDebug(2,Form("pattern: %s", pattern.Data()));
973
974         TString addFolder = "";
975         if (!path.Contains("*")){
976                 if (!path.BeginsWith("/")) addFolder += "/";
977                 addFolder += path;
978         }
979         else{
980                 if (path.BeginsWith("/")) path.Remove(0,1);
981                 if (path.EndsWith("/")) path.Remove(path.Length()-1,1); 
982                 TObjArray* tokenArr = path.Tokenize("/");
983                 if (tokenArr->GetEntries() != 3) {
984                         AliError("Not a 3 level path! Keeping old query...");
985                         pattern.Prepend(path+"/");
986                 }
987                 else{
988                         TString str0 = ((TObjString*)tokenArr->At(0))->String();
989                         TString str1 = ((TObjString*)tokenArr->At(1))->String();
990                         TString str2 = ((TObjString*)tokenArr->At(2))->String();
991                         if (str0 != "*" && str1 != "*" && str2 == "*"){
992                                 // e.g. "ITS/Calib/*"
993                                 addFolder = "/"+str0+"/"+str1;
994                         }
995                         else if (str0 != "*" && str1 == "*" && str2 == "*"){    
996                                 // e.g. "ITS/*/*"
997                                 addFolder = "/"+str0;
998                         }
999                         else if (str0 == "*" && str1 == "*" && str2 == "*"){    
1000                                 // e.g. "*/*/*"
1001                                 // do nothing: addFolder is already an empty string;
1002                         }
1003                         else{
1004                                 // e.g. "ITS/*/RecoParam"
1005                                 pattern.Prepend(path+"/");
1006                         }
1007                 }
1008                 delete tokenArr; tokenArr=0;
1009         }
1010
1011         TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1012
1013         AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1014
1015         if (optionQuery == "-y"){
1016                 AliInfo("Only latest version will be returned");
1017         } 
1018
1019         TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());  
1020
1021         if (!res) {
1022                 AliError("Grid query failed");
1023                 return;
1024         }
1025
1026         TIter next(res);
1027         TMap *map;
1028         while ((map = (TMap*)next())) {
1029           TObjString *entry;
1030           if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
1031             TString& filename = entry->String();
1032             if(filename.IsNull()) continue;
1033             AliDebug(2,Form("Found valid file: %s", filename.Data()));
1034             AliCDBId *validFileId = new AliCDBId;
1035             Bool_t result = FilenameToId(filename, *validFileId);
1036             if(result) {
1037               fValidFileIds.AddLast(validFileId);
1038             }
1039             else {
1040               delete validFileId;
1041             }
1042           }
1043         }
1044         delete res;
1045
1046 }
1047
1048 //_____________________________________________________________________________
1049 void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
1050                                         const AliCDBMetaData* md, TString& result) const
1051 {
1052 // create filter for file query
1053
1054         result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
1055
1056 //      if(version >= 0) {
1057 //              result += Form(" and CDB:version=%d", version);
1058 //      }
1059 //      if(pathFilter.GetLevel0() != "*") {
1060 //              result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
1061 //      }
1062 //      if(pathFilter.GetLevel1() != "*") {
1063 //              result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
1064 //      }
1065 //      if(pathFilter.GetLevel2() != "*") {
1066 //              result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
1067 //      }
1068
1069         if(md){
1070                 if(md->GetObjectClassName()[0] != '\0') {
1071                         result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
1072                 }
1073                 if(md->GetResponsible()[0] != '\0') {
1074                         result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
1075                 }
1076                 if(md->GetBeamPeriod() != 0) {
1077                         result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
1078                 }
1079                 if(md->GetAliRootVersion()[0] != '\0') {
1080                         result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
1081                 }
1082                 if(md->GetComment()[0] != '\0') {
1083                         result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
1084                 }
1085         }
1086         AliDebug(2, Form("filter: %s",result.Data()));
1087
1088 }
1089
1090 //_____________________________________________________________________________
1091 Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
1092 // get last version found in the database valid for run and path
1093
1094         AliCDBPath aCDBPath(path);
1095         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
1096                 AliError(Form("Invalid path in request: %s", path));
1097                 return -1;
1098         }
1099         AliCDBId query(path, run, run, -1, -1);
1100
1101         AliCDBId* dataId = 0;
1102         // look for file matching query requests (path, runRange, version)
1103         if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
1104                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
1105                 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
1106                                         run, path));
1107                 dataId = GetId(fValidFileIds, query);
1108                 if (!dataId) return -1;
1109                 Int_t version = dataId->GetVersion();
1110                 delete dataId;
1111                 return version;
1112
1113         }
1114         // List of files valid for reqested run was not loaded. Looking directly into CDB
1115         AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
1116                                 run, path));
1117
1118         TObjArray validFileIds;
1119         validFileIds.SetOwner(1);
1120
1121         TString filter;
1122         MakeQueryFilter(run, run, 0, filter);
1123
1124         TString pattern = ".root";
1125
1126         TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),path));
1127
1128         AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1129         TGridResult *res = gGrid->Query(folderCopy, pattern, filter, "-y -m");
1130
1131         AliCDBId validFileId;
1132         if (res->GetEntries()>1){
1133                 AliWarning("Number of found entries >1, even if option -y was used");
1134                 for(int i=0; i<res->GetEntries(); i++){
1135                         TString filename = res->GetKey(i, "lfn");
1136                         if(filename == "") continue;
1137                         if(FilenameToId(filename, validFileId))
1138                                 validFileIds.AddLast(validFileId.Clone());
1139                 }
1140                 dataId = GetId(validFileIds, query);
1141                 if (!dataId) return -1;
1142                 
1143                 Int_t version = dataId->GetVersion();
1144                 delete dataId;
1145                 return version;
1146         }
1147         else if (res->GetEntries()==1){
1148                 TString filename = res->GetKey(0, "lfn");
1149                 if(filename == "") {
1150                         AliError("The only entry found has filename empty");
1151                         return -1;
1152                 }
1153                 if(FilenameToId(filename, validFileId)) return validFileId.GetVersion();
1154                 else{
1155                         AliError("Impossible to get FileId from filename");
1156                         return -1;
1157                 }
1158         }
1159         else {
1160                 AliError("No entries found");
1161                 return -1;
1162         }
1163
1164         delete res;
1165
1166
1167 }
1168
1169 //_____________________________________________________________________________
1170 Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
1171 // get last subversion found in the database valid for run and path
1172         AliError("Objects in GRID storage have no sub version!");
1173         return -1;
1174 }
1175
1176
1177 /////////////////////////////////////////////////////////////////////////////////////////////////
1178 //                                                                                             //
1179 // AliCDBGrid factory                                                                          //
1180 //                                                                                             //
1181 /////////////////////////////////////////////////////////////////////////////////////////////////
1182
1183 ClassImp(AliCDBGridFactory)
1184
1185 //_____________________________________________________________________________
1186 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1187 // check if the string is valid Grid URI
1188
1189         TRegexp gridPattern("^alien://.+$");
1190
1191         return TString(gridString).Contains(gridPattern);
1192 }
1193
1194 //_____________________________________________________________________________
1195 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1196 // create AliCDBGridParam class from the URI string
1197
1198         if (!Validate(gridString)) {
1199                 return NULL;
1200         }
1201
1202         TString buffer(gridString);
1203
1204         TString gridUrl         = "alien://";
1205         TString user            = "";
1206         TString dbFolder        = "";
1207         TString se              = "default";
1208         TString cacheFolder     = "";
1209         Bool_t  operateDisconnected = kTRUE;
1210         Long64_t cacheSize          = (UInt_t) 1024*1024*1024; // 1GB
1211         Long_t cleanupInterval      = 0;
1212
1213         TObjArray *arr = buffer.Tokenize('?');
1214         TIter iter(arr);
1215         TObjString *str = 0;
1216
1217         while((str = (TObjString*) iter.Next())){
1218                 TString entry(str->String());
1219                 Int_t indeq = entry.Index('=');
1220                 if(indeq == -1) {
1221                         if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1222                                 gridUrl = entry;
1223                                 continue;
1224                         } else {
1225                                 AliError(Form("Invalid entry! %s",entry.Data()));
1226                                 continue;
1227                         }
1228                 }
1229                 
1230                 TString key = entry(0,indeq);
1231                 TString value = entry(indeq+1,entry.Length()-indeq);
1232
1233                 if(key.Contains("grid",TString::kIgnoreCase)) {
1234                         gridUrl += value;
1235                 }
1236                 else if (key.Contains("user",TString::kIgnoreCase)){
1237                         user = value;
1238                 }
1239                 else if (key.Contains("se",TString::kIgnoreCase)){
1240                         se = value;
1241                 }
1242                 else if (key.Contains("cacheF",TString::kIgnoreCase)){
1243                         cacheFolder = value;
1244                         if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
1245                                 cacheFolder += "/";
1246                 }
1247                 else if (key.Contains("folder",TString::kIgnoreCase)){
1248                         dbFolder = value;
1249                 }
1250                 else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1251                         if(value == "kTRUE") {
1252                                 operateDisconnected = kTRUE;
1253                         } else if (value == "kFALSE") {
1254                                 operateDisconnected = kFALSE;
1255                         } else if (value == "0" || value == "1") {
1256                                 operateDisconnected = (Bool_t) value.Atoi();
1257                         } else {
1258                                 AliError(Form("Invalid entry! %s",entry.Data()));
1259                                 return NULL;
1260                         }
1261                 }
1262                 else if (key.Contains("cacheS",TString::kIgnoreCase)){
1263                         if(value.IsDigit()) {
1264                                 cacheSize = value.Atoi();
1265                         } else {
1266                                 AliError(Form("Invalid entry! %s",entry.Data()));
1267                                 return NULL;
1268                         }
1269                 }
1270                 else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1271                         if(value.IsDigit()) {
1272                                 cleanupInterval = value.Atoi();
1273                         } else {
1274                                 AliError(Form("Invalid entry! %s",entry.Data()));
1275                                 return NULL;
1276                         }
1277                 }
1278                 else{
1279                         AliError(Form("Invalid entry! %s",entry.Data()));
1280                         return NULL;
1281                 }
1282         }
1283         delete arr; arr=0;
1284
1285         AliDebug(2, Form("gridUrl:      %s", gridUrl.Data()));
1286         AliDebug(2, Form("user: %s", user.Data()));
1287         AliDebug(2, Form("dbFolder:     %s", dbFolder.Data()));
1288         AliDebug(2, Form("s.e.: %s", se.Data()));
1289         AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1290         AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
1291         AliDebug(2, Form("local cache size: %lld", cacheSize));
1292         AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
1293
1294         if(dbFolder == ""){
1295                 AliError("Base folder must be specified!");
1296                 return NULL;
1297         }
1298
1299         return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1300                           dbFolder.Data(), se.Data(), cacheFolder.Data(),
1301                           operateDisconnected, cacheSize, cleanupInterval);
1302 }
1303
1304 //_____________________________________________________________________________
1305 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1306 // create AliCDBGrid storage instance from parameters
1307         
1308         AliCDBGrid *grid = 0;
1309         if (AliCDBGridParam::Class() == param->IsA()) {
1310
1311                 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1312                 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1313                                       gridParam->GetUser().Data(),
1314                                       gridParam->GetDBFolder().Data(),
1315                                       gridParam->GetSE().Data(),
1316                                       gridParam->GetCacheFolder().Data(),
1317                                       gridParam->GetOperateDisconnected(),
1318                                       gridParam->GetCacheSize(),
1319                                       gridParam->GetCleanupInterval());
1320
1321         }
1322
1323         if(!gGrid && grid) {
1324                 delete grid; grid=0;
1325         }
1326
1327         return grid;
1328 }
1329
1330 /////////////////////////////////////////////////////////////////////////////////////////////////
1331 //                                                                                             //
1332 // AliCDBGrid Parameter class                                                                  //                                         //
1333 //                                                                                             //
1334 /////////////////////////////////////////////////////////////////////////////////////////////////
1335
1336 ClassImp(AliCDBGridParam)
1337
1338 //_____________________________________________________________________________
1339 AliCDBGridParam::AliCDBGridParam():
1340  AliCDBParam(),
1341  fGridUrl(),
1342  fUser(),
1343  fDBFolder(),
1344  fSE(),
1345  fCacheFolder(),
1346  fOperateDisconnected(),
1347  fCacheSize(),
1348  fCleanupInterval()
1349
1350  {
1351 // default constructor
1352
1353 }
1354
1355 //_____________________________________________________________________________
1356 AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
1357                                 const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1358                                 Long64_t cacheSize, Long_t cleanupInterval):
1359  AliCDBParam(),
1360  fGridUrl(gridUrl),
1361  fUser(user),
1362  fDBFolder(dbFolder),
1363  fSE(se),
1364  fCacheFolder(cacheFolder),
1365  fOperateDisconnected(operateDisconnected),
1366  fCacheSize(cacheSize),
1367  fCleanupInterval(cleanupInterval)
1368 {
1369 // constructor
1370
1371         SetType("alien");
1372
1373         TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
1374                         "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
1375                         fGridUrl.Data(), fUser.Data(),
1376                         fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1377                         fOperateDisconnected, fCacheSize, fCleanupInterval);
1378
1379         SetURI(uri.Data());
1380 }
1381
1382 //_____________________________________________________________________________
1383 AliCDBGridParam::~AliCDBGridParam() {
1384 // destructor
1385
1386 }
1387
1388 //_____________________________________________________________________________
1389 AliCDBParam* AliCDBGridParam::CloneParam() const {
1390 // clone parameter
1391
1392         return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1393                                         fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1394                                         fOperateDisconnected, fCacheSize, fCleanupInterval);
1395 }
1396
1397 //_____________________________________________________________________________
1398 ULong_t AliCDBGridParam::Hash() const {
1399 // return Hash function
1400
1401         return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
1402 }
1403
1404 //_____________________________________________________________________________
1405 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1406 // check if this object is equal to AliCDBParam obj
1407
1408         if (this == obj) {
1409                 return kTRUE;
1410         }
1411
1412         if (AliCDBGridParam::Class() != obj->IsA()) {
1413                 return kFALSE;
1414         }
1415
1416         AliCDBGridParam* other = (AliCDBGridParam*) obj;
1417
1418         if(fGridUrl != other->fGridUrl) return kFALSE;
1419         if(fUser != other->fUser) return kFALSE;
1420         if(fDBFolder != other->fDBFolder) return kFALSE;
1421         if(fSE != other->fSE) return kFALSE;
1422         if(fCacheFolder != other->fCacheFolder) return kFALSE;
1423         if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1424         if(fCacheSize != other->fCacheSize) return kFALSE;
1425         if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
1426         return kTRUE;
1427 }
1428