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