]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCDBGrid.cxx
Example macro for the creation of tags (P.Christakoglou)
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
CommitLineData
9e1ceb13 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16/////////////////////////////////////////////////////////////////////////////////////////////////
17// //
18// AliCDBGrid //
19// access class to a DataBase in an AliEn storage //
20// //
21/////////////////////////////////////////////////////////////////////////////////////////////////
22
23
24#include <TGrid.h>
25#include <TGridResult.h>
26#include <TFile.h>
27#include <TKey.h>
28#include <TROOT.h>
29#include <TSystem.h>
30#include <TObjArray.h>
31#include <TObjString.h>
32#include <TRegexp.h>
33
34#include "AliLog.h"
fdf65bb5 35#include "AliCDBEntry.h"
9e1ceb13 36#include "AliCDBGrid.h"
37
38
39ClassImp(AliCDBGrid)
40
41//_____________________________________________________________________________
42AliCDBGrid::AliCDBGrid(const char *host, const Int_t port,
43 const char *user, const char *dbPath, const char *se) :
44AliCDBStorage(),
45fHost(host),
46fPort(port),
47fUser(user),
48fDBPath(dbPath),
49fSE(se)
50{
51// constructor //
52
53 TString grid="alien://";
54 grid+=host; grid+=":"; grid+=port;
55
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());
61 }
62
63 if(!gGrid) {
64 AliError("Connection failed!");
65 return;
66 }
67
68 TString initDir(gGrid->Pwd(0));
69 if (fDBPath[0] != '/') {
70 fDBPath.Prepend(initDir);
71 }
72
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()));
78 }
79 } else {
80 AliDebug(2,Form("Folder <%s> found",fDBPath.Data()));
81 }
82
83 // removes any '/' at the end of path, then append one '/'
84 while(fDBPath.EndsWith("/")) fDBPath.Remove(fDBPath.Last('/'));
85 fDBPath+="/";
86
87 // return to the initial directory
88 gGrid->Cd(initDir.Data(),0);
89}
90
91//_____________________________________________________________________________
92AliCDBGrid::~AliCDBGrid()
93{
94// destructor
95
96}
97
98//_____________________________________________________________________________
99Bool_t AliCDBGrid::FilenameToId(const char* filename, AliCDBRunRange& runRange,
100 Int_t& gridVersion) {
101// build AliCDBId from filename numbers
102
103 Ssiz_t mSize;
104
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);
108 if (!mSize) {
109 AliDebug(2,Form("Bad filename <%s>.", filename));
110 return kFALSE;
111 }
112
113 TString idString(filename);
114 idString.Resize(idString.Length() - sizeof(".root") + 1);
115
116 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
117
118 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
119 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
120 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
121
122 TString verString(((TObjString*) strArray->At(2))->GetString());
123 gridVersion = atoi(verString.Data() + 1);
124
125 delete strArray;
126
127 return kTRUE;
128}
129
130//_____________________________________________________________________________
131Bool_t AliCDBGrid::IdToFilename(const AliCDBRunRange& runRange, Int_t gridVersion,
132 TString& filename) {
133// build file name from AliCDBId data (run range, version)
134
135 if (!runRange.IsValid()) {
136 AliWarning(Form("Invalid run range <%d, %d>.",
137 runRange.GetFirstRun(), runRange.GetLastRun()));
138 return kFALSE;
139 }
140
141 if (gridVersion < 0) {
142 AliWarning(Form("Invalid version <%d>.", gridVersion));
143 return kFALSE;
144 }
145
146 filename += "Run";
147 filename += runRange.GetFirstRun();
148 filename += "_";
149 filename += runRange.GetLastRun();
150 filename += "_v";
151 filename += gridVersion;
152 filename += ".root";
153
154 return kTRUE;
155}
156
157//_____________________________________________________________________________
158Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
159// prepare id (version) of the object that will be stored (called by PutEntry)
160
161 TString initDir(gGrid->Pwd(0));
162 TString pathName= id.GetPath();
163
164 TString dirName(fDBPath);
165
166 Bool_t dirExist=kFALSE;
167
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);
174 if (!dirExist) {
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());
179 return kFALSE;
180 }
181 }
182 }
183 delete arrName;
184 gGrid->Cd(initDir,0);
185
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
191
192 TGridResult *res = gGrid->Ls(dirName);
193
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;
201 }
202
203 }
204 delete res;
205
206 id.SetVersion(lastVersion + 1);
207
208 TString lastStorage = id.GetLastStorage();
209 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
7541e2ae 210 AliWarning(Form("*** WARNING! a NEW object is being stored with version %d",
9e1ceb13 211 id.GetVersion()));
7541e2ae 212 AliWarning(Form("and it will hide previously stored object with version %d!",
9e1ceb13 213 id.GetVersion()-1));
214 }
215
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()));
219
220 return kTRUE;
221}
222
223//_____________________________________________________________________________
224AliCDBId AliCDBGrid::GetId(const AliCDBId& query) {
225// look for filename matching query (called by GetEntry)
226
227 TString initDir(gGrid->Pwd(0));
228
229 AliCDBId result(query.GetAliCDBPath(), -1, -1, -1, -1);
230
231 TString dirName(fDBPath);
232 dirName += query.GetPath(); // dirName = fDBPath/idPath
233
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()));
237 return result;
238 }
239
240 TGridResult *res = gGrid->Ls(dirName);
241
242 const char* filename;
243 AliCDBRunRange aRunRange; // the runRange got from filename
244 Int_t aVersion; // the version got from filename
245
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
250
251 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
252 // aRunRange contains requested run!
253
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);
260 return result;
261 }
262 result.SetVersion(aVersion);
263 result.SetFirstRun(aRunRange.GetFirstRun());
264 result.SetLastRun(aRunRange.GetLastRun());
265
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);
272 return result;
273 }
274 result.SetVersion(aVersion);
275 result.SetFirstRun(aRunRange.GetFirstRun());
276 result.SetLastRun(aRunRange.GetLastRun());
277 }
278 } // end loop on filenames
279 delete res;
280
281 gGrid->Cd(initDir.Data(),0);
282
283 return result;
284}
285
286//_____________________________________________________________________________
287AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
288// get AliCDBEntry from the database
289
290 AliCDBId dataId;
291
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));
296 } else {
297 dataId = GetId(queryId);
298 }
299
300 if (!dataId.IsSpecified()) return NULL;
301
302 TString filename;
303 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
304 AliError("Bad data ID encountered! Subnormal error!");
305 return NULL;
306 }
307
308 filename.Prepend("/alien" + fDBPath + queryId.GetPath() + '/');
309 filename += "?se="; filename += fSE.Data();
310
311 AliInfo(Form("Opening file: %s",filename.Data()));
312 TFile *file = TFile::Open(filename);
313 if (!file) {
314 AliError(Form("Can't open file <%s>!", filename.Data()));
315 return NULL;
316 }
317
318 // get the only AliCDBEntry object from the file
319 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
320
321 TObject* anObject = file->Get("AliCDBEntry");
322
323 if (!anObject) {
324 AliError("Bad storage data: NULL entry object!");
325 return NULL;
326 }
327
328 if (AliCDBEntry::Class() != anObject->IsA()) {
329 AliError("Bad storage data: Invalid entry object!");
330 return NULL;
331 }
332
333 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
334
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.
338
339 ((AliCDBEntry*) anObject)->SetLastStorage("grid");
340
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!"));
345 }
346
347 // close file, return retieved entry
348 file->Close(); delete file; file=0;
349 return (AliCDBEntry*) anObject;
350}
351
352//_____________________________________________________________________________
353void AliCDBGrid::GetEntriesForLevel0(const char* level0,
354 const AliCDBId& queryId, TList* result) {
355// multiple request (AliCDBStorage::GetAll)
356
357 TString level0Dir=fDBPath;
358 level0Dir += level0;
359
360 if (!gGrid->Cd(level0Dir,0)) {
361 AliError(Form("Level0 directory <%s> not found", level0Dir.Data()));
362 return;
363 }
364
365 TGridResult *res = gGrid->Ls(level0Dir);
366 TString level1;
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);
371 }
372 delete res;
373}
374
375//_____________________________________________________________________________
376void AliCDBGrid::GetEntriesForLevel1(const char* level0, const char* level1,
377 const AliCDBId& queryId, TList* result) {
378// multiple request (AliCDBStorage::GetAll)
379
380 TString level1Dir=fDBPath;
381 level1Dir += level0;
382 level1Dir += '/';
383 level1Dir += level1;
384
385 if (!gGrid->Cd(level1Dir,0)) {
386 AliError(Form("Level1 directory <%s> not found", level1Dir.Data()));
387 return;
388 }
389
390 TGridResult *res = gGrid->Ls(level1Dir);
391 TString level2;
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());
400
401 AliCDBEntry* anEntry = GetEntry(entryId);
402 if (anEntry) result->Add(anEntry);
403
404 }
405 }
406 delete res;
407}
408
409//_____________________________________________________________________________
410TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
411// multiple request (AliCDBStorage::GetAll)
412
413 TList* result = new TList();
414 result->SetOwner();
415
416 TString initDir(gGrid->Pwd(0));
417
418 TGridResult *res = gGrid->Ls(fDBPath);
419 TString level0;
420
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);
425 }
426 delete res;
427
428 gGrid->Cd(initDir.Data(),0);
429 return result;
430}
431
432//_____________________________________________________________________________
433Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
434// put an AliCDBEntry object into the database
435
436 AliCDBId& id = entry->GetId();
437
438 // set version for the entry to be stored
439 if (!PrepareId(id)) return kFALSE;
440
441 // build filename from entry's id
442 TString filename;
443 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(), filename)) {
444 AliError("Bad ID encountered! Subnormal error!");
445 return kFALSE;
446 }
447
448 filename.Prepend("/alien" + fDBPath + id.GetPath() + '/');
449 TString filenameCopy(filename);
450 filename += "?se="; filename += fSE.Data();
451
452 TDirectory* saveDir = gDirectory;
453
454 // open file
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;
459 return kFALSE;
460 }
461
462 file->cd();
463
464 entry->SetVersion(id.GetVersion());
465
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()));
469
470
471 if (saveDir) saveDir->cd(); else gROOT->cd();
472 file->Close(); delete file; file=0;
473 if(result) {
474 AliInfo(Form("AliCDBEntry stored into file %s",filenameCopy.Data()));
475 AliInfo(Form("using S.E. %s", fSE.Data()));
476 }
477
478 return result;
479}
480
481/////////////////////////////////////////////////////////////////////////////////////////////////
482// //
483// AliCDBGrid factory //
484// //
485/////////////////////////////////////////////////////////////////////////////////////////////////
486
487ClassImp(AliCDBGridFactory)
488
489//_____________________________________________________________________________
490Bool_t AliCDBGridFactory::Validate(const char* gridString) {
491// check if the string is valid Grid URI
492
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_-.]+;.+;.+$");
497
498 return TString(gridString).Contains(gridPattern);
499}
500
501//_____________________________________________________________________________
502AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
503// create AliCDBGridParam class from the URI string
504
505 if (!Validate(gridString)) {
506 return NULL;
507 }
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)
518
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()));
524
525 return new AliCDBGridParam(host, port, user, dbPath, se);
526}
527
528//_____________________________________________________________________________
529AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
530// create AliCDBGrid storage instance from parameters
531
532 if (AliCDBGridParam::Class() == param->IsA()) {
533
534 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
535 AliCDBGrid *grid = new AliCDBGrid(gridParam->GetHost(), gridParam->GetPort(),
536 gridParam->GetUser(), gridParam->GetPath(),
537 gridParam->GetSE());
538
539 if(gGrid) return grid;
540 }
541
542 return NULL;
543}
544
545/////////////////////////////////////////////////////////////////////////////////////////////////
546// //
547// AliCDBGrid Parameter class // //
548// //
549/////////////////////////////////////////////////////////////////////////////////////////////////
550
551ClassImp(AliCDBGridParam)
552
553//_____________________________________________________________________________
554AliCDBGridParam::AliCDBGridParam() {
555// default constructor
556
557}
558
559//_____________________________________________________________________________
560AliCDBGridParam::AliCDBGridParam(const char* host,
561 const Int_t port,
562 const char* user,
563 const char* dbPath,
564 const char* se):
565 fHost(host),
566 fPort(port),
567 fUser(user),
568 fDBPath(dbPath),
569 fSE(se)
570{
571// constructor
572
573 SetType("alien");
574
575 TString uri=("alien://");
576 uri+=host; uri+=":"; uri+=port; uri+=";";
577 uri+=user; uri+=";"; uri+=dbPath; uri+=";";
578 uri+=se;
579
580 SetURI(uri);
581}
582
583//_____________________________________________________________________________
584AliCDBGridParam::~AliCDBGridParam() {
585// destructor
586
587}
588
589//_____________________________________________________________________________
590AliCDBParam* AliCDBGridParam::CloneParam() const {
591// clone parameter
592
593 return new AliCDBGridParam(fHost, fPort, fUser, fDBPath, fSE);
594}
595
596//_____________________________________________________________________________
597ULong_t AliCDBGridParam::Hash() const {
598// return Hash function
599
600 return fHost.Hash()+fPort+fUser.Hash()+fDBPath.Hash()+fSE.Hash();
601}
602
603//_____________________________________________________________________________
604Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
605// check if this object is equal to AliCDBParam obj
606
607 if (this == obj) {
608 return kTRUE;
609 }
610
611 if (AliCDBGridParam::Class() != obj->IsA()) {
612 return kFALSE;
613 }
614
615 AliCDBGridParam* other = (AliCDBGridParam*) obj;
616
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;
622 return kTRUE;
623}
624