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