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"));
51 //_____________________________________________________________________________
52 AliCDBDump::~AliCDBDump() {
62 //_____________________________________________________________________________
63 Bool_t AliCDBDump::KeyNameToId(const char* keyname, AliCDBRunRange& runRange,
64 Int_t& version, Int_t& subVersion) {
65 // build AliCDBId from keyname numbers
69 // valid keyname: Run#firstRun_#lastRun_v#version_s#subVersion.root
70 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+$");
71 keyPattern.Index(keyname, &mSize);
73 AliDebug(2,Form("Bad keyname <%s>.", keyname));
77 TObjArray* strArray = (TObjArray*) TString(keyname).Tokenize("_");
79 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
80 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
81 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
83 TString verString(((TObjString*) strArray->At(2))->GetString());
84 version = atoi(verString.Data() + 1);
86 TString subVerString(((TObjString*) strArray->At(3))->GetString());
87 subVersion = atoi(subVerString.Data() + 1);
94 //_____________________________________________________________________________
95 Bool_t AliCDBDump::IdToKeyName(const AliCDBRunRange& runRange, Int_t version,
96 Int_t subVersion, TString& keyname) {
97 // build key name from AliCDBId data (run range, version, subVersion)
99 if (!runRange.IsValid()) {
100 AliDebug(2,Form("Invalid run range <%d, %d>.",
101 runRange.GetFirstRun(), runRange.GetLastRun()));
106 AliDebug(2,Form("Invalid version <%d>.", version));
110 if (subVersion < 0) {
111 AliDebug(2,Form("Invalid subversion <%s>.", subVersion));
116 keyname += runRange.GetFirstRun();
118 keyname += runRange.GetLastRun();
122 keyname += subVersion;
127 //_____________________________________________________________________________
128 Bool_t AliCDBDump::MkDir(const TString& path) {
129 // descend into TDirectory, making TDirectories if they don't exist
130 TObjArray* strArray = (TObjArray*) path.Tokenize("/");
132 TIter iter(strArray);
135 while ((str = (TObjString*) iter.Next())) {
137 TString dirName(str->GetString());
138 if (!dirName.Length()) {
142 if (gDirectory->cd(dirName)) {
146 TDirectory* aDir = gDirectory->mkdir(dirName, "");
148 AliError(Form("Can't create directory <%s>!",
163 //_____________________________________________________________________________
164 Bool_t AliCDBDump::PrepareId(AliCDBId& id) {
165 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
167 AliCDBRunRange aRunRange; // the runRange got from filename
168 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
169 Int_t aVersion, aSubVersion; // the version subVersion got from filename
170 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
173 TIter iter(gDirectory->GetListOfKeys());
176 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
178 while ((key = (TKey*) iter.Next())) { // loop on keys
180 const char* keyName = key->GetName();
182 if (!KeyNameToId(keyName, aRunRange, aVersion,
185 "Bad keyname <%s>!I'll skip it.", keyName));
189 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
190 if(aVersion < lastVersion) continue;
191 if(aVersion > lastVersion) lastSubVersion = -1;
192 if(aSubVersion < lastSubVersion) continue;
193 lastVersion = aVersion;
194 lastSubVersion = aSubVersion;
195 lastRunRange = aRunRange;
198 id.SetVersion(lastVersion);
199 id.SetSubVersion(lastSubVersion + 1);
201 } else { // version specified, look for highest subVersion only
203 while ((key = (TKey*) iter.Next())) { // loop on the keys
205 const char* keyName = key->GetName();
207 if (!KeyNameToId(keyName, aRunRange, aVersion,
210 "Bad keyname <%s>!I'll skip it.", keyName));
214 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
215 && aVersion == id.GetVersion()
216 && aSubVersion > lastSubVersion) {
217 lastSubVersion = aSubVersion;
218 lastRunRange = aRunRange;
223 id.SetSubVersion(lastSubVersion + 1);
226 TString lastStorage = id.GetLastStorage();
227 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
228 id.GetSubVersion() > 0 ){
229 AliError(Form("Grid to Dump Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
230 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
234 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
235 id.GetSubVersion() > 0 ){
236 AliWarning(Form("A NEW object is being stored with version v%d_s%d",
237 id.GetVersion(),id.GetSubVersion()));
238 AliWarning(Form("and it will hide previously stored object with v%d_s%d!",
239 id.GetVersion(),id.GetSubVersion()-1));
242 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
243 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
244 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
245 id.GetVersion(), id.GetSubVersion()-1));
252 //_____________________________________________________________________________
253 Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
254 // look for filename matching query (called by GetEntry)
257 AliCDBRunRange aRunRange; // the runRange got from filename
258 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
260 TIter iter(gDirectory->GetListOfKeys());
263 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
265 while ((key = (TKey*) iter.Next())) { // loop on the keys
267 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
268 // aRunRange, aVersion, aSubVersion filled from filename
270 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
271 // aRunRange contains requested run!
273 if (result.GetVersion() < aVersion) {
274 result.SetVersion(aVersion);
275 result.SetSubVersion(aSubVersion);
278 aRunRange.GetFirstRun());
280 aRunRange.GetLastRun());
282 } else if (result.GetVersion() == aVersion
283 && result.GetSubVersion()
286 result.SetSubVersion(aSubVersion);
289 aRunRange.GetFirstRun());
291 aRunRange.GetLastRun());
292 } else if (result.GetVersion() == aVersion
293 && result.GetSubVersion() == aSubVersion){
294 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
295 query.GetFirstRun(), aVersion, aSubVersion));
296 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
301 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
303 result.SetVersion(query.GetVersion());
305 while ((key = (TKey*) iter.Next())) { // loop on the keys
307 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
308 // aRunRange, aVersion, aSubVersion filled from filename
310 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
311 // aRunRange contains requested run!
313 if(query.GetVersion() != aVersion) continue;
314 // aVersion is requested version!
316 if(result.GetSubVersion() == aSubVersion){
317 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
318 query.GetFirstRun(), aVersion, aSubVersion));
319 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
322 if( result.GetSubVersion() < aSubVersion) {
324 result.SetSubVersion(aSubVersion);
327 aRunRange.GetFirstRun());
329 aRunRange.GetLastRun());
333 } else { // both version and subversion specified
335 while ((key = (TKey*) iter.Next())) { // loop on the keys
337 if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
338 // aRunRange, aVersion, aSubVersion filled from filename
340 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
341 // aRunRange contains requested run!
343 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
344 // aVersion and aSubVersion are requested version and subVersion!
346 if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
347 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
348 query.GetFirstRun(), aVersion, aSubVersion));
349 result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
352 result.SetVersion(aVersion);
353 result.SetSubVersion(aSubVersion);
354 result.SetFirstRun(aRunRange.GetFirstRun());
355 result.SetLastRun(aRunRange.GetLastRun());
363 //_____________________________________________________________________________
364 AliCDBEntry* AliCDBDump::GetEntry(const AliCDBId& queryId) {
365 // get AliCDBEntry from the database
367 TDirectory::TContext context(gDirectory, fFile);
369 if (!(fFile && fFile->IsOpen())) {
370 AliError("AliCDBDump storage is not initialized properly");
374 if (!gDirectory->cd(queryId.GetPath())) {
378 AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
381 // look for a filename matching query requests (path, runRange, version, subVersion)
382 if (!queryId.HasVersion()) {
383 // if version is not specified, first check the selection criteria list
384 AliCDBId selectedId(queryId);
385 GetSelection(&selectedId);
386 result = GetId(selectedId, dataId);
388 result = GetId(queryId, dataId);
391 if (!result || !dataId.IsSpecified()) {
396 if (!IdToKeyName(dataId.GetAliCDBRunRange(), dataId.GetVersion(),
397 dataId.GetSubVersion(), keyname)) {
398 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
402 // get the only AliCDBEntry object from the file
403 // the object in the file is an AliCDBEntry entry named keyname
404 // keyName = Run#firstRun_#lastRun_v#version_s#subVersion
406 TObject* anObject = gDirectory->Get(keyname);
408 AliDebug(2,Form("Bad storage data: NULL entry object!"));
412 if (AliCDBEntry::Class() != anObject->IsA()) {
413 AliDebug(2,Form("Bad storage data: Invalid entry object!"));
417 ((AliCDBEntry*) anObject)->SetLastStorage("dump");
419 return (AliCDBEntry*) anObject;
422 //_____________________________________________________________________________
423 void AliCDBDump::GetEntriesForLevel0(const AliCDBId& queryId, TList* result) {
424 // multiple request (AliCDBStorage::GetAll)
426 TDirectory* saveDir = gDirectory;
428 TIter iter(gDirectory->GetListOfKeys());
431 while ((key = (TKey*) iter.Next())) {
433 TString keyNameStr(key->GetName());
434 if (queryId.GetAliCDBPath().Level1Comprises(keyNameStr)) {
435 gDirectory->cd(keyNameStr);
436 GetEntriesForLevel1(queryId, result);
443 //_____________________________________________________________________________
444 void AliCDBDump::GetEntriesForLevel1(const AliCDBId& queryId, TList* result) {
445 // multiple request (AliCDBStorage::GetAll)
447 TIter iter(gDirectory->GetListOfKeys());
450 TDirectory* level0Dir = (TDirectory*) gDirectory->GetMother();
452 while ((key = (TKey*) iter.Next())) {
454 TString keyNameStr(key->GetName());
455 if (queryId.GetAliCDBPath().Level2Comprises(keyNameStr)) {
457 AliCDBPath aPath(level0Dir->GetName(),
458 gDirectory->GetName(), keyNameStr);
459 AliCDBId anId(aPath, queryId.GetAliCDBRunRange(),
460 queryId.GetVersion(), -1);
462 AliCDBEntry* anEntry = GetEntry(anId);
464 result->Add(anEntry);
473 //_____________________________________________________________________________
474 TList* AliCDBDump::GetEntries(const AliCDBId& queryId) {
475 // multiple request (AliCDBStorage::GetAll)
477 TDirectory::TContext context(gDirectory, fFile);
479 if (!(fFile && fFile->IsOpen())) {
480 AliError("AliCDBDump storage is not initialized properly");
484 TList* result = new TList();
487 TIter iter(gDirectory->GetListOfKeys());
490 while ((key = (TKey*) iter.Next())) {
492 TString keyNameStr(key->GetName());
493 if (queryId.GetAliCDBPath().Level0Comprises(keyNameStr)) {
494 gDirectory->cd(keyNameStr);
495 GetEntriesForLevel0(queryId, result);
504 //_____________________________________________________________________________
505 Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry) {
506 // put an AliCDBEntry object into the database
508 TDirectory::TContext context(gDirectory, fFile);
510 if (!(fFile && fFile->IsOpen())) {
511 AliError("AliCDBDump storage is not initialized properly");
516 AliError("AliCDBDump storage is read only!");
520 AliCDBId& id = entry->GetId();
522 if (!gDirectory->cd(id.GetPath())) {
523 if (!MkDir(id.GetPath())) {
524 AliError(Form("Can't open directory <%s>!",
525 id.GetPath().Data()));
530 // set version and subVersion for the entry to be stored
531 if (!PrepareId(id)) {
535 // build keyname from entry's id
537 if (!IdToKeyName(id.GetAliCDBRunRange(), id.GetVersion(), id.GetSubVersion(), keyname)) {
538 AliError("Invalid ID encountered! Subnormal error!");
542 // write object (key name: Run#firstRun_#lastRun_v#version_s#subVersion)
543 Bool_t result = gDirectory->WriteTObject(entry, keyname);
545 AliError(Form("Can't write entry to file: %s",
550 AliInfo(Form("CDB object stored into file %s",fFile->GetName()));
551 AliInfo(Form("TDirectory/key name: %s/%s",id.GetPath().Data(),keyname.Data()));
556 //_____________________________________________________________________________
557 TList* AliCDBDump::GetIdListFromFile(const char* fileName){
559 TString turl(fileName);
560 if (turl[0] != '/') {
561 turl.Prepend(TString(gSystem->WorkingDirectory()) + '/');
563 TFile *file = TFile::Open(turl);
565 AliError(Form("Can't open selection file <%s>!", turl.Data()));
570 TList *list = new TList();
578 keycycle = "AliCDBId;";
581 id = (AliCDBId*) file->Get(keycycle);
585 file->Close(); delete file; file=0;
589 //_____________________________________________________________________________
590 Bool_t AliCDBDump::Contains(const char* path) const{
591 // check for path in storage
593 TDirectory::TContext context(gDirectory, fFile);
594 if (!(fFile && fFile->IsOpen())) {
595 AliError("AliCDBDump storage is not initialized properly");
599 return gDirectory->cd(path);
603 /////////////////////////////////////////////////////////////////////////////////////////////////
605 // AliCDBDump factory //
607 /////////////////////////////////////////////////////////////////////////////////////////////////
609 ClassImp(AliCDBDumpFactory)
611 //_____________________________________________________________________________
612 Bool_t AliCDBDumpFactory::Validate(const char* dbString) {
613 // check if the string is valid dump URI
615 TRegexp dbPattern("^dump://.+$");
617 return TString(dbString).Contains(dbPattern);
620 //_____________________________________________________________________________
621 AliCDBParam* AliCDBDumpFactory::CreateParameter(const char* dbString) {
622 // create AliCDBDumpParam class from the URI string
624 if (!Validate(dbString)) {
628 TString pathname(dbString + sizeof("dump://") - 1);
632 if (pathname.Contains(TRegexp(";ReadOnly$"))) {
633 pathname.Resize(pathname.Length() - sizeof(";ReadOnly") + 1);
639 gSystem->ExpandPathName(pathname);
641 if (pathname[0] != '/') {
642 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
645 return new AliCDBDumpParam(pathname, readOnly);
648 //_____________________________________________________________________________
649 AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
650 // create AliCDBDump storage instance from parameters
652 if (AliCDBDumpParam::Class() == param->IsA()) {
654 const AliCDBDumpParam* dumpParam =
655 (const AliCDBDumpParam*) param;
657 return new AliCDBDump(dumpParam->GetPath(),
658 dumpParam->IsReadOnly());
664 /////////////////////////////////////////////////////////////////////////////////////////////////
666 // AliCDBDump parameter class //
668 /////////////////////////////////////////////////////////////////////////////////////////////////
670 ClassImp(AliCDBDumpParam)
672 //_____________________________________________________________________________
673 AliCDBDumpParam::AliCDBDumpParam() {
674 // default constructor
678 //_____________________________________________________________________________
679 AliCDBDumpParam::AliCDBDumpParam(const char* dbPath, Bool_t readOnly):
680 fDBPath(dbPath), fReadOnly(readOnly)
696 //_____________________________________________________________________________
697 AliCDBDumpParam::~AliCDBDumpParam() {
702 //_____________________________________________________________________________
703 AliCDBParam* AliCDBDumpParam::CloneParam() const {
706 return new AliCDBDumpParam(fDBPath, fReadOnly);
709 //_____________________________________________________________________________
710 ULong_t AliCDBDumpParam::Hash() const {
711 // return Hash function
713 return fDBPath.Hash();
716 //_____________________________________________________________________________
717 Bool_t AliCDBDumpParam::IsEqual(const TObject* obj) const {
718 // check if this object is equal to AliCDBParam obj
724 if (AliCDBDumpParam::Class() != obj->IsA()) {
728 AliCDBDumpParam* other = (AliCDBDumpParam*) obj;
730 return fDBPath == other->fDBPath;