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