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 /////////////////////////////////////////////////////////////////////
18 // class AliCDBDump //
19 // access class to a DataBase in a dump storage (single file) //
21 /////////////////////////////////////////////////////////////////////
27 #include <TObjString.h>
30 #include "AliCDBDump.h"
31 #include "AliCDBEntry.h"
36 //_____________________________________________________________________________
37 AliCDBDump::AliCDBDump(const char* dbFile, Bool_t readOnly):
38 fFile(NULL), fReadOnly(readOnly) {
42 fFile = TFile::Open(dbFile, fReadOnly ? "READ" : "UPDATE");
44 AliError(Form("Can't open file <%s>!" , dbFile));
46 AliDebug(2,Form("File <%s> opened",dbFile));
47 if(fReadOnly) AliDebug(2,Form("in read-only mode"));
54 //_____________________________________________________________________________
55 AliCDBDump::~AliCDBDump() {
65 //_____________________________________________________________________________
66 Bool_t AliCDBDump::KeyNameToId(const char* keyname, AliCDBRunRange& runRange,
67 Int_t& version, Int_t& subVersion) {
68 // build AliCDBId from keyname numbers
72 // valid keyname: Run#firstRun_#lastRun_v#version_s#subVersion.root
73 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+$");
74 keyPattern.Index(keyname, &mSize);
76 AliDebug(2,Form("Bad keyname <%s>.", keyname));
80 TObjArray* strArray = (TObjArray*) TString(keyname).Tokenize("_");
82 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
83 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
84 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
86 TString verString(((TObjString*) strArray->At(2))->GetString());
87 version = atoi(verString.Data() + 1);
89 TString subVerString(((TObjString*) strArray->At(3))->GetString());
90 subVersion = atoi(subVerString.Data() + 1);
97 //_____________________________________________________________________________
98 Bool_t AliCDBDump::IdToKeyName(const AliCDBRunRange& runRange, Int_t version,
99 Int_t subVersion, TString& keyname) {
100 // build key name from AliCDBId data (run range, version, subVersion)
102 if (!runRange.IsValid()) {
103 AliDebug(2,Form("Invalid run range <%d, %d>.",
104 runRange.GetFirstRun(), runRange.GetLastRun()));
109 AliDebug(2,Form("Invalid version <%d>.", version));
113 if (subVersion < 0) {
114 AliDebug(2,Form("Invalid subversion <%s>.", subVersion));
119 keyname += runRange.GetFirstRun();
121 keyname += runRange.GetLastRun();
125 keyname += subVersion;
130 //_____________________________________________________________________________
131 Bool_t AliCDBDump::MkDir(const TString& path) {
132 // descend into TDirectory, making TDirectories if they don't exist
133 TObjArray* strArray = (TObjArray*) path.Tokenize("/");
135 TIter iter(strArray);
138 while ((str = (TObjString*) iter.Next())) {
140 TString dirName(str->GetString());
141 if (!dirName.Length()) {
145 if (gDirectory->cd(dirName)) {
149 TDirectory* aDir = gDirectory->mkdir(dirName, "");
151 AliError(Form("Can't create directory <%s>!",
166 //_____________________________________________________________________________
167 Bool_t AliCDBDump::PrepareId(AliCDBId& id) {
168 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
170 AliCDBRunRange aRunRange; // the runRange got from filename
171 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
172 Int_t aVersion, aSubVersion; // the version subVersion got from filename
173 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
176 TIter iter(gDirectory->GetListOfKeys());
179 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
181 while ((key = (TKey*) iter.Next())) { // loop on keys
183 const char* keyName = key->GetName();
185 if (!KeyNameToId(keyName, aRunRange, aVersion,
188 "Bad keyname <%s>!I'll skip it.", keyName));
192 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
193 if(aVersion < lastVersion) continue;
194 if(aVersion > lastVersion) lastSubVersion = -1;
195 if(aSubVersion < lastSubVersion) continue;
196 lastVersion = aVersion;
197 lastSubVersion = aSubVersion;
198 lastRunRange = aRunRange;
201 id.SetVersion(lastVersion);
202 id.SetSubVersion(lastSubVersion + 1);
204 } else { // version specified, look for highest subVersion only
206 while ((key = (TKey*) iter.Next())) { // loop on the keys
208 const char* keyName = key->GetName();
210 if (!KeyNameToId(keyName, aRunRange, aVersion,
213 "Bad keyname <%s>!I'll skip it.", keyName));
217 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
218 && aVersion == id.GetVersion()
219 && aSubVersion > lastSubVersion) {
220 lastSubVersion = aSubVersion;
221 lastRunRange = aRunRange;
226 id.SetSubVersion(lastSubVersion + 1);
229 TString lastStorage = id.GetLastStorage();
230 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
231 id.GetSubVersion() > 0 ){
232 AliError(Form("Grid to Dump Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
233 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
237 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
238 id.GetSubVersion() > 0 ){
239 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
240 id.GetVersion(),id.GetSubVersion()));
241 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
242 id.GetVersion(),id.GetSubVersion()-1));
245 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
246 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
247 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
248 id.GetVersion(), id.GetSubVersion()-1));
255 //_____________________________________________________________________________
256 Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
257 // look for filename matching query (called by GetEntry)
260 AliCDBRunRange aRunRange; // the runRange got from filename
261 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
263 TIter iter(gDirectory->GetListOfKeys());
266 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
268 while ((key = (TKey*) iter.Next())) { // loop on the keys
270 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
271 // aRunRange, aVersion, aSubVersion filled from filename
273 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
274 // aRunRange contains requested run!
276 if (result.GetVersion() < aVersion) {
277 result.SetVersion(aVersion);
278 result.SetSubVersion(aSubVersion);
281 aRunRange.GetFirstRun());
283 aRunRange.GetLastRun());
285 } else if (result.GetVersion() == aVersion
286 && result.GetSubVersion()
289 result.SetSubVersion(aSubVersion);
292 aRunRange.GetFirstRun());
294 aRunRange.GetLastRun());
295 } else if (result.GetVersion() == aVersion
296 && result.GetSubVersion() == aSubVersion){
297 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
298 query.GetFirstRun(), aVersion, aSubVersion));
299 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
304 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
306 result.SetVersion(query.GetVersion());
308 while ((key = (TKey*) iter.Next())) { // loop on the keys
310 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
311 // aRunRange, aVersion, aSubVersion filled from filename
313 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
314 // aRunRange contains requested run!
316 if(query.GetVersion() != aVersion) continue;
317 // aVersion is requested version!
319 if(result.GetSubVersion() == aSubVersion){
320 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
321 query.GetFirstRun(), aVersion, aSubVersion));
322 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
325 if( result.GetSubVersion() < aSubVersion) {
327 result.SetSubVersion(aSubVersion);
330 aRunRange.GetFirstRun());
332 aRunRange.GetLastRun());
336 } else { // both version and subversion specified
338 while ((key = (TKey*) iter.Next())) { // loop on the keys
340 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
341 // aRunRange, aVersion, aSubVersion filled from filename
343 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
344 // aRunRange contains requested run!
346 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
347 // aVersion and aSubVersion are requested version and subVersion!
349 if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
350 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
351 query.GetFirstRun(), aVersion, aSubVersion));
352 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
355 result.SetVersion(aVersion);
356 result.SetSubVersion(aSubVersion);
357 result.SetFirstRun(aRunRange.GetFirstRun());
358 result.SetLastRun(aRunRange.GetLastRun());
366 //_____________________________________________________________________________
367 AliCDBEntry* AliCDBDump::GetEntry(const AliCDBId& queryId) {
368 // get AliCDBEntry from the database
370 TDirectory::TContext context(gDirectory, fFile);
372 if (!(fFile && fFile->IsOpen())) {
373 AliError("AliCDBDump storage is not initialized properly");
377 if (!gDirectory->cd(queryId.GetPath())) {
381 AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
384 // look for a filename matching query requests (path, runRange, version, subVersion)
385 if (!queryId.HasVersion()) {
386 // if version is not specified, first check the selection criteria list
387 AliCDBId selectedId(queryId);
388 GetSelection(&selectedId);
389 result = GetId(selectedId, dataId);
391 result = GetId(queryId, dataId);
394 if (!result || !dataId.IsSpecified()) {
399 if (!IdToKeyName(dataId.GetAliCDBRunRange(), dataId.GetVersion(),
400 dataId.GetSubVersion(), keyname)) {
401 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
405 // get the only AliCDBEntry object from the file
406 // the object in the file is an AliCDBEntry entry named keyname
407 // keyName = Run#firstRun_#lastRun_v#version_s#subVersion
409 TObject* anObject = gDirectory->Get(keyname);
411 AliDebug(2,Form("Bad storage data: NULL entry object!"));
415 if (AliCDBEntry::Class() != anObject->IsA()) {
416 AliDebug(2,Form("Bad storage data: Invalid entry object!"));
420 ((AliCDBEntry*) anObject)->SetLastStorage("dump");
422 return (AliCDBEntry*) anObject;
425 //_____________________________________________________________________________
426 void AliCDBDump::GetEntriesForLevel0(const AliCDBId& queryId, TList* result) {
427 // multiple request (AliCDBStorage::GetAll)
429 TDirectory* saveDir = gDirectory;
431 TIter iter(gDirectory->GetListOfKeys());
434 while ((key = (TKey*) iter.Next())) {
436 TString keyNameStr(key->GetName());
437 if (queryId.GetAliCDBPath().Level1Comprises(keyNameStr)) {
438 gDirectory->cd(keyNameStr);
439 GetEntriesForLevel1(queryId, result);
446 //_____________________________________________________________________________
447 void AliCDBDump::GetEntriesForLevel1(const AliCDBId& queryId, TList* result) {
448 // multiple request (AliCDBStorage::GetAll)
450 TIter iter(gDirectory->GetListOfKeys());
453 TDirectory* level0Dir = (TDirectory*) gDirectory->GetMother();
455 while ((key = (TKey*) iter.Next())) {
457 TString keyNameStr(key->GetName());
458 if (queryId.GetAliCDBPath().Level2Comprises(keyNameStr)) {
460 AliCDBPath aPath(level0Dir->GetName(),
461 gDirectory->GetName(), keyNameStr);
462 AliCDBId anId(aPath, queryId.GetAliCDBRunRange(),
463 queryId.GetVersion(), -1);
465 AliCDBEntry* anEntry = GetEntry(anId);
467 result->Add(anEntry);
476 //_____________________________________________________________________________
477 TList* AliCDBDump::GetEntries(const AliCDBId& queryId) {
478 // multiple request (AliCDBStorage::GetAll)
480 TDirectory::TContext context(gDirectory, fFile);
482 if (!(fFile && fFile->IsOpen())) {
483 AliError("AliCDBDump storage is not initialized properly");
487 TList* result = new TList();
490 TIter iter(gDirectory->GetListOfKeys());
493 while ((key = (TKey*) iter.Next())) {
495 TString keyNameStr(key->GetName());
496 if (queryId.GetAliCDBPath().Level0Comprises(keyNameStr)) {
497 gDirectory->cd(keyNameStr);
498 GetEntriesForLevel0(queryId, result);
507 //_____________________________________________________________________________
508 Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry) {
509 // put an AliCDBEntry object into the database
511 TDirectory::TContext context(gDirectory, fFile);
513 if (!(fFile && fFile->IsOpen())) {
514 AliError("AliCDBDump storage is not initialized properly");
519 AliError("AliCDBDump storage is read only!");
523 AliCDBId& id = entry->GetId();
525 if (!gDirectory->cd(id.GetPath())) {
526 if (!MkDir(id.GetPath())) {
527 AliError(Form("Can't open directory <%s>!",
528 id.GetPath().Data()));
533 // set version and subVersion for the entry to be stored
534 if (!PrepareId(id)) {
538 // build keyname from entry's id
540 if (!IdToKeyName(id.GetAliCDBRunRange(), id.GetVersion(), id.GetSubVersion(), keyname)) {
541 AliError("Invalid ID encountered! Subnormal error!");
545 // write object (key name: Run#firstRun_#lastRun_v#version_s#subVersion)
546 Bool_t result = gDirectory->WriteTObject(entry, keyname);
548 AliError(Form("Can't write entry to file: %s",
553 AliInfo(Form("CDB object stored into file %s",fFile->GetName()));
554 AliInfo(Form("TDirectory/key name: %s/%s",id.GetPath().Data(),keyname.Data()));
559 //_____________________________________________________________________________
560 TList* AliCDBDump::GetIdListFromFile(const char* fileName){
562 TString turl(fileName);
563 if (turl[0] != '/') {
564 turl.Prepend(TString(gSystem->WorkingDirectory()) + '/');
566 TFile *file = TFile::Open(turl);
568 AliError(Form("Can't open selection file <%s>!", turl.Data()));
573 TList *list = new TList();
581 keycycle = "AliCDBId;";
584 id = (AliCDBId*) file->Get(keycycle);
588 file->Close(); delete file; file=0;
592 //_____________________________________________________________________________
593 Bool_t AliCDBDump::Contains(const char* path) const{
594 // check for path in storage
596 TDirectory::TContext context(gDirectory, fFile);
597 if (!(fFile && fFile->IsOpen())) {
598 AliError("AliCDBDump storage is not initialized properly");
602 return gDirectory->cd(path);
606 //_____________________________________________________________________________
607 void AliCDBDump::QueryValidFiles()
614 /////////////////////////////////////////////////////////////////////////////////////////////////
616 // AliCDBDump factory //
618 /////////////////////////////////////////////////////////////////////////////////////////////////
620 ClassImp(AliCDBDumpFactory)
622 //_____________________________________________________________________________
623 Bool_t AliCDBDumpFactory::Validate(const char* dbString) {
624 // check if the string is valid dump URI
626 TRegexp dbPattern("^dump://.+$");
628 return TString(dbString).Contains(dbPattern);
631 //_____________________________________________________________________________
632 AliCDBParam* AliCDBDumpFactory::CreateParameter(const char* dbString) {
633 // create AliCDBDumpParam class from the URI string
635 if (!Validate(dbString)) {
639 TString pathname(dbString + sizeof("dump://") - 1);
643 if (pathname.Contains(TRegexp(";ReadOnly$"))) {
644 pathname.Resize(pathname.Length() - sizeof(";ReadOnly") + 1);
650 gSystem->ExpandPathName(pathname);
652 if (pathname[0] != '/') {
653 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
656 return new AliCDBDumpParam(pathname, readOnly);
659 //_____________________________________________________________________________
660 AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
661 // create AliCDBDump storage instance from parameters
663 if (AliCDBDumpParam::Class() == param->IsA()) {
665 const AliCDBDumpParam* dumpParam =
666 (const AliCDBDumpParam*) param;
668 return new AliCDBDump(dumpParam->GetPath(),
669 dumpParam->IsReadOnly());
675 /////////////////////////////////////////////////////////////////////////////////////////////////
677 // AliCDBDump parameter class //
679 /////////////////////////////////////////////////////////////////////////////////////////////////
681 ClassImp(AliCDBDumpParam)
683 //_____________________________________________________________________________
684 AliCDBDumpParam::AliCDBDumpParam():
685 fDBPath(), fReadOnly(kFALSE)
687 // default constructor
691 //_____________________________________________________________________________
692 AliCDBDumpParam::AliCDBDumpParam(const char* dbPath, Bool_t readOnly):
693 fDBPath(dbPath), fReadOnly(readOnly)
709 //_____________________________________________________________________________
710 AliCDBDumpParam::~AliCDBDumpParam() {
715 //_____________________________________________________________________________
716 AliCDBParam* AliCDBDumpParam::CloneParam() const {
719 return new AliCDBDumpParam(fDBPath, fReadOnly);
722 //_____________________________________________________________________________
723 ULong_t AliCDBDumpParam::Hash() const {
724 // return Hash function
726 return fDBPath.Hash();
729 //_____________________________________________________________________________
730 Bool_t AliCDBDumpParam::IsEqual(const TObject* obj) const {
731 // check if this object is equal to AliCDBParam obj
737 if (AliCDBDumpParam::Class() != obj->IsA()) {
741 AliCDBDumpParam* other = (AliCDBDumpParam*) obj;
743 return fDBPath == other->fDBPath;