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 AliCDBRunRange& runRange, Int_t version,
102 Int_t subVersion, TString& filename) {
103 // build file name from AliCDBId data (run range, version, subVersion)
105 if (!runRange.IsValid()) {
106 AliDebug(2,Form("Invalid run range <%d, %d>.",
107 runRange.GetFirstRun(), runRange.GetLastRun()));
112 AliDebug(2,Form("Invalid version <%d>.", version));
116 if (subVersion < 0) {
117 AliDebug(2,Form("Invalid subversion <%s>.", subVersion));
122 filename += runRange.GetFirstRun();
124 filename += runRange.GetLastRun();
128 filename += subVersion;
134 //_____________________________________________________________________________
135 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
136 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
139 dirName += fBaseDirectory;
141 dirName += id.GetPath();
143 // go to the path; if directory does not exist, create it
144 void* dirPtr = gSystem->OpenDirectory(dirName);
146 gSystem->mkdir(dirName, kTRUE);
147 dirPtr = gSystem->OpenDirectory(dirName);
150 AliError(Form("Can't create directory <%s>!",
156 const char* filename;
157 AliCDBRunRange aRunRange; // the runRange got from filename
158 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
159 Int_t aVersion, aSubVersion; // the version subVersion got from filename
160 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
162 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
164 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
166 TString aString(filename);
167 if (aString == "." || aString == "..") continue;
169 if (!FilenameToId(filename, aRunRange, aVersion,
172 "Bad filename <%s>! I'll skip it.",
177 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
178 if(aVersion < lastVersion) continue;
179 if(aVersion > lastVersion) lastSubVersion = -1;
180 if(aSubVersion < lastSubVersion) continue;
181 lastVersion = aVersion;
182 lastSubVersion = aSubVersion;
183 lastRunRange = aRunRange;
186 id.SetVersion(lastVersion);
187 id.SetSubVersion(lastSubVersion + 1);
189 } else { // version specified, look for highest subVersion only
191 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
193 TString aString(filename);
194 if (aString == "." || aString == "..") {
198 if (!FilenameToId(filename, aRunRange, aVersion,
201 "Bad filename <%s>!I'll skip it.",
206 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
207 && aVersion == id.GetVersion()
208 && aSubVersion > lastSubVersion) {
209 lastSubVersion = aSubVersion;
210 lastRunRange = aRunRange;
215 id.SetSubVersion(lastSubVersion + 1);
218 gSystem->FreeDirectory(dirPtr);
220 TString lastStorage = id.GetLastStorage();
221 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
222 id.GetSubVersion() > 0 ){
223 AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
224 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
228 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
229 id.GetSubVersion() > 0 ){
230 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
231 id.GetVersion(),id.GetSubVersion()));
232 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
233 id.GetVersion(),id.GetSubVersion()-1));
236 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
237 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
238 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
239 id.GetVersion(), id.GetSubVersion()-1));
244 //_____________________________________________________________________________
245 Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
246 // look for filename matching query (called by GetEntry)
249 dirName += fBaseDirectory;
251 dirName += query.GetPath(); // dirName = fDBPath/idPath
253 void* dirPtr = gSystem->OpenDirectory(dirName);
255 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
256 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
260 const char* filename;
262 AliCDBRunRange aRunRange; // the runRange got from filename
263 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
265 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
267 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
269 TString aString(filename);
270 if (aString == "." || aString == "..") continue;
272 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
273 // aRunRange, aVersion, aSubVersion filled from filename
275 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
276 // aRunRange contains requested run!
278 if (result.GetVersion() < aVersion) {
279 result.SetVersion(aVersion);
280 result.SetSubVersion(aSubVersion);
283 aRunRange.GetFirstRun());
285 aRunRange.GetLastRun());
287 } else if (result.GetVersion() == aVersion
288 && result.GetSubVersion()
291 result.SetSubVersion(aSubVersion);
294 aRunRange.GetFirstRun());
296 aRunRange.GetLastRun());
297 } else if (result.GetVersion() == aVersion
298 && result.GetSubVersion() == aSubVersion){
299 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
300 query.GetFirstRun(), aVersion, aSubVersion));
301 gSystem->FreeDirectory(dirPtr);
306 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
308 result.SetVersion(query.GetVersion());
310 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
312 TString aString(filename);
313 if (aString == "." || aString == "..") continue;
315 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
316 // aRunRange, aVersion, aSubVersion filled from filename
318 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
319 // aRunRange contains requested run!
321 if(query.GetVersion() != aVersion) continue;
322 // aVersion is requested version!
324 if(result.GetSubVersion() == aSubVersion){
325 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
326 query.GetFirstRun(), aVersion, aSubVersion));
327 gSystem->FreeDirectory(dirPtr);
330 if( result.GetSubVersion() < aSubVersion) {
332 result.SetSubVersion(aSubVersion);
335 aRunRange.GetFirstRun());
337 aRunRange.GetLastRun());
341 } else { // both version and subversion specified
343 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
345 TString aString(filename);
346 if (aString == "." || aString == "..") continue;
348 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
349 // aRunRange, aVersion, aSubVersion filled from filename
351 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
352 // aRunRange contains requested run!
354 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
355 // aVersion and aSubVersion are requested version and subVersion!
357 if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
358 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
359 query.GetFirstRun(), aVersion, aSubVersion));
360 gSystem->FreeDirectory(dirPtr);
363 result.SetVersion(aVersion);
364 result.SetSubVersion(aSubVersion);
365 result.SetFirstRun(aRunRange.GetFirstRun());
366 result.SetLastRun(aRunRange.GetLastRun());
371 gSystem->FreeDirectory(dirPtr);
376 //_____________________________________________________________________________
377 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
378 // get AliCDBEntry from the database
380 AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
383 // look for a filename matching query requests (path, runRange, version, subVersion)
384 if (!queryId.HasVersion()) {
385 // if version is not specified, first check the selection criteria list
386 AliCDBId selectedId(queryId);
387 GetSelection(&selectedId);
388 result = GetId(selectedId, dataId);
390 result = GetId(queryId, dataId);
393 if (!result || !dataId.IsSpecified()) return NULL;
396 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),
397 dataId.GetSubVersion(), filename)) {
399 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
403 filename.Prepend((fBaseDirectory +'/' + queryId.GetPath() + '/'));
405 TFile file(filename, "READ"); // open file
406 if (!file.IsOpen()) {
407 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
411 // get the only AliCDBEntry object from the file
412 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
414 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
416 AliDebug(2,Form("Bad storage data: No AliCDBEntry in file!"));
421 AliCDBId entryId = anEntry->GetId();
423 // The object's Id are not reset during storage
424 // If object's Id runRange or version do not match with filename,
425 // it means that someone renamed file by hand. In this case a warning msg is issued.
427 anEntry-> SetLastStorage("local");
429 if(!((entryId.GetAliCDBRunRange()).IsEqual(& dataId.GetAliCDBRunRange())) ||
430 (entryId.GetVersion() != dataId.GetVersion()) || (entryId.GetSubVersion() != dataId.GetSubVersion())){
431 AliWarning(Form("Either object Id's RunRange or version do noth match with file name:"));
432 AliWarning("someone renamed file by hand!");
435 // close file, return retieved entry
440 //_____________________________________________________________________________
441 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
442 const AliCDBId& queryId, TList* result) {
443 // multiple request (AliCDBStorage::GetAll)
446 level0Dir += fBaseDirectory;
450 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
452 AliDebug(2,Form("Can't open level0 directory <%s>!",
458 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
460 TString level1Str(level1);
461 if (level1Str == "." || level1Str == "..") {
465 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
466 GetEntriesForLevel1(level0, level1, queryId, result);
470 gSystem->FreeDirectory(level0DirPtr);
473 //_____________________________________________________________________________
474 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
475 const AliCDBId& queryId, TList* result) {
476 // multiple request (AliCDBStorage::GetAll)
479 level1Dir += fBaseDirectory;
485 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
487 AliDebug(2,Form("Can't open level1 directory <%s>!",
493 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
495 TString level2Str(level2);
496 if (level2Str == "." || level2Str == "..") {
500 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
502 AliCDBPath entryPath(level0, level1, level2);
503 AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
504 queryId.GetVersion(), queryId.GetSubVersion());
506 AliCDBEntry* anEntry = GetEntry(entryId);
508 result->Add(anEntry);
513 gSystem->FreeDirectory(level1DirPtr);
516 //_____________________________________________________________________________
517 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
518 // multiple request (AliCDBStorage::GetAll)
520 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
521 if (!storageDirPtr) {
522 AliDebug(2,Form("Can't open storage directory <%s>",
523 fBaseDirectory.Data()));
527 TList* result = new TList();
531 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
533 TString level0Str(level0);
534 if (level0Str == "." || level0Str == "..") {
538 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
539 GetEntriesForLevel0(level0, queryId, result);
543 gSystem->FreeDirectory(storageDirPtr);
548 //_____________________________________________________________________________
549 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
550 // put an AliCDBEntry object into the database
552 AliCDBId& id = entry->GetId();
554 // set version and subVersion for the entry to be stored
555 if (!PrepareId(id)) return kFALSE;
558 // build filename from entry's id
560 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(),
561 id.GetSubVersion(), filename)) {
563 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
567 filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
570 TFile file(filename, "CREATE");
571 if (!file.IsOpen()) {
572 AliError(Form("Can't open file <%s>!", filename.Data()));
576 entry->SetVersion(id.GetVersion());
577 entry->SetSubVersion(id.GetSubVersion());
579 // write object (key name: "AliCDBEntry")
580 Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
581 if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
585 // TODO this is to make the SHUTTLE output lighter
586 if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
587 AliInfo(Form("CDB object stored into file %s",filename.Data()));
593 //_____________________________________________________________________________
594 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
596 TString fullFileName(fileName);
597 fullFileName.Prepend(fBaseDirectory+'/');
598 TFile *file = TFile::Open(fullFileName);
600 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
605 TList *list = new TList();
613 keycycle = "AliCDBId;";
616 id = (AliCDBId*) file->Get(keycycle);
620 file->Close(); delete file; file=0;
624 //_____________________________________________________________________________
625 Bool_t AliCDBLocal::Contains(const char* path) const{
626 // check for path in storage's fBaseDirectory
629 dirName += fBaseDirectory;
631 dirName += path; // dirName = fDBPath/path
632 Bool_t result=kFALSE;
634 void* dirPtr = gSystem->OpenDirectory(dirName);
635 if (dirPtr) result=kTRUE;
636 gSystem->FreeDirectory(dirPtr);
641 //_____________________________________________________________________________
642 void AliCDBLocal::QueryValidFiles()
648 /////////////////////////////////////////////////////////////////////////////////////////////////
650 // AliCDBLocal factory //
652 /////////////////////////////////////////////////////////////////////////////////////////////////
654 ClassImp(AliCDBLocalFactory)
656 //_____________________________________________________________________________
657 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
658 // check if the string is valid local URI
660 TRegexp dbPattern("^local://.+$");
662 return TString(dbString).Contains(dbPattern);
665 //_____________________________________________________________________________
666 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
667 // create AliCDBLocalParam class from the URI string
669 if (!Validate(dbString)) {
673 TString pathname(dbString + sizeof("local://") - 1);
675 gSystem->ExpandPathName(pathname);
677 if (pathname[0] != '/') {
678 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
681 return new AliCDBLocalParam(pathname);
684 //_____________________________________________________________________________
685 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
686 // create AliCDBLocal storage instance from parameters
688 if (AliCDBLocalParam::Class() == param->IsA()) {
690 const AliCDBLocalParam* localParam =
691 (const AliCDBLocalParam*) param;
693 return new AliCDBLocal(localParam->GetPath());
699 /////////////////////////////////////////////////////////////////////////////////////////////////
701 // AliCDBLocal Parameter class // //
703 /////////////////////////////////////////////////////////////////////////////////////////////////
705 ClassImp(AliCDBLocalParam)
707 //_____________________________________________________________________________
708 AliCDBLocalParam::AliCDBLocalParam():
712 // default constructor
716 //_____________________________________________________________________________
717 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
724 SetURI(TString("local://") + dbPath);
727 //_____________________________________________________________________________
728 AliCDBLocalParam::~AliCDBLocalParam() {
733 //_____________________________________________________________________________
734 AliCDBParam* AliCDBLocalParam::CloneParam() const {
737 return new AliCDBLocalParam(fDBPath);
740 //_____________________________________________________________________________
741 ULong_t AliCDBLocalParam::Hash() const {
742 // return Hash function
744 return fDBPath.Hash();
747 //_____________________________________________________________________________
748 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
749 // check if this object is equal to AliCDBParam obj
755 if (AliCDBLocalParam::Class() != obj->IsA()) {
759 AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
761 return fDBPath == other->fDBPath;