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 a local storage //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
24 #include <TObjString.h>
29 #include "AliCDBLocal.h"
30 #include "AliCDBEntry.h"
35 //_____________________________________________________________________________
36 AliCDBLocal::AliCDBLocal(const char* baseDir):
37 fBaseDirectory(baseDir)
41 // check baseDire: trying to cd to baseDir; if it does not exist, create it
42 void* dir = gSystem->OpenDirectory(baseDir);
44 if (gSystem->mkdir(baseDir, kTRUE)) {
45 AliError(Form("Can't open directory <%s>!", baseDir));
49 AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data()));
50 gSystem->FreeDirectory(dir);
53 fBaseFolder = fBaseDirectory;
56 //_____________________________________________________________________________
57 AliCDBLocal::~AliCDBLocal() {
63 //_____________________________________________________________________________
64 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
65 Int_t& version, Int_t& subVersion) {
66 // build AliCDBId from filename numbers
71 // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root
72 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
73 keyPattern.Index(filename, &mSize);
75 AliDebug(2, Form("Bad filename <%s>.", filename));
79 TString idString(filename);
80 idString.Resize(idString.Length() - sizeof(".root") + 1);
82 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
84 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
85 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
86 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
88 TString verString(((TObjString*) strArray->At(2))->GetString());
89 version = atoi(verString.Data() + 1);
91 TString subVerString(((TObjString*) strArray->At(3))->GetString());
92 subVersion = atoi(subVerString.Data() + 1);
100 //_____________________________________________________________________________
101 Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
102 // build file name from AliCDBId data (run range, version, subVersion)
104 if (!id.GetAliCDBRunRange().IsValid()) {
105 AliDebug(2,Form("Invalid run range <%d, %d>.",
106 id.GetFirstRun(), id.GetLastRun()));
110 if (id.GetVersion() < 0) {
111 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
115 if (id.GetSubVersion() < 0) {
116 AliDebug(2,Form("Invalid subversion <%s>.", id.GetSubVersion()));
120 filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(),
121 id.GetVersion(), id.GetSubVersion());
123 filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
128 //_____________________________________________________________________________
129 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
130 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
132 TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
134 // go to the path; if directory does not exist, create it
135 void* dirPtr = gSystem->OpenDirectory(dirName);
137 gSystem->mkdir(dirName, kTRUE);
138 dirPtr = gSystem->OpenDirectory(dirName);
141 AliError(Form("Can't create directory <%s>!",
147 const char* filename;
148 AliCDBRunRange aRunRange; // the runRange got from filename
149 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
150 Int_t aVersion, aSubVersion; // the version subVersion got from filename
151 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
153 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
155 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
157 TString aString(filename);
158 if (aString == "." || aString == "..") continue;
160 if (!FilenameToId(filename, aRunRange, aVersion,
163 "Bad filename <%s>! I'll skip it.",
168 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
169 if(aVersion < lastVersion) continue;
170 if(aVersion > lastVersion) lastSubVersion = -1;
171 if(aSubVersion < lastSubVersion) continue;
172 lastVersion = aVersion;
173 lastSubVersion = aSubVersion;
174 lastRunRange = aRunRange;
177 id.SetVersion(lastVersion);
178 id.SetSubVersion(lastSubVersion + 1);
180 } else { // version specified, look for highest subVersion only
182 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
184 TString aString(filename);
185 if (aString == "." || aString == "..") {
189 if (!FilenameToId(filename, aRunRange, aVersion,
192 "Bad filename <%s>!I'll skip it.",
197 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
198 && aVersion == id.GetVersion()
199 && aSubVersion > lastSubVersion) {
200 lastSubVersion = aSubVersion;
201 lastRunRange = aRunRange;
206 id.SetSubVersion(lastSubVersion + 1);
209 gSystem->FreeDirectory(dirPtr);
211 TString lastStorage = id.GetLastStorage();
212 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
213 id.GetSubVersion() > 0 ){
214 AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
215 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
219 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
220 id.GetSubVersion() > 0 ){
221 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
222 id.GetVersion(),id.GetSubVersion()));
223 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
224 id.GetVersion(),id.GetSubVersion()-1));
227 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
228 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
229 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
230 id.GetVersion(), id.GetSubVersion()-1));
235 //_____________________________________________________________________________
236 Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
237 // look for filename matching query (called by GetEntry)
239 TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
241 void* dirPtr = gSystem->OpenDirectory(dirName);
243 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
244 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
248 const char* filename;
250 AliCDBRunRange aRunRange; // the runRange got from filename
251 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
253 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
255 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
257 TString aString(filename);
258 if (aString == "." || aString == "..") continue;
260 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
261 // aRunRange, aVersion, aSubVersion filled from filename
263 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
264 // aRunRange contains requested run!
266 if (result.GetVersion() < aVersion) {
267 result.SetVersion(aVersion);
268 result.SetSubVersion(aSubVersion);
271 aRunRange.GetFirstRun());
273 aRunRange.GetLastRun());
275 } else if (result.GetVersion() == aVersion
276 && result.GetSubVersion()
279 result.SetSubVersion(aSubVersion);
282 aRunRange.GetFirstRun());
284 aRunRange.GetLastRun());
285 } else if (result.GetVersion() == aVersion
286 && result.GetSubVersion() == aSubVersion){
287 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
288 query.GetFirstRun(), aVersion, aSubVersion));
289 gSystem->FreeDirectory(dirPtr);
294 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
296 result.SetVersion(query.GetVersion());
298 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
300 TString aString(filename);
301 if (aString == "." || aString == "..") continue;
303 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
304 // aRunRange, aVersion, aSubVersion filled from filename
306 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
307 // aRunRange contains requested run!
309 if(query.GetVersion() != aVersion) continue;
310 // aVersion is requested version!
312 if(result.GetSubVersion() == aSubVersion){
313 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
314 query.GetFirstRun(), aVersion, aSubVersion));
315 gSystem->FreeDirectory(dirPtr);
318 if( result.GetSubVersion() < aSubVersion) {
320 result.SetSubVersion(aSubVersion);
323 aRunRange.GetFirstRun());
325 aRunRange.GetLastRun());
329 } else { // both version and subversion specified
331 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
333 TString aString(filename);
334 if (aString == "." || aString == "..") continue;
336 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
337 // aRunRange, aVersion, aSubVersion filled from filename
339 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
340 // aRunRange contains requested run!
342 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
343 // aVersion and aSubVersion are requested version and subVersion!
345 if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
346 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
347 query.GetFirstRun(), aVersion, aSubVersion));
348 gSystem->FreeDirectory(dirPtr);
351 result.SetVersion(aVersion);
352 result.SetSubVersion(aSubVersion);
353 result.SetFirstRun(aRunRange.GetFirstRun());
354 result.SetLastRun(aRunRange.GetLastRun());
359 gSystem->FreeDirectory(dirPtr);
364 //_____________________________________________________________________________
365 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
366 // get AliCDBEntry from the database
368 AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
371 // look for a filename matching query requests (path, runRange, version, subVersion)
372 if (!queryId.HasVersion()) {
373 // if version is not specified, first check the selection criteria list
374 AliCDBId selectedId(queryId);
375 GetSelection(&selectedId);
376 result = GetId(selectedId, dataId);
378 result = GetId(queryId, dataId);
381 if (!result || !dataId.IsSpecified()) return NULL;
384 if (!IdToFilename(dataId, filename)) {
386 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
390 TFile file(filename, "READ"); // open file
391 if (!file.IsOpen()) {
392 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
396 // get the only AliCDBEntry object from the file
397 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
399 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
401 AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
406 AliCDBId entryId = anEntry->GetId();
408 // The object's Id are not reset during storage
409 // If object's Id runRange or version do not match with filename,
410 // it means that someone renamed file by hand. In this case a warning msg is issued.
412 anEntry-> SetLastStorage("local");
414 if(!((entryId.GetAliCDBRunRange()).IsEqual(& dataId.GetAliCDBRunRange())) ||
415 (entryId.GetVersion() != dataId.GetVersion()) || (entryId.GetSubVersion() != dataId.GetSubVersion())){
416 AliWarning(Form("Mismatch between file name and object's Id!"));
417 AliWarning(Form("File name: %s", dataId.ToString().Data()));
418 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
421 // close file, return retieved entry
426 //_____________________________________________________________________________
427 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
428 const AliCDBId& queryId, TList* result) {
429 // multiple request (AliCDBStorage::GetAll)
431 TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
433 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
435 AliDebug(2,Form("Can't open level0 directory <%s>!",
441 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
443 TString level1Str(level1);
444 if (level1Str == "." || level1Str == "..") {
448 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
449 GetEntriesForLevel1(level0, level1, queryId, result);
453 gSystem->FreeDirectory(level0DirPtr);
456 //_____________________________________________________________________________
457 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
458 const AliCDBId& queryId, TList* result) {
459 // multiple request (AliCDBStorage::GetAll)
461 TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
463 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
465 AliDebug(2,Form("Can't open level1 directory <%s>!",
471 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
473 TString level2Str(level2);
474 if (level2Str == "." || level2Str == "..") {
478 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
480 AliCDBPath entryPath(level0, level1, level2);
481 AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
482 queryId.GetVersion(), queryId.GetSubVersion());
484 AliCDBEntry* anEntry = GetEntry(entryId);
486 result->Add(anEntry);
491 gSystem->FreeDirectory(level1DirPtr);
494 //_____________________________________________________________________________
495 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
496 // multiple request (AliCDBStorage::GetAll)
498 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
499 if (!storageDirPtr) {
500 AliDebug(2,Form("Can't open storage directory <%s>",
501 fBaseDirectory.Data()));
505 TList* result = new TList();
509 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
511 TString level0Str(level0);
512 if (level0Str == "." || level0Str == "..") {
516 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
517 GetEntriesForLevel0(level0, queryId, result);
521 gSystem->FreeDirectory(storageDirPtr);
526 //_____________________________________________________________________________
527 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
528 // put an AliCDBEntry object into the database
530 AliCDBId& id = entry->GetId();
532 // set version and subVersion for the entry to be stored
533 if (!PrepareId(id)) return kFALSE;
536 // build filename from entry's id
538 if (!IdToFilename(id, filename)) {
540 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
545 TFile file(filename, "CREATE");
546 if (!file.IsOpen()) {
547 AliError(Form("Can't open file <%s>!", filename.Data()));
551 entry->SetVersion(id.GetVersion());
552 entry->SetSubVersion(id.GetSubVersion());
554 // write object (key name: "AliCDBEntry")
555 Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
556 if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
560 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
561 AliInfo(Form("CDB object stored into file %s",filename.Data()));
567 //_____________________________________________________________________________
568 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
570 TString fullFileName(fileName);
571 fullFileName.Prepend(fBaseDirectory+'/');
572 TFile *file = TFile::Open(fullFileName);
574 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
579 TList *list = new TList();
587 keycycle = "AliCDBId;";
590 id = (AliCDBId*) file->Get(keycycle);
594 file->Close(); delete file; file=0;
598 //_____________________________________________________________________________
599 Bool_t AliCDBLocal::Contains(const char* path) const{
600 // check for path in storage's fBaseDirectory
602 TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
603 Bool_t result=kFALSE;
605 void* dirPtr = gSystem->OpenDirectory(dirName);
606 if (dirPtr) result=kTRUE;
607 gSystem->FreeDirectory(dirPtr);
612 //_____________________________________________________________________________
613 void AliCDBLocal::QueryValidFiles()
615 // Query the CDB for files valid for AliCDBStorage::fRun
616 // fills list fValidFileIds with AliCDBId objects created from file name
618 if(fVersion != -1) AliWarning ("Version parameter is not used by local storage query!");
619 if(fMetaDataFilter) {
620 AliWarning ("CDB meta data parameters are not used by local storage query!");
621 delete fMetaDataFilter; fMetaDataFilter=0;
624 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
627 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
629 TString level0Str(level0);
630 if (level0Str == "." || level0Str == "..") {
634 if (fPathFilter.Level0Comprises(level0)) {
635 TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
636 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
638 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
640 TString level1Str(level1);
641 if (level1Str == "." || level1Str == "..") {
645 if (fPathFilter.Level1Comprises(level1)) {
646 TString level1Dir = Form("%s/%s/%s",
647 fBaseDirectory.Data(),level0,level1);
649 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
651 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
653 TString level2Str(level2);
654 if (level2Str == "." || level2Str == "..") {
658 if (fPathFilter.Level2Comprises(level2)) {
659 TString dirName = Form("%s/%s/%s/%s",
660 fBaseDirectory.Data(),level0,level1,level2);
662 void* dirPtr = gSystem->OpenDirectory(dirName);
664 const char* filename;
666 AliCDBRunRange aRunRange; // the runRange got from filename
667 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
669 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
671 TString aString(filename);
672 if (aString == "." || aString == "..") continue;
674 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
675 AliCDBRunRange runrg(fRun,fRun);
676 if (!aRunRange.Comprises(runrg)) continue;
677 // aRunRange contains requested run!
678 AliCDBPath validPath(level0,level1,level2);
679 AliCDBId validId(validPath,aRunRange,aVersion,aSubVersion);
680 fValidFileIds.AddLast(validId.Clone());
682 gSystem->FreeDirectory(dirPtr);
685 gSystem->FreeDirectory(level1DirPtr);
688 gSystem->FreeDirectory(level0DirPtr);
691 gSystem->FreeDirectory(storageDirPtr);
695 //_____________________________________________________________________________
696 Int_t AliCDBLocal::GetLatestVersion(const char* path, Int_t run){
697 // get last version found in the database valid for run and path
699 AliCDBPath aCDBPath(path);
700 if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
701 AliError(Form("Invalid path in request: %s", path));
705 AliCDBId query(path, run, run, -1, -1);
710 return dataId.GetVersion();
714 //_____________________________________________________________________________
715 Int_t AliCDBLocal::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
716 // get last version found in the database valid for run and path
718 AliCDBPath aCDBPath(path);
719 if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
720 AliError(Form("Invalid path in request: %s", path));
724 AliCDBId query(path, run, run, version, -1);
729 return dataId.GetSubVersion();
733 /////////////////////////////////////////////////////////////////////////////////////////////////
735 // AliCDBLocal factory //
737 /////////////////////////////////////////////////////////////////////////////////////////////////
739 ClassImp(AliCDBLocalFactory)
741 //_____________________________________________________________________________
742 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
743 // check if the string is valid local URI
745 TRegexp dbPattern("^local://.+$");
747 return TString(dbString).Contains(dbPattern);
750 //_____________________________________________________________________________
751 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
752 // create AliCDBLocalParam class from the URI string
754 if (!Validate(dbString)) {
758 TString pathname(dbString + sizeof("local://") - 1);
760 gSystem->ExpandPathName(pathname);
762 if (pathname[0] != '/') {
763 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
766 return new AliCDBLocalParam(pathname);
769 //_____________________________________________________________________________
770 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
771 // create AliCDBLocal storage instance from parameters
773 if (AliCDBLocalParam::Class() == param->IsA()) {
775 const AliCDBLocalParam* localParam =
776 (const AliCDBLocalParam*) param;
778 return new AliCDBLocal(localParam->GetPath());
784 /////////////////////////////////////////////////////////////////////////////////////////////////
786 // AliCDBLocal Parameter class // //
788 /////////////////////////////////////////////////////////////////////////////////////////////////
790 ClassImp(AliCDBLocalParam)
792 //_____________________________________________________________________________
793 AliCDBLocalParam::AliCDBLocalParam():
797 // default constructor
801 //_____________________________________________________________________________
802 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
809 SetURI(TString("local://") + dbPath);
812 //_____________________________________________________________________________
813 AliCDBLocalParam::~AliCDBLocalParam() {
818 //_____________________________________________________________________________
819 AliCDBParam* AliCDBLocalParam::CloneParam() const {
822 return new AliCDBLocalParam(fDBPath);
825 //_____________________________________________________________________________
826 ULong_t AliCDBLocalParam::Hash() const {
827 // return Hash function
829 return fDBPath.Hash();
832 //_____________________________________________________________________________
833 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
834 // check if this object is equal to AliCDBParam obj
840 if (AliCDBLocalParam::Class() != obj->IsA()) {
844 AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
846 return fDBPath == other->fDBPath;