Updated version of the CDB classes. Bug fix in AliCDBGrid. New AliCDBStorage function...
[u/mrichter/AliRoot.git] / STEER / 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
24 #include <TGrid.h>
25 #include <TGridResult.h>
26 #include <TFile.h>
27 #include <TKey.h>
28 #include <TROOT.h>
29 #include <TSystem.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
32 #include <TRegexp.h>
33
34 #include "AliLog.h"
35 #include "AliCDBEntry.h"
36 #include "AliCDBGrid.h"
37
38
39 ClassImp(AliCDBGrid)
40
41 //_____________________________________________________________________________
42 AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder, const char *se) :
43 AliCDBStorage(),
44 fGridUrl(gridUrl),
45 fUser(user),
46 fDBFolder(dbFolder),
47 fSE(se)
48 {
49 // constructor //
50
51         // if the same Grid is alreay active, skip connection
52         if (!gGrid || fGridUrl != gGrid->GridUrl()  
53              || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
54                 // connection to the Grid
55                 AliInfo("Connection to the Grid...");
56                 if(gGrid){
57                         AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
58                         AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
59                 }
60                 TGrid::Connect(fGridUrl.Data(),fUser.Data());
61         }
62
63         if(!gGrid) {
64                 AliError("Connection failed!");
65                 return;
66         }
67
68         TString initDir(gGrid->Pwd(0));
69         if (fDBFolder[0] != '/') {
70                 fDBFolder.Prepend(initDir);
71         }
72
73         // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
74         if(!gGrid->Cd(fDBFolder.Data(),0)){
75                 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
76                 if(!gGrid->Mkdir(fDBFolder.Data(),"",0)){
77                         AliError(Form("Cannot create folder <%s> !",fDBFolder.Data())); 
78                 }
79         } else {
80                 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
81         }
82
83         // removes any '/' at the end of path, then append one '/'
84         while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/')); 
85         fDBFolder+="/";
86
87         fType="alien";
88         fBaseFolder = fDBFolder;
89
90         // return to the initial directory
91         gGrid->Cd(initDir.Data(),0);
92 }
93
94 //_____________________________________________________________________________
95 AliCDBGrid::~AliCDBGrid()
96 {
97 // destructor
98         delete gGrid; gGrid=0;
99
100 }
101
102 //_____________________________________________________________________________
103 Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
104 // build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z.root)
105
106         if(filename.Contains(fDBFolder)){
107                 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
108         }
109
110         TString idPath = filename(0,filename.Last('/'));
111         id.SetPath(idPath);
112         if(!id.IsValid()) return kFALSE;
113
114         filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
115
116         Ssiz_t mSize;
117         // valid filename: Run#firstRun_#lastRun_v#version.root
118         TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
119         keyPattern.Index(filename, &mSize);
120         if (!mSize) {
121                 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
122                 return kFALSE;
123         }
124
125         filename.Resize(filename.Length() - sizeof(".root") + 1);
126
127         TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
128
129         TString firstRunString(((TObjString*) strArray->At(0))->GetString());
130         id.SetFirstRun(atoi(firstRunString.Data() + 3));
131         id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
132
133         TString verString(((TObjString*) strArray->At(2))->GetString());
134         id.SetVersion(atoi(verString.Data() + 1));
135
136         delete strArray;
137
138         return kTRUE;
139 }
140
141 //_____________________________________________________________________________
142 Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
143 // build file name from AliCDBId (path, run range, version) and fDBFolder
144
145         if (!id.GetAliCDBRunRange().IsValid()) {
146                 AliDebug(2,Form("Invalid run range <%d, %d>.",
147                         id.GetFirstRun(), id.GetLastRun()));
148                 return kFALSE;
149         }
150
151         if (id.GetVersion() < 0) {
152                 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
153                 return kFALSE;
154         }
155
156         filename = Form("Run%d_%d_v%d.root",
157                                 id.GetFirstRun(),
158                                 id.GetLastRun(),
159                                 id.GetVersion());
160
161         filename.Prepend(fDBFolder + id.GetPath() + '/');
162
163         return kTRUE;
164 }
165
166 //_____________________________________________________________________________
167 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
168 // prepare id (version) of the object that will be stored (called by PutEntry)
169
170         TString initDir(gGrid->Pwd(0));
171         TString pathName= id.GetPath();
172
173         TString dirName(fDBFolder);
174
175         Bool_t dirExist=kFALSE;
176  
177         // go to the path; if directory does not exist, create it
178         TObjArray *arrName=pathName.Tokenize("/");
179         for(int i=0;i<arrName->GetEntries();i++){
180                 TString buffer((arrName->At(i))->GetName());
181                 dirName+=buffer; dirName+="/";
182                 dirExist=gGrid->Cd(dirName,0);
183                 if (!dirExist) {
184                         AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
185                         if(!gGrid->Mkdir(dirName,"",0)){
186                                 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
187                                 gGrid->Cd(initDir.Data());
188                         return kFALSE;
189                         }
190                 }
191         }
192         delete arrName;
193         gGrid->Cd(initDir,0);
194
195         TString filename;
196         AliCDBId anId; // the id got from filename
197         AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
198         Int_t lastVersion=0; // highest version found
199
200         TGridResult *res = gGrid->Ls(dirName);
201
202         //loop on the files in the directory, look for highest version
203         for(int i=0; i < res->GetEntries(); i++){
204                 filename=res->GetFileNamePath(i);
205                 if (!FilenameToId(filename, anId)) continue;
206                 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
207                         lastVersion = anId.GetVersion();
208                         lastRunRange = anId.GetAliCDBRunRange();
209                 }
210
211         }
212         delete res; 
213  
214         id.SetVersion(lastVersion + 1);
215
216         TString lastStorage = id.GetLastStorage();
217         if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
218                 AliDebug(2, Form("A NEW object is being stored with version %d",
219                                         id.GetVersion()));
220                 AliDebug(2, Form("and it will hide previously stored object with version %d!",
221                                         id.GetVersion()-1));
222         }
223
224         if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange()))) 
225                 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
226                         lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
227     
228         return kTRUE;
229 }
230
231 //_____________________________________________________________________________
232 AliCDBId* AliCDBGrid::GetId(const TList& validFileIds, const AliCDBId& query) {
233 // look for the Id that matches query's requests (highest or exact version)
234
235         if(validFileIds.GetEntries() < 1) {
236                 return NULL;
237         } else if (validFileIds.GetEntries() == 1) {
238                 return dynamic_cast<AliCDBId*> (validFileIds.At(0));
239         }
240
241         TIter iter(&validFileIds);
242
243         AliCDBId *anIdPtr=0;
244         AliCDBId* result=0;
245
246         while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
247                 if(anIdPtr->GetPath() != query.GetPath()) continue;
248
249                 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
250
251                 if (!query.HasVersion()){ // look for highest version
252                         if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
253                         if(result && result->GetVersion() == anIdPtr->GetVersion()) {
254                                 AliDebug(2,Form("More than one object valid for run %d, version %d!",
255                                         query.GetFirstRun(), anIdPtr->GetVersion()));
256                                 return NULL;
257                         }
258                         result = anIdPtr;
259                 } else { // look for specified version
260                         if(query.GetVersion() != anIdPtr->GetVersion()) continue;
261                         if(result && result->GetVersion() == anIdPtr->GetVersion()){
262                                 AliDebug(2,Form("More than one object valid for run %d, version %d!",
263                                         query.GetFirstRun(), anIdPtr->GetVersion()));
264                                 return NULL;
265                         }
266                         result = anIdPtr;
267                 }
268
269         }
270
271
272         return result;
273 }
274
275 //_____________________________________________________________________________
276 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
277 // get AliCDBEntry from the database
278
279         AliCDBId* dataId=0;
280
281         AliCDBId selectedId(queryId);
282         if (!selectedId.HasVersion()) {
283                 // if version is not specified, first check the selection criteria list
284                 GetSelection(&selectedId);
285         }
286
287         TList validFileIds;
288         validFileIds.SetOwner(1);
289
290         // look for file matching query requests (path, runRange, version)
291         if(selectedId.GetFirstRun() == fRun &&
292                         fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
293                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
294                 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
295                                         selectedId.GetFirstRun(), selectedId.GetPath().Data()));
296                 dataId = GetId(fValidFileIds, selectedId);
297
298         } else {
299                 // List of files valid for reqested run was not loaded. Looking directly into CDB
300                 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
301                                         selectedId.GetFirstRun(), selectedId.GetPath().Data()));
302
303                 TString filter;
304                 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(),
305                                 selectedId.GetAliCDBPath(), selectedId.GetVersion(), 0, filter);
306
307                 TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
308                 AliCDBId validFileId;
309                 for(int i=0; i<res->GetEntries(); i++){
310                         TString filename = res->GetKey(i, "lfn");
311                         if(filename == "") continue;
312                         if(FilenameToId(filename, validFileId))
313                                         validFileIds.AddLast(validFileId.Clone());
314                 }
315                 delete res;
316                 dataId = GetId(validFileIds, selectedId);
317         }
318
319         if (!dataId) return NULL;
320
321         TString filename;
322         if (!IdToFilename(*dataId, filename)) {
323                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
324                 return NULL;
325         }
326
327         AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
328
329         return anEntry;
330 }
331
332 //_____________________________________________________________________________
333 AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, const AliCDBId* dataId){
334 // Get AliCBEntry object from file "filename"
335
336         AliDebug(2,Form("Opening file: %s",filename.Data()));
337
338         filename.Prepend("/alien");
339         TFile *file = TFile::Open(filename);
340         if (!file) {
341                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
342                 return NULL;
343         }
344
345         // get the only AliCDBEntry object from the file
346         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
347
348         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
349
350         if (!anEntry) {
351                 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
352                 file->Close();
353                 return NULL;
354         }
355
356         // The object's Id is not reset during storage
357         // If object's Id runRange or version do not match with filename,
358         // it means that someone renamed file by hand. In this case a warning msg is issued.
359
360         if(anEntry){
361                 AliCDBId entryId = anEntry->GetId();
362                 if(!((entryId.GetAliCDBRunRange()).IsEqual(&(dataId->GetAliCDBRunRange()))) ||
363                         entryId.GetVersion() != dataId->GetVersion()){
364                         AliWarning(Form("Either RunRange or gridVersion in the object's metadata"));
365                         AliWarning(Form("do noth match with fileName numbers:"));
366                         AliWarning(Form("someone renamed file by hand!"));
367                 }
368         }
369
370         anEntry->SetLastStorage("grid");
371
372         // close file, return retieved entry
373         file->Close(); delete file; file=0;
374
375         return anEntry;
376 }
377
378 //_____________________________________________________________________________
379 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
380 // multiple request (AliCDBStorage::GetAll)
381
382         TList* result = new TList();
383         result->SetOwner();
384
385         TList validFileIds;
386         validFileIds.SetOwner(1);
387
388         Bool_t alreadyLoaded = kFALSE;
389
390         // look for file matching query requests (path, runRange)
391         if(queryId.GetFirstRun() == fRun &&
392                         fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
393                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
394                 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
395                                         queryId.GetFirstRun(), queryId.GetPath().Data()));
396
397                 alreadyLoaded = kTRUE;
398
399         } else {
400                 // List of files valid for reqested run was not loaded. Looking directly into CDB
401                 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
402                                         queryId.GetFirstRun(), queryId.GetPath().Data()));
403
404                 TString filter;
405                 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(),
406                                 queryId.GetAliCDBPath(), queryId.GetVersion(), 0, filter);
407
408                 TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
409                 AliCDBId validFileId;
410                 for(int i=0; i<res->GetEntries(); i++){
411                         TString filename = res->GetKey(i, "lfn");
412                         if(filename == "") continue;
413                         if(FilenameToId(filename, validFileId))
414                                         validFileIds.AddLast(validFileId.Clone());
415                 }
416                 delete res;
417         }
418
419         TIter *iter=0;
420         if(alreadyLoaded){
421                 iter = new TIter(&fValidFileIds);
422         } else {
423                 iter = new TIter(&validFileIds);
424         }
425
426         TList selectedIds;
427         selectedIds.SetOwner(1);
428
429         // loop on list of valid Ids to select the right version to get.
430         // According to query and to the selection criteria list, version can be the highest or exact
431         AliCDBPath pathCopy;
432         AliCDBId* anIdPtr=0;
433         AliCDBId* dataId=0;
434         AliCDBPath queryPath = queryId.GetAliCDBPath();
435         while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
436                 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
437                 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
438                 pathCopy = thisCDBPath;
439
440                 // check the selection criteria list for this query
441                 AliCDBId thisId(*anIdPtr);
442                 thisId.SetVersion(queryId.GetVersion());
443                 if(!thisId.HasVersion()) GetSelection(&thisId);
444
445                 if(alreadyLoaded){
446                         dataId = GetId(fValidFileIds, thisId);
447                 } else {
448                         dataId = GetId(validFileIds, thisId);
449                 }
450                 if(dataId) selectedIds.Add(dataId->Clone());
451         }
452
453         delete iter; iter=0;
454
455         // selectedIds contains the Ids of the files matching all requests of query!
456         // All the objects are now ready to be retrieved
457         iter = new TIter(&selectedIds);
458         while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
459                 TString filename;
460                 if (!IdToFilename(*anIdPtr, filename)) {
461                         AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
462                         continue;
463                 }
464
465                 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
466
467                 if(anEntry) result->Add(anEntry);
468
469         }
470         delete iter; iter=0;
471
472         return result;
473 }
474
475 //_____________________________________________________________________________
476 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
477 // put an AliCDBEntry object into the database
478
479         AliCDBId& id = entry->GetId();
480
481         // set version for the entry to be stored
482         if (!PrepareId(id)) return kFALSE;
483
484         // build filename from entry's id
485         TString filename;
486         if (!IdToFilename(id, filename)) {
487                 AliError("Bad ID encountered! Subnormal error!");
488                 return kFALSE;
489         }
490
491         TString folderToTag = Form("%s%s",
492                                         fDBFolder.Data(),
493                                         id.GetPath().Data());
494
495         // add CDB and CDB_MD tag to folder
496         // TODO how to check that folder has already tags?
497         AddTag(folderToTag,"CDB");
498         AddTag(folderToTag,"CDB_MD");
499
500         TDirectory* saveDir = gDirectory;
501
502         // specify SE to filename
503         TString fullFilename = Form("/alien%s?se=%s", filename.Data(), fSE.Data());
504
505         // open file
506         TFile *file = TFile::Open(fullFilename,"CREATE");
507         if(!file || !file->IsWritable()){
508                 AliError(Form("Can't open file <%s>!", filename.Data()));
509                 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
510                 return kFALSE;
511         }
512
513         file->cd();
514
515         entry->SetVersion(id.GetVersion());
516
517         // write object (key name: "AliCDBEntry")
518         Bool_t result = (entry->Write("AliCDBEntry") != 0); 
519         if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
520
521
522         if (saveDir) saveDir->cd(); else gROOT->cd();
523         file->Close(); delete file; file=0;
524
525         if(result) {
526                 AliInfo(Form("CDB object stored into file %s", filename.Data()));
527                 AliInfo(Form("using S.E. %s", fSE.Data()));
528
529                 if(!TagFileId(filename, &id)){
530                         AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
531                         if(!gGrid->Rm(filename.Data()))
532                                 AliError("Can't delete file!");
533                         return kFALSE;
534                 }
535
536                 TagFileMetaData(filename, entry->GetMetaData());
537         }
538
539         return result;
540 }
541 //_____________________________________________________________________________
542 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
543 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
544
545         Bool_t result = kTRUE;
546         AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
547         TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
548         TGridResult *gridres = gGrid->Command(addTag.Data());
549         const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
550         if(resCode[0] != '1') {
551                 AliError(Form("Couldn't add %s tags to folder %s !",
552                                                 tagname, folderToTag.Data()));
553                 result = kFALSE;
554         }
555         delete gridres;
556         return result;
557 }
558
559 //_____________________________________________________________________________
560 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
561 // tag stored object in CDB table using object Id's parameters
562
563         TString addTagValue_1 = Form("addTagValue %s CDB ", filename.Data());
564         TString addTagValue_2 = Form("first_run=%d last_run=%d version=%d ",
565                                         id->GetFirstRun(),
566                                         id->GetLastRun(),
567                                         id->GetVersion());
568         TString addTagValue_3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
569                                         id->GetLevel0().Data(),
570                                         id->GetLevel1().Data(),
571                                         id->GetLevel2().Data());
572         TString addTagValue = Form("%s%s%s",
573                                         addTagValue_1.Data(),
574                                         addTagValue_2.Data(),
575                                         addTagValue_3.Data());
576
577         Bool_t result = kFALSE;
578         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
579         TGridResult* res = gGrid->Command(addTagValue.Data());
580         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
581         if(resCode[0] != '1') {
582                 AliError(Form("Couldn't add CDB tag value to file %s !",
583                                                 filename.Data()));
584                 result = kFALSE;
585         } else {
586                 AliInfo("Object successfully tagged.");
587                 result = kTRUE;
588         }
589         delete res;
590         return result;
591
592 }
593
594 //_____________________________________________________________________________
595 Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
596 // tag stored object in CDB table using object Id's parameters
597
598         TString addTagValue_1 = Form("addTagValue %s CDB_MD ", filename.Data());
599         TString addTagValue_2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
600                                         md->GetObjectClassName(),
601                                         md->GetResponsible(),
602                                         md->GetBeamPeriod());
603         TString addTagValue_3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
604                                         md->GetAliRootVersion(),
605                                         md->GetComment());
606         TString addTagValue = Form("%s%s%s",
607                                         addTagValue_1.Data(),
608                                         addTagValue_2.Data(),
609                                         addTagValue_3.Data());
610
611         Bool_t result = kFALSE;
612         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
613         TGridResult* res = gGrid->Command(addTagValue.Data());
614         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
615         if(resCode[0] != '1') {
616                 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
617                                                 filename.Data()));
618                 result = kFALSE;
619         } else {
620                 AliInfo("Object successfully tagged.");
621                 result = kTRUE;
622         }
623         return result;
624 }
625
626 //_____________________________________________________________________________
627 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
628
629         TString turl(fileName);
630         turl.Prepend("/alien" + fDBFolder);
631         turl += "?se="; turl += fSE.Data();
632         TFile *file = TFile::Open(turl);
633         if (!file) {
634                 AliError(Form("Can't open selection file <%s>!", turl.Data()));
635                 return NULL;
636         }
637
638         TList *list = new TList();
639         list->SetOwner();
640         int i=0;
641         TString keycycle;
642
643         AliCDBId *id;
644         while(1){
645                 i++;
646                 keycycle = "AliCDBId;";
647                 keycycle+=i;
648                 
649                 id = (AliCDBId*) file->Get(keycycle);
650                 if(!id) break;
651                 list->AddFirst(id);
652         }
653         file->Close(); delete file; file=0;
654         
655         return list;
656
657
658 }
659
660 //_____________________________________________________________________________
661 Bool_t AliCDBGrid::Contains(const char* path) const{
662 // check for path in storage's DBFolder
663
664         TString initDir(gGrid->Pwd(0));
665         TString dirName(fDBFolder);
666         dirName += path; // dirName = fDBFolder/path
667         Bool_t result=kFALSE;
668         if (gGrid->Cd(dirName,0)) result=kTRUE;
669         gGrid->Cd(initDir.Data(),0);
670         return result;
671 }
672
673 //_____________________________________________________________________________
674 void AliCDBGrid::QueryValidFiles()
675 {
676 // Query the CDB for files valid for AliCDBStorage::fRun
677 // fills list fValidFileIds with AliCDBId objects created from file name
678
679         TString filter;
680         MakeQueryFilter(fRun, fRun, fPathFilter, fVersion, fMetaDataFilter, filter);
681
682         TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
683         AliCDBId validFileId;
684         for(int i=0; i<res->GetEntries(); i++){
685                 TString filename = res->GetKey(i, "lfn");
686                 if(filename == "") continue;
687                 AliDebug(2,Form("Found valid file: %s", filename.Data()));
688                 Bool_t result = FilenameToId(filename, validFileId);
689                 if(result) {
690                         fValidFileIds.AddLast(validFileId.Clone());
691                 }
692         }
693         delete res;
694
695 }
696
697 //_____________________________________________________________________________
698 void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
699                                         const AliCDBPath& pathFilter, Int_t version,
700                                         const AliCDBMetaData* md, TString& result) const
701 {
702 // create filter for file query
703
704         result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
705
706         if(version >= 0) {
707                 result += Form(" and CDB:version=%d", version);
708         }
709         if(pathFilter.GetLevel0() != "*") {
710                 result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
711         }
712         if(pathFilter.GetLevel1() != "*") {
713                 result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
714         }
715         if(pathFilter.GetLevel2() != "*") {
716                 result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
717         }
718
719         if(md){
720                 if(md->GetObjectClassName()[0] != '\0') {
721                         result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
722                 }
723                 if(md->GetResponsible()[0] != '\0') {
724                         result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
725                 }
726                 if(md->GetBeamPeriod() != 0) {
727                         result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
728                 }
729                 if(md->GetAliRootVersion()[0] != '\0') {
730                         result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
731                 }
732                 if(md->GetComment()[0] != '\0') {
733                         result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
734                 }
735         }
736         AliDebug(2, Form("filter: %s",result.Data()));
737
738 }
739
740 //_____________________________________________________________________________
741 Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
742 // get last version found in the database valid for run and path
743
744         TList validFileIds;
745         validFileIds.SetOwner(1);
746
747         AliCDBPath aCDBPath(path);
748         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
749                 AliError(Form("Invalid path in request: %s", path));
750                 return -1;
751         }
752         AliCDBId query(path, run, run, -1, -1);
753         AliCDBId* dataId = 0;
754
755         // look for file matching query requests (path, runRange, version)
756         if(run == fRun &&
757                         fPathFilter.Comprises(aCDBPath) && fVersion < 0){
758                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
759                 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
760                                         run, path));
761                 dataId = GetId(fValidFileIds, query);
762                 if (!dataId) return -1;
763                 return dataId->GetVersion();
764
765         }
766         // List of files valid for reqested run was not loaded. Looking directly into CDB
767         AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
768                                 run, path));
769
770         TString filter;
771         MakeQueryFilter(run, run, aCDBPath, -1, 0, filter);
772
773         TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
774         AliCDBId validFileId;
775         for(int i=0; i<res->GetEntries(); i++){
776                 TString filename = res->GetKey(i, "lfn");
777                 if(filename == "") continue;
778                 if(FilenameToId(filename, validFileId))
779                                 validFileIds.AddLast(validFileId.Clone());
780         }
781         delete res;
782
783         dataId = GetId(validFileIds, query);
784         if (!dataId) return -1;
785
786         return dataId->GetVersion();
787
788 }
789
790 //_____________________________________________________________________________
791 Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
792 // get last subversion found in the database valid for run and path
793         AliError("Objects in GRID storage have no sub version!");
794         return -1;
795 }
796
797
798 /////////////////////////////////////////////////////////////////////////////////////////////////
799 //                                                                                             //
800 // AliCDBGrid factory                                                                          //
801 //                                                                                             //
802 /////////////////////////////////////////////////////////////////////////////////////////////////
803
804 ClassImp(AliCDBGridFactory)
805
806 //_____________________________________________________________________________
807 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
808 // check if the string is valid Grid URI
809
810         // pattern: alien://hostName:Port;user;dbPath;SE
811         // example of a valid pattern:
812         // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
813 //        TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
814         TRegexp gridPattern("^alien://.+$");
815
816         return TString(gridString).Contains(gridPattern);
817 }
818
819 //_____________________________________________________________________________
820 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
821 // create AliCDBGridParam class from the URI string
822
823         if (!Validate(gridString)) {
824                 return NULL;
825         }
826         //TString buffer(gridString + sizeof("alien://") - 1);
827         TString buffer(gridString);
828
829         TString gridUrl         = "alien://";
830         TString user            = "";
831         TString dbFolder        = "DBGrid";
832         TString se              = "ALICE::CERN::se01";
833
834         TObjArray *arr = buffer.Tokenize('?');
835         TIter iter(arr);
836         TObjString *str = 0;
837         
838         while((str = (TObjString*) iter.Next())){
839                 TString entry(str->String());
840                 Int_t indeq = entry.Index('=');
841                 if(indeq == -1) {
842                         if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
843                                 gridUrl = entry;
844                                 continue;
845                         } else {
846                                 AliError(Form("Invalid entry! %s",entry.Data()));
847                                 continue;
848                         }
849                 }
850                 
851                 TString key = entry(0,indeq);
852                 TString value = entry(indeq+1,entry.Length()-indeq);
853
854                 if(key.Contains("grid",TString::kIgnoreCase)) {
855                         gridUrl += value;
856                 } 
857                 else if (key.Contains("user",TString::kIgnoreCase)){
858                         user = value;
859                 }
860                 else if (key.Contains("folder",TString::kIgnoreCase)){
861                         dbFolder = value;
862                 }
863                 else if (key.Contains("se",TString::kIgnoreCase)){
864                         se = value;
865                 }
866                 else{
867                         AliError(Form("Invalid entry! %s",entry.Data()));
868                 }
869         }
870         delete arr; arr=0;
871                 
872         AliDebug(2, Form("gridUrl:      %s",gridUrl.Data()));
873         AliDebug(2, Form("user: %s",user.Data()));
874         AliDebug(2, Form("dbFolder:     %s",dbFolder.Data()));
875         AliDebug(2, Form("s.e.: %s",se.Data()));
876
877         return new AliCDBGridParam(gridUrl.Data(), user.Data(), dbFolder.Data(), se.Data());
878 }
879
880 //_____________________________________________________________________________
881 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
882 // create AliCDBGrid storage instance from parameters
883         
884         AliCDBGrid *grid = 0;
885         if (AliCDBGridParam::Class() == param->IsA()) {
886
887                 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
888                 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
889                                       gridParam->GetUser().Data(),
890                                       gridParam->GetDBFolder().Data(),
891                                       gridParam->GetSE().Data());
892
893         }
894
895         if(!gGrid && grid) {
896                 delete grid; grid=0;
897         }
898
899         return grid;
900 }
901
902 /////////////////////////////////////////////////////////////////////////////////////////////////
903 //                                                                                             //
904 // AliCDBGrid Parameter class                                                                  //                                          //
905 //                                                                                             //
906 /////////////////////////////////////////////////////////////////////////////////////////////////
907
908 ClassImp(AliCDBGridParam)
909
910 //_____________________________________________________________________________
911 AliCDBGridParam::AliCDBGridParam():
912  AliCDBParam(),
913  fGridUrl(),
914  fUser(),
915  fDBFolder(),
916  fSE()
917  {
918 // default constructor
919
920 }
921
922 //_____________________________________________________________________________
923 AliCDBGridParam::AliCDBGridParam(const char* gridUrl, 
924                                 const char* user,
925                                 const char* dbFolder, 
926                                 const char* se):
927  AliCDBParam(),
928  fGridUrl(gridUrl),
929  fUser(user),
930  fDBFolder(dbFolder),
931  fSE(se)
932 {
933 // constructor
934         
935         SetType("alien");
936
937         TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s",
938                         fGridUrl.Data(), fUser.Data(),
939                         fDBFolder.Data(), fSE.Data());
940
941         SetURI(uri.Data());
942 }
943
944 //_____________________________________________________________________________
945 AliCDBGridParam::~AliCDBGridParam() {
946 // destructor
947
948 }
949
950 //_____________________________________________________________________________
951 AliCDBParam* AliCDBGridParam::CloneParam() const {
952 // clone parameter
953
954         return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
955                                         fDBFolder.Data(), fSE.Data());
956 }
957
958 //_____________________________________________________________________________
959 ULong_t AliCDBGridParam::Hash() const {
960 // return Hash function
961
962         return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
963 }
964
965 //_____________________________________________________________________________
966 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
967 // check if this object is equal to AliCDBParam obj
968
969         if (this == obj) {
970                 return kTRUE;
971         }
972
973         if (AliCDBGridParam::Class() != obj->IsA()) {
974                 return kFALSE;
975         }
976
977         AliCDBGridParam* other = (AliCDBGridParam*) obj;
978
979         if(fGridUrl != other->fGridUrl) return kFALSE;
980         if(fUser != other->fUser) return kFALSE;
981         if(fDBFolder != other->fDBFolder) return kFALSE;
982         if(fSE != other->fSE) return kFALSE;
983         return kTRUE;
984 }
985