]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/CDB/AliCDBGrid.cxx
adding getters for the LHC period (OCDB folder) and its first/last run
[u/mrichter/AliRoot.git] / STEER / CDB / 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
b09247a2 23#include <cstdlib>
9e1ceb13 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>
89004e35 32#include <TMath.h>
9e1ceb13 33#include <TRegexp.h>
34
35#include "AliLog.h"
fdf65bb5 36#include "AliCDBEntry.h"
9e1ceb13 37#include "AliCDBGrid.h"
4667c116 38#include "AliCDBManager.h"
9e1ceb13 39
40
41ClassImp(AliCDBGrid)
42
43//_____________________________________________________________________________
c75fabed 44AliCDBGrid::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) :
9e1ceb13 47AliCDBStorage(),
b05400be 48fGridUrl(gridUrl),
9e1ceb13 49fUser(user),
b05400be 50fDBFolder(dbFolder),
c75fabed 51fSE(se),
52fCacheFolder(cacheFolder),
53fOperateDisconnected(operateDisconnected),
54fCacheSize(cacheSize),
55fCleanupInterval(cleanupInterval)
9e1ceb13 56{
57// constructor //
58
9e1ceb13 59 // if the same Grid is alreay active, skip connection
4667c116 60 if (!gGrid || fGridUrl != gGrid->GridUrl()
024cf675 61 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
9e1ceb13 62 // connection to the Grid
62032124 63 AliInfo("Connection to the Grid...");
024cf675 64 if(gGrid){
6720fa1f 65 AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
024cf675 66 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
67 }
b05400be 68 TGrid::Connect(fGridUrl.Data(),fUser.Data());
9e1ceb13 69 }
70
71 if(!gGrid) {
72 AliError("Connection failed!");
73 return;
74 }
75
76 TString initDir(gGrid->Pwd(0));
b05400be 77 if (fDBFolder[0] != '/') {
78 fDBFolder.Prepend(initDir);
9e1ceb13 79 }
80
81 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
b05400be 82 if(!gGrid->Cd(fDBFolder.Data(),0)){
83 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
d7744171 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;
9e1ceb13 89 }
90 } else {
b05400be 91 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
9e1ceb13 92 }
93
94 // removes any '/' at the end of path, then append one '/'
b05400be 95 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
96 fDBFolder+="/";
9e1ceb13 97
62032124 98 fType="alien";
99 fBaseFolder = fDBFolder;
100
c75fabed 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 "
d2a0a483 126 "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !",
c75fabed 127 fCacheSize, fCleanupInterval));
128 }
129 }
130
9e1ceb13 131 // return to the initial directory
132 gGrid->Cd(initDir.Data(),0);
5bfe3965 133
134 fNretry = 3; // default
135 fInitRetrySeconds = 5; // default
9e1ceb13 136}
137
138//_____________________________________________________________________________
139AliCDBGrid::~AliCDBGrid()
140{
141// destructor
62032124 142 delete gGrid; gGrid=0;
9e1ceb13 143
144}
145
146//_____________________________________________________________________________
62032124 147Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
4005d0b5 148// build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
62032124 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());
9e1ceb13 159
160 Ssiz_t mSize;
4005d0b5 161 // valid filename: Run#firstRun_#lastRun_v#version_s0.root
162 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
9e1ceb13 163 keyPattern.Index(filename, &mSize);
164 if (!mSize) {
4005d0b5 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 }
9e1ceb13 181
62032124 182 filename.Resize(filename.Length() - sizeof(".root") + 1);
9e1ceb13 183
62032124 184 TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
9e1ceb13 185
186 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
62032124 187 id.SetFirstRun(atoi(firstRunString.Data() + 3));
188 id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
189
9e1ceb13 190 TString verString(((TObjString*) strArray->At(2))->GetString());
62032124 191 id.SetVersion(atoi(verString.Data() + 1));
9e1ceb13 192
193 delete strArray;
194
195 return kTRUE;
196}
197
198//_____________________________________________________________________________
c3a7b59a 199Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
62032124 200// build file name from AliCDBId (path, run range, version) and fDBFolder
9e1ceb13 201
62032124 202 if (!id.GetAliCDBRunRange().IsValid()) {
203 AliDebug(2,Form("Invalid run range <%d, %d>.",
204 id.GetFirstRun(), id.GetLastRun()));
9e1ceb13 205 return kFALSE;
206 }
207
62032124 208 if (id.GetVersion() < 0) {
209 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
9e1ceb13 210 return kFALSE;
211 }
62032124 212
4005d0b5 213 filename = Form("Run%d_%d_v%d",
62032124 214 id.GetFirstRun(),
215 id.GetLastRun(),
216 id.GetVersion());
217
4005d0b5 218 if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
219 filename += ".root";
220
62032124 221 filename.Prepend(fDBFolder + id.GetPath() + '/');
9e1ceb13 222
223 return kTRUE;
224}
225
89004e35 226//_____________________________________________________________________________
5bfe3965 227void AliCDBGrid::SetRetry(Int_t nretry, Int_t initsec) {
89004e35 228
229 // Function to set the exponential retry for putting entries in the OCDB
230
5bfe3965 231 AliWarning("WARNING!!! You are changing the exponential retry times and delay: this function should be used by experts!");
89004e35 232 fNretry = nretry;
5bfe3965 233 fInitRetrySeconds = initsec;
234 AliDebug(2,Form("fNretry = %d, fInitRetrySeconds = %d", fNretry, fInitRetrySeconds));
89004e35 235}
236
237
9e1ceb13 238//_____________________________________________________________________________
239Bool_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));
9e1ceb13 243
b05400be 244 TString dirName(fDBFolder);
9e1ceb13 245
246 Bool_t dirExist=kFALSE;
4005d0b5 247
4667c116 248
249
9e1ceb13 250 // go to the path; if directory does not exist, create it
4667c116 251 for(int i=0;i<3;i++){
0e10c8fd 252 //TString cmd("find -d ");
253 //cmd += Form("%s ",dirName);
254 //cmd +=
255 //gGrid->Command(cmd.Data());
4667c116 256 dirName+=Form("%s/",id.GetPathLevel(i).Data());
9e1ceb13 257 dirExist=gGrid->Cd(dirName,0);
258 if (!dirExist) {
b05400be 259 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
9e1ceb13 260 if(!gGrid->Mkdir(dirName,"",0)){
261 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
262 gGrid->Cd(initDir.Data());
263 return kFALSE;
264 }
4667c116 265
266 // if folders are new add tags to them
267 if(i == 1) {
268 // TODO Currently disabled
c75fabed 269 // add short lived tag!
4667c116 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) {
c75fabed 280 AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
4667c116 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
4667c116 296 // TODO Currently disabled
c75fabed 297 // add short lived tag!
4667c116 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 }
9e1ceb13 319 }
320 }
9e1ceb13 321 gGrid->Cd(initDir,0);
322
62032124 323 TString filename;
324 AliCDBId anId; // the id got from filename
9e1ceb13 325 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
9e1ceb13 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++){
62032124 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();
9e1ceb13 337 }
338
339 }
4005d0b5 340 delete res;
341
be1b1008 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;
1ac347b2 347 }
05ca3c19 348
349 id.SetVersion(lastVersion + 1);
4005d0b5 350 id.SetSubVersion(0);
9e1ceb13 351
352 TString lastStorage = id.GetLastStorage();
353 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
84090f85 354 AliDebug(2, Form("A NEW object is being stored with version %d",
9e1ceb13 355 id.GetVersion()));
84090f85 356 AliDebug(2, Form("and it will hide previously stored object with version %d!",
9e1ceb13 357 id.GetVersion()-1));
358 }
359
4005d0b5 360 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
9e1ceb13 361 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
362 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
4005d0b5 363
9e1ceb13 364 return kTRUE;
365}
366
367//_____________________________________________________________________________
6dc56e97 368AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
62032124 369// look for the Id that matches query's requests (highest or exact version)
9e1ceb13 370
98873e41 371 if(validFileIds.GetEntriesFast() < 1)
62032124 372 return NULL;
9e1ceb13 373
62032124 374 TIter iter(&validFileIds);
9e1ceb13 375
62032124 376 AliCDBId *anIdPtr=0;
377 AliCDBId* result=0;
9e1ceb13 378
62032124 379 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
62032124 380 if(anIdPtr->GetPath() != query.GetPath()) continue;
381
382 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
9e1ceb13 383
384 if (!query.HasVersion()){ // look for highest version
62032124 385 if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
386 if(result && result->GetVersion() == anIdPtr->GetVersion()) {
4667c116 387 AliError(Form("More than one object valid for run %d, version %d!",
62032124 388 query.GetFirstRun(), anIdPtr->GetVersion()));
389 return NULL;
9e1ceb13 390 }
62032124 391 result = anIdPtr;
9e1ceb13 392 } else { // look for specified version
62032124 393 if(query.GetVersion() != anIdPtr->GetVersion()) continue;
394 if(result && result->GetVersion() == anIdPtr->GetVersion()){
4667c116 395 AliError(Form("More than one object valid for run %d, version %d!",
62032124 396 query.GetFirstRun(), anIdPtr->GetVersion()));
397 return NULL;
9e1ceb13 398 }
62032124 399 result = anIdPtr;
9e1ceb13 400 }
62032124 401
402 }
c75fabed 403
404 if (!result) return NULL;
62032124 405
4667c116 406 return dynamic_cast<AliCDBId*> (result->Clone());
62032124 407}
408
9e1ceb13 409//_____________________________________________________________________________
4667c116 410AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
411// get AliCDBId from the database
412// User must delete returned object
9e1ceb13 413
62032124 414 AliCDBId* dataId=0;
415
416 AliCDBId selectedId(queryId);
417 if (!selectedId.HasVersion()) {
9e1ceb13 418 // if version is not specified, first check the selection criteria list
4b5e0dce 419 GetSelection(&selectedId);
62032124 420 }
421
6dc56e97 422 TObjArray validFileIds;
62032124 423 validFileIds.SetOwner(1);
424
425 // look for file matching query requests (path, runRange, version)
9e96e52d 426 if(selectedId.GetFirstRun() == fRun && fPathFilter.Comprises(selectedId.GetAliCDBPath()) &&
427 fVersion == selectedId.GetVersion() && !fMetaDataFilter){
62032124 428 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
98873e41 429 AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!",
62032124 430 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
431 dataId = GetId(fValidFileIds, selectedId);
432
9e1ceb13 433 } else {
62032124 434 // List of files valid for reqested run was not loaded. Looking directly into CDB
9e96e52d 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()));
62032124 437
438 TString filter;
6dc56e97 439 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
62032124 440
ca0602ee 441 TString pattern = ".root";
442 TString optionQuery = "-y -m";
0e10c8fd 443 if(selectedId.GetVersion() >= 0) {
61f0b0b6 444 pattern.Prepend(Form("_v%d_s0",selectedId.GetVersion()));
0e10c8fd 445 optionQuery = "";
446 }
447
ca0602ee 448 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),selectedId.GetPath().Data()));
6dc56e97 449
ca0602ee 450 if (optionQuery.Contains("-y")){
0e10c8fd 451 AliInfo("Only latest version will be returned");
452 }
453
ca0602ee 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());
ecae96f8 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 }
62032124 466 dataId = GetId(validFileIds, selectedId);
9e1ceb13 467 }
468
4667c116 469 return dataId;
470}
471
472//_____________________________________________________________________________
473AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
474// get AliCDBEntry from the database
475
476 AliCDBId* dataId = GetEntryId(queryId);
477
62032124 478 if (!dataId) return NULL;
9e1ceb13 479
480 TString filename;
62032124 481 if (!IdToFilename(*dataId, filename)) {
024cf675 482 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
4667c116 483 delete dataId;
9e1ceb13 484 return NULL;
485 }
486
62032124 487 AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
488
4667c116 489 delete dataId;
62032124 490 return anEntry;
491}
492
493//_____________________________________________________________________________
8e245d15 494AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
62032124 495// Get AliCBEntry object from file "filename"
9e1ceb13 496
024cf675 497 AliDebug(2,Form("Opening file: %s",filename.Data()));
62032124 498
499 filename.Prepend("/alien");
c75fabed 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);
9e1ceb13 520 if (!file) {
024cf675 521 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
9e1ceb13 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
62032124 528 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
9e1ceb13 529
62032124 530 if (!anEntry) {
531 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
532 file->Close();
9e1ceb13 533 return NULL;
534 }
535
9e1ceb13 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.
62032124 539
540 if(anEntry){
541 AliCDBId entryId = anEntry->GetId();
4005d0b5 542 Int_t tmpSubVersion = dataId->GetSubVersion();
543 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
8e245d15 544 if(!entryId.IsEqual(dataId)){
b8ec52f6 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()));
62032124 548 }
4005d0b5 549 dataId->SetSubVersion(tmpSubVersion);
9e1ceb13 550 }
551
62032124 552 anEntry->SetLastStorage("grid");
553
145f45a2 554 // Check whether entry contains a TTree. In case load the tree in memory!
555 LoadTreeFromFile(anEntry);
556
9e1ceb13 557 // close file, return retieved entry
558 file->Close(); delete file; file=0;
62032124 559
560 return anEntry;
9e1ceb13 561}
562
563//_____________________________________________________________________________
62032124 564TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
9e1ceb13 565// multiple request (AliCDBStorage::GetAll)
566
62032124 567 TList* result = new TList();
568 result->SetOwner();
9e1ceb13 569
6dc56e97 570 TObjArray validFileIds;
62032124 571 validFileIds.SetOwner(1);
9e1ceb13 572
62032124 573 Bool_t alreadyLoaded = kFALSE;
9e1ceb13 574
62032124 575 // look for file matching query requests (path, runRange)
576 if(queryId.GetFirstRun() == fRun &&
c3a7b59a 577 fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
62032124 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()));
9e1ceb13 581
62032124 582 alreadyLoaded = kTRUE;
9e1ceb13 583
62032124 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;
6dc56e97 590 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
591
5ef33267 592 TString path = queryId.GetPath();
6dc56e97 593
5ef33267 594 TString pattern = "Run*.root";
595 TString optionQuery = "-y";
0e10c8fd 596
5ef33267 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;
0e10c8fd 632 }
633
5ef33267 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 }
62032124 644
62032124 645 AliCDBId validFileId;
646 for(int i=0; i<res->GetEntries(); i++){
647 TString filename = res->GetKey(i, "lfn");
c3a7b59a 648 if(filename == "") continue;
62032124 649 if(FilenameToId(filename, validFileId))
650 validFileIds.AddLast(validFileId.Clone());
651 }
652 delete res;
9e1ceb13 653 }
654
62032124 655 TIter *iter=0;
656 if(alreadyLoaded){
657 iter = new TIter(&fValidFileIds);
658 } else {
659 iter = new TIter(&validFileIds);
660 }
9e1ceb13 661
6dc56e97 662 TObjArray selectedIds;
62032124 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);
9e1ceb13 685 }
4667c116 686 if(dataId) selectedIds.Add(dataId);
9e1ceb13 687 }
9e1ceb13 688
62032124 689 delete iter; iter=0;
9e1ceb13 690
62032124 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 }
9e1ceb13 700
62032124 701 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
9e1ceb13 702
62032124 703 if(anEntry) result->Add(anEntry);
9e1ceb13 704
62032124 705 }
706 delete iter; iter=0;
707
708 return result;
9e1ceb13 709}
710
711//_____________________________________________________________________________
712Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
713// put an AliCDBEntry object into the database
62032124 714
9e1ceb13 715 AliCDBId& id = entry->GetId();
716
717 // set version for the entry to be stored
62032124 718 if (!PrepareId(id)) return kFALSE;
9e1ceb13 719
720 // build filename from entry's id
721 TString filename;
62032124 722 if (!IdToFilename(id, filename)) {
9e1ceb13 723 AliError("Bad ID encountered! Subnormal error!");
724 return kFALSE;
62032124 725 }
726
727 TString folderToTag = Form("%s%s",
728 fDBFolder.Data(),
729 id.GetPath().Data());
730
9e1ceb13 731 TDirectory* saveDir = gDirectory;
732
fb33abde 733 TString fullFilename = Form("/alien%s", filename.Data());
62032124 734 // specify SE to filename
fb33abde 735 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
62032124 736
5bfe3965 737 Int_t nsleep = fInitRetrySeconds;
9e1ceb13 738 // open file
89004e35 739 TFile *file=0;
5bfe3965 740 AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
89004e35 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;
5bfe3965 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 }
89004e35 755 }
756 else {
757 AliDebug(2, " Successful!");
758 break;
759 }
5bfe3965 760 nsleep*=fInitRetrySeconds;
89004e35 761 }
62032124 762
763 file->cd();
9e1ceb13 764
ed343d5e 765 //SetTreeToFile(entry, file);
145f45a2 766
9e1ceb13 767 entry->SetVersion(id.GetVersion());
768
769 // write object (key name: "AliCDBEntry")
145f45a2 770 Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
62032124 771 if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
9e1ceb13 772
773
774 if (saveDir) saveDir->cd(); else gROOT->cd();
775 file->Close(); delete file; file=0;
c3a7b59a 776
9e1ceb13 777 if(result) {
c75fabed 778
c3a7b59a 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());
9e1ceb13 787 }
62032124 788
c75fabed 789 AliInfo(Form("CDB object stored into file %s", filename.Data()));
790 AliInfo(Form("Storage Element: %s", fSE.Data()));
62032124 791 return result;
792}
793//_____________________________________________________________________________
794Bool_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;
9e1ceb13 808 return result;
809}
810
62032124 811//_____________________________________________________________________________
c3a7b59a 812Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
62032124 813// tag stored object in CDB table using object Id's parameters
814
0e10c8fd 815
816 TString dirname(filename);
817 Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
818
f430a9ce 819 TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
820 TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
62032124 821 id->GetFirstRun(),
822 id->GetLastRun(),
823 id->GetVersion());
0e10c8fd 824 TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
4667c116 825 id->GetPathLevel(0).Data(),
826 id->GetPathLevel(1).Data(),
827 id->GetPathLevel(2).Data());
0e10c8fd 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",
f430a9ce 831 addTagValue1.Data(),
832 addTagValue2.Data(),
0e10c8fd 833 addTagValue3.Data(),
834 addTagValue4.Data());
62032124 835
c3a7b59a 836 Bool_t result = kFALSE;
62032124 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') {
c3a7b59a 841 AliError(Form("Couldn't add CDB tag value to file %s !",
62032124 842 filename.Data()));
c3a7b59a 843 result = kFALSE;
62032124 844 } else {
c75fabed 845 AliDebug(2, "Object successfully tagged.");
c3a7b59a 846 result = kTRUE;
62032124 847 }
848 delete res;
c3a7b59a 849 return result;
62032124 850
851}
852
4667c116 853//_____________________________________________________________________________
854Bool_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 {
c75fabed 867 AliDebug(2,"Object successfully tagged.");
4667c116 868 result = kTRUE;
869 }
870 delete res;
871 return result;
872
873}
874
62032124 875//_____________________________________________________________________________
c3a7b59a 876Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
62032124 877// tag stored object in CDB table using object Id's parameters
878
f430a9ce 879 TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
880 TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
62032124 881 md->GetObjectClassName(),
882 md->GetResponsible(),
883 md->GetBeamPeriod());
f430a9ce 884 TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
62032124 885 md->GetAliRootVersion(),
886 md->GetComment());
887 TString addTagValue = Form("%s%s%s",
f430a9ce 888 addTagValue1.Data(),
889 addTagValue2.Data(),
890 addTagValue3.Data());
62032124 891
c3a7b59a 892 Bool_t result = kFALSE;
62032124 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()));
c3a7b59a 899 result = kFALSE;
62032124 900 } else {
c75fabed 901 AliDebug(2,"Object successfully tagged.");
c3a7b59a 902 result = kTRUE;
62032124 903 }
c3a7b59a 904 return result;
62032124 905}
906
b05400be 907//_____________________________________________________________________________
908TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
6dc56e97 909
b05400be 910 TString turl(fileName);
911 turl.Prepend("/alien" + fDBFolder);
62032124 912 turl += "?se="; turl += fSE.Data();
b05400be 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;
62032124 923
b05400be 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//_____________________________________________________________________________
942Bool_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
62032124 954//_____________________________________________________________________________
955void 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;
6dc56e97 961 MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
962
61f0b0b6 963 TString path = fPathFilter.GetPath();
964
965 TString pattern = "Run*";
0e10c8fd 966 TString optionQuery = "-y";
967 if(fVersion >= 0) {
61f0b0b6 968 pattern += Form("_v%d_s0", fVersion);
0e10c8fd 969 optionQuery = "";
970 }
6dc56e97 971 pattern += ".root";
972 AliDebug(2,Form("pattern: %s", pattern.Data()));
973
61f0b0b6 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...");
5ef33267 985 pattern.Prepend(path+"/");
61f0b0b6 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"
5ef33267 1005 pattern.Prepend(path+"/");
61f0b0b6 1006 }
1007 }
1008 delete tokenArr; tokenArr=0;
1009 }
1010
1011 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1012
5ef33267 1013 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
0e10c8fd 1014
1015 if (optionQuery == "-y"){
1016 AliInfo("Only latest version will be returned");
1017 }
1018
5ef33267 1019 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
62032124 1020
b7e6419d 1021 if (!res) {
1022 AliError("Grid query failed");
1023 return;
1024 }
1025
b68a24b3 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 }
62032124 1043 }
1044 delete res;
1045
1046}
1047
1048//_____________________________________________________________________________
c3a7b59a 1049void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
62032124 1050 const AliCDBMetaData* md, TString& result) const
1051{
1052// create filter for file query
1053
c3a7b59a 1054 result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
62032124 1055
6dc56e97 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// }
62032124 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
c3a7b59a 1090//_____________________________________________________________________________
1091Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
1092// get last version found in the database valid for run and path
1093
c3a7b59a 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);
c3a7b59a 1100
0e10c8fd 1101 AliCDBId* dataId = 0;
c3a7b59a 1102 // look for file matching query requests (path, runRange, version)
6dc56e97 1103 if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
c3a7b59a 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;
4667c116 1109 Int_t version = dataId->GetVersion();
1110 delete dataId;
1111 return version;
c3a7b59a 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
4667c116 1118 TObjArray validFileIds;
1119 validFileIds.SetOwner(1);
1120
c3a7b59a 1121 TString filter;
6dc56e97 1122 MakeQueryFilter(run, run, 0, filter);
1123
5ef33267 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");
c3a7b59a 1130
c3a7b59a 1131 AliCDBId validFileId;
0e10c8fd 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))
c3a7b59a 1138 validFileIds.AddLast(validFileId.Clone());
0e10c8fd 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;
c3a7b59a 1162 }
c3a7b59a 1163
0e10c8fd 1164 delete res;
62ea513e 1165
c3a7b59a 1166
1167}
1168
1169//_____________________________________________________________________________
1170Int_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
9e1ceb13 1177/////////////////////////////////////////////////////////////////////////////////////////////////
1178// //
1179// AliCDBGrid factory //
1180// //
1181/////////////////////////////////////////////////////////////////////////////////////////////////
1182
1183ClassImp(AliCDBGridFactory)
1184
1185//_____________________________________________________________________________
1186Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1187// check if the string is valid Grid URI
1188
b05400be 1189 TRegexp gridPattern("^alien://.+$");
9e1ceb13 1190
27cac7e3 1191 return TString(gridString).Contains(gridPattern);
9e1ceb13 1192}
1193
1194//_____________________________________________________________________________
1195AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1196// create AliCDBGridParam class from the URI string
1197
1198 if (!Validate(gridString)) {
1199 return NULL;
1200 }
c75fabed 1201
b05400be 1202 TString buffer(gridString);
c3a7b59a 1203
1204 TString gridUrl = "alien://";
b05400be 1205 TString user = "";
4667c116 1206 TString dbFolder = "";
fb33abde 1207 TString se = "default";
c75fabed 1208 TString cacheFolder = "";
1209 Bool_t operateDisconnected = kTRUE;
1210 Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB
1211 Long_t cleanupInterval = 0;
b05400be 1212
1213 TObjArray *arr = buffer.Tokenize('?');
1214 TIter iter(arr);
62032124 1215 TObjString *str = 0;
4667c116 1216
b05400be 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;
c75fabed 1235 }
b05400be 1236 else if (key.Contains("user",TString::kIgnoreCase)){
1237 user = value;
1238 }
b05400be 1239 else if (key.Contains("se",TString::kIgnoreCase)){
1240 se = value;
1241 }
d7744171 1242 else if (key.Contains("cacheF",TString::kIgnoreCase)){
c75fabed 1243 cacheFolder = value;
f8703115 1244 if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
c75fabed 1245 cacheFolder += "/";
1246 }
d7744171 1247 else if (key.Contains("folder",TString::kIgnoreCase)){
1248 dbFolder = value;
1249 }
c75fabed 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 }
d7744171 1262 else if (key.Contains("cacheS",TString::kIgnoreCase)){
c75fabed 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 }
b05400be 1278 else{
1279 AliError(Form("Invalid entry! %s",entry.Data()));
c75fabed 1280 return NULL;
b05400be 1281 }
1282 }
1283 delete arr; arr=0;
c75fabed 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));
d2a0a483 1291 AliDebug(2, Form("local cache size: %lld", cacheSize));
6720fa1f 1292 AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
9e1ceb13 1293
27cac7e3 1294 if(dbFolder == ""){
c75fabed 1295 AliError("Base folder must be specified!");
4667c116 1296 return NULL;
1297 }
1298
c75fabed 1299 return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1300 dbFolder.Data(), se.Data(), cacheFolder.Data(),
1301 operateDisconnected, cacheSize, cleanupInterval);
9e1ceb13 1302}
1303
1304//_____________________________________________________________________________
1305AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1306// create AliCDBGrid storage instance from parameters
1307
62032124 1308 AliCDBGrid *grid = 0;
9e1ceb13 1309 if (AliCDBGridParam::Class() == param->IsA()) {
62032124 1310
9e1ceb13 1311 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
62032124 1312 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1313 gridParam->GetUser().Data(),
1314 gridParam->GetDBFolder().Data(),
c75fabed 1315 gridParam->GetSE().Data(),
1316 gridParam->GetCacheFolder().Data(),
1317 gridParam->GetOperateDisconnected(),
1318 gridParam->GetCacheSize(),
1319 gridParam->GetCleanupInterval());
9e1ceb13 1320
9e1ceb13 1321 }
1322
c3a7b59a 1323 if(!gGrid && grid) {
1324 delete grid; grid=0;
1325 }
1326
62032124 1327 return grid;
9e1ceb13 1328}
1329
1330/////////////////////////////////////////////////////////////////////////////////////////////////
1331// //
c75fabed 1332// AliCDBGrid Parameter class // //
9e1ceb13 1333// //
1334/////////////////////////////////////////////////////////////////////////////////////////////////
1335
1336ClassImp(AliCDBGridParam)
1337
1338//_____________________________________________________________________________
62032124 1339AliCDBGridParam::AliCDBGridParam():
1340 AliCDBParam(),
1341 fGridUrl(),
1342 fUser(),
1343 fDBFolder(),
c75fabed 1344 fSE(),
1345 fCacheFolder(),
1346 fOperateDisconnected(),
1347 fCacheSize(),
1348 fCleanupInterval()
1349
62032124 1350 {
9e1ceb13 1351// default constructor
1352
1353}
1354
1355//_____________________________________________________________________________
c75fabed 1356AliCDBGridParam::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):
62032124 1359 AliCDBParam(),
b05400be 1360 fGridUrl(gridUrl),
9e1ceb13 1361 fUser(user),
b05400be 1362 fDBFolder(dbFolder),
c75fabed 1363 fSE(se),
1364 fCacheFolder(cacheFolder),
1365 fOperateDisconnected(operateDisconnected),
1366 fCacheSize(cacheSize),
1367 fCleanupInterval(cleanupInterval)
9e1ceb13 1368{
1369// constructor
c75fabed 1370
9e1ceb13 1371 SetType("alien");
1372
c75fabed 1373 TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
d2a0a483 1374 "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
62032124 1375 fGridUrl.Data(), fUser.Data(),
c75fabed 1376 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1377 fOperateDisconnected, fCacheSize, fCleanupInterval);
62032124 1378
1379 SetURI(uri.Data());
9e1ceb13 1380}
1381
1382//_____________________________________________________________________________
1383AliCDBGridParam::~AliCDBGridParam() {
1384// destructor
1385
1386}
1387
1388//_____________________________________________________________________________
1389AliCDBParam* AliCDBGridParam::CloneParam() const {
1390// clone parameter
1391
62032124 1392 return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
c75fabed 1393 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1394 fOperateDisconnected, fCacheSize, fCleanupInterval);
9e1ceb13 1395}
1396
1397//_____________________________________________________________________________
1398ULong_t AliCDBGridParam::Hash() const {
1399// return Hash function
1400
c75fabed 1401 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
9e1ceb13 1402}
1403
1404//_____________________________________________________________________________
1405Bool_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
b05400be 1418 if(fGridUrl != other->fGridUrl) return kFALSE;
9e1ceb13 1419 if(fUser != other->fUser) return kFALSE;
b05400be 1420 if(fDBFolder != other->fDBFolder) return kFALSE;
9e1ceb13 1421 if(fSE != other->fSE) return kFALSE;
c75fabed 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;
9e1ceb13 1426 return kTRUE;
1427}
1428