Changes related to the removal of the deprecated AliCDBGrid::GetLatestVersion:
[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),
c5941d67 52fMirrorSEs(""),
c75fabed 53fCacheFolder(cacheFolder),
54fOperateDisconnected(operateDisconnected),
55fCacheSize(cacheSize),
56fCleanupInterval(cleanupInterval)
9e1ceb13 57{
58// constructor //
59
9e1ceb13 60 // if the same Grid is alreay active, skip connection
4667c116 61 if (!gGrid || fGridUrl != gGrid->GridUrl()
024cf675 62 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
9e1ceb13 63 // connection to the Grid
62032124 64 AliInfo("Connection to the Grid...");
024cf675 65 if(gGrid){
6720fa1f 66 AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
024cf675 67 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
68 }
b05400be 69 TGrid::Connect(fGridUrl.Data(),fUser.Data());
9e1ceb13 70 }
71
72 if(!gGrid) {
73 AliError("Connection failed!");
74 return;
75 }
76
77 TString initDir(gGrid->Pwd(0));
b05400be 78 if (fDBFolder[0] != '/') {
79 fDBFolder.Prepend(initDir);
9e1ceb13 80 }
81
82 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
b05400be 83 if(!gGrid->Cd(fDBFolder.Data(),0)){
84 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
d7744171 85 TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data()));
86 TString result = res->GetKey(0,"__result__");
87 if(result == "0"){
88 AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data()));
89 return;
9e1ceb13 90 }
91 } else {
b05400be 92 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
9e1ceb13 93 }
94
95 // removes any '/' at the end of path, then append one '/'
b05400be 96 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
97 fDBFolder+="/";
9e1ceb13 98
62032124 99 fType="alien";
100 fBaseFolder = fDBFolder;
101
c75fabed 102 // Setting the cache
103
104 // Check if local cache folder is already defined
105 TString origCache(TFile::GetCacheFileDir());
106 if(fCacheFolder.Length() > 0) {
107 if(origCache.Length() == 0) {
108 AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data()));
109 } else if(fCacheFolder != origCache) {
110 AliWarning(Form("Local cache folder was already defined, changing it to: %s",
111 fCacheFolder.Data()));
112 }
113
114 // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE
115 if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) {
116 AliError(Form("Could not set cache folder %s !", fCacheFolder.Data()));
117 fCacheFolder = "";
118 } else {
119 // reset fCacheFolder because the function may have
120 // slightly changed the folder name (e.g. '/' added)
121 fCacheFolder = TFile::GetCacheFileDir();
122 }
123
124 // default settings are: cacheSize=1GB, cleanupInterval = 0
125 if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) {
126 AliError(Form("Could not set following values "
d2a0a483 127 "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !",
c75fabed 128 fCacheSize, fCleanupInterval));
129 }
130 }
131
9e1ceb13 132 // return to the initial directory
133 gGrid->Cd(initDir.Data(),0);
5bfe3965 134
135 fNretry = 3; // default
136 fInitRetrySeconds = 5; // default
9e1ceb13 137}
138
139//_____________________________________________________________________________
140AliCDBGrid::~AliCDBGrid()
141{
142// destructor
62032124 143 delete gGrid; gGrid=0;
9e1ceb13 144
145}
146
147//_____________________________________________________________________________
62032124 148Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
4005d0b5 149// build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
62032124 150
151 if(filename.Contains(fDBFolder)){
152 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
153 }
154
155 TString idPath = filename(0,filename.Last('/'));
156 id.SetPath(idPath);
157 if(!id.IsValid()) return kFALSE;
158
159 filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
9e1ceb13 160
161 Ssiz_t mSize;
4005d0b5 162 // valid filename: Run#firstRun_#lastRun_v#version_s0.root
163 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
9e1ceb13 164 keyPattern.Index(filename, &mSize);
165 if (!mSize) {
4005d0b5 166
167 // TODO backward compatibility ... maybe remove later!
168 Ssiz_t oldmSize;
169 TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
170 oldKeyPattern.Index(filename, &oldmSize);
171 if(!oldmSize) {
172 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
173 return kFALSE;
174 } else {
175 AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
176 id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
177 }
178
179 } else {
180 id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
181 }
9e1ceb13 182
62032124 183 filename.Resize(filename.Length() - sizeof(".root") + 1);
9e1ceb13 184
62032124 185 TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
9e1ceb13 186
187 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
62032124 188 id.SetFirstRun(atoi(firstRunString.Data() + 3));
189 id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
190
9e1ceb13 191 TString verString(((TObjString*) strArray->At(2))->GetString());
62032124 192 id.SetVersion(atoi(verString.Data() + 1));
9e1ceb13 193
194 delete strArray;
195
196 return kTRUE;
197}
198
199//_____________________________________________________________________________
c3a7b59a 200Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
62032124 201// build file name from AliCDBId (path, run range, version) and fDBFolder
9e1ceb13 202
62032124 203 if (!id.GetAliCDBRunRange().IsValid()) {
204 AliDebug(2,Form("Invalid run range <%d, %d>.",
205 id.GetFirstRun(), id.GetLastRun()));
9e1ceb13 206 return kFALSE;
207 }
208
62032124 209 if (id.GetVersion() < 0) {
210 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
9e1ceb13 211 return kFALSE;
212 }
62032124 213
4005d0b5 214 filename = Form("Run%d_%d_v%d",
62032124 215 id.GetFirstRun(),
216 id.GetLastRun(),
217 id.GetVersion());
218
4005d0b5 219 if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
220 filename += ".root";
221
62032124 222 filename.Prepend(fDBFolder + id.GetPath() + '/');
9e1ceb13 223
224 return kTRUE;
225}
226
89004e35 227//_____________________________________________________________________________
5bfe3965 228void AliCDBGrid::SetRetry(Int_t nretry, Int_t initsec) {
89004e35 229
230 // Function to set the exponential retry for putting entries in the OCDB
231
5bfe3965 232 AliWarning("WARNING!!! You are changing the exponential retry times and delay: this function should be used by experts!");
89004e35 233 fNretry = nretry;
5bfe3965 234 fInitRetrySeconds = initsec;
235 AliDebug(2,Form("fNretry = %d, fInitRetrySeconds = %d", fNretry, fInitRetrySeconds));
89004e35 236}
237
238
9e1ceb13 239//_____________________________________________________________________________
240Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
241// prepare id (version) of the object that will be stored (called by PutEntry)
242
243 TString initDir(gGrid->Pwd(0));
9e1ceb13 244
b05400be 245 TString dirName(fDBFolder);
9e1ceb13 246
247 Bool_t dirExist=kFALSE;
4005d0b5 248
4667c116 249
250
9e1ceb13 251 // go to the path; if directory does not exist, create it
4667c116 252 for(int i=0;i<3;i++){
0e10c8fd 253 //TString cmd("find -d ");
254 //cmd += Form("%s ",dirName);
255 //cmd +=
256 //gGrid->Command(cmd.Data());
4667c116 257 dirName+=Form("%s/",id.GetPathLevel(i).Data());
9e1ceb13 258 dirExist=gGrid->Cd(dirName,0);
259 if (!dirExist) {
b05400be 260 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
9e1ceb13 261 if(!gGrid->Mkdir(dirName,"",0)){
262 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
263 gGrid->Cd(initDir.Data());
264 return kFALSE;
265 }
4667c116 266
267 // if folders are new add tags to them
268 if(i == 1) {
269 // TODO Currently disabled
c75fabed 270 // add short lived tag!
4667c116 271 // AliInfo("Tagging level 1 folder with \"ShortLived\" tag");
272 // if(!AddTag(dirName,"ShortLived_try")){
273 // AliError(Form("Could not tag folder %s !", dirName.Data()));
274 // if(!gGrid->Rmdir(dirName.Data())){
275 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
276 // }
277 // return 0;
278 //}
279
280 } else if(i == 2) {
c75fabed 281 AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
4667c116 282 if(!AddTag(dirName,"CDB")){
283 AliError(Form("Could not tag folder %s !", dirName.Data()));
284 if(!gGrid->Rmdir(dirName.Data())){
285 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
286 }
287 return 0;
288 }
289 if(!AddTag(dirName,"CDB_MD")){
290 AliError(Form("Could not tag folder %s !", dirName.Data()));
291 if(!gGrid->Rmdir(dirName.Data())){
292 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
293 }
294 return 0;
295 }
296
4667c116 297 // TODO Currently disabled
c75fabed 298 // add short lived tag!
4667c116 299 // TString path=id.GetPath();
300 // if(AliCDBManager::Instance()->IsShortLived(path.Data())) {
301 // AliInfo(Form("Tagging %s as short lived", dirName.Data()));
302 // if(!TagShortLived(dirName, kTRUE)){
303 // AliError(Form("Could not tag folder %s !", dirName.Data()));
304 // if(!gGrid->Rmdir(dirName.Data())){
305 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
306 // }
307 // return 0;
308 // }
309 // } else {
310 // AliInfo(Form("Tagging %s as long lived", dirName.Data()));
311 // if(!TagShortLived(dirName, kFALSE)){
312 // AliError(Form("Could not tag folder %s !", dirName.Data()));
313 // if(!gGrid->Rmdir(dirName.Data())){
314 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
315 // }
316 // return 0;
317 // }
318 // }
319 }
9e1ceb13 320 }
321 }
9e1ceb13 322 gGrid->Cd(initDir,0);
323
62032124 324 TString filename;
325 AliCDBId anId; // the id got from filename
9e1ceb13 326 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
9e1ceb13 327 Int_t lastVersion=0; // highest version found
328
329 TGridResult *res = gGrid->Ls(dirName);
330
331 //loop on the files in the directory, look for highest version
332 for(int i=0; i < res->GetEntries(); i++){
62032124 333 filename=res->GetFileNamePath(i);
334 if (!FilenameToId(filename, anId)) continue;
335 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
336 lastVersion = anId.GetVersion();
337 lastRunRange = anId.GetAliCDBRunRange();
9e1ceb13 338 }
339
340 }
4005d0b5 341 delete res;
342
be1b1008 343 // GRP entries with explicitly set version escape default incremental versioning
344 if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0)
345 {
346 AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data()));
347 return kFALSE;
1ac347b2 348 }
05ca3c19 349
350 id.SetVersion(lastVersion + 1);
4005d0b5 351 id.SetSubVersion(0);
9e1ceb13 352
353 TString lastStorage = id.GetLastStorage();
354 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
84090f85 355 AliDebug(2, Form("A NEW object is being stored with version %d",
9e1ceb13 356 id.GetVersion()));
84090f85 357 AliDebug(2, Form("and it will hide previously stored object with version %d!",
9e1ceb13 358 id.GetVersion()-1));
359 }
360
4005d0b5 361 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
9e1ceb13 362 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
363 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
4005d0b5 364
9e1ceb13 365 return kTRUE;
366}
367
368//_____________________________________________________________________________
6dc56e97 369AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
62032124 370// look for the Id that matches query's requests (highest or exact version)
9e1ceb13 371
98873e41 372 if(validFileIds.GetEntriesFast() < 1)
62032124 373 return NULL;
9e1ceb13 374
62032124 375 TIter iter(&validFileIds);
9e1ceb13 376
62032124 377 AliCDBId *anIdPtr=0;
378 AliCDBId* result=0;
9e1ceb13 379
62032124 380 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
62032124 381 if(anIdPtr->GetPath() != query.GetPath()) continue;
382
383 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
9e1ceb13 384
385 if (!query.HasVersion()){ // look for highest version
62032124 386 if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
387 if(result && result->GetVersion() == anIdPtr->GetVersion()) {
4667c116 388 AliError(Form("More than one object valid for run %d, version %d!",
62032124 389 query.GetFirstRun(), anIdPtr->GetVersion()));
390 return NULL;
9e1ceb13 391 }
62032124 392 result = anIdPtr;
9e1ceb13 393 } else { // look for specified version
62032124 394 if(query.GetVersion() != anIdPtr->GetVersion()) continue;
395 if(result && result->GetVersion() == anIdPtr->GetVersion()){
4667c116 396 AliError(Form("More than one object valid for run %d, version %d!",
62032124 397 query.GetFirstRun(), anIdPtr->GetVersion()));
398 return NULL;
9e1ceb13 399 }
62032124 400 result = anIdPtr;
9e1ceb13 401 }
62032124 402
403 }
c75fabed 404
405 if (!result) return NULL;
62032124 406
4667c116 407 return dynamic_cast<AliCDBId*> (result->Clone());
62032124 408}
409
9e1ceb13 410//_____________________________________________________________________________
4667c116 411AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
412// get AliCDBId from the database
413// User must delete returned object
9e1ceb13 414
62032124 415 AliCDBId* dataId=0;
416
417 AliCDBId selectedId(queryId);
418 if (!selectedId.HasVersion()) {
9e1ceb13 419 // if version is not specified, first check the selection criteria list
4b5e0dce 420 GetSelection(&selectedId);
62032124 421 }
422
6dc56e97 423 TObjArray validFileIds;
62032124 424 validFileIds.SetOwner(1);
425
426 // look for file matching query requests (path, runRange, version)
9e96e52d 427 if(selectedId.GetFirstRun() == fRun && fPathFilter.Comprises(selectedId.GetAliCDBPath()) &&
428 fVersion == selectedId.GetVersion() && !fMetaDataFilter){
62032124 429 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
98873e41 430 AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!",
62032124 431 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
432 dataId = GetId(fValidFileIds, selectedId);
433
9e1ceb13 434 } else {
62032124 435 // List of files valid for reqested run was not loaded. Looking directly into CDB
9e96e52d 436 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!",
437 selectedId.GetFirstRun(), selectedId.GetVersion(), selectedId.GetPath().Data()));
62032124 438
439 TString filter;
6dc56e97 440 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
62032124 441
ca0602ee 442 TString pattern = ".root";
443 TString optionQuery = "-y -m";
0e10c8fd 444 if(selectedId.GetVersion() >= 0) {
61f0b0b6 445 pattern.Prepend(Form("_v%d_s0",selectedId.GetVersion()));
0e10c8fd 446 optionQuery = "";
447 }
448
ca0602ee 449 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),selectedId.GetPath().Data()));
6dc56e97 450
ca0602ee 451 if (optionQuery.Contains("-y")){
0e10c8fd 452 AliInfo("Only latest version will be returned");
453 }
454
ca0602ee 455 AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
456 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
ecae96f8 457 if (res) {
458 AliCDBId validFileId;
459 for(int i=0; i<res->GetEntries(); i++){
460 TString filename = res->GetKey(i, "lfn");
461 if(filename == "") continue;
462 if(FilenameToId(filename, validFileId))
463 validFileIds.AddLast(validFileId.Clone());
464 }
465 delete res;
f2331eb1 466 }else{
467 return 0; // this should be only in case of file catalogue glitch
468 }
469
62032124 470 dataId = GetId(validFileIds, selectedId);
9e1ceb13 471 }
472
4667c116 473 return dataId;
474}
475
476//_____________________________________________________________________________
477AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
478// get AliCDBEntry from the database
479
e6608c2e 480 Printf("Entering AliCDBGrid::GetEntry");
481
4667c116 482 AliCDBId* dataId = GetEntryId(queryId);
483
e6608c2e 484 if (!dataId){
485 AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
486 return NULL;
487 }
9e1ceb13 488
489 TString filename;
62032124 490 if (!IdToFilename(*dataId, filename)) {
024cf675 491 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
4667c116 492 delete dataId;
e6608c2e 493 AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
9e1ceb13 494 return NULL;
495 }
496
62032124 497 AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
498
4667c116 499 delete dataId;
e6608c2e 500 if(!anEntry){
501 Printf("follows an alifatal");
502 AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
503 }
504
62032124 505 return anEntry;
506}
507
508//_____________________________________________________________________________
8e245d15 509AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
62032124 510// Get AliCBEntry object from file "filename"
9e1ceb13 511
024cf675 512 AliDebug(2,Form("Opening file: %s",filename.Data()));
62032124 513
514 filename.Prepend("/alien");
c75fabed 515
516 // if option="CACHEREAD" TFile will use the local caching facility!
517 TString option="READ";
518 if(fCacheFolder != ""){
519
520 // Check if local cache folder was changed in the meanwhile
521 TString origCache(TFile::GetCacheFileDir());
522 if(fCacheFolder != origCache) {
523 AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
524 fCacheFolder.Data(), origCache.Data()));
525 TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
526 TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
527 }
528
529 option.Prepend("CACHE");
530 }
531
532 AliDebug(2, Form("Option: %s", option.Data()));
533
534 TFile *file = TFile::Open(filename, option);
9e1ceb13 535 if (!file) {
024cf675 536 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
9e1ceb13 537 return NULL;
538 }
539
540 // get the only AliCDBEntry object from the file
541 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
542
62032124 543 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
9e1ceb13 544
62032124 545 if (!anEntry) {
546 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
547 file->Close();
9e1ceb13 548 return NULL;
549 }
550
9e1ceb13 551 // The object's Id is not reset during storage
552 // If object's Id runRange or version do not match with filename,
553 // it means that someone renamed file by hand. In this case a warning msg is issued.
62032124 554
555 if(anEntry){
556 AliCDBId entryId = anEntry->GetId();
4005d0b5 557 Int_t tmpSubVersion = dataId->GetSubVersion();
558 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
8e245d15 559 if(!entryId.IsEqual(dataId)){
b8ec52f6 560 AliWarning(Form("Mismatch between file name and object's Id!"));
561 AliWarning(Form("File name: %s", dataId->ToString().Data()));
562 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
62032124 563 }
4005d0b5 564 dataId->SetSubVersion(tmpSubVersion);
9e1ceb13 565 }
566
62032124 567 anEntry->SetLastStorage("grid");
568
145f45a2 569 // Check whether entry contains a TTree. In case load the tree in memory!
570 LoadTreeFromFile(anEntry);
571
9e1ceb13 572 // close file, return retieved entry
573 file->Close(); delete file; file=0;
62032124 574
575 return anEntry;
9e1ceb13 576}
577
578//_____________________________________________________________________________
62032124 579TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
9e1ceb13 580// multiple request (AliCDBStorage::GetAll)
581
62032124 582 TList* result = new TList();
583 result->SetOwner();
9e1ceb13 584
6dc56e97 585 TObjArray validFileIds;
62032124 586 validFileIds.SetOwner(1);
9e1ceb13 587
62032124 588 Bool_t alreadyLoaded = kFALSE;
9e1ceb13 589
62032124 590 // look for file matching query requests (path, runRange)
591 if(queryId.GetFirstRun() == fRun &&
c3a7b59a 592 fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
62032124 593 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
594 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
595 queryId.GetFirstRun(), queryId.GetPath().Data()));
9e1ceb13 596
62032124 597 alreadyLoaded = kTRUE;
9e1ceb13 598
62032124 599 } else {
600 // List of files valid for reqested run was not loaded. Looking directly into CDB
601 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
602 queryId.GetFirstRun(), queryId.GetPath().Data()));
603
604 TString filter;
6dc56e97 605 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
606
5ef33267 607 TString path = queryId.GetPath();
6dc56e97 608
5ef33267 609 TString pattern = "Run*.root";
610 TString optionQuery = "-y";
0e10c8fd 611
5ef33267 612 TString addFolder = "";
613 if (!path.Contains("*")){
614 if (!path.BeginsWith("/")) addFolder += "/";
615 addFolder += path;
616 }
617 else{
618 if (path.BeginsWith("/")) path.Remove(0,1);
619 if (path.EndsWith("/")) path.Remove(path.Length()-1,1);
620 TObjArray* tokenArr = path.Tokenize("/");
621 if (tokenArr->GetEntries() != 3) {
622 AliError("Not a 3 level path! Keeping old query...");
623 pattern.Prepend(path+"/");
624 }
625 else{
626 TString str0 = ((TObjString*)tokenArr->At(0))->String();
627 TString str1 = ((TObjString*)tokenArr->At(1))->String();
628 TString str2 = ((TObjString*)tokenArr->At(2))->String();
629 if (str0 != "*" && str1 != "*" && str2 == "*"){
630 // e.g. "ITS/Calib/*"
631 addFolder = "/"+str0+"/"+str1;
632 }
633 else if (str0 != "*" && str1 == "*" && str2 == "*"){
634 // e.g. "ITS/*/*"
635 addFolder = "/"+str0;
636 }
637 else if (str0 == "*" && str1 == "*" && str2 == "*"){
638 // e.g. "*/*/*"
639 // do nothing: addFolder is already an empty string;
640 }
641 else{
642 // e.g. "ITS/*/RecoParam"
643 pattern.Prepend(path+"/");
644 }
645 }
646 delete tokenArr; tokenArr=0;
0e10c8fd 647 }
648
5ef33267 649 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
650
651 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
652
653 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
654
655 if (!res) {
656 AliError("Grid query failed");
657 return 0;
658 }
62032124 659
62032124 660 AliCDBId validFileId;
661 for(int i=0; i<res->GetEntries(); i++){
662 TString filename = res->GetKey(i, "lfn");
c3a7b59a 663 if(filename == "") continue;
62032124 664 if(FilenameToId(filename, validFileId))
665 validFileIds.AddLast(validFileId.Clone());
666 }
667 delete res;
9e1ceb13 668 }
669
62032124 670 TIter *iter=0;
671 if(alreadyLoaded){
672 iter = new TIter(&fValidFileIds);
673 } else {
674 iter = new TIter(&validFileIds);
675 }
9e1ceb13 676
6dc56e97 677 TObjArray selectedIds;
62032124 678 selectedIds.SetOwner(1);
679
680 // loop on list of valid Ids to select the right version to get.
681 // According to query and to the selection criteria list, version can be the highest or exact
682 AliCDBPath pathCopy;
683 AliCDBId* anIdPtr=0;
684 AliCDBId* dataId=0;
685 AliCDBPath queryPath = queryId.GetAliCDBPath();
686 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
687 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
688 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
689 pathCopy = thisCDBPath;
690
691 // check the selection criteria list for this query
692 AliCDBId thisId(*anIdPtr);
693 thisId.SetVersion(queryId.GetVersion());
694 if(!thisId.HasVersion()) GetSelection(&thisId);
695
696 if(alreadyLoaded){
697 dataId = GetId(fValidFileIds, thisId);
698 } else {
699 dataId = GetId(validFileIds, thisId);
9e1ceb13 700 }
4667c116 701 if(dataId) selectedIds.Add(dataId);
9e1ceb13 702 }
9e1ceb13 703
62032124 704 delete iter; iter=0;
9e1ceb13 705
62032124 706 // selectedIds contains the Ids of the files matching all requests of query!
707 // All the objects are now ready to be retrieved
708 iter = new TIter(&selectedIds);
709 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
710 TString filename;
711 if (!IdToFilename(*anIdPtr, filename)) {
712 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
713 continue;
714 }
9e1ceb13 715
62032124 716 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
9e1ceb13 717
62032124 718 if(anEntry) result->Add(anEntry);
9e1ceb13 719
62032124 720 }
721 delete iter; iter=0;
722
723 return result;
9e1ceb13 724}
725
726//_____________________________________________________________________________
06eeadea 727Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry, const char* mirrors) {
703128af 728 // put an AliCDBEntry object into the database
62032124 729
9e1ceb13 730 AliCDBId& id = entry->GetId();
731
732 // set version for the entry to be stored
62032124 733 if (!PrepareId(id)) return kFALSE;
9e1ceb13 734
735 // build filename from entry's id
736 TString filename;
62032124 737 if (!IdToFilename(id, filename)) {
9e1ceb13 738 AliError("Bad ID encountered! Subnormal error!");
739 return kFALSE;
62032124 740 }
741
742 TString folderToTag = Form("%s%s",
703128af 743 fDBFolder.Data(),
744 id.GetPath().Data());
62032124 745
9e1ceb13 746 TDirectory* saveDir = gDirectory;
747
fb33abde 748 TString fullFilename = Form("/alien%s", filename.Data());
06eeadea 749 TString seMirrors(mirrors);
c5941d67 750 if(seMirrors.IsNull() || seMirrors.IsWhitespace()) seMirrors=GetMirrorSEs();
62032124 751 // specify SE to filename
c5941d67 752 // if a list of SEs was passed to this method or set via SetMirrorSEs, set the first as SE for opening the file.
753 // The other SEs will be used in cascade in case of failure in opening the file.
754 // The remaining SEs will be used to create replicas.
06eeadea 755 TObjArray *arraySEs = seMirrors.Tokenize(',');
756 Int_t nSEs = arraySEs->GetEntries();
757 Int_t remainingSEs = 1;
758 if(nSEs == 0){
703128af 759 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
06eeadea 760 }else{
703128af 761 remainingSEs = nSEs;
06eeadea 762 }
62032124 763
9e1ceb13 764 // open file
89004e35 765 TFile *file=0;
5bfe3965 766 AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
06eeadea 767 TString targetSE("");
703128af 768
769 Bool_t result = kFALSE;
770 Bool_t reOpenResult = kFALSE;
771 Int_t reOpenAttempts=0;
772 while( !reOpenResult && reOpenAttempts<2){ //loop to check the file after closing it, to catch the unlikely but possible case when the file
773 // is cleaned up by alien just before closing as a consequence of a network disconnection while writing
774
775 while( !file && remainingSEs>0){
776 if(nSEs!=0){
777 TObjString *target = (TObjString*) arraySEs->At(nSEs-remainingSEs);
778 targetSE=target->String();
779 if ( !(targetSE.BeginsWith("ALICE::") && targetSE.CountChar(':')==4) ) {
780 AliError(Form("\"%s\" is an invalid storage element identifier.",targetSE.Data()));
781 continue;
782 }
783 if(fullFilename.Contains('?')) fullFilename.Remove(fullFilename.Last('?'));
784 fullFilename += Form("?se=%s",targetSE.Data());
785 }
786 Int_t remainingAttempts=fNretry;
787 Int_t nsleep = fInitRetrySeconds; // number of seconds between attempts. We let it increase exponentially
788 AliDebug(2, Form("Uploading file into SE #%d: %s",nSEs-remainingSEs+1,targetSE.Data()));
789 while(remainingAttempts > 0) {
790 AliDebug(2, Form("Uploading file into OCDB at %s - Attempt #%d",targetSE.Data(),fNretry-remainingAttempts+1));
791 remainingAttempts--;
792 file = TFile::Open(fullFilename,"CREATE");
793 if(!file || !file->IsWritable()){
794 if(file) file->Close(); delete file; file=0; // file is not writable
795 TString message(TString::Format("Attempt %d failed.",fNretry-remainingAttempts));
796 if(remainingAttempts>0) {
797 message += " Sleeping for "; message += nsleep; message += " seconds";
798 }else{
799 if(remainingSEs>0) message += " Trying to upload at next SE";
800 }
801 AliDebug(2, message.Data());
802 if(remainingAttempts>0) sleep(nsleep);
803 }else{
703128af 804 remainingAttempts=0;
805 }
806 nsleep*=fInitRetrySeconds;
807 }
808 remainingSEs--;
06eeadea 809 }
703128af 810 if(!file){
811 AliError(Form("All %d attempts have failed on all %d SEs. Returning...",fNretry,nSEs));
812 return kFALSE;
89004e35 813 }
9e1ceb13 814
703128af 815 file->cd();
145f45a2 816
703128af 817 //SetTreeToFile(entry, file);
818 entry->SetVersion(id.GetVersion());
9e1ceb13 819
703128af 820 // write object (key name: "AliCDBEntry")
821 result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
822 if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
823 file->Close();
9e1ceb13 824
703128af 825 if(result)
826 {
827 AliDebug(2, Form("Reopening file %s for checking its correctness",fullFilename.Data()));
828 TFile* ffile = TFile::Open(fullFilename.Data(),"READ");
829 if(!ffile){
830 reOpenResult = kFALSE;
831 AliInfo(Form("The file %s was closed successfully but cannot be reopened. Trying now to regenerate it (regeneration attempt number %d)",
832 fullFilename.Data(),++reOpenAttempts));
833 delete file; file=0;
834 AliDebug(2, Form("Removing file %s", filename.Data()));
835 if(!gGrid->Rm(filename.Data()))
836 AliError("Can't delete file!");
837 remainingSEs++;
838 }else{
839 reOpenResult = kTRUE;
840 ffile->Close();
841 }
842 delete ffile; ffile=0;
843 }
844 }
9e1ceb13 845
846 if (saveDir) saveDir->cd(); else gROOT->cd();
703128af 847 delete file; file=0;
848
849 if(result && reOpenResult) {
c3a7b59a 850
c3a7b59a 851 if(!TagFileId(filename, &id)){
852 AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
853 if(!gGrid->Rm(filename.Data()))
854 AliError("Can't delete file!");
855 return kFALSE;
856 }
857
858 TagFileMetaData(filename, entry->GetMetaData());
703128af 859 }else{
860 AliError("The file could not be opend or the object could not be written");
861 if(!gGrid->Rm(filename.Data()))
862 AliError("Can't delete file!");
863 return kFALSE;
9e1ceb13 864 }
62032124 865
c75fabed 866 AliInfo(Form("CDB object stored into file %s", filename.Data()));
06eeadea 867 if(nSEs==0)
703128af 868 AliInfo(Form("Storage Element: %s", fSE.Data()));
06eeadea 869 else
703128af 870 AliInfo(Form("Storage Element: %s", targetSE.Data()));
06eeadea 871
703128af 872 //In case of other SEs specified by the user, mirror the file to the remaining SEs
a4a99395 873 for(Int_t i=0; i<nSEs; i++){
703128af 874 if(i==nSEs-remainingSEs-1) continue; // skip mirroring to the SE where the file was saved
875 TString mirrorCmd("mirror ");
876 mirrorCmd += filename;
877 mirrorCmd += " ";
878 TObjString *target = (TObjString*) arraySEs->At(i);
879 TString mirrorSE(target->String());
880 mirrorCmd += mirrorSE;
881 AliDebug(5,Form("mirror command: \"%s\"",mirrorCmd.Data()));
882 AliInfo(Form("Mirroring to storage element: %s", mirrorSE.Data()));
883 gGrid->Command(mirrorCmd.Data());
06eeadea 884 }
e0a451f6 885 arraySEs->Delete(); arraySEs=0;
06eeadea 886
703128af 887 return kTRUE;
62032124 888}
c5941d67 889
62032124 890//_____________________________________________________________________________
891Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
892// add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
893
894 Bool_t result = kTRUE;
895 AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
896 TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
897 TGridResult *gridres = gGrid->Command(addTag.Data());
898 const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
899 if(resCode[0] != '1') {
900 AliError(Form("Couldn't add %s tags to folder %s !",
901 tagname, folderToTag.Data()));
902 result = kFALSE;
903 }
904 delete gridres;
9e1ceb13 905 return result;
906}
907
62032124 908//_____________________________________________________________________________
c3a7b59a 909Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
62032124 910// tag stored object in CDB table using object Id's parameters
911
0e10c8fd 912
913 TString dirname(filename);
914 Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
915
f430a9ce 916 TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
917 TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
62032124 918 id->GetFirstRun(),
919 id->GetLastRun(),
920 id->GetVersion());
0e10c8fd 921 TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
4667c116 922 id->GetPathLevel(0).Data(),
923 id->GetPathLevel(1).Data(),
924 id->GetPathLevel(2).Data());
0e10c8fd 925 //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber);
926 TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber);
927 TString addTagValue = Form("%s%s%s%s",
f430a9ce 928 addTagValue1.Data(),
929 addTagValue2.Data(),
0e10c8fd 930 addTagValue3.Data(),
931 addTagValue4.Data());
62032124 932
c3a7b59a 933 Bool_t result = kFALSE;
62032124 934 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
935 TGridResult* res = gGrid->Command(addTagValue.Data());
936 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
937 if(resCode[0] != '1') {
c3a7b59a 938 AliError(Form("Couldn't add CDB tag value to file %s !",
62032124 939 filename.Data()));
c3a7b59a 940 result = kFALSE;
62032124 941 } else {
c75fabed 942 AliDebug(2, "Object successfully tagged.");
c3a7b59a 943 result = kTRUE;
62032124 944 }
945 delete res;
c3a7b59a 946 return result;
62032124 947
948}
949
4667c116 950//_____________________________________________________________________________
951Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
952// tag folder with ShortLived tag
953
954 TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
955
956 Bool_t result = kFALSE;
957 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
958 TGridResult* res = gGrid->Command(addTagValue.Data());
959 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
960 if(resCode[0] != '1') {
961 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
962 result = kFALSE;
963 } else {
c75fabed 964 AliDebug(2,"Object successfully tagged.");
4667c116 965 result = kTRUE;
966 }
967 delete res;
968 return result;
969
970}
971
62032124 972//_____________________________________________________________________________
c3a7b59a 973Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
62032124 974// tag stored object in CDB table using object Id's parameters
975
f430a9ce 976 TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
977 TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
62032124 978 md->GetObjectClassName(),
979 md->GetResponsible(),
980 md->GetBeamPeriod());
f430a9ce 981 TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
62032124 982 md->GetAliRootVersion(),
983 md->GetComment());
984 TString addTagValue = Form("%s%s%s",
f430a9ce 985 addTagValue1.Data(),
986 addTagValue2.Data(),
987 addTagValue3.Data());
62032124 988
c3a7b59a 989 Bool_t result = kFALSE;
62032124 990 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
991 TGridResult* res = gGrid->Command(addTagValue.Data());
992 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
993 if(resCode[0] != '1') {
994 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
995 filename.Data()));
c3a7b59a 996 result = kFALSE;
62032124 997 } else {
c75fabed 998 AliDebug(2,"Object successfully tagged.");
c3a7b59a 999 result = kTRUE;
62032124 1000 }
c3a7b59a 1001 return result;
62032124 1002}
1003
b05400be 1004//_____________________________________________________________________________
1005TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
6dc56e97 1006
b05400be 1007 TString turl(fileName);
1008 turl.Prepend("/alien" + fDBFolder);
62032124 1009 turl += "?se="; turl += fSE.Data();
b05400be 1010 TFile *file = TFile::Open(turl);
1011 if (!file) {
1012 AliError(Form("Can't open selection file <%s>!", turl.Data()));
1013 return NULL;
1014 }
1015
1016 TList *list = new TList();
1017 list->SetOwner();
1018 int i=0;
1019 TString keycycle;
62032124 1020
b05400be 1021 AliCDBId *id;
1022 while(1){
1023 i++;
1024 keycycle = "AliCDBId;";
1025 keycycle+=i;
1026
1027 id = (AliCDBId*) file->Get(keycycle);
1028 if(!id) break;
1029 list->AddFirst(id);
1030 }
1031 file->Close(); delete file; file=0;
1032
1033 return list;
1034
1035
1036}
1037
1038//_____________________________________________________________________________
1039Bool_t AliCDBGrid::Contains(const char* path) const{
1040// check for path in storage's DBFolder
1041
1042 TString initDir(gGrid->Pwd(0));
1043 TString dirName(fDBFolder);
1044 dirName += path; // dirName = fDBFolder/path
1045 Bool_t result=kFALSE;
1046 if (gGrid->Cd(dirName,0)) result=kTRUE;
1047 gGrid->Cd(initDir.Data(),0);
1048 return result;
1049}
1050
62032124 1051//_____________________________________________________________________________
1052void AliCDBGrid::QueryValidFiles()
1053{
1054// Query the CDB for files valid for AliCDBStorage::fRun
1055// fills list fValidFileIds with AliCDBId objects created from file name
1056
1057 TString filter;
6dc56e97 1058 MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
1059
61f0b0b6 1060 TString path = fPathFilter.GetPath();
1061
1062 TString pattern = "Run*";
0e10c8fd 1063 TString optionQuery = "-y";
1064 if(fVersion >= 0) {
61f0b0b6 1065 pattern += Form("_v%d_s0", fVersion);
0e10c8fd 1066 optionQuery = "";
1067 }
6dc56e97 1068 pattern += ".root";
1069 AliDebug(2,Form("pattern: %s", pattern.Data()));
1070
61f0b0b6 1071 TString addFolder = "";
1072 if (!path.Contains("*")){
1073 if (!path.BeginsWith("/")) addFolder += "/";
1074 addFolder += path;
1075 }
1076 else{
1077 if (path.BeginsWith("/")) path.Remove(0,1);
1078 if (path.EndsWith("/")) path.Remove(path.Length()-1,1);
1079 TObjArray* tokenArr = path.Tokenize("/");
1080 if (tokenArr->GetEntries() != 3) {
1081 AliError("Not a 3 level path! Keeping old query...");
5ef33267 1082 pattern.Prepend(path+"/");
61f0b0b6 1083 }
1084 else{
1085 TString str0 = ((TObjString*)tokenArr->At(0))->String();
1086 TString str1 = ((TObjString*)tokenArr->At(1))->String();
1087 TString str2 = ((TObjString*)tokenArr->At(2))->String();
1088 if (str0 != "*" && str1 != "*" && str2 == "*"){
1089 // e.g. "ITS/Calib/*"
1090 addFolder = "/"+str0+"/"+str1;
1091 }
1092 else if (str0 != "*" && str1 == "*" && str2 == "*"){
1093 // e.g. "ITS/*/*"
1094 addFolder = "/"+str0;
1095 }
1096 else if (str0 == "*" && str1 == "*" && str2 == "*"){
1097 // e.g. "*/*/*"
1098 // do nothing: addFolder is already an empty string;
1099 }
1100 else{
1101 // e.g. "ITS/*/RecoParam"
5ef33267 1102 pattern.Prepend(path+"/");
61f0b0b6 1103 }
1104 }
1105 delete tokenArr; tokenArr=0;
1106 }
1107
1108 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1109
5ef33267 1110 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
0e10c8fd 1111
1112 if (optionQuery == "-y"){
1113 AliInfo("Only latest version will be returned");
1114 }
1115
5ef33267 1116 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
62032124 1117
b7e6419d 1118 if (!res) {
1119 AliError("Grid query failed");
1120 return;
1121 }
1122
b68a24b3 1123 TIter next(res);
1124 TMap *map;
1125 while ((map = (TMap*)next())) {
1126 TObjString *entry;
1127 if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
1128 TString& filename = entry->String();
1129 if(filename.IsNull()) continue;
1130 AliDebug(2,Form("Found valid file: %s", filename.Data()));
1131 AliCDBId *validFileId = new AliCDBId;
1132 Bool_t result = FilenameToId(filename, *validFileId);
1133 if(result) {
1134 fValidFileIds.AddLast(validFileId);
1135 }
1136 else {
1137 delete validFileId;
1138 }
1139 }
62032124 1140 }
1141 delete res;
1142
1143}
1144
1145//_____________________________________________________________________________
c3a7b59a 1146void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
62032124 1147 const AliCDBMetaData* md, TString& result) const
1148{
1149// create filter for file query
1150
c3a7b59a 1151 result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
62032124 1152
6dc56e97 1153// if(version >= 0) {
1154// result += Form(" and CDB:version=%d", version);
1155// }
1156// if(pathFilter.GetLevel0() != "*") {
1157// result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
1158// }
1159// if(pathFilter.GetLevel1() != "*") {
1160// result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
1161// }
1162// if(pathFilter.GetLevel2() != "*") {
1163// result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
1164// }
62032124 1165
1166 if(md){
1167 if(md->GetObjectClassName()[0] != '\0') {
1168 result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
1169 }
1170 if(md->GetResponsible()[0] != '\0') {
1171 result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
1172 }
1173 if(md->GetBeamPeriod() != 0) {
1174 result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
1175 }
1176 if(md->GetAliRootVersion()[0] != '\0') {
1177 result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
1178 }
1179 if(md->GetComment()[0] != '\0') {
1180 result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
1181 }
1182 }
1183 AliDebug(2, Form("filter: %s",result.Data()));
1184
1185}
1186
c3a7b59a 1187//_____________________________________________________________________________
1188Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
1189// get last version found in the database valid for run and path
1190
c3a7b59a 1191 AliCDBPath aCDBPath(path);
1192 if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
1193 AliError(Form("Invalid path in request: %s", path));
1194 return -1;
1195 }
1196 AliCDBId query(path, run, run, -1, -1);
c3a7b59a 1197
0e10c8fd 1198 AliCDBId* dataId = 0;
c3a7b59a 1199 // look for file matching query requests (path, runRange, version)
6dc56e97 1200 if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
c3a7b59a 1201 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
1202 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
1203 run, path));
1204 dataId = GetId(fValidFileIds, query);
1205 if (!dataId) return -1;
4667c116 1206 Int_t version = dataId->GetVersion();
1207 delete dataId;
1208 return version;
c3a7b59a 1209
1210 }
1211 // List of files valid for reqested run was not loaded. Looking directly into CDB
1212 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
1213 run, path));
1214
4667c116 1215 TObjArray validFileIds;
1216 validFileIds.SetOwner(1);
1217
c3a7b59a 1218 TString filter;
6dc56e97 1219 MakeQueryFilter(run, run, 0, filter);
1220
5ef33267 1221 TString pattern = ".root";
1222
1223 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),path));
1224
1225 AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1226 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, "-y -m");
c3a7b59a 1227
c3a7b59a 1228 AliCDBId validFileId;
0e10c8fd 1229 if (res->GetEntries()>1){
1230 AliWarning("Number of found entries >1, even if option -y was used");
1231 for(int i=0; i<res->GetEntries(); i++){
1232 TString filename = res->GetKey(i, "lfn");
1233 if(filename == "") continue;
1234 if(FilenameToId(filename, validFileId))
c3a7b59a 1235 validFileIds.AddLast(validFileId.Clone());
0e10c8fd 1236 }
1237 dataId = GetId(validFileIds, query);
1238 if (!dataId) return -1;
1239
1240 Int_t version = dataId->GetVersion();
1241 delete dataId;
1242 return version;
1243 }
1244 else if (res->GetEntries()==1){
1245 TString filename = res->GetKey(0, "lfn");
1246 if(filename == "") {
1247 AliError("The only entry found has filename empty");
1248 return -1;
1249 }
1250 if(FilenameToId(filename, validFileId)) return validFileId.GetVersion();
1251 else{
1252 AliError("Impossible to get FileId from filename");
1253 return -1;
1254 }
1255 }
1256 else {
1257 AliError("No entries found");
1258 return -1;
c3a7b59a 1259 }
c3a7b59a 1260
0e10c8fd 1261 delete res;
62ea513e 1262
c3a7b59a 1263
1264}
1265
1266//_____________________________________________________________________________
1267Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
1268// get last subversion found in the database valid for run and path
1269 AliError("Objects in GRID storage have no sub version!");
1270 return -1;
1271}
1272
1273
9e1ceb13 1274/////////////////////////////////////////////////////////////////////////////////////////////////
1275// //
1276// AliCDBGrid factory //
1277// //
1278/////////////////////////////////////////////////////////////////////////////////////////////////
1279
1280ClassImp(AliCDBGridFactory)
1281
1282//_____________________________________________________________________________
1283Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1284// check if the string is valid Grid URI
1285
b05400be 1286 TRegexp gridPattern("^alien://.+$");
9e1ceb13 1287
27cac7e3 1288 return TString(gridString).Contains(gridPattern);
9e1ceb13 1289}
1290
1291//_____________________________________________________________________________
1292AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1293// create AliCDBGridParam class from the URI string
1294
1295 if (!Validate(gridString)) {
1296 return NULL;
1297 }
c75fabed 1298
b05400be 1299 TString buffer(gridString);
c3a7b59a 1300
1301 TString gridUrl = "alien://";
b05400be 1302 TString user = "";
4667c116 1303 TString dbFolder = "";
fb33abde 1304 TString se = "default";
c75fabed 1305 TString cacheFolder = "";
1306 Bool_t operateDisconnected = kTRUE;
1307 Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB
1308 Long_t cleanupInterval = 0;
b05400be 1309
1310 TObjArray *arr = buffer.Tokenize('?');
1311 TIter iter(arr);
62032124 1312 TObjString *str = 0;
4667c116 1313
b05400be 1314 while((str = (TObjString*) iter.Next())){
1315 TString entry(str->String());
1316 Int_t indeq = entry.Index('=');
1317 if(indeq == -1) {
1318 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1319 gridUrl = entry;
1320 continue;
1321 } else {
1322 AliError(Form("Invalid entry! %s",entry.Data()));
1323 continue;
1324 }
1325 }
1326
1327 TString key = entry(0,indeq);
1328 TString value = entry(indeq+1,entry.Length()-indeq);
1329
1330 if(key.Contains("grid",TString::kIgnoreCase)) {
1331 gridUrl += value;
c75fabed 1332 }
b05400be 1333 else if (key.Contains("user",TString::kIgnoreCase)){
1334 user = value;
1335 }
b05400be 1336 else if (key.Contains("se",TString::kIgnoreCase)){
1337 se = value;
1338 }
d7744171 1339 else if (key.Contains("cacheF",TString::kIgnoreCase)){
c75fabed 1340 cacheFolder = value;
f8703115 1341 if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
c75fabed 1342 cacheFolder += "/";
1343 }
d7744171 1344 else if (key.Contains("folder",TString::kIgnoreCase)){
1345 dbFolder = value;
1346 }
c75fabed 1347 else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1348 if(value == "kTRUE") {
1349 operateDisconnected = kTRUE;
1350 } else if (value == "kFALSE") {
1351 operateDisconnected = kFALSE;
1352 } else if (value == "0" || value == "1") {
1353 operateDisconnected = (Bool_t) value.Atoi();
1354 } else {
1355 AliError(Form("Invalid entry! %s",entry.Data()));
1356 return NULL;
1357 }
1358 }
d7744171 1359 else if (key.Contains("cacheS",TString::kIgnoreCase)){
c75fabed 1360 if(value.IsDigit()) {
1361 cacheSize = value.Atoi();
1362 } else {
1363 AliError(Form("Invalid entry! %s",entry.Data()));
1364 return NULL;
1365 }
1366 }
1367 else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1368 if(value.IsDigit()) {
1369 cleanupInterval = value.Atoi();
1370 } else {
1371 AliError(Form("Invalid entry! %s",entry.Data()));
1372 return NULL;
1373 }
1374 }
b05400be 1375 else{
1376 AliError(Form("Invalid entry! %s",entry.Data()));
c75fabed 1377 return NULL;
b05400be 1378 }
1379 }
1380 delete arr; arr=0;
c75fabed 1381
1382 AliDebug(2, Form("gridUrl: %s", gridUrl.Data()));
1383 AliDebug(2, Form("user: %s", user.Data()));
1384 AliDebug(2, Form("dbFolder: %s", dbFolder.Data()));
1385 AliDebug(2, Form("s.e.: %s", se.Data()));
1386 AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1387 AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
d2a0a483 1388 AliDebug(2, Form("local cache size: %lld", cacheSize));
6720fa1f 1389 AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
9e1ceb13 1390
27cac7e3 1391 if(dbFolder == ""){
c75fabed 1392 AliError("Base folder must be specified!");
4667c116 1393 return NULL;
1394 }
1395
c75fabed 1396 return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1397 dbFolder.Data(), se.Data(), cacheFolder.Data(),
1398 operateDisconnected, cacheSize, cleanupInterval);
9e1ceb13 1399}
1400
1401//_____________________________________________________________________________
1402AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1403// create AliCDBGrid storage instance from parameters
1404
62032124 1405 AliCDBGrid *grid = 0;
9e1ceb13 1406 if (AliCDBGridParam::Class() == param->IsA()) {
62032124 1407
9e1ceb13 1408 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
62032124 1409 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1410 gridParam->GetUser().Data(),
1411 gridParam->GetDBFolder().Data(),
c75fabed 1412 gridParam->GetSE().Data(),
1413 gridParam->GetCacheFolder().Data(),
1414 gridParam->GetOperateDisconnected(),
1415 gridParam->GetCacheSize(),
1416 gridParam->GetCleanupInterval());
9e1ceb13 1417
9e1ceb13 1418 }
1419
c3a7b59a 1420 if(!gGrid && grid) {
1421 delete grid; grid=0;
1422 }
1423
62032124 1424 return grid;
9e1ceb13 1425}
1426
1427/////////////////////////////////////////////////////////////////////////////////////////////////
1428// //
c75fabed 1429// AliCDBGrid Parameter class // //
9e1ceb13 1430// //
1431/////////////////////////////////////////////////////////////////////////////////////////////////
1432
1433ClassImp(AliCDBGridParam)
1434
1435//_____________________________________________________________________________
62032124 1436AliCDBGridParam::AliCDBGridParam():
1437 AliCDBParam(),
1438 fGridUrl(),
1439 fUser(),
1440 fDBFolder(),
c75fabed 1441 fSE(),
1442 fCacheFolder(),
1443 fOperateDisconnected(),
1444 fCacheSize(),
1445 fCleanupInterval()
1446
62032124 1447 {
9e1ceb13 1448// default constructor
1449
1450}
1451
1452//_____________________________________________________________________________
c75fabed 1453AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
1454 const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1455 Long64_t cacheSize, Long_t cleanupInterval):
62032124 1456 AliCDBParam(),
b05400be 1457 fGridUrl(gridUrl),
9e1ceb13 1458 fUser(user),
b05400be 1459 fDBFolder(dbFolder),
c75fabed 1460 fSE(se),
1461 fCacheFolder(cacheFolder),
1462 fOperateDisconnected(operateDisconnected),
1463 fCacheSize(cacheSize),
1464 fCleanupInterval(cleanupInterval)
9e1ceb13 1465{
1466// constructor
c75fabed 1467
9e1ceb13 1468 SetType("alien");
1469
c75fabed 1470 TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
d2a0a483 1471 "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
62032124 1472 fGridUrl.Data(), fUser.Data(),
c75fabed 1473 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1474 fOperateDisconnected, fCacheSize, fCleanupInterval);
62032124 1475
1476 SetURI(uri.Data());
9e1ceb13 1477}
1478
1479//_____________________________________________________________________________
1480AliCDBGridParam::~AliCDBGridParam() {
1481// destructor
1482
1483}
1484
1485//_____________________________________________________________________________
1486AliCDBParam* AliCDBGridParam::CloneParam() const {
1487// clone parameter
1488
62032124 1489 return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
c75fabed 1490 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1491 fOperateDisconnected, fCacheSize, fCleanupInterval);
9e1ceb13 1492}
1493
1494//_____________________________________________________________________________
1495ULong_t AliCDBGridParam::Hash() const {
1496// return Hash function
1497
c75fabed 1498 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
9e1ceb13 1499}
1500
1501//_____________________________________________________________________________
1502Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1503// check if this object is equal to AliCDBParam obj
1504
1505 if (this == obj) {
1506 return kTRUE;
1507 }
1508
1509 if (AliCDBGridParam::Class() != obj->IsA()) {
1510 return kFALSE;
1511 }
1512
1513 AliCDBGridParam* other = (AliCDBGridParam*) obj;
1514
b05400be 1515 if(fGridUrl != other->fGridUrl) return kFALSE;
9e1ceb13 1516 if(fUser != other->fUser) return kFALSE;
b05400be 1517 if(fDBFolder != other->fDBFolder) return kFALSE;
9e1ceb13 1518 if(fSE != other->fSE) return kFALSE;
c75fabed 1519 if(fCacheFolder != other->fCacheFolder) return kFALSE;
1520 if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1521 if(fCacheSize != other->fCacheSize) return kFALSE;
1522 if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
9e1ceb13 1523 return kTRUE;
1524}
1525