28 #include <TObjString.h> 47 AliDebug(1, Form(
"fBaseDirectory = %s",fBaseDirectory.Data()));
50 void* dir = gSystem->OpenDirectory(baseDir);
52 if (gSystem->mkdir(baseDir, kTRUE)) {
53 AliError(Form(
"Can't open directory <%s>!", baseDir));
57 AliDebug(2,Form(
"Folder <%s> found",fBaseDirectory.Data()));
58 gSystem->FreeDirectory(dir);
61 fBaseFolder = fBaseDirectory;
73 Int_t& version, Int_t& subVersion) {
79 TRegexp keyPattern(
"^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
80 keyPattern.Index(filename, &mSize);
82 AliDebug(2, Form(
"Bad filename <%s>.", filename));
86 TString idString(filename);
87 idString.Resize(idString.Length() -
sizeof(
".root") + 1);
91 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
92 runRange.
SetFirstRun(atoi(firstRunString.Data() + 3));
93 runRange.
SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
95 TString verString(((TObjString*) strArray->At(2))->GetString());
96 version = atoi(verString.Data() + 1);
98 TString subVerString(((TObjString*) strArray->At(3))->GetString());
99 subVersion = atoi(subVerString.Data() + 1);
111 AliDebug(1, Form(
"fBaseDirectory = %s",fBaseDirectory.Data()));
113 if (!
id.GetAliCDBRunRange().IsValid()) {
114 AliDebug(2,Form(
"Invalid run range <%d, %d>.",
115 id.GetFirstRun(),
id.GetLastRun()));
119 if (
id.GetVersion() < 0) {
120 AliDebug(2,Form(
"Invalid version <%d>.",
id.GetVersion()));
124 if (
id.GetSubVersion() < 0) {
125 AliDebug(2,Form(
"Invalid subversion <%d>.",
id.GetSubVersion()));
129 filename = Form(
"Run%d_%d_v%d_s%d.root",
id.GetFirstRun(),
id.GetLastRun(),
130 id.GetVersion(),
id.GetSubVersion());
132 filename.Prepend(fBaseDirectory +
'/' +
id.GetPath() +
'/');
141 TString dirName = Form(
"%s/%s", fBaseDirectory.Data(),
id.GetPath().Data());
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;
159 Int_t aVersion, aSubVersion;
160 Int_t lastVersion = 0, lastSubVersion = -1;
162 if (!
id.HasVersion()) {
164 while ((filename = gSystem->GetDirEntry(dirPtr))) {
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);
191 while ((filename = gSystem->GetDirEntry(dirPtr))) {
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)",
239 id.GetVersion(),
id.GetSubVersion()-1));
253 TIter iter(&fValidFileIds);
258 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
268 TString dirName = Form(
"%s/%s", fBaseDirectory.Data(), query.
GetPath().Data());
270 void* dirPtr = gSystem->OpenDirectory(dirName);
272 AliDebug(2,Form(
"Directory <%s> not found", (query.
GetPath()).Data()));
273 AliDebug(2,Form(
"in DB folder %s", fBaseDirectory.Data()));
277 const char* filename;
282 Int_t aVersion, aSubVersion;
286 while ((filename = gSystem->GetDirEntry(dirPtr))) {
288 TString aString(filename);
289 if (aString.BeginsWith(
'.'))
continue;
291 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion))
continue;
297 AliDebug(1,Form(
"Filename %s matches\n",filename));
320 AliError(Form(
"More than one object valid for run %d, version %d_%d!",
322 gSystem->FreeDirectory(dirPtr);
331 while ((filename = gSystem->GetDirEntry(dirPtr))) {
333 TString aString(filename);
334 if (aString.BeginsWith(
'.'))
continue;
336 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion))
continue;
346 AliError(Form(
"More than one object valid for run %d, version %d_%d!",
348 gSystem->FreeDirectory(dirPtr);
367 while ((filename = gSystem->GetDirEntry(dirPtr))) {
369 TString aString(filename);
370 if (aString.BeginsWith(
'.'))
continue;
372 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)){
373 AliDebug(5, Form(
"Could not make id from file: %s", filename));
394 gSystem->FreeDirectory(dirPtr);
404 AliCDBId* dataId = GetEntryId(queryId);
406 TString errMessage(TString::Format(
"No valid CDB object found! request was: %s", queryId.
ToString().Data()));
408 AliError(Form(
"No file found matching this id!"));
409 throw std::runtime_error(errMessage.Data());
414 if (!IdToFilename(*dataId, filename)) {
415 AliError(Form(
"Bad data ID encountered!"));
417 throw std::runtime_error(errMessage.Data());
421 TFile file(filename,
"READ");
422 if (!file.IsOpen()) {
423 AliError(Form(
"Can't open file <%s>!", filename.Data()));
425 throw std::runtime_error(errMessage.Data());
434 AliError(Form(
"Bad storage data: No AliCDBEntry in file!"));
437 throw std::runtime_error(errMessage.Data());
447 anEntry-> SetLastStorage(
"local");
450 AliWarning(Form(
"Mismatch between file name and object's Id!"));
456 LoadTreeFromFile(anEntry);
477 GetSelection(&selectedId);
478 dataId = GetId(selectedId);
480 dataId = GetId(queryId);
493 const AliCDBId& queryId, TList* result) {
496 TString level0Dir = Form(
"%s/%s", fBaseDirectory.Data(), level0);
498 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
500 AliDebug(2,Form(
"Can't open level0 directory <%s>!",
507 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
509 TString level1Str(level1);
511 if (level1Str.BeginsWith(
'.')) {
515 TString fullPath = Form(
"%s/%s",level0Dir.Data(), level1);
517 Int_t
res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
520 AliDebug(2, Form(
"Error reading entry %s !",level1Str.Data()));
523 if(!(flag&2))
continue;
526 GetEntriesForLevel1(level0, level1, queryId, result);
530 gSystem->FreeDirectory(level0DirPtr);
535 const AliCDBId& queryId, TList* result) {
538 TString level1Dir = Form(
"%s/%s/%s", fBaseDirectory.Data(), level0,level1);
540 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
542 AliDebug(2,Form(
"Can't open level1 directory <%s>!",
549 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
551 TString level2Str(level2);
553 if (level2Str.BeginsWith(
'.')) {
557 TString fullPath = Form(
"%s/%s",level1Dir.Data(), level2);
559 Int_t
res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
562 AliDebug(2, Form(
"Error reading entry %s !",level2Str.Data()));
565 if(!(flag&2))
continue;
575 void* level2DirPtr = gSystem->OpenDirectory(fullPath);
577 AliDebug(2,Form(
"Can't open level2 directory <%s>!", fullPath.Data()));
582 while ((level3 = gSystem->GetDirEntry(level2DirPtr))) {
584 TString fullFileName = Form(
"%s/%s", fullPath.Data(), level3);
586 Int_t file_res=gSystem->GetPathInfo(fullFileName.Data(), 0, (Long64_t*) 0, &file_flag, 0);
589 AliDebug(2, Form(
"Error reading entry %s !",level2Str.Data()));
596 Bool_t alreadyLoaded = kFALSE;
597 Int_t nEntries = result->GetEntries();
598 for(
int i=0; i<nEntries; i++){
602 if(lPath.EqualTo(entryPath.
GetPath())){
603 alreadyLoaded = kTRUE;
607 if (alreadyLoaded)
continue;
610 TRegexp re(
"^Run[0-9]+_[0-9]+_");
611 if(!fileName.Contains(re))
617 TString fn =
fileName( 3, fileName.Length()-3 );
618 TString firstRunStr = fn( 0, fn.First(
'_') );
619 fn.Remove( 0, firstRunStr.Length()+1 );
620 TString lastRunStr = fn( 0, fn.First(
'_') );
621 fn.Remove( 0, lastRunStr.Length()+1 );
622 TString versionStr = fn( 1, fn.First(
'_')-1 );
623 fn.Remove( 0, versionStr.Length()+2 );
624 TString subvStr = fn(1, fn.First(
'.')-1);
625 Int_t firstRun = firstRunStr.Atoi();
626 Int_t lastRun = lastRunStr.Atoi();
628 Int_t version = versionStr.Atoi();
629 Int_t subVersion = subvStr.Atoi();
632 Bool_t versionOK = kTRUE, subVersionOK = kTRUE;
636 subVersionOK = kFALSE;
639 anEntry = GetEntry(entryId);
640 result->Add(anEntry);
646 gSystem->FreeDirectory(level1DirPtr);
653 TList* result =
new TList();
659 TIter *iter =
new TIter(&fValidFileIds);
661 selectedIds.SetOwner(1);
668 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
675 dataId = GetId(thisId);
677 selectedIds.Add(dataId);
684 iter =
new TIter(&selectedIds);
685 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
687 if(anEntry) result->Add(anEntry);
693 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
694 if (!storageDirPtr) {
695 AliDebug(2,Form(
"Can't open storage directory <%s>",
696 fBaseDirectory.Data()));
702 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
704 TString level0Str(level0);
706 if (level0Str.BeginsWith(
'.')) {
710 TString fullPath = Form(
"%s/%s",fBaseDirectory.Data(), level0);
712 Int_t
res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
715 AliDebug(2, Form(
"Error reading entry %s !",level0Str.Data()));
719 if(!(flag&2))
continue;
722 GetEntriesForLevel0(level0, queryId, result);
726 gSystem->FreeDirectory(storageDirPtr);
738 if (!PrepareId(
id))
return kFALSE;
743 if (!IdToFilename(
id, filename)) {
745 AliDebug(2,Form(
"Bad ID encountered! Subnormal error!"));
749 TString mirrorsString(mirrors);
750 if(!mirrorsString.IsNull())
751 AliWarning(
"AliCDBLocal storage cannot take mirror SEs into account. They will be ignored.");
754 TFile file(filename,
"CREATE");
755 if (!file.IsOpen()) {
756 AliError(Form(
"Can't open file <%s>!", filename.Data()));
766 Bool_t result = file.WriteTObject(entry,
"AliCDBEntry");
767 if (!result)
AliDebug(2,Form(
"Can't write entry to file: %s", filename.Data()));
771 if(!(
id.GetPath().Contains(
"SHUTTLE/STATUS")))
772 AliInfo(Form(
"CDB object stored into file %s",filename.Data()));
781 TString fullFileName(fileName);
782 fullFileName.Prepend(fBaseDirectory+
'/');
785 AliError(Form(
"Can't open selection file <%s>!", fullFileName.Data()));
790 TList *list =
new TList();
798 keycycle =
"AliCDBId;";
801 id = (
AliCDBId*) file->Get(keycycle);
805 file->Close();
delete file; file=0;
813 TString dirName = Form(
"%s/%s", fBaseDirectory.Data(),
path);
814 Bool_t result=kFALSE;
816 void* dirPtr = gSystem->OpenDirectory(dirName);
817 if (dirPtr) result=kTRUE;
818 gSystem->FreeDirectory(dirPtr);
830 if(fVersion != -1)
AliWarning (
"Version parameter is not used by local storage query!");
831 if(fMetaDataFilter) {
832 AliWarning (
"CDB meta data parameters are not used by local storage query!");
833 delete fMetaDataFilter; fMetaDataFilter=0;
835 void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
838 while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
840 TString level0Str(level0);
841 if (level0Str.BeginsWith(
".")) {
845 if (fPathFilter.Level0Comprises(level0)) {
846 TString level0Dir = Form(
"%s/%s",fBaseDirectory.Data(),level0);
847 void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
849 while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
851 TString level1Str(level1);
852 if (level1Str.BeginsWith(
".")) {
856 if (fPathFilter.Level1Comprises(level1)) {
857 TString level1Dir = Form(
"%s/%s/%s",
858 fBaseDirectory.Data(),level0,level1);
860 void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
862 while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
864 TString level2Str(level2);
865 if (level2Str.BeginsWith(
".")) {
869 if (fPathFilter.Level2Comprises(level2)) {
870 TString dirName = Form(
"%s/%s/%s/%s", fBaseDirectory.Data(), level0, level1, level2);
872 void* dirPtr = gSystem->OpenDirectory(dirName);
874 const char* filename;
878 Int_t aVersion, aSubVersion;
879 Int_t highestV=-1, highestSubV=-1;
881 while ((filename = gSystem->GetDirEntry(dirPtr))) {
883 TString aString(filename);
884 if (aString.BeginsWith(
"."))
continue;
886 if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) {
895 if (aVersion > highestV) {
897 highestSubV = aSubVersion;
898 hvRunRange = aRunRange;
899 }
else if (aVersion == highestV) {
900 if (aSubVersion > highestSubV) {
901 highestSubV = aSubVersion;
902 hvRunRange = aRunRange;
908 AliCDBId *validId =
new AliCDBId(validPath, hvRunRange, highestV, highestSubV);
909 fValidFileIds.AddLast(validId);
912 gSystem->FreeDirectory(dirPtr);
915 gSystem->FreeDirectory(level1DirPtr);
918 gSystem->FreeDirectory(level0DirPtr);
921 gSystem->FreeDirectory(storageDirPtr);
937 TRegexp dbPatternLocal(
"^local://.+$");
939 return (TString(dbString).Contains(dbPatternLocal) || TString(dbString).BeginsWith(
"snapshot://folder="));
946 if (!Validate(dbString)) {
950 TString checkSS(dbString);
951 if(checkSS.BeginsWith(
"snapshot://"))
953 TString snapshotPath(
"OCDB");
954 snapshotPath.Prepend(TString(gSystem->WorkingDirectory()) +
'/');
955 checkSS.Remove(0,checkSS.First(
':')+3);
960 TString pathname(dbString +
sizeof(
"local://") - 1);
962 if(gSystem->ExpandPathName(pathname))
965 if (pathname[0] !=
'/') {
966 pathname.Prepend(TString(gSystem->WorkingDirectory()) +
'/');
977 if (AliCDBLocalParam::Class() == param->IsA()) {
992 AliInfo(
"This function sets the exponential retry for putting entries in the OCDB - to be used ONLY for AliCDBGrid --> returning without doing anything");
1023 SetURI(TString(
"local://") + dbPath);
1034 SetURI(TString(
"alien://") + uri);
1065 if (AliCDBLocalParam::Class() != obj->IsA()) {
void SetFirstRun(Int_t firstRun)
void SetURI(const char *uri)
TFile * Open(const char *filename, Long64_t &nevents)
const TString & GetPath() const
void SetVersion(Int_t version)
Bool_t HasSubVersion() const
Bool_t Comprises(const AliCDBPath &other) const
virtual Bool_t Contains(const char *path) const
void SetPath(const char *path)
Bool_t Level1Comprises(const TString &str) const
virtual AliCDBEntry * GetEntry(const AliCDBId &queryId)
virtual AliCDBId * GetEntryId(const AliCDBId &queryId)
virtual AliCDBStorage * Create(const AliCDBParam *param)
Bool_t Overlaps(const AliCDBRunRange &other) const
void SetSubVersion(Int_t subVersion)
const AliCDBPath & GetAliCDBPath() const
virtual Bool_t IsEqual(const TObject *obj) const
AliCDBId * GetId(const AliCDBId &query)
const AliCDBRunRange & GetAliCDBRunRange() const
Bool_t Level2Comprises(const TString &str) const
virtual Bool_t PutEntry(AliCDBEntry *entry, const char *mirrors="")
virtual void QueryValidFiles()
TString fileName(const char *dir, int runNumber, const char *da, int i, const char *type)
#define AliWarning(message)
Bool_t Level0Comprises(const TString &str) const
virtual TList * GetEntries(const AliCDBId &queryId)
virtual Bool_t IsEqual(const TObject *obj) const
virtual AliCDBParam * CloneParam() const
void SetVersion(Int_t version)
void GetEntriesForLevel0(const char *level0, const AliCDBId &query, TList *result)
void SetFirstRun(Int_t firstRun)
Bool_t PrepareId(AliCDBId &id)
virtual Bool_t IsEqual(const TObject *obj) const
Int_t GetFirstRun() const
virtual Bool_t IdToFilename(const AliCDBId &id, TString &filename) const
virtual AliCDBParam * CreateParameter(const char *dbString)
Bool_t IsAnyRange() const
#define AliDebug(logLevel, message)
void SetType(const char *type)
Bool_t HasVersion() const
const TString & GetPath() const
Bool_t IsSpecified() const
virtual void SetRetry(Int_t, Int_t)
virtual ~AliCDBLocalParam()
#define AliError(message)
static AliCDBManager * Instance(TMap *entryCache=NULL, Int_t run=-1)
void SetLastRun(Int_t lastRun)
Bool_t Comprises(const AliCDBRunRange &other) const
Int_t GetFirstRun() const
void GetEntriesForLevel1(const char *level0, const char *Level1, const AliCDBId &query, TList *result)
void SetSubVersion(Int_t subVersion)
virtual TList * GetIdListFromFile(const char *fileName)
const TString & GetPath() const
Bool_t FilenameToId(const char *filename, AliCDBRunRange &runRange, Int_t &version, Int_t &subVersion)
void SetLastRun(Int_t lastRun)
virtual ULong_t Hash() const
Int_t GetSubVersion() const