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 *host, const Int_t port,
43 const char *user, const char *dbPath, const char *se) :
53 TString grid="alien://";
54 grid+=host; grid+=":"; grid+=port;
56 // if the same Grid is alreay active, skip connection
57 if (!gGrid || TString(host) != gGrid->GetHost() ||
58 port != gGrid->GetPort() || TString(user) != gGrid->GetUser()) {
59 // connection to the Grid
60 TGrid::Connect(grid.Data(),fUser.Data());
64 AliError("Connection failed!");
68 TString initDir(gGrid->Pwd(0));
69 if (fDBPath[0] != '/') {
70 fDBPath.Prepend(initDir);
73 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
74 if(!gGrid->Cd(fDBPath.Data(),0)){
75 AliDebug(2,Form("Creating new folder <%s> ...",fDBPath.Data()));
76 if(!gGrid->Mkdir(fDBPath.Data(),"",0)){
77 AliError(Form("Cannot create folder <%s> !",fDBPath.Data()));
80 AliDebug(2,Form("Folder <%s> found",fDBPath.Data()));
83 // removes any '/' at the end of path, then append one '/'
84 while(fDBPath.EndsWith("/")) fDBPath.Remove(fDBPath.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 AliWarning(Form("Invalid run range <%d, %d>.",
137 runRange.GetFirstRun(), runRange.GetLastRun()));
141 if (gridVersion < 0) {
142 AliWarning(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(fDBPath);
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 AliInfo(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("*** WARNING! 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 AliCDBId AliCDBGrid::GetId(const AliCDBId& query) {
225 // look for filename matching query (called by GetEntry)
227 TString initDir(gGrid->Pwd(0));
229 AliCDBId result(query.GetAliCDBPath(), -1, -1, -1, -1);
231 TString dirName(fDBPath);
232 dirName += query.GetPath(); // dirName = fDBPath/idPath
234 if (!gGrid->Cd(dirName,0)) {
235 AliError(Form("Directory <%s> not found", (query.GetPath()).Data()));
236 AliError(Form("in DB folder %s", fDBPath.Data()));
240 TGridResult *res = gGrid->Ls(dirName);
242 const char* filename;
243 AliCDBRunRange aRunRange; // the runRange got from filename
244 Int_t aVersion; // the version got from filename
246 for(int i=0; i < res->GetEntries(); i++){
247 filename=res->GetFileName(i);
248 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
249 // aRunRange and aVersion filled from filename
251 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
252 // aRunRange contains requested run!
254 if (!query.HasVersion()){ // look for highest version
255 if(result.GetVersion() > aVersion) continue;
256 if(result.GetVersion() == aVersion) {
257 AliError(Form("More than one object valid for run %d, version %d!",
258 query.GetFirstRun(), aVersion));
259 result.SetRunRange(-1,-1); result.SetVersion(-1);
262 result.SetVersion(aVersion);
263 result.SetFirstRun(aRunRange.GetFirstRun());
264 result.SetLastRun(aRunRange.GetLastRun());
266 } else { // look for specified version
267 if(query.GetVersion() != aVersion) continue;
268 if(result.GetVersion() == aVersion){
269 AliError(Form("More than one object valid for run %d, version %d!",
270 query.GetFirstRun(), aVersion));
271 result.SetRunRange(-1,-1); result.SetVersion(-1);
274 result.SetVersion(aVersion);
275 result.SetFirstRun(aRunRange.GetFirstRun());
276 result.SetLastRun(aRunRange.GetLastRun());
278 } // end loop on filenames
281 gGrid->Cd(initDir.Data(),0);
286 //_____________________________________________________________________________
287 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
288 // get AliCDBEntry from the database
292 // look for a filename matching query requests (path, runRange, version, subVersion)
293 if (!queryId.HasVersion()) {
294 // if version is not specified, first check the selection criteria list
295 dataId = GetId(GetSelection(queryId));
297 dataId = GetId(queryId);
300 if (!dataId.IsSpecified()) return NULL;
303 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
304 AliError("Bad data ID encountered! Subnormal error!");
308 filename.Prepend("/alien" + fDBPath + queryId.GetPath() + '/');
309 filename += "?se="; filename += fSE.Data();
311 AliInfo(Form("Opening file: %s",filename.Data()));
312 TFile *file = TFile::Open(filename);
314 AliError(Form("Can't open file <%s>!", filename.Data()));
318 // get the only AliCDBEntry object from the file
319 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
321 TObject* anObject = file->Get("AliCDBEntry");
324 AliError("Bad storage data: NULL entry object!");
328 if (AliCDBEntry::Class() != anObject->IsA()) {
329 AliError("Bad storage data: Invalid entry object!");
333 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
335 // The object's Id is not reset during storage
336 // If object's Id runRange or version do not match with filename,
337 // it means that someone renamed file by hand. In this case a warning msg is issued.
339 ((AliCDBEntry*) anObject)->SetLastStorage("grid");
341 if(!((entryId.GetAliCDBRunRange()).IsEqual(&dataId.GetAliCDBRunRange())) ||
342 entryId.GetVersion() != dataId.GetVersion()){
343 AliWarning(Form("Either RunRange or gridVersion in the object's metadata do noth match with fileName numbers:"));
344 AliWarning(Form("someone renamed file by hand!"));
347 // close file, return retieved entry
348 file->Close(); delete file; file=0;
349 return (AliCDBEntry*) anObject;
352 //_____________________________________________________________________________
353 void AliCDBGrid::GetEntriesForLevel0(const char* level0,
354 const AliCDBId& queryId, TList* result) {
355 // multiple request (AliCDBStorage::GetAll)
357 TString level0Dir=fDBPath;
360 if (!gGrid->Cd(level0Dir,0)) {
361 AliError(Form("Level0 directory <%s> not found", level0Dir.Data()));
365 TGridResult *res = gGrid->Ls(level0Dir);
367 for(int i=0; i < res->GetEntries(); i++){
368 level1=res->GetFileName(i);
369 if (queryId.GetAliCDBPath().Level1Comprises(level1))
370 GetEntriesForLevel1(level0, level1, queryId, result);
375 //_____________________________________________________________________________
376 void AliCDBGrid::GetEntriesForLevel1(const char* level0, const char* level1,
377 const AliCDBId& queryId, TList* result) {
378 // multiple request (AliCDBStorage::GetAll)
380 TString level1Dir=fDBPath;
385 if (!gGrid->Cd(level1Dir,0)) {
386 AliError(Form("Level1 directory <%s> not found", level1Dir.Data()));
390 TGridResult *res = gGrid->Ls(level1Dir);
392 for(int i=0; i < res->GetEntries(); i++){
393 level2=res->GetFileName(i);
394 if (queryId.GetAliCDBPath().Level2Comprises(level2)){
395 AliCDBPath entryPath(level0, level1, level2);
396 AliCDBId entryId(entryPath,
397 queryId.GetAliCDBRunRange(),
398 queryId.GetVersion(),
399 queryId.GetSubVersion());
401 AliCDBEntry* anEntry = GetEntry(entryId);
402 if (anEntry) result->Add(anEntry);
409 //_____________________________________________________________________________
410 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
411 // multiple request (AliCDBStorage::GetAll)
413 TList* result = new TList();
416 TString initDir(gGrid->Pwd(0));
418 TGridResult *res = gGrid->Ls(fDBPath);
421 for(int i=0; i < res->GetEntries(); i++){
422 level0=res->GetFileName(i);
423 if (queryId.GetAliCDBPath().Level0Comprises(level0))
424 GetEntriesForLevel0(level0, queryId, result);
428 gGrid->Cd(initDir.Data(),0);
432 //_____________________________________________________________________________
433 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
434 // put an AliCDBEntry object into the database
436 AliCDBId& id = entry->GetId();
438 // set version for the entry to be stored
439 if (!PrepareId(id)) return kFALSE;
441 // build filename from entry's id
443 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(), filename)) {
444 AliError("Bad ID encountered! Subnormal error!");
448 filename.Prepend("/alien" + fDBPath + id.GetPath() + '/');
449 TString filenameCopy(filename);
450 filename += "?se="; filename += fSE.Data();
452 TDirectory* saveDir = gDirectory;
455 TFile *file = TFile::Open(filename,"CREATE");
456 if(!file || !file->IsWritable()){
457 AliError(Form("Can't open file <%s>!", filename.Data()));
458 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
464 entry->SetVersion(id.GetVersion());
466 // write object (key name: "AliCDBEntry")
467 Bool_t result = (entry->Write("AliCDBEntry") != 0);
468 if (!result) AliError(Form("Can't write entry to file <%s>!",filename.Data()));
471 if (saveDir) saveDir->cd(); else gROOT->cd();
472 file->Close(); delete file; file=0;
474 AliInfo(Form("AliCDBEntry stored into file %s",filenameCopy.Data()));
475 AliInfo(Form("using S.E. %s", fSE.Data()));
481 /////////////////////////////////////////////////////////////////////////////////////////////////
483 // AliCDBGrid factory //
485 /////////////////////////////////////////////////////////////////////////////////////////////////
487 ClassImp(AliCDBGridFactory)
489 //_____________________________________________________________________________
490 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
491 // check if the string is valid Grid URI
493 // pattern: alien://hostName:Port;user;dbPath;SE
494 // example of a valid pattern:
495 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
496 TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
498 return TString(gridString).Contains(gridPattern);
501 //_____________________________________________________________________________
502 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
503 // create AliCDBGridParam class from the URI string
505 if (!Validate(gridString)) {
508 TString buffer(gridString + sizeof("alien://") - 1);
509 TString host = buffer(0,buffer.First(':')); // host (ex. aliendb4.cern.ch)
510 buffer = buffer(host.Sizeof(),buffer.Sizeof());
511 TString strPort = buffer(0, buffer.First(';'));
512 Int_t port = atoi(strPort.Data()); // port number (ex. 9000)
513 buffer = buffer(strPort.Sizeof(),buffer.Sizeof());
514 TString user = buffer(0,buffer.First(';')); // user (ex. colla)
515 buffer = buffer(user.Sizeof(),buffer.Sizeof());
516 TString dbPath = buffer(0,buffer.First(';')); // DB path (ex. /alice/cern.ch/user/c/colla/DB)
517 TString se = buffer(dbPath.Sizeof(),buffer.Sizeof()); // storage element (ex. ALICE::CERN::Server)
519 AliInfo(Form("host: %s",host.Data()));
520 AliInfo(Form("port: %d",port));
521 AliInfo(Form("user: %s",user.Data()));
522 AliInfo(Form("dbPath: %s",dbPath.Data()));
523 AliInfo(Form("s.e.: %s",se.Data()));
525 return new AliCDBGridParam(host, port, user, dbPath, se);
528 //_____________________________________________________________________________
529 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
530 // create AliCDBGrid storage instance from parameters
532 if (AliCDBGridParam::Class() == param->IsA()) {
534 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
535 AliCDBGrid *grid = new AliCDBGrid(gridParam->GetHost(), gridParam->GetPort(),
536 gridParam->GetUser(), gridParam->GetPath(),
539 if(gGrid) return grid;
545 /////////////////////////////////////////////////////////////////////////////////////////////////
547 // AliCDBGrid Parameter class // //
549 /////////////////////////////////////////////////////////////////////////////////////////////////
551 ClassImp(AliCDBGridParam)
553 //_____________________________________________________________________________
554 AliCDBGridParam::AliCDBGridParam() {
555 // default constructor
559 //_____________________________________________________________________________
560 AliCDBGridParam::AliCDBGridParam(const char* host,
575 TString uri=("alien://");
576 uri+=host; uri+=":"; uri+=port; uri+=";";
577 uri+=user; uri+=";"; uri+=dbPath; uri+=";";
583 //_____________________________________________________________________________
584 AliCDBGridParam::~AliCDBGridParam() {
589 //_____________________________________________________________________________
590 AliCDBParam* AliCDBGridParam::CloneParam() const {
593 return new AliCDBGridParam(fHost, fPort, fUser, fDBPath, fSE);
596 //_____________________________________________________________________________
597 ULong_t AliCDBGridParam::Hash() const {
598 // return Hash function
600 return fHost.Hash()+fPort+fUser.Hash()+fDBPath.Hash()+fSE.Hash();
603 //_____________________________________________________________________________
604 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
605 // check if this object is equal to AliCDBParam obj
611 if (AliCDBGridParam::Class() != obj->IsA()) {
615 AliCDBGridParam* other = (AliCDBGridParam*) obj;
617 if(fHost != other->fHost) return kFALSE;
618 if(fPort != other->fPort) return kFALSE;
619 if(fUser != other->fUser) return kFALSE;
620 if(fDBPath != other->fDBPath) return kFALSE;
621 if(fSE != other->fSE) return kFALSE;