1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /////////////////////////////////////////////////////////////////////////////////////////////////
19 // access class to a DataBase in an AliEn storage //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
25 #include <TGridResult.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
35 #include "AliCDBEntry.h"
36 #include "AliCDBGrid.h"
37 #include "AliCDBManager.h"
42 //_____________________________________________________________________________
43 AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder, const char *se) :
52 // if the same Grid is alreay active, skip connection
53 if (!gGrid || fGridUrl != gGrid->GridUrl()
54 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
55 // connection to the Grid
56 AliInfo("Connection to the Grid...");
58 AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
59 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
61 TGrid::Connect(fGridUrl.Data(),fUser.Data());
65 AliError("Connection failed!");
69 TString initDir(gGrid->Pwd(0));
70 if (fDBFolder[0] != '/') {
71 fDBFolder.Prepend(initDir);
74 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
75 if(!gGrid->Cd(fDBFolder.Data(),0)){
76 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
77 if(!gGrid->Mkdir(fDBFolder.Data(),"",0)){
78 AliError(Form("Cannot create folder <%s> !",fDBFolder.Data()));
81 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
84 // removes any '/' at the end of path, then append one '/'
85 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
89 fBaseFolder = fDBFolder;
91 // return to the initial directory
92 gGrid->Cd(initDir.Data(),0);
95 //_____________________________________________________________________________
96 AliCDBGrid::~AliCDBGrid()
99 delete gGrid; gGrid=0;
103 //_____________________________________________________________________________
104 Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
105 // build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
107 if(filename.Contains(fDBFolder)){
108 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
111 TString idPath = filename(0,filename.Last('/'));
113 if(!id.IsValid()) return kFALSE;
115 filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
118 // valid filename: Run#firstRun_#lastRun_v#version_s0.root
119 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
120 keyPattern.Index(filename, &mSize);
123 // TODO backward compatibility ... maybe remove later!
125 TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
126 oldKeyPattern.Index(filename, &oldmSize);
128 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
131 AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
132 id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
136 id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
139 filename.Resize(filename.Length() - sizeof(".root") + 1);
141 TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
143 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
144 id.SetFirstRun(atoi(firstRunString.Data() + 3));
145 id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
147 TString verString(((TObjString*) strArray->At(2))->GetString());
148 id.SetVersion(atoi(verString.Data() + 1));
155 //_____________________________________________________________________________
156 Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
157 // build file name from AliCDBId (path, run range, version) and fDBFolder
159 if (!id.GetAliCDBRunRange().IsValid()) {
160 AliDebug(2,Form("Invalid run range <%d, %d>.",
161 id.GetFirstRun(), id.GetLastRun()));
165 if (id.GetVersion() < 0) {
166 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
170 filename = Form("Run%d_%d_v%d",
175 if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
178 filename.Prepend(fDBFolder + id.GetPath() + '/');
183 //_____________________________________________________________________________
184 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
185 // prepare id (version) of the object that will be stored (called by PutEntry)
187 TString initDir(gGrid->Pwd(0));
189 TString dirName(fDBFolder);
191 Bool_t dirExist=kFALSE;
195 // go to the path; if directory does not exist, create it
196 for(int i=0;i<3;i++){
197 dirName+=Form("%s/",id.GetPathLevel(i).Data());
198 dirExist=gGrid->Cd(dirName,0);
200 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
201 if(!gGrid->Mkdir(dirName,"",0)){
202 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
203 gGrid->Cd(initDir.Data());
207 // if folders are new add tags to them
209 // TODO Currently disabled
210 // AliInfo("Tagging level 1 folder with \"ShortLived\" tag");
211 // if(!AddTag(dirName,"ShortLived_try")){
212 // AliError(Form("Could not tag folder %s !", dirName.Data()));
213 // if(!gGrid->Rmdir(dirName.Data())){
214 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
220 AliInfo("Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
221 if(!AddTag(dirName,"CDB")){
222 AliError(Form("Could not tag folder %s !", dirName.Data()));
223 if(!gGrid->Rmdir(dirName.Data())){
224 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
228 if(!AddTag(dirName,"CDB_MD")){
229 AliError(Form("Could not tag folder %s !", dirName.Data()));
230 if(!gGrid->Rmdir(dirName.Data())){
231 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
236 // add short lived tag!
237 // TODO Currently disabled
238 // TString path=id.GetPath();
239 // if(AliCDBManager::Instance()->IsShortLived(path.Data())) {
240 // AliInfo(Form("Tagging %s as short lived", dirName.Data()));
241 // if(!TagShortLived(dirName, kTRUE)){
242 // AliError(Form("Could not tag folder %s !", dirName.Data()));
243 // if(!gGrid->Rmdir(dirName.Data())){
244 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
249 // AliInfo(Form("Tagging %s as long lived", dirName.Data()));
250 // if(!TagShortLived(dirName, kFALSE)){
251 // AliError(Form("Could not tag folder %s !", dirName.Data()));
252 // if(!gGrid->Rmdir(dirName.Data())){
253 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
261 gGrid->Cd(initDir,0);
264 AliCDBId anId; // the id got from filename
265 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
266 Int_t lastVersion=0; // highest version found
268 TGridResult *res = gGrid->Ls(dirName);
270 //loop on the files in the directory, look for highest version
271 for(int i=0; i < res->GetEntries(); i++){
272 filename=res->GetFileNamePath(i);
273 if (!FilenameToId(filename, anId)) continue;
274 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
275 lastVersion = anId.GetVersion();
276 lastRunRange = anId.GetAliCDBRunRange();
282 id.SetVersion(lastVersion + 1);
285 TString lastStorage = id.GetLastStorage();
286 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
287 AliDebug(2, Form("A NEW object is being stored with version %d",
289 AliDebug(2, Form("and it will hide previously stored object with version %d!",
293 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
294 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
295 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
300 //_____________________________________________________________________________
301 AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
302 // look for the Id that matches query's requests (highest or exact version)
304 if(validFileIds.GetEntriesFast() < 1) {
306 } else if (validFileIds.GetEntriesFast() == 1) {
307 return dynamic_cast<AliCDBId*> (validFileIds.At(0)->Clone());
310 TIter iter(&validFileIds);
315 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
316 if(anIdPtr->GetPath() != query.GetPath()) continue;
318 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
320 if (!query.HasVersion()){ // look for highest version
321 if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
322 if(result && result->GetVersion() == anIdPtr->GetVersion()) {
323 AliError(Form("More than one object valid for run %d, version %d!",
324 query.GetFirstRun(), anIdPtr->GetVersion()));
328 } else { // look for specified version
329 if(query.GetVersion() != anIdPtr->GetVersion()) continue;
330 if(result && result->GetVersion() == anIdPtr->GetVersion()){
331 AliError(Form("More than one object valid for run %d, version %d!",
332 query.GetFirstRun(), anIdPtr->GetVersion()));
340 return dynamic_cast<AliCDBId*> (result->Clone());
343 //_____________________________________________________________________________
344 AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
345 // get AliCDBId from the database
346 // User must delete returned object
350 AliCDBId selectedId(queryId);
351 if (!selectedId.HasVersion()) {
352 // if version is not specified, first check the selection criteria list
353 GetSelection(&selectedId);
356 TObjArray validFileIds;
357 validFileIds.SetOwner(1);
359 // look for file matching query requests (path, runRange, version)
360 if(selectedId.GetFirstRun() == fRun &&
361 fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
362 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
363 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
364 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
365 dataId = GetId(fValidFileIds, selectedId);
368 // List of files valid for reqested run was not loaded. Looking directly into CDB
369 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
370 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
373 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
375 TString pattern = Form("%s/Run*", selectedId.GetPath().Data());
376 if(selectedId.GetVersion() >= 0) pattern += Form("_v%d*",selectedId.GetVersion());
378 AliDebug(2,Form("pattern: %s", pattern.Data()));
380 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
381 AliCDBId validFileId;
382 for(int i=0; i<res->GetEntries(); i++){
383 TString filename = res->GetKey(i, "lfn");
384 if(filename == "") continue;
385 if(FilenameToId(filename, validFileId))
386 validFileIds.AddLast(validFileId.Clone());
389 dataId = GetId(validFileIds, selectedId);
395 //_____________________________________________________________________________
396 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
397 // get AliCDBEntry from the database
399 AliCDBId* dataId = GetEntryId(queryId);
401 if (!dataId) return NULL;
404 if (!IdToFilename(*dataId, filename)) {
405 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
410 AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
416 //_____________________________________________________________________________
417 AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
418 // Get AliCBEntry object from file "filename"
420 AliDebug(2,Form("Opening file: %s",filename.Data()));
422 filename.Prepend("/alien");
423 TFile *file = TFile::Open(filename);
425 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
429 // get the only AliCDBEntry object from the file
430 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
432 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
435 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
440 // The object's Id is not reset during storage
441 // If object's Id runRange or version do not match with filename,
442 // it means that someone renamed file by hand. In this case a warning msg is issued.
445 AliCDBId entryId = anEntry->GetId();
446 Int_t tmpSubVersion = dataId->GetSubVersion();
447 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
448 if(!entryId.IsEqual(dataId)){
449 AliWarning(Form("Mismatch between file name and object's Id!"));
450 AliWarning(Form("File name: %s", dataId->ToString().Data()));
451 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
453 dataId->SetSubVersion(tmpSubVersion);
456 anEntry->SetLastStorage("grid");
458 // Check whether entry contains a TTree. In case load the tree in memory!
459 LoadTreeFromFile(anEntry);
461 // close file, return retieved entry
462 file->Close(); delete file; file=0;
467 //_____________________________________________________________________________
468 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
469 // multiple request (AliCDBStorage::GetAll)
471 TList* result = new TList();
474 TObjArray validFileIds;
475 validFileIds.SetOwner(1);
477 Bool_t alreadyLoaded = kFALSE;
479 // look for file matching query requests (path, runRange)
480 if(queryId.GetFirstRun() == fRun &&
481 fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
482 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
483 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
484 queryId.GetFirstRun(), queryId.GetPath().Data()));
486 alreadyLoaded = kTRUE;
489 // List of files valid for reqested run was not loaded. Looking directly into CDB
490 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
491 queryId.GetFirstRun(), queryId.GetPath().Data()));
494 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
496 TString pattern = Form("%s/Run*.root", queryId.GetPath().Data());
497 AliDebug(2,Form("pattern: %s", pattern.Data()));
499 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
501 AliCDBId validFileId;
502 for(int i=0; i<res->GetEntries(); i++){
503 TString filename = res->GetKey(i, "lfn");
504 if(filename == "") continue;
505 if(FilenameToId(filename, validFileId))
506 validFileIds.AddLast(validFileId.Clone());
513 iter = new TIter(&fValidFileIds);
515 iter = new TIter(&validFileIds);
518 TObjArray selectedIds;
519 selectedIds.SetOwner(1);
521 // loop on list of valid Ids to select the right version to get.
522 // According to query and to the selection criteria list, version can be the highest or exact
526 AliCDBPath queryPath = queryId.GetAliCDBPath();
527 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
528 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
529 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
530 pathCopy = thisCDBPath;
532 // check the selection criteria list for this query
533 AliCDBId thisId(*anIdPtr);
534 thisId.SetVersion(queryId.GetVersion());
535 if(!thisId.HasVersion()) GetSelection(&thisId);
538 dataId = GetId(fValidFileIds, thisId);
540 dataId = GetId(validFileIds, thisId);
542 if(dataId) selectedIds.Add(dataId);
547 // selectedIds contains the Ids of the files matching all requests of query!
548 // All the objects are now ready to be retrieved
549 iter = new TIter(&selectedIds);
550 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
552 if (!IdToFilename(*anIdPtr, filename)) {
553 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
557 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
559 if(anEntry) result->Add(anEntry);
567 //_____________________________________________________________________________
568 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
569 // put an AliCDBEntry object into the database
571 AliCDBId& id = entry->GetId();
573 // set version for the entry to be stored
574 if (!PrepareId(id)) return kFALSE;
576 // build filename from entry's id
578 if (!IdToFilename(id, filename)) {
579 AliError("Bad ID encountered! Subnormal error!");
583 TString folderToTag = Form("%s%s",
585 id.GetPath().Data());
587 TDirectory* saveDir = gDirectory;
589 TString fullFilename = Form("/alien%s", filename.Data());
590 // specify SE to filename
591 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
594 TFile *file = TFile::Open(fullFilename,"CREATE");
595 if(!file || !file->IsWritable()){
596 AliError(Form("Can't open file <%s>!", filename.Data()));
597 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
603 //SetTreeToFile(entry, file);
605 entry->SetVersion(id.GetVersion());
607 // write object (key name: "AliCDBEntry")
608 Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
609 if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
612 if (saveDir) saveDir->cd(); else gROOT->cd();
613 file->Close(); delete file; file=0;
616 AliInfo(Form("CDB object stored into file %s", filename.Data()));
617 AliInfo(Form("using S.E.: %s", fSE.Data()));
619 if(!TagFileId(filename, &id)){
620 AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
621 if(!gGrid->Rm(filename.Data()))
622 AliError("Can't delete file!");
626 TagFileMetaData(filename, entry->GetMetaData());
631 //_____________________________________________________________________________
632 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
633 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
635 Bool_t result = kTRUE;
636 AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
637 TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
638 TGridResult *gridres = gGrid->Command(addTag.Data());
639 const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
640 if(resCode[0] != '1') {
641 AliError(Form("Couldn't add %s tags to folder %s !",
642 tagname, folderToTag.Data()));
649 //_____________________________________________________________________________
650 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
651 // tag stored object in CDB table using object Id's parameters
653 TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
654 TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
658 TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
659 id->GetPathLevel(0).Data(),
660 id->GetPathLevel(1).Data(),
661 id->GetPathLevel(2).Data());
662 TString addTagValue = Form("%s%s%s",
665 addTagValue3.Data());
667 Bool_t result = kFALSE;
668 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
669 TGridResult* res = gGrid->Command(addTagValue.Data());
670 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
671 if(resCode[0] != '1') {
672 AliError(Form("Couldn't add CDB tag value to file %s !",
676 AliInfo("Object successfully tagged.");
684 //_____________________________________________________________________________
685 Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
686 // tag folder with ShortLived tag
688 TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
690 Bool_t result = kFALSE;
691 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
692 TGridResult* res = gGrid->Command(addTagValue.Data());
693 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
694 if(resCode[0] != '1') {
695 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
698 AliInfo("Object successfully tagged.");
706 //_____________________________________________________________________________
707 Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
708 // tag stored object in CDB table using object Id's parameters
710 TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
711 TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
712 md->GetObjectClassName(),
713 md->GetResponsible(),
714 md->GetBeamPeriod());
715 TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
716 md->GetAliRootVersion(),
718 TString addTagValue = Form("%s%s%s",
721 addTagValue3.Data());
723 Bool_t result = kFALSE;
724 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
725 TGridResult* res = gGrid->Command(addTagValue.Data());
726 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
727 if(resCode[0] != '1') {
728 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
732 AliInfo("Object successfully tagged.");
738 //_____________________________________________________________________________
739 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
741 TString turl(fileName);
742 turl.Prepend("/alien" + fDBFolder);
743 turl += "?se="; turl += fSE.Data();
744 TFile *file = TFile::Open(turl);
746 AliError(Form("Can't open selection file <%s>!", turl.Data()));
750 TList *list = new TList();
758 keycycle = "AliCDBId;";
761 id = (AliCDBId*) file->Get(keycycle);
765 file->Close(); delete file; file=0;
772 //_____________________________________________________________________________
773 Bool_t AliCDBGrid::Contains(const char* path) const{
774 // check for path in storage's DBFolder
776 TString initDir(gGrid->Pwd(0));
777 TString dirName(fDBFolder);
778 dirName += path; // dirName = fDBFolder/path
779 Bool_t result=kFALSE;
780 if (gGrid->Cd(dirName,0)) result=kTRUE;
781 gGrid->Cd(initDir.Data(),0);
785 //_____________________________________________________________________________
786 void AliCDBGrid::QueryValidFiles()
788 // Query the CDB for files valid for AliCDBStorage::fRun
789 // fills list fValidFileIds with AliCDBId objects created from file name
792 MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
794 TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data());
795 if(fVersion >= 0) pattern += Form("_v%d*", fVersion);
797 AliDebug(2,Form("pattern: %s", pattern.Data()));
799 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
801 AliCDBId validFileId;
802 for(int i=0; i<res->GetEntries(); i++){
803 TString filename = res->GetKey(i, "lfn");
804 if(filename == "") continue;
805 AliDebug(2,Form("Found valid file: %s", filename.Data()));
806 Bool_t result = FilenameToId(filename, validFileId);
808 fValidFileIds.AddLast(validFileId.Clone());
815 //_____________________________________________________________________________
816 void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
817 const AliCDBMetaData* md, TString& result) const
819 // create filter for file query
821 result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
823 // if(version >= 0) {
824 // result += Form(" and CDB:version=%d", version);
826 // if(pathFilter.GetLevel0() != "*") {
827 // result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
829 // if(pathFilter.GetLevel1() != "*") {
830 // result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
832 // if(pathFilter.GetLevel2() != "*") {
833 // result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
837 if(md->GetObjectClassName()[0] != '\0') {
838 result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
840 if(md->GetResponsible()[0] != '\0') {
841 result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
843 if(md->GetBeamPeriod() != 0) {
844 result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
846 if(md->GetAliRootVersion()[0] != '\0') {
847 result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
849 if(md->GetComment()[0] != '\0') {
850 result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
853 AliDebug(2, Form("filter: %s",result.Data()));
857 //_____________________________________________________________________________
858 Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
859 // get last version found in the database valid for run and path
861 AliCDBPath aCDBPath(path);
862 if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
863 AliError(Form("Invalid path in request: %s", path));
866 AliCDBId query(path, run, run, -1, -1);
867 AliCDBId* dataId = 0;
869 // look for file matching query requests (path, runRange, version)
870 if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
871 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
872 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
874 dataId = GetId(fValidFileIds, query);
875 if (!dataId) return -1;
876 Int_t version = dataId->GetVersion();
881 // List of files valid for reqested run was not loaded. Looking directly into CDB
882 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
885 TObjArray validFileIds;
886 validFileIds.SetOwner(1);
889 MakeQueryFilter(run, run, 0, filter);
891 TString pattern = Form("%s/Run*.root", path);
892 AliDebug(2,Form("pattern: %s", pattern.Data()));
894 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
895 AliCDBId validFileId;
896 for(int i=0; i<res->GetEntries(); i++){
897 TString filename = res->GetKey(i, "lfn");
898 if(filename == "") continue;
899 if(FilenameToId(filename, validFileId))
900 validFileIds.AddLast(validFileId.Clone());
904 dataId = GetId(validFileIds, query);
905 if (!dataId) return -1;
907 Int_t version = dataId->GetVersion();
913 //_____________________________________________________________________________
914 Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
915 // get last subversion found in the database valid for run and path
916 AliError("Objects in GRID storage have no sub version!");
921 /////////////////////////////////////////////////////////////////////////////////////////////////
923 // AliCDBGrid factory //
925 /////////////////////////////////////////////////////////////////////////////////////////////////
927 ClassImp(AliCDBGridFactory)
929 //_____________________________________________________________________________
930 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
931 // check if the string is valid Grid URI
933 // pattern: alien://hostName:Port;user;dbPath;SE
934 // example of a valid pattern:
935 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
936 // TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
937 TRegexp gridPattern("^alien://.+$");
939 return TString(gridString).Contains(gridPattern);
942 //_____________________________________________________________________________
943 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
944 // create AliCDBGridParam class from the URI string
946 if (!Validate(gridString)) {
949 //TString buffer(gridString + sizeof("alien://") - 1);
950 TString buffer(gridString);
952 TString gridUrl = "alien://";
954 TString dbFolder = "";
955 TString se = "default";
957 TObjArray *arr = buffer.Tokenize('?');
961 while((str = (TObjString*) iter.Next())){
962 TString entry(str->String());
963 Int_t indeq = entry.Index('=');
965 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
969 AliError(Form("Invalid entry! %s",entry.Data()));
974 TString key = entry(0,indeq);
975 TString value = entry(indeq+1,entry.Length()-indeq);
977 if(key.Contains("grid",TString::kIgnoreCase)) {
980 else if (key.Contains("user",TString::kIgnoreCase)){
983 else if (key.Contains("folder",TString::kIgnoreCase)){
986 else if (key.Contains("se",TString::kIgnoreCase)){
990 AliError(Form("Invalid entry! %s",entry.Data()));
995 AliDebug(2, Form("gridUrl: %s",gridUrl.Data()));
996 AliDebug(2, Form("user: %s",user.Data()));
997 AliDebug(2, Form("dbFolder: %s",dbFolder.Data()));
998 AliDebug(2, Form("s.e.: %s",se.Data()));
1001 AliError("DBFolder must be specified!");
1005 return new AliCDBGridParam(gridUrl.Data(), user.Data(), dbFolder.Data(), se.Data());
1008 //_____________________________________________________________________________
1009 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1010 // create AliCDBGrid storage instance from parameters
1012 AliCDBGrid *grid = 0;
1013 if (AliCDBGridParam::Class() == param->IsA()) {
1015 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1016 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1017 gridParam->GetUser().Data(),
1018 gridParam->GetDBFolder().Data(),
1019 gridParam->GetSE().Data());
1023 if(!gGrid && grid) {
1024 delete grid; grid=0;
1030 /////////////////////////////////////////////////////////////////////////////////////////////////
1032 // AliCDBGrid Parameter class // //
1034 /////////////////////////////////////////////////////////////////////////////////////////////////
1036 ClassImp(AliCDBGridParam)
1038 //_____________________________________________________________________________
1039 AliCDBGridParam::AliCDBGridParam():
1046 // default constructor
1050 //_____________________________________________________________________________
1051 AliCDBGridParam::AliCDBGridParam(const char* gridUrl,
1053 const char* dbFolder,
1058 fDBFolder(dbFolder),
1065 TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s",
1066 fGridUrl.Data(), fUser.Data(),
1067 fDBFolder.Data(), fSE.Data());
1072 //_____________________________________________________________________________
1073 AliCDBGridParam::~AliCDBGridParam() {
1078 //_____________________________________________________________________________
1079 AliCDBParam* AliCDBGridParam::CloneParam() const {
1082 return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1083 fDBFolder.Data(), fSE.Data());
1086 //_____________________________________________________________________________
1087 ULong_t AliCDBGridParam::Hash() const {
1088 // return Hash function
1090 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
1093 //_____________________________________________________________________________
1094 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1095 // check if this object is equal to AliCDBParam obj
1101 if (AliCDBGridParam::Class() != obj->IsA()) {
1105 AliCDBGridParam* other = (AliCDBGridParam*) obj;
1107 if(fGridUrl != other->fGridUrl) return kFALSE;
1108 if(fUser != other->fUser) return kFALSE;
1109 if(fDBFolder != other->fDBFolder) return kFALSE;
1110 if(fSE != other->fSE) return kFALSE;