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"
41 //_____________________________________________________________________________
42 AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder, const char *se) :
51 // if the same Grid is alreay active, skip connection
52 if (!gGrid || fGridUrl != gGrid->GridUrl()
53 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
54 // connection to the Grid
55 AliInfo("Connection to the Grid!!!!");
57 AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
58 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
60 TGrid::Connect(fGridUrl.Data(),fUser.Data());
64 AliError("Connection failed!");
68 TString initDir(gGrid->Pwd(0));
69 if (fDBFolder[0] != '/') {
70 fDBFolder.Prepend(initDir);
73 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
74 if(!gGrid->Cd(fDBFolder.Data(),0)){
75 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
76 if(!gGrid->Mkdir(fDBFolder.Data(),"",0)){
77 AliError(Form("Cannot create folder <%s> !",fDBFolder.Data()));
80 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
83 // removes any '/' at the end of path, then append one '/'
84 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
87 // return to the initial directory
88 gGrid->Cd(initDir.Data(),0);
91 //_____________________________________________________________________________
92 AliCDBGrid::~AliCDBGrid()
98 //_____________________________________________________________________________
99 Bool_t AliCDBGrid::FilenameToId(const char* filename, AliCDBRunRange& runRange,
100 Int_t& gridVersion) {
101 // build AliCDBId from filename numbers
105 // valid filename: Run#firstRun_#lastRun_v#version.root
106 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
107 keyPattern.Index(filename, &mSize);
109 AliDebug(2,Form("Bad filename <%s>.", filename));
113 TString idString(filename);
114 idString.Resize(idString.Length() - sizeof(".root") + 1);
116 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
118 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
119 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
120 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
122 TString verString(((TObjString*) strArray->At(2))->GetString());
123 gridVersion = atoi(verString.Data() + 1);
130 //_____________________________________________________________________________
131 Bool_t AliCDBGrid::IdToFilename(const AliCDBRunRange& runRange, Int_t gridVersion,
133 // build file name from AliCDBId data (run range, version)
135 if (!runRange.IsValid()) {
136 AliDebug(2,Form("Invalid run range <%d, %d>.",
137 runRange.GetFirstRun(), runRange.GetLastRun()));
141 if (gridVersion < 0) {
142 AliDebug(2,Form("Invalid version <%d>.", gridVersion));
147 filename += runRange.GetFirstRun();
149 filename += runRange.GetLastRun();
151 filename += gridVersion;
157 //_____________________________________________________________________________
158 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
159 // prepare id (version) of the object that will be stored (called by PutEntry)
161 TString initDir(gGrid->Pwd(0));
162 TString pathName= id.GetPath();
164 TString dirName(fDBFolder);
166 Bool_t dirExist=kFALSE;
168 // go to the path; if directory does not exist, create it
169 TObjArray *arrName=pathName.Tokenize("/");
170 for(int i=0;i<arrName->GetEntries();i++){
171 TString buffer((arrName->At(i))->GetName());
172 dirName+=buffer; dirName+="/";
173 dirExist=gGrid->Cd(dirName,0);
175 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
176 if(!gGrid->Mkdir(dirName,"",0)){
177 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
178 gGrid->Cd(initDir.Data());
184 gGrid->Cd(initDir,0);
186 const char* filename;
187 AliCDBRunRange aRunRange; // the runRange got from filename
188 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
189 Int_t aVersion; // the version got from filename
190 Int_t lastVersion=0; // highest version found
192 TGridResult *res = gGrid->Ls(dirName);
194 //loop on the files in the directory, look for highest version
195 for(int i=0; i < res->GetEntries(); i++){
196 filename=res->GetFileName(i);
197 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
198 if (aRunRange.Overlaps(id.GetAliCDBRunRange()) && aVersion > lastVersion) {
199 lastVersion = aVersion;
200 lastRunRange = aRunRange;
206 id.SetVersion(lastVersion + 1);
208 TString lastStorage = id.GetLastStorage();
209 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
210 AliWarning(Form("A NEW object is being stored with version %d",
212 AliWarning(Form("and it will hide previously stored object with version %d!",
216 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
217 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
218 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
223 //_____________________________________________________________________________
224 Bool_t AliCDBGrid::GetId(const AliCDBId& query, AliCDBId& result) {
225 // look for filename matching query (called by GetEntry)
227 TString initDir(gGrid->Pwd(0));
229 TString dirName(fDBFolder);
230 dirName += query.GetPath(); // dirName = fDBFolder/idPath
232 if (!gGrid->Cd(dirName,0)) {
233 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
234 AliDebug(2,Form("in DB folder %s", fDBFolder.Data()));
238 TGridResult *res = gGrid->Ls(dirName);
240 const char* filename;
241 AliCDBRunRange aRunRange; // the runRange got from filename
242 Int_t aVersion; // the version got from filename
244 for(int i=0; i < res->GetEntries(); i++){
245 filename=res->GetFileName(i);
246 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
247 // aRunRange and aVersion filled from filename
249 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
250 // aRunRange contains requested run!
252 if (!query.HasVersion()){ // look for highest version
253 if(result.GetVersion() > aVersion) continue;
254 if(result.GetVersion() == aVersion) {
255 AliDebug(2,Form("More than one object valid for run %d, version %d!",
256 query.GetFirstRun(), aVersion));
259 result.SetVersion(aVersion);
260 result.SetFirstRun(aRunRange.GetFirstRun());
261 result.SetLastRun(aRunRange.GetLastRun());
263 } else { // look for specified version
264 if(query.GetVersion() != aVersion) continue;
265 if(result.GetVersion() == aVersion){
266 AliDebug(2,Form("More than one object valid for run %d, version %d!",
267 query.GetFirstRun(), aVersion));
270 result.SetVersion(aVersion);
271 result.SetFirstRun(aRunRange.GetFirstRun());
272 result.SetLastRun(aRunRange.GetLastRun());
274 } // end loop on filenames
277 gGrid->Cd(initDir.Data(),0);
282 //_____________________________________________________________________________
283 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
284 // get AliCDBEntry from the database
286 AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
289 // look for a filename matching query requests (path, runRange, version, subVersion)
290 if (!queryId.HasVersion()) {
291 // if version is not specified, first check the selection criteria list
292 AliCDBId selectedId(queryId);
293 GetSelection(&selectedId);
294 result = GetId(selectedId,dataId);
296 result = GetId(queryId,dataId);
299 if (!result || !dataId.IsSpecified()) return NULL;
302 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
303 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
307 filename.Prepend("/alien" + fDBFolder + queryId.GetPath() + '/');
308 filename += "?se="; filename += fSE.Data();
310 AliDebug(2,Form("Opening file: %s",filename.Data()));
311 TFile *file = TFile::Open(filename);
313 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
317 // get the only AliCDBEntry object from the file
318 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
320 TObject* anObject = file->Get("AliCDBEntry");
323 AliDebug(2,Form("Bad storage data: NULL entry object!"));
327 if (AliCDBEntry::Class() != anObject->IsA()) {
328 AliDebug(2,Form("Bad storage data: Invalid entry object!"));
332 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
334 // The object's Id is not reset during storage
335 // If object's Id runRange or version do not match with filename,
336 // it means that someone renamed file by hand. In this case a warning msg is issued.
338 ((AliCDBEntry*) anObject)->SetLastStorage("grid");
340 if(!((entryId.GetAliCDBRunRange()).IsEqual(&dataId.GetAliCDBRunRange())) ||
341 entryId.GetVersion() != dataId.GetVersion()){
342 AliWarning(Form("Either RunRange or gridVersion in the object's metadata do noth match with fileName numbers:"));
343 AliWarning(Form("someone renamed file by hand!"));
346 // close file, return retieved entry
347 file->Close(); delete file; file=0;
348 return (AliCDBEntry*) anObject;
351 //_____________________________________________________________________________
352 void AliCDBGrid::GetEntriesForLevel0(const char* level0,
353 const AliCDBId& queryId, TList* result) {
354 // multiple request (AliCDBStorage::GetAll)
356 TString level0Dir=fDBFolder;
359 if (!gGrid->Cd(level0Dir,0)) {
360 AliDebug(2,Form("Level0 directory <%s> not found", level0Dir.Data()));
364 TGridResult *res = gGrid->Ls(level0Dir);
366 for(int i=0; i < res->GetEntries(); i++){
367 level1=res->GetFileName(i);
368 if (queryId.GetAliCDBPath().Level1Comprises(level1))
369 GetEntriesForLevel1(level0, level1, queryId, result);
374 //_____________________________________________________________________________
375 void AliCDBGrid::GetEntriesForLevel1(const char* level0, const char* level1,
376 const AliCDBId& queryId, TList* result) {
377 // multiple request (AliCDBStorage::GetAll)
379 TString level1Dir=fDBFolder;
384 if (!gGrid->Cd(level1Dir,0)) {
385 AliDebug(2,Form("Level1 directory <%s> not found", level1Dir.Data()));
389 TGridResult *res = gGrid->Ls(level1Dir);
391 for(int i=0; i < res->GetEntries(); i++){
392 level2=res->GetFileName(i);
393 if (queryId.GetAliCDBPath().Level2Comprises(level2)){
394 AliCDBPath entryPath(level0, level1, level2);
395 AliCDBId entryId(entryPath,
396 queryId.GetAliCDBRunRange(),
397 queryId.GetVersion(),
398 queryId.GetSubVersion());
400 AliCDBEntry* anEntry = GetEntry(entryId);
401 if (anEntry) result->Add(anEntry);
408 //_____________________________________________________________________________
409 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
410 // multiple request (AliCDBStorage::GetAll)
412 TList* result = new TList();
415 TString initDir(gGrid->Pwd(0));
417 TGridResult *res = gGrid->Ls(fDBFolder);
420 for(int i=0; i < res->GetEntries(); i++){
421 level0=res->GetFileName(i);
422 if (queryId.GetAliCDBPath().Level0Comprises(level0))
423 GetEntriesForLevel0(level0, queryId, result);
427 gGrid->Cd(initDir.Data(),0);
431 //_____________________________________________________________________________
432 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
433 // put an AliCDBEntry object into the database
435 AliCDBId& id = entry->GetId();
437 // set version for the entry to be stored
438 if (!PrepareId(id)) return kFALSE;
440 // build filename from entry's id
442 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(), filename)) {
443 AliError("Bad ID encountered! Subnormal error!");
447 filename.Prepend("/alien" + fDBFolder + id.GetPath() + '/');
448 TString filenameCopy(filename);
449 filename += "?se="; filename += fSE.Data();
451 TDirectory* saveDir = gDirectory;
454 TFile *file = TFile::Open(filename,"CREATE");
455 if(!file || !file->IsWritable()){
456 AliError(Form("Can't open file <%s>!", filename.Data()));
457 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
463 entry->SetVersion(id.GetVersion());
465 // write object (key name: "AliCDBEntry")
466 Bool_t result = (entry->Write("AliCDBEntry") != 0);
467 if (!result) AliError(Form("Can't write entry to file <%s>!",filename.Data()));
470 if (saveDir) saveDir->cd(); else gROOT->cd();
471 file->Close(); delete file; file=0;
473 AliInfo(Form("CDB object stored into file %s",filenameCopy.Data()));
474 AliInfo(Form("using S.E. %s", fSE.Data()));
480 //_____________________________________________________________________________
481 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
483 TString turl(fileName);
484 turl.Prepend("/alien" + fDBFolder);
485 turl += "?se="; turl += fSE.Data();
486 TFile *file = TFile::Open(turl);
488 AliError(Form("Can't open selection file <%s>!", turl.Data()));
492 TList *list = new TList();
500 keycycle = "AliCDBId;";
503 id = (AliCDBId*) file->Get(keycycle);
507 file->Close(); delete file; file=0;
514 //_____________________________________________________________________________
515 Bool_t AliCDBGrid::Contains(const char* path) const{
516 // check for path in storage's DBFolder
518 TString initDir(gGrid->Pwd(0));
519 TString dirName(fDBFolder);
520 dirName += path; // dirName = fDBFolder/path
521 Bool_t result=kFALSE;
522 if (gGrid->Cd(dirName,0)) result=kTRUE;
523 gGrid->Cd(initDir.Data(),0);
527 /////////////////////////////////////////////////////////////////////////////////////////////////
529 // AliCDBGrid factory //
531 /////////////////////////////////////////////////////////////////////////////////////////////////
533 ClassImp(AliCDBGridFactory)
535 //_____________________________________________________________________________
536 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
537 // check if the string is valid Grid URI
539 // pattern: alien://hostName:Port;user;dbPath;SE
540 // example of a valid pattern:
541 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
542 // TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
543 TRegexp gridPattern("^alien://.+$");
545 return TString(gridString).Contains(gridPattern);
548 //_____________________________________________________________________________
549 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
550 // create AliCDBGridParam class from the URI string
552 if (!Validate(gridString)) {
555 //TString buffer(gridString + sizeof("alien://") - 1);
556 TString buffer(gridString);
558 TString gridUrl = "alien://";
560 TString dbFolder = "DBGrid";
561 TString se = "ALICE::CERN::se01";
563 TObjArray *arr = buffer.Tokenize('?');
567 while((str = (TObjString*) iter.Next())){
568 TString entry(str->String());
569 Int_t indeq = entry.Index('=');
571 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
575 AliError(Form("Invalid entry! %s",entry.Data()));
580 TString key = entry(0,indeq);
581 TString value = entry(indeq+1,entry.Length()-indeq);
583 if(key.Contains("grid",TString::kIgnoreCase)) {
586 else if (key.Contains("user",TString::kIgnoreCase)){
589 else if (key.Contains("folder",TString::kIgnoreCase)){
592 else if (key.Contains("se",TString::kIgnoreCase)){
596 AliError(Form("Invalid entry! %s",entry.Data()));
601 AliInfo(Form("gridUrl: %s",gridUrl.Data()));
602 AliInfo(Form("user: %s",user.Data()));
603 AliInfo(Form("dbFolder: %s",dbFolder.Data()));
604 AliInfo(Form("s.e.: %s",se.Data()));
606 return new AliCDBGridParam(gridUrl, user, dbFolder, se);
609 //_____________________________________________________________________________
610 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
611 // create AliCDBGrid storage instance from parameters
613 if (AliCDBGridParam::Class() == param->IsA()) {
615 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
616 AliCDBGrid *grid = new AliCDBGrid(gridParam->GridUrl(),
617 gridParam->GetUser(), gridParam->GetDBFolder(),
620 if(gGrid) return grid;
626 /////////////////////////////////////////////////////////////////////////////////////////////////
628 // AliCDBGrid Parameter class // //
630 /////////////////////////////////////////////////////////////////////////////////////////////////
632 ClassImp(AliCDBGridParam)
634 //_____________________________________________________________________________
635 AliCDBGridParam::AliCDBGridParam() {
636 // default constructor
640 //_____________________________________________________________________________
641 AliCDBGridParam::AliCDBGridParam(const char* gridUrl,
643 const char* dbFolder,
655 uri+=fGridUrl; uri+="?";
656 uri+="User="; uri+=fUser; uri+="?";
657 uri+="DBFolder="; uri+=fDBFolder; uri+="?";
658 uri+="SE="; uri+=fSE;
663 //_____________________________________________________________________________
664 AliCDBGridParam::~AliCDBGridParam() {
669 //_____________________________________________________________________________
670 AliCDBParam* AliCDBGridParam::CloneParam() const {
673 return new AliCDBGridParam(fGridUrl, fUser, fDBFolder, fSE);
676 //_____________________________________________________________________________
677 ULong_t AliCDBGridParam::Hash() const {
678 // return Hash function
680 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
683 //_____________________________________________________________________________
684 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
685 // check if this object is equal to AliCDBParam obj
691 if (AliCDBGridParam::Class() != obj->IsA()) {
695 AliCDBGridParam* other = (AliCDBGridParam*) obj;
697 if(fGridUrl != other->fGridUrl) return kFALSE;
698 if(fUser != other->fUser) return kFALSE;
699 if(fDBFolder != other->fDBFolder) return kFALSE;
700 if(fSE != other->fSE) return kFALSE;