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 != gGrid->GetUser()) {
54 // connection to the Grid
55 TGrid::Connect(fGridUrl.Data(),fUser.Data());
59 AliError("Connection failed!");
63 TString initDir(gGrid->Pwd(0));
64 if (fDBFolder[0] != '/') {
65 fDBFolder.Prepend(initDir);
68 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
69 if(!gGrid->Cd(fDBFolder.Data(),0)){
70 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
71 if(!gGrid->Mkdir(fDBFolder.Data(),"",0)){
72 AliError(Form("Cannot create folder <%s> !",fDBFolder.Data()));
75 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
78 // removes any '/' at the end of path, then append one '/'
79 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
82 // return to the initial directory
83 gGrid->Cd(initDir.Data(),0);
86 //_____________________________________________________________________________
87 AliCDBGrid::~AliCDBGrid()
93 //_____________________________________________________________________________
94 Bool_t AliCDBGrid::FilenameToId(const char* filename, AliCDBRunRange& runRange,
96 // build AliCDBId from filename numbers
100 // valid filename: Run#firstRun_#lastRun_v#version.root
101 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
102 keyPattern.Index(filename, &mSize);
104 AliDebug(2,Form("Bad filename <%s>.", filename));
108 TString idString(filename);
109 idString.Resize(idString.Length() - sizeof(".root") + 1);
111 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
113 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
114 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
115 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
117 TString verString(((TObjString*) strArray->At(2))->GetString());
118 gridVersion = atoi(verString.Data() + 1);
125 //_____________________________________________________________________________
126 Bool_t AliCDBGrid::IdToFilename(const AliCDBRunRange& runRange, Int_t gridVersion,
128 // build file name from AliCDBId data (run range, version)
130 if (!runRange.IsValid()) {
131 AliDebug(2,Form("Invalid run range <%d, %d>.",
132 runRange.GetFirstRun(), runRange.GetLastRun()));
136 if (gridVersion < 0) {
137 AliDebug(2,Form("Invalid version <%d>.", gridVersion));
142 filename += runRange.GetFirstRun();
144 filename += runRange.GetLastRun();
146 filename += gridVersion;
152 //_____________________________________________________________________________
153 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
154 // prepare id (version) of the object that will be stored (called by PutEntry)
156 TString initDir(gGrid->Pwd(0));
157 TString pathName= id.GetPath();
159 TString dirName(fDBFolder);
161 Bool_t dirExist=kFALSE;
163 // go to the path; if directory does not exist, create it
164 TObjArray *arrName=pathName.Tokenize("/");
165 for(int i=0;i<arrName->GetEntries();i++){
166 TString buffer((arrName->At(i))->GetName());
167 dirName+=buffer; dirName+="/";
168 dirExist=gGrid->Cd(dirName,0);
170 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
171 if(!gGrid->Mkdir(dirName,"",0)){
172 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
173 gGrid->Cd(initDir.Data());
179 gGrid->Cd(initDir,0);
181 const char* filename;
182 AliCDBRunRange aRunRange; // the runRange got from filename
183 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
184 Int_t aVersion; // the version got from filename
185 Int_t lastVersion=0; // highest version found
187 TGridResult *res = gGrid->Ls(dirName);
189 //loop on the files in the directory, look for highest version
190 for(int i=0; i < res->GetEntries(); i++){
191 filename=res->GetFileName(i);
192 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
193 if (aRunRange.Overlaps(id.GetAliCDBRunRange()) && aVersion > lastVersion) {
194 lastVersion = aVersion;
195 lastRunRange = aRunRange;
201 id.SetVersion(lastVersion + 1);
203 TString lastStorage = id.GetLastStorage();
204 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
205 AliWarning(Form("A NEW object is being stored with version %d",
207 AliWarning(Form("and it will hide previously stored object with version %d!",
211 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
212 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
213 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
218 //_____________________________________________________________________________
219 Bool_t AliCDBGrid::GetId(const AliCDBId& query, AliCDBId& result) {
220 // look for filename matching query (called by GetEntry)
222 TString initDir(gGrid->Pwd(0));
224 TString dirName(fDBFolder);
225 dirName += query.GetPath(); // dirName = fDBFolder/idPath
227 if (!gGrid->Cd(dirName,0)) {
228 AliError(Form("Directory <%s> not found", (query.GetPath()).Data()));
229 AliError(Form("in DB folder %s", fDBFolder.Data()));
233 TGridResult *res = gGrid->Ls(dirName);
235 const char* filename;
236 AliCDBRunRange aRunRange; // the runRange got from filename
237 Int_t aVersion; // the version got from filename
239 for(int i=0; i < res->GetEntries(); i++){
240 filename=res->GetFileName(i);
241 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
242 // aRunRange and aVersion filled from filename
244 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
245 // aRunRange contains requested run!
247 if (!query.HasVersion()){ // look for highest version
248 if(result.GetVersion() > aVersion) continue;
249 if(result.GetVersion() == aVersion) {
250 AliError(Form("More than one object valid for run %d, version %d!",
251 query.GetFirstRun(), aVersion));
254 result.SetVersion(aVersion);
255 result.SetFirstRun(aRunRange.GetFirstRun());
256 result.SetLastRun(aRunRange.GetLastRun());
258 } else { // look for specified version
259 if(query.GetVersion() != aVersion) continue;
260 if(result.GetVersion() == aVersion){
261 AliError(Form("More than one object valid for run %d, version %d!",
262 query.GetFirstRun(), aVersion));
265 result.SetVersion(aVersion);
266 result.SetFirstRun(aRunRange.GetFirstRun());
267 result.SetLastRun(aRunRange.GetLastRun());
269 } // end loop on filenames
272 gGrid->Cd(initDir.Data(),0);
277 //_____________________________________________________________________________
278 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
279 // get AliCDBEntry from the database
281 AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
284 // look for a filename matching query requests (path, runRange, version, subVersion)
285 if (!queryId.HasVersion()) {
286 // if version is not specified, first check the selection criteria list
287 AliCDBId selectedId(queryId);
288 GetSelection(&selectedId);
289 result = GetId(selectedId,dataId);
291 result = GetId(queryId,dataId);
294 if (!result || !dataId.IsSpecified()) return NULL;
297 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
298 AliError("Bad data ID encountered! Subnormal error!");
302 filename.Prepend("/alien" + fDBFolder + queryId.GetPath() + '/');
303 filename += "?se="; filename += fSE.Data();
305 AliInfo(Form("Opening file: %s",filename.Data()));
306 TFile *file = TFile::Open(filename);
308 AliError(Form("Can't open file <%s>!", filename.Data()));
312 // get the only AliCDBEntry object from the file
313 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
315 TObject* anObject = file->Get("AliCDBEntry");
318 AliError("Bad storage data: NULL entry object!");
322 if (AliCDBEntry::Class() != anObject->IsA()) {
323 AliError("Bad storage data: Invalid entry object!");
327 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
329 // The object's Id is not reset during storage
330 // If object's Id runRange or version do not match with filename,
331 // it means that someone renamed file by hand. In this case a warning msg is issued.
333 ((AliCDBEntry*) anObject)->SetLastStorage("grid");
335 if(!((entryId.GetAliCDBRunRange()).IsEqual(&dataId.GetAliCDBRunRange())) ||
336 entryId.GetVersion() != dataId.GetVersion()){
337 AliWarning(Form("Either RunRange or gridVersion in the object's metadata do noth match with fileName numbers:"));
338 AliWarning(Form("someone renamed file by hand!"));
341 // close file, return retieved entry
342 file->Close(); delete file; file=0;
343 return (AliCDBEntry*) anObject;
346 //_____________________________________________________________________________
347 void AliCDBGrid::GetEntriesForLevel0(const char* level0,
348 const AliCDBId& queryId, TList* result) {
349 // multiple request (AliCDBStorage::GetAll)
351 TString level0Dir=fDBFolder;
354 if (!gGrid->Cd(level0Dir,0)) {
355 AliError(Form("Level0 directory <%s> not found", level0Dir.Data()));
359 TGridResult *res = gGrid->Ls(level0Dir);
361 for(int i=0; i < res->GetEntries(); i++){
362 level1=res->GetFileName(i);
363 if (queryId.GetAliCDBPath().Level1Comprises(level1))
364 GetEntriesForLevel1(level0, level1, queryId, result);
369 //_____________________________________________________________________________
370 void AliCDBGrid::GetEntriesForLevel1(const char* level0, const char* level1,
371 const AliCDBId& queryId, TList* result) {
372 // multiple request (AliCDBStorage::GetAll)
374 TString level1Dir=fDBFolder;
379 if (!gGrid->Cd(level1Dir,0)) {
380 AliError(Form("Level1 directory <%s> not found", level1Dir.Data()));
384 TGridResult *res = gGrid->Ls(level1Dir);
386 for(int i=0; i < res->GetEntries(); i++){
387 level2=res->GetFileName(i);
388 if (queryId.GetAliCDBPath().Level2Comprises(level2)){
389 AliCDBPath entryPath(level0, level1, level2);
390 AliCDBId entryId(entryPath,
391 queryId.GetAliCDBRunRange(),
392 queryId.GetVersion(),
393 queryId.GetSubVersion());
395 AliCDBEntry* anEntry = GetEntry(entryId);
396 if (anEntry) result->Add(anEntry);
403 //_____________________________________________________________________________
404 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
405 // multiple request (AliCDBStorage::GetAll)
407 TList* result = new TList();
410 TString initDir(gGrid->Pwd(0));
412 TGridResult *res = gGrid->Ls(fDBFolder);
415 for(int i=0; i < res->GetEntries(); i++){
416 level0=res->GetFileName(i);
417 if (queryId.GetAliCDBPath().Level0Comprises(level0))
418 GetEntriesForLevel0(level0, queryId, result);
422 gGrid->Cd(initDir.Data(),0);
426 //_____________________________________________________________________________
427 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
428 // put an AliCDBEntry object into the database
430 AliCDBId& id = entry->GetId();
432 // set version for the entry to be stored
433 if (!PrepareId(id)) return kFALSE;
435 // build filename from entry's id
437 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(), filename)) {
438 AliError("Bad ID encountered! Subnormal error!");
442 filename.Prepend("/alien" + fDBFolder + id.GetPath() + '/');
443 TString filenameCopy(filename);
444 filename += "?se="; filename += fSE.Data();
446 TDirectory* saveDir = gDirectory;
449 TFile *file = TFile::Open(filename,"CREATE");
450 if(!file || !file->IsWritable()){
451 AliError(Form("Can't open file <%s>!", filename.Data()));
452 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
458 entry->SetVersion(id.GetVersion());
460 // write object (key name: "AliCDBEntry")
461 Bool_t result = (entry->Write("AliCDBEntry") != 0);
462 if (!result) AliError(Form("Can't write entry to file <%s>!",filename.Data()));
465 if (saveDir) saveDir->cd(); else gROOT->cd();
466 file->Close(); delete file; file=0;
468 AliInfo(Form("CDB object stored into file %s",filenameCopy.Data()));
469 AliInfo(Form("using S.E. %s", fSE.Data()));
475 //_____________________________________________________________________________
476 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
478 TString turl(fileName);
479 turl.Prepend("/alien" + fDBFolder);
480 turl += "?se="; turl += fSE.Data();
481 TFile *file = TFile::Open(turl);
483 AliError(Form("Can't open selection file <%s>!", turl.Data()));
487 TList *list = new TList();
495 keycycle = "AliCDBId;";
498 id = (AliCDBId*) file->Get(keycycle);
502 file->Close(); delete file; file=0;
509 //_____________________________________________________________________________
510 Bool_t AliCDBGrid::Contains(const char* path) const{
511 // check for path in storage's DBFolder
513 TString initDir(gGrid->Pwd(0));
514 TString dirName(fDBFolder);
515 dirName += path; // dirName = fDBFolder/path
516 Bool_t result=kFALSE;
517 if (gGrid->Cd(dirName,0)) result=kTRUE;
518 gGrid->Cd(initDir.Data(),0);
522 /////////////////////////////////////////////////////////////////////////////////////////////////
524 // AliCDBGrid factory //
526 /////////////////////////////////////////////////////////////////////////////////////////////////
528 ClassImp(AliCDBGridFactory)
530 //_____________________________________________________________________________
531 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
532 // check if the string is valid Grid URI
534 // pattern: alien://hostName:Port;user;dbPath;SE
535 // example of a valid pattern:
536 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
537 // TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
538 TRegexp gridPattern("^alien://.+$");
540 return TString(gridString).Contains(gridPattern);
543 //_____________________________________________________________________________
544 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
545 // create AliCDBGridParam class from the URI string
547 if (!Validate(gridString)) {
550 //TString buffer(gridString + sizeof("alien://") - 1);
551 TString buffer(gridString);
553 TString gridUrl = "alien://";
555 TString dbFolder = "DBGrid";
556 TString se = "ALICE::CERN::se01";
558 TObjArray *arr = buffer.Tokenize('?');
562 while((str = (TObjString*) iter.Next())){
563 TString entry(str->String());
564 Int_t indeq = entry.Index('=');
566 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
570 AliError(Form("Invalid entry! %s",entry.Data()));
575 TString key = entry(0,indeq);
576 TString value = entry(indeq+1,entry.Length()-indeq);
578 if(key.Contains("grid",TString::kIgnoreCase)) {
581 else if (key.Contains("user",TString::kIgnoreCase)){
584 else if (key.Contains("folder",TString::kIgnoreCase)){
587 else if (key.Contains("se",TString::kIgnoreCase)){
591 AliError(Form("Invalid entry! %s",entry.Data()));
596 AliInfo(Form("gridUrl: %s",gridUrl.Data()));
597 AliInfo(Form("user: %s",user.Data()));
598 AliInfo(Form("dbFolder: %s",dbFolder.Data()));
599 AliInfo(Form("s.e.: %s",se.Data()));
601 return new AliCDBGridParam(gridUrl, user, dbFolder, se);
604 //_____________________________________________________________________________
605 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
606 // create AliCDBGrid storage instance from parameters
608 if (AliCDBGridParam::Class() == param->IsA()) {
610 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
611 AliCDBGrid *grid = new AliCDBGrid(gridParam->GridUrl(),
612 gridParam->GetUser(), gridParam->GetDBFolder(),
615 if(gGrid) return grid;
621 /////////////////////////////////////////////////////////////////////////////////////////////////
623 // AliCDBGrid Parameter class // //
625 /////////////////////////////////////////////////////////////////////////////////////////////////
627 ClassImp(AliCDBGridParam)
629 //_____________________________________________________________________________
630 AliCDBGridParam::AliCDBGridParam() {
631 // default constructor
635 //_____________________________________________________________________________
636 AliCDBGridParam::AliCDBGridParam(const char* gridUrl,
638 const char* dbFolder,
650 uri+=fGridUrl; uri+="?";
651 uri+="User="; uri+=fUser; uri+="?";
652 uri+="DBFolder="; uri+=fDBFolder; uri+="?";
653 uri+="SE="; uri+=fSE;
658 //_____________________________________________________________________________
659 AliCDBGridParam::~AliCDBGridParam() {
664 //_____________________________________________________________________________
665 AliCDBParam* AliCDBGridParam::CloneParam() const {
668 return new AliCDBGridParam(fGridUrl, fUser, fDBFolder, fSE);
671 //_____________________________________________________________________________
672 ULong_t AliCDBGridParam::Hash() const {
673 // return Hash function
675 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
678 //_____________________________________________________________________________
679 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
680 // check if this object is equal to AliCDBParam obj
686 if (AliCDBGridParam::Class() != obj->IsA()) {
690 AliCDBGridParam* other = (AliCDBGridParam*) obj;
692 if(fGridUrl != other->fGridUrl) return kFALSE;
693 if(fUser != other->fUser) return kFALSE;
694 if(fDBFolder != other->fDBFolder) return kFALSE;
695 if(fSE != other->fSE) return kFALSE;