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 AliCDBId AliCDBGrid::GetId(const AliCDBId& query) {
220 // look for filename matching query (called by GetEntry)
222 TString initDir(gGrid->Pwd(0));
224 AliCDBId result(query.GetAliCDBPath(), -1, -1, -1, -1);
226 TString dirName(fDBFolder);
227 dirName += query.GetPath(); // dirName = fDBFolder/idPath
229 if (!gGrid->Cd(dirName,0)) {
230 AliError(Form("Directory <%s> not found", (query.GetPath()).Data()));
231 AliError(Form("in DB folder %s", fDBFolder.Data()));
235 TGridResult *res = gGrid->Ls(dirName);
237 const char* filename;
238 AliCDBRunRange aRunRange; // the runRange got from filename
239 Int_t aVersion; // the version got from filename
241 for(int i=0; i < res->GetEntries(); i++){
242 filename=res->GetFileName(i);
243 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
244 // aRunRange and aVersion filled from filename
246 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
247 // aRunRange contains requested run!
249 if (!query.HasVersion()){ // look for highest version
250 if(result.GetVersion() > aVersion) continue;
251 if(result.GetVersion() == aVersion) {
252 AliError(Form("More than one object valid for run %d, version %d!",
253 query.GetFirstRun(), aVersion));
254 result.SetRunRange(-1,-1); result.SetVersion(-1);
257 result.SetVersion(aVersion);
258 result.SetFirstRun(aRunRange.GetFirstRun());
259 result.SetLastRun(aRunRange.GetLastRun());
261 } else { // look for specified version
262 if(query.GetVersion() != aVersion) continue;
263 if(result.GetVersion() == aVersion){
264 AliError(Form("More than one object valid for run %d, version %d!",
265 query.GetFirstRun(), aVersion));
266 result.SetRunRange(-1,-1); result.SetVersion(-1);
269 result.SetVersion(aVersion);
270 result.SetFirstRun(aRunRange.GetFirstRun());
271 result.SetLastRun(aRunRange.GetLastRun());
273 } // end loop on filenames
276 gGrid->Cd(initDir.Data(),0);
281 //_____________________________________________________________________________
282 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
283 // get AliCDBEntry from the database
287 // look for a filename matching query requests (path, runRange, version, subVersion)
288 if (!queryId.HasVersion()) {
289 // if version is not specified, first check the selection criteria list
290 dataId = GetId(GetSelection(queryId));
292 dataId = GetId(queryId);
295 if (!dataId.IsSpecified()) return NULL;
298 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
299 AliError("Bad data ID encountered! Subnormal error!");
303 filename.Prepend("/alien" + fDBFolder + queryId.GetPath() + '/');
304 filename += "?se="; filename += fSE.Data();
306 AliInfo(Form("Opening file: %s",filename.Data()));
307 TFile *file = TFile::Open(filename);
309 AliError(Form("Can't open file <%s>!", filename.Data()));
313 // get the only AliCDBEntry object from the file
314 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
316 TObject* anObject = file->Get("AliCDBEntry");
319 AliError("Bad storage data: NULL entry object!");
323 if (AliCDBEntry::Class() != anObject->IsA()) {
324 AliError("Bad storage data: Invalid entry object!");
328 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
330 // The object's Id is not reset during storage
331 // If object's Id runRange or version do not match with filename,
332 // it means that someone renamed file by hand. In this case a warning msg is issued.
334 ((AliCDBEntry*) anObject)->SetLastStorage("grid");
336 if(!((entryId.GetAliCDBRunRange()).IsEqual(&dataId.GetAliCDBRunRange())) ||
337 entryId.GetVersion() != dataId.GetVersion()){
338 AliWarning(Form("Either RunRange or gridVersion in the object's metadata do noth match with fileName numbers:"));
339 AliWarning(Form("someone renamed file by hand!"));
342 // close file, return retieved entry
343 file->Close(); delete file; file=0;
344 return (AliCDBEntry*) anObject;
347 //_____________________________________________________________________________
348 void AliCDBGrid::GetEntriesForLevel0(const char* level0,
349 const AliCDBId& queryId, TList* result) {
350 // multiple request (AliCDBStorage::GetAll)
352 TString level0Dir=fDBFolder;
355 if (!gGrid->Cd(level0Dir,0)) {
356 AliError(Form("Level0 directory <%s> not found", level0Dir.Data()));
360 TGridResult *res = gGrid->Ls(level0Dir);
362 for(int i=0; i < res->GetEntries(); i++){
363 level1=res->GetFileName(i);
364 if (queryId.GetAliCDBPath().Level1Comprises(level1))
365 GetEntriesForLevel1(level0, level1, queryId, result);
370 //_____________________________________________________________________________
371 void AliCDBGrid::GetEntriesForLevel1(const char* level0, const char* level1,
372 const AliCDBId& queryId, TList* result) {
373 // multiple request (AliCDBStorage::GetAll)
375 TString level1Dir=fDBFolder;
380 if (!gGrid->Cd(level1Dir,0)) {
381 AliError(Form("Level1 directory <%s> not found", level1Dir.Data()));
385 TGridResult *res = gGrid->Ls(level1Dir);
387 for(int i=0; i < res->GetEntries(); i++){
388 level2=res->GetFileName(i);
389 if (queryId.GetAliCDBPath().Level2Comprises(level2)){
390 AliCDBPath entryPath(level0, level1, level2);
391 AliCDBId entryId(entryPath,
392 queryId.GetAliCDBRunRange(),
393 queryId.GetVersion(),
394 queryId.GetSubVersion());
396 AliCDBEntry* anEntry = GetEntry(entryId);
397 if (anEntry) result->Add(anEntry);
404 //_____________________________________________________________________________
405 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
406 // multiple request (AliCDBStorage::GetAll)
408 TList* result = new TList();
411 TString initDir(gGrid->Pwd(0));
413 TGridResult *res = gGrid->Ls(fDBFolder);
416 for(int i=0; i < res->GetEntries(); i++){
417 level0=res->GetFileName(i);
418 if (queryId.GetAliCDBPath().Level0Comprises(level0))
419 GetEntriesForLevel0(level0, queryId, result);
423 gGrid->Cd(initDir.Data(),0);
427 //_____________________________________________________________________________
428 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
429 // put an AliCDBEntry object into the database
431 AliCDBId& id = entry->GetId();
433 // set version for the entry to be stored
434 if (!PrepareId(id)) return kFALSE;
436 // build filename from entry's id
438 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(), filename)) {
439 AliError("Bad ID encountered! Subnormal error!");
443 filename.Prepend("/alien" + fDBFolder + id.GetPath() + '/');
444 TString filenameCopy(filename);
445 filename += "?se="; filename += fSE.Data();
447 TDirectory* saveDir = gDirectory;
450 TFile *file = TFile::Open(filename,"CREATE");
451 if(!file || !file->IsWritable()){
452 AliError(Form("Can't open file <%s>!", filename.Data()));
453 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
459 entry->SetVersion(id.GetVersion());
461 // write object (key name: "AliCDBEntry")
462 Bool_t result = (entry->Write("AliCDBEntry") != 0);
463 if (!result) AliError(Form("Can't write entry to file <%s>!",filename.Data()));
466 if (saveDir) saveDir->cd(); else gROOT->cd();
467 file->Close(); delete file; file=0;
469 AliInfo(Form("CDB object stored into file %s",filenameCopy.Data()));
470 AliInfo(Form("using S.E. %s", fSE.Data()));
476 //_____________________________________________________________________________
477 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
479 TString turl(fileName);
480 turl.Prepend("/alien" + fDBFolder);
481 turl += "?se="; turl += fSE.Data();
482 TFile *file = TFile::Open(turl);
484 AliError(Form("Can't open selection file <%s>!", turl.Data()));
488 TList *list = new TList();
496 keycycle = "AliCDBId;";
499 id = (AliCDBId*) file->Get(keycycle);
503 file->Close(); delete file; file=0;
510 //_____________________________________________________________________________
511 Bool_t AliCDBGrid::Contains(const char* path) const{
512 // check for path in storage's DBFolder
514 TString initDir(gGrid->Pwd(0));
515 TString dirName(fDBFolder);
516 dirName += path; // dirName = fDBFolder/path
517 Bool_t result=kFALSE;
518 if (gGrid->Cd(dirName,0)) result=kTRUE;
519 gGrid->Cd(initDir.Data(),0);
523 /////////////////////////////////////////////////////////////////////////////////////////////////
525 // AliCDBGrid factory //
527 /////////////////////////////////////////////////////////////////////////////////////////////////
529 ClassImp(AliCDBGridFactory)
531 //_____________________________________________________________________________
532 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
533 // check if the string is valid Grid URI
535 // pattern: alien://hostName:Port;user;dbPath;SE
536 // example of a valid pattern:
537 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
538 // TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
539 TRegexp gridPattern("^alien://.+$");
541 return TString(gridString).Contains(gridPattern);
544 //_____________________________________________________________________________
545 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
546 // create AliCDBGridParam class from the URI string
548 if (!Validate(gridString)) {
551 //TString buffer(gridString + sizeof("alien://") - 1);
552 TString buffer(gridString);
554 TString gridUrl = "alien://";
556 TString dbFolder = "DBGrid";
557 TString se = "ALICE::CERN::se01";
559 TObjArray *arr = buffer.Tokenize('?');
563 while((str = (TObjString*) iter.Next())){
564 TString entry(str->String());
565 Int_t indeq = entry.Index('=');
567 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
571 AliError(Form("Invalid entry! %s",entry.Data()));
576 TString key = entry(0,indeq);
577 TString value = entry(indeq+1,entry.Length()-indeq);
579 if(key.Contains("grid",TString::kIgnoreCase)) {
582 else if (key.Contains("user",TString::kIgnoreCase)){
585 else if (key.Contains("folder",TString::kIgnoreCase)){
588 else if (key.Contains("se",TString::kIgnoreCase)){
592 AliError(Form("Invalid entry! %s",entry.Data()));
597 AliInfo(Form("gridUrl: %s",gridUrl.Data()));
598 AliInfo(Form("user: %s",user.Data()));
599 AliInfo(Form("dbFolder: %s",dbFolder.Data()));
600 AliInfo(Form("s.e.: %s",se.Data()));
602 return new AliCDBGridParam(gridUrl, user, dbFolder, se);
605 //_____________________________________________________________________________
606 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
607 // create AliCDBGrid storage instance from parameters
609 if (AliCDBGridParam::Class() == param->IsA()) {
611 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
612 AliCDBGrid *grid = new AliCDBGrid(gridParam->GridUrl(),
613 gridParam->GetUser(), gridParam->GetDBFolder(),
616 if(gGrid) return grid;
622 /////////////////////////////////////////////////////////////////////////////////////////////////
624 // AliCDBGrid Parameter class // //
626 /////////////////////////////////////////////////////////////////////////////////////////////////
628 ClassImp(AliCDBGridParam)
630 //_____________________________________________________________________________
631 AliCDBGridParam::AliCDBGridParam() {
632 // default constructor
636 //_____________________________________________________________________________
637 AliCDBGridParam::AliCDBGridParam(const char* gridUrl,
639 const char* dbFolder,
651 uri+=fGridUrl; uri+="?";
652 uri+="User="; uri+=fUser; uri+="?";
653 uri+="DBFolder="; uri+=fDBFolder; uri+="?";
654 uri+="SE="; uri+=fSE;
659 //_____________________________________________________________________________
660 AliCDBGridParam::~AliCDBGridParam() {
665 //_____________________________________________________________________________
666 AliCDBParam* AliCDBGridParam::CloneParam() const {
669 return new AliCDBGridParam(fGridUrl, fUser, fDBFolder, fSE);
672 //_____________________________________________________________________________
673 ULong_t AliCDBGridParam::Hash() const {
674 // return Hash function
676 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
679 //_____________________________________________________________________________
680 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
681 // check if this object is equal to AliCDBParam obj
687 if (AliCDBGridParam::Class() != obj->IsA()) {
691 AliCDBGridParam* other = (AliCDBGridParam*) obj;
693 if(fGridUrl != other->fGridUrl) return kFALSE;
694 if(fUser != other->fUser) return kFALSE;
695 if(fDBFolder != other->fDBFolder) return kFALSE;
696 if(fSE != other->fSE) return kFALSE;