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 "AliCDBGrid.h"
40 //_____________________________________________________________________________
41 AliCDBGrid::AliCDBGrid(const char *host, const Int_t port,
42 const char *user, const char *dbPath, const char *se) :
52 TString grid="alien://";
53 grid+=host; grid+=":"; grid+=port;
55 // if the same Grid is alreay active, skip connection
56 if (!gGrid || TString(host) != gGrid->GetHost() ||
57 port != gGrid->GetPort() || TString(user) != gGrid->GetUser()) {
58 // connection to the Grid
59 TGrid::Connect(grid.Data(),fUser.Data());
63 AliError("Connection failed!");
67 TString initDir(gGrid->Pwd(0));
68 if (fDBPath[0] != '/') {
69 fDBPath.Prepend(initDir);
72 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
73 if(!gGrid->Cd(fDBPath.Data(),0)){
74 AliDebug(2,Form("Creating new folder <%s> ...",fDBPath.Data()));
75 if(!gGrid->Mkdir(fDBPath.Data(),"",0)){
76 AliError(Form("Cannot create folder <%s> !",fDBPath.Data()));
79 AliDebug(2,Form("Folder <%s> found",fDBPath.Data()));
82 // removes any '/' at the end of path, then append one '/'
83 while(fDBPath.EndsWith("/")) fDBPath.Remove(fDBPath.Last('/'));
86 // return to the initial directory
87 gGrid->Cd(initDir.Data(),0);
90 //_____________________________________________________________________________
91 AliCDBGrid::~AliCDBGrid()
97 //_____________________________________________________________________________
98 Bool_t AliCDBGrid::FilenameToId(const char* filename, AliCDBRunRange& runRange,
100 // build AliCDBId from filename numbers
104 // valid filename: Run#firstRun_#lastRun_v#version.root
105 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
106 keyPattern.Index(filename, &mSize);
108 AliDebug(2,Form("Bad filename <%s>.", filename));
112 TString idString(filename);
113 idString.Resize(idString.Length() - sizeof(".root") + 1);
115 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
117 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
118 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
119 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
121 TString verString(((TObjString*) strArray->At(2))->GetString());
122 gridVersion = atoi(verString.Data() + 1);
129 //_____________________________________________________________________________
130 Bool_t AliCDBGrid::IdToFilename(const AliCDBRunRange& runRange, Int_t gridVersion,
132 // build file name from AliCDBId data (run range, version)
134 if (!runRange.IsValid()) {
135 AliWarning(Form("Invalid run range <%d, %d>.",
136 runRange.GetFirstRun(), runRange.GetLastRun()));
140 if (gridVersion < 0) {
141 AliWarning(Form("Invalid version <%d>.", gridVersion));
146 filename += runRange.GetFirstRun();
148 filename += runRange.GetLastRun();
150 filename += gridVersion;
156 //_____________________________________________________________________________
157 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
158 // prepare id (version) of the object that will be stored (called by PutEntry)
160 TString initDir(gGrid->Pwd(0));
161 TString pathName= id.GetPath();
163 TString dirName(fDBPath);
165 Bool_t dirExist=kFALSE;
167 // go to the path; if directory does not exist, create it
168 TObjArray *arrName=pathName.Tokenize("/");
169 for(int i=0;i<arrName->GetEntries();i++){
170 TString buffer((arrName->At(i))->GetName());
171 dirName+=buffer; dirName+="/";
172 dirExist=gGrid->Cd(dirName,0);
174 AliInfo(Form("Creating new folder <%s> ...",dirName.Data()));
175 if(!gGrid->Mkdir(dirName,"",0)){
176 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
177 gGrid->Cd(initDir.Data());
183 gGrid->Cd(initDir,0);
185 const char* filename;
186 AliCDBRunRange aRunRange; // the runRange got from filename
187 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
188 Int_t aVersion; // the version got from filename
189 Int_t lastVersion=0; // highest version found
191 TGridResult *res = gGrid->Ls(dirName);
193 //loop on the files in the directory, look for highest version
194 for(int i=0; i < res->GetEntries(); i++){
195 filename=res->GetFileName(i);
196 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
197 if (aRunRange.Overlaps(id.GetAliCDBRunRange()) && aVersion > lastVersion) {
198 lastVersion = aVersion;
199 lastRunRange = aRunRange;
205 id.SetVersion(lastVersion + 1);
207 TString lastStorage = id.GetLastStorage();
208 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
209 AliWarning(Form("*** WARNING! a NEW object is being stored with version v%d_s%d",
211 AliWarning(Form("and it will hide previously stored object with v%d_s%d!",
215 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
216 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
217 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
222 //_____________________________________________________________________________
223 AliCDBId AliCDBGrid::GetId(const AliCDBId& query) {
224 // look for filename matching query (called by GetEntry)
226 TString initDir(gGrid->Pwd(0));
228 AliCDBId result(query.GetAliCDBPath(), -1, -1, -1, -1);
230 TString dirName(fDBPath);
231 dirName += query.GetPath(); // dirName = fDBPath/idPath
233 if (!gGrid->Cd(dirName,0)) {
234 AliError(Form("Directory <%s> not found", (query.GetPath()).Data()));
235 AliError(Form("in DB folder %s", fDBPath.Data()));
239 TGridResult *res = gGrid->Ls(dirName);
241 const char* filename;
242 AliCDBRunRange aRunRange; // the runRange got from filename
243 Int_t aVersion; // the version got from filename
245 for(int i=0; i < res->GetEntries(); i++){
246 filename=res->GetFileName(i);
247 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
248 // aRunRange and aVersion filled from filename
250 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
251 // aRunRange contains requested run!
253 if (!query.HasVersion()){ // look for highest version
254 if(result.GetVersion() > aVersion) continue;
255 if(result.GetVersion() == aVersion) {
256 AliError(Form("More than one object valid for run %d, version %d!",
257 query.GetFirstRun(), aVersion));
258 result.SetRunRange(-1,-1); result.SetVersion(-1);
261 result.SetVersion(aVersion);
262 result.SetFirstRun(aRunRange.GetFirstRun());
263 result.SetLastRun(aRunRange.GetLastRun());
265 } else { // look for specified version
266 if(query.GetVersion() != aVersion) continue;
267 if(result.GetVersion() == aVersion){
268 AliError(Form("More than one object valid for run %d, version %d!",
269 query.GetFirstRun(), aVersion));
270 result.SetRunRange(-1,-1); result.SetVersion(-1);
273 result.SetVersion(aVersion);
274 result.SetFirstRun(aRunRange.GetFirstRun());
275 result.SetLastRun(aRunRange.GetLastRun());
277 } // end loop on filenames
280 gGrid->Cd(initDir.Data(),0);
285 //_____________________________________________________________________________
286 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
287 // get AliCDBEntry from the database
291 // look for a filename matching query requests (path, runRange, version, subVersion)
292 if (!queryId.HasVersion()) {
293 // if version is not specified, first check the selection criteria list
294 dataId = GetId(GetSelection(queryId));
296 dataId = GetId(queryId);
299 if (!dataId.IsSpecified()) return NULL;
302 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
303 AliError("Bad data ID encountered! Subnormal error!");
307 filename.Prepend("/alien" + fDBPath + queryId.GetPath() + '/');
308 filename += "?se="; filename += fSE.Data();
310 AliInfo(Form("Opening file: %s",filename.Data()));
311 TFile *file = TFile::Open(filename);
313 AliError(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 AliError("Bad storage data: NULL entry object!");
327 if (AliCDBEntry::Class() != anObject->IsA()) {
328 AliError("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=fDBPath;
359 if (!gGrid->Cd(level0Dir,0)) {
360 AliError(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=fDBPath;
384 if (!gGrid->Cd(level1Dir,0)) {
385 AliError(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(fDBPath);
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" + fDBPath + 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("AliCDBEntry stored into file %s",filenameCopy.Data()));
474 AliInfo(Form("using S.E. %s", fSE.Data()));
480 /////////////////////////////////////////////////////////////////////////////////////////////////
482 // AliCDBGrid factory //
484 /////////////////////////////////////////////////////////////////////////////////////////////////
486 ClassImp(AliCDBGridFactory)
488 //_____________________________________________________________________________
489 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
490 // check if the string is valid Grid URI
492 // pattern: alien://hostName:Port;user;dbPath;SE
493 // example of a valid pattern:
494 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
495 TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
497 return TString(gridString).Contains(gridPattern);
500 //_____________________________________________________________________________
501 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
502 // create AliCDBGridParam class from the URI string
504 if (!Validate(gridString)) {
507 TString buffer(gridString + sizeof("alien://") - 1);
508 TString host = buffer(0,buffer.First(':')); // host (ex. aliendb4.cern.ch)
509 buffer = buffer(host.Sizeof(),buffer.Sizeof());
510 TString strPort = buffer(0, buffer.First(';'));
511 Int_t port = atoi(strPort.Data()); // port number (ex. 9000)
512 buffer = buffer(strPort.Sizeof(),buffer.Sizeof());
513 TString user = buffer(0,buffer.First(';')); // user (ex. colla)
514 buffer = buffer(user.Sizeof(),buffer.Sizeof());
515 TString dbPath = buffer(0,buffer.First(';')); // DB path (ex. /alice/cern.ch/user/c/colla/DB)
516 TString se = buffer(dbPath.Sizeof(),buffer.Sizeof()); // storage element (ex. ALICE::CERN::Server)
518 AliInfo(Form("host: %s",host.Data()));
519 AliInfo(Form("port: %d",port));
520 AliInfo(Form("user: %s",user.Data()));
521 AliInfo(Form("dbPath: %s",dbPath.Data()));
522 AliInfo(Form("s.e.: %s",se.Data()));
524 return new AliCDBGridParam(host, port, user, dbPath, se);
527 //_____________________________________________________________________________
528 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
529 // create AliCDBGrid storage instance from parameters
531 if (AliCDBGridParam::Class() == param->IsA()) {
533 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
534 AliCDBGrid *grid = new AliCDBGrid(gridParam->GetHost(), gridParam->GetPort(),
535 gridParam->GetUser(), gridParam->GetPath(),
538 if(gGrid) return grid;
544 /////////////////////////////////////////////////////////////////////////////////////////////////
546 // AliCDBGrid Parameter class // //
548 /////////////////////////////////////////////////////////////////////////////////////////////////
550 ClassImp(AliCDBGridParam)
552 //_____________________________________________________________________________
553 AliCDBGridParam::AliCDBGridParam() {
554 // default constructor
558 //_____________________________________________________________________________
559 AliCDBGridParam::AliCDBGridParam(const char* host,
574 TString uri=("alien://");
575 uri+=host; uri+=":"; uri+=port; uri+=";";
576 uri+=user; uri+=";"; uri+=dbPath; uri+=";";
582 //_____________________________________________________________________________
583 AliCDBGridParam::~AliCDBGridParam() {
588 //_____________________________________________________________________________
589 AliCDBParam* AliCDBGridParam::CloneParam() const {
592 return new AliCDBGridParam(fHost, fPort, fUser, fDBPath, fSE);
595 //_____________________________________________________________________________
596 ULong_t AliCDBGridParam::Hash() const {
597 // return Hash function
599 return fHost.Hash()+fPort+fUser.Hash()+fDBPath.Hash()+fSE.Hash();
602 //_____________________________________________________________________________
603 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
604 // check if this object is equal to AliCDBParam obj
610 if (AliCDBGridParam::Class() != obj->IsA()) {
614 AliCDBGridParam* other = (AliCDBGridParam*) obj;
616 if(fHost != other->fHost) return kFALSE;
617 if(fPort != other->fPort) return kFALSE;
618 if(fUser != other->fUser) return kFALSE;
619 if(fDBPath != other->fDBPath) return kFALSE;
620 if(fSE != other->fSE) return kFALSE;