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);
54 //_____________________________________________________________________________
55 AliCDBLocal::~AliCDBLocal() {
61 //_____________________________________________________________________________
62 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
63 Int_t& version, Int_t& subVersion) {
64 // build AliCDBId from filename numbers
69 // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root
70 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
71 keyPattern.Index(filename, &mSize);
73 AliDebug(2, Form("Bad filename <%s>.", filename));
77 TString idString(filename);
78 idString.Resize(idString.Length() - sizeof(".root") + 1);
80 TObjArray* strArray = (TObjArray*) idString.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);
98 //_____________________________________________________________________________
99 Bool_t AliCDBLocal::IdToFilename(const AliCDBRunRange& runRange, Int_t version,
100 Int_t subVersion, TString& filename) {
101 // build file name from AliCDBId data (run range, version, subVersion)
103 if (!runRange.IsValid()) {
104 AliDebug(2,Form("Invalid run range <%d, %d>.",
105 runRange.GetFirstRun(), runRange.GetLastRun()));
110 AliDebug(2,Form("Invalid version <%d>.", version));
114 if (subVersion < 0) {
115 AliDebug(2,Form("Invalid subversion <%s>.", subVersion));
120 filename += runRange.GetFirstRun();
122 filename += runRange.GetLastRun();
126 filename += subVersion;
132 //_____________________________________________________________________________
133 Bool_t AliCDBLocal::PrepareId(AliCDBId& id) {
134 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
137 dirName += fBaseDirectory;
139 dirName += id.GetPath();
141 // go to the path; if directory does not exist, create it
142 void* dirPtr = gSystem->OpenDirectory(dirName);
144 gSystem->mkdir(dirName, kTRUE);
145 dirPtr = gSystem->OpenDirectory(dirName);
148 AliError(Form("Can't create directory <%s>!",
154 const char* filename;
155 AliCDBRunRange aRunRange; // the runRange got from filename
156 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
157 Int_t aVersion, aSubVersion; // the version subVersion got from filename
158 Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
160 if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
162 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
164 TString aString(filename);
165 if (aString == "." || aString == "..") continue;
167 if (!FilenameToId(filename, aRunRange, aVersion,
170 "Bad filename <%s>! I'll skip it.",
175 if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
176 if(aVersion < lastVersion) continue;
177 if(aVersion > lastVersion) lastSubVersion = -1;
178 if(aSubVersion < lastSubVersion) continue;
179 lastVersion = aVersion;
180 lastSubVersion = aSubVersion;
181 lastRunRange = aRunRange;
184 id.SetVersion(lastVersion);
185 id.SetSubVersion(lastSubVersion + 1);
187 } else { // version specified, look for highest subVersion only
189 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
191 TString aString(filename);
192 if (aString == "." || aString == "..") {
196 if (!FilenameToId(filename, aRunRange, aVersion,
199 "Bad filename <%s>!I'll skip it.",
204 if (aRunRange.Overlaps(id.GetAliCDBRunRange())
205 && aVersion == id.GetVersion()
206 && aSubVersion > lastSubVersion) {
207 lastSubVersion = aSubVersion;
208 lastRunRange = aRunRange;
213 id.SetSubVersion(lastSubVersion + 1);
216 gSystem->FreeDirectory(dirPtr);
218 TString lastStorage = id.GetLastStorage();
219 if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
220 id.GetSubVersion() > 0 ){
221 AliError(Form("Grid to Local Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
222 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
226 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
227 id.GetSubVersion() > 0 ){
228 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
229 id.GetVersion(),id.GetSubVersion()));
230 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
231 id.GetVersion(),id.GetSubVersion()-1));
234 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange())))
235 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
236 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(),
237 id.GetVersion(), id.GetSubVersion()-1));
242 //_____________________________________________________________________________
243 Bool_t AliCDBLocal::GetId(const AliCDBId& query, AliCDBId& result) {
244 // look for filename matching query (called by GetEntry)
247 dirName += fBaseDirectory;
249 dirName += query.GetPath(); // dirName = fDBPath/idPath
251 void* dirPtr = gSystem->OpenDirectory(dirName);
253 AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
254 AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
258 const char* filename;
260 AliCDBRunRange aRunRange; // the runRange got from filename
261 Int_t aVersion, aSubVersion; // the version and subVersion got from filename
263 if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
265 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
267 TString aString(filename);
268 if (aString == "." || aString == "..") continue;
270 if (!FilenameToId(filename, 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 gSystem->FreeDirectory(dirPtr);
304 } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
306 result.SetVersion(query.GetVersion());
308 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
310 TString aString(filename);
311 if (aString == "." || aString == "..") continue;
313 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
314 // aRunRange, aVersion, aSubVersion filled from filename
316 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
317 // aRunRange contains requested run!
319 if(query.GetVersion() != aVersion) continue;
320 // aVersion is requested version!
322 if(result.GetSubVersion() == aSubVersion){
323 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
324 query.GetFirstRun(), aVersion, aSubVersion));
325 gSystem->FreeDirectory(dirPtr);
328 if( result.GetSubVersion() < aSubVersion) {
330 result.SetSubVersion(aSubVersion);
333 aRunRange.GetFirstRun());
335 aRunRange.GetLastRun());
339 } else { // both version and subversion specified
341 while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
343 TString aString(filename);
344 if (aString == "." || aString == "..") continue;
346 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
347 // aRunRange, aVersion, aSubVersion filled from filename
349 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
350 // aRunRange contains requested run!
352 if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
353 // aVersion and aSubVersion are requested version and subVersion!
355 if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
356 AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
357 query.GetFirstRun(), aVersion, aSubVersion));
358 gSystem->FreeDirectory(dirPtr);
361 result.SetVersion(aVersion);
362 result.SetSubVersion(aSubVersion);
363 result.SetFirstRun(aRunRange.GetFirstRun());
364 result.SetLastRun(aRunRange.GetLastRun());
369 gSystem->FreeDirectory(dirPtr);
374 //_____________________________________________________________________________
375 AliCDBEntry* AliCDBLocal::GetEntry(const AliCDBId& queryId) {
376 // get AliCDBEntry from the database
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()) return NULL;
394 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),
395 dataId.GetSubVersion(), filename)) {
397 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
401 filename.Prepend((fBaseDirectory +'/' + queryId.GetPath() + '/'));
403 TFile file(filename, "READ"); // open file
404 if (!file.IsOpen()) {
405 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
409 // get the only AliCDBEntry object from the file
410 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
412 TObject* anObject = file.Get("AliCDBEntry");
414 AliDebug(2,Form("Bad storage data: NULL entry object!"));
418 if (AliCDBEntry::Class() != anObject->IsA()) {
419 AliDebug(2,Form("Bad storage data: Invalid entry object!"));
423 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
425 // The object's Id are not reset during storage
426 // If object's Id runRange or version do not match with filename,
427 // it means that someone renamed file by hand. In this case a warning msg is issued.
429 ((AliCDBEntry*) anObject)-> SetLastStorage("local");
431 if(!((entryId.GetAliCDBRunRange()).IsEqual(& dataId.GetAliCDBRunRange())) ||
432 (entryId.GetVersion() != dataId.GetVersion()) || (entryId.GetSubVersion() != dataId.GetSubVersion())){
433 AliWarning(Form("Either object Id's RunRange or version do noth match with file name:"));
434 AliWarning("someone renamed file by hand!");
437 // close file, return retieved entry
439 return (AliCDBEntry*) anObject;
442 //_____________________________________________________________________________
443 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
444 const AliCDBId& queryId, TList* result) {
445 // multiple request (AliCDBStorage::GetAll)
448 level0Dir += fBaseDirectory;
452 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
454 AliDebug(2,Form("Can't open level0 directory <%s>!",
460 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
462 TString level1Str(level1);
463 if (level1Str == "." || level1Str == "..") {
467 if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
468 GetEntriesForLevel1(level0, level1, queryId, result);
472 gSystem->FreeDirectory(level0DirPtr);
475 //_____________________________________________________________________________
476 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
477 const AliCDBId& queryId, TList* result) {
478 // multiple request (AliCDBStorage::GetAll)
481 level1Dir += fBaseDirectory;
487 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
489 AliDebug(2,Form("Can't open level1 directory <%s>!",
495 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
497 TString level2Str(level2);
498 if (level2Str == "." || level2Str == "..") {
502 if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
504 AliCDBPath entryPath(level0, level1, level2);
505 AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
506 queryId.GetVersion(), queryId.GetSubVersion());
508 AliCDBEntry* anEntry = GetEntry(entryId);
510 result->Add(anEntry);
515 gSystem->FreeDirectory(level1DirPtr);
518 //_____________________________________________________________________________
519 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
520 // multiple request (AliCDBStorage::GetAll)
522 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
523 if (!storageDirPtr) {
524 AliDebug(2,Form("Can't open storage directory <%s>",
525 fBaseDirectory.Data()));
529 TList* result = new TList();
533 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
535 TString level0Str(level0);
536 if (level0Str == "." || level0Str == "..") {
540 if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
541 GetEntriesForLevel0(level0, queryId, result);
545 gSystem->FreeDirectory(storageDirPtr);
550 //_____________________________________________________________________________
551 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry) {
552 // put an AliCDBEntry object into the database
554 AliCDBId& id = entry->GetId();
556 // set version and subVersion for the entry to be stored
557 if (!PrepareId(id)) return kFALSE;
560 // build filename from entry's id
562 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(),
563 id.GetSubVersion(), filename)) {
565 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
569 filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
572 TFile file(filename, "CREATE");
573 if (!file.IsOpen()) {
574 AliError(Form("Can't open file <%s>!", filename.Data()));
578 entry->SetVersion(id.GetVersion());
579 entry->SetSubVersion(id.GetSubVersion());
581 // write object (key name: "AliCDBEntry")
582 Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
583 if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
586 if(result) AliInfo(Form("CDB object stored into file %s",filename.Data()));
591 //_____________________________________________________________________________
592 TList* AliCDBLocal::GetIdListFromFile(const char* fileName){
594 TString fullFileName(fileName);
595 fullFileName.Prepend(fBaseDirectory+'/');
596 TFile *file = TFile::Open(fullFileName);
598 AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
603 TList *list = new TList();
611 keycycle = "AliCDBId;";
614 id = (AliCDBId*) file->Get(keycycle);
618 file->Close(); delete file; file=0;
622 //_____________________________________________________________________________
623 Bool_t AliCDBLocal::Contains(const char* path) const{
624 // check for path in storage's fBaseDirectory
627 dirName += fBaseDirectory;
629 dirName += path; // dirName = fDBPath/path
630 Bool_t result=kFALSE;
632 void* dirPtr = gSystem->OpenDirectory(dirName);
633 if (dirPtr) result=kTRUE;
634 gSystem->FreeDirectory(dirPtr);
639 /////////////////////////////////////////////////////////////////////////////////////////////////
641 // AliCDBLocal factory //
643 /////////////////////////////////////////////////////////////////////////////////////////////////
645 ClassImp(AliCDBLocalFactory)
647 //_____________________________________________________________________________
648 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
649 // check if the string is valid local URI
651 TRegexp dbPattern("^local://.+$");
653 return TString(dbString).Contains(dbPattern);
656 //_____________________________________________________________________________
657 AliCDBParam* AliCDBLocalFactory::CreateParameter(const char* dbString) {
658 // create AliCDBLocalParam class from the URI string
660 if (!Validate(dbString)) {
664 TString pathname(dbString + sizeof("local://") - 1);
666 gSystem->ExpandPathName(pathname);
668 if (pathname[0] != '/') {
669 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
672 return new AliCDBLocalParam(pathname);
675 //_____________________________________________________________________________
676 AliCDBStorage* AliCDBLocalFactory::Create(const AliCDBParam* param) {
677 // create AliCDBLocal storage instance from parameters
679 if (AliCDBLocalParam::Class() == param->IsA()) {
681 const AliCDBLocalParam* localParam =
682 (const AliCDBLocalParam*) param;
684 return new AliCDBLocal(localParam->GetPath());
690 /////////////////////////////////////////////////////////////////////////////////////////////////
692 // AliCDBLocal Parameter class // //
694 /////////////////////////////////////////////////////////////////////////////////////////////////
696 ClassImp(AliCDBLocalParam)
698 //_____________________________________________________________________________
699 AliCDBLocalParam::AliCDBLocalParam() {
700 // default constructor
704 //_____________________________________________________________________________
705 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath):
711 SetURI(TString("local://") + dbPath);
714 //_____________________________________________________________________________
715 AliCDBLocalParam::~AliCDBLocalParam() {
720 //_____________________________________________________________________________
721 AliCDBParam* AliCDBLocalParam::CloneParam() const {
724 return new AliCDBLocalParam(fDBPath);
727 //_____________________________________________________________________________
728 ULong_t AliCDBLocalParam::Hash() const {
729 // return Hash function
731 return fDBPath.Hash();
734 //_____________________________________________________________________________
735 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
736 // check if this object is equal to AliCDBParam obj
742 if (AliCDBLocalParam::Class() != obj->IsA()) {
746 AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
748 return fDBPath == other->fDBPath;