AliRoot Core  edcc906 (edcc906)
AliCDBLocal.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: The ALICE Off-line Project. *
5  * Contributors are mentioned in the code where appropriate. *
6  * *
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  **************************************************************************/
15 
17 // //
18 // AliCDBLocal //
19 // access class to a DataBase in a local storage //
20 // //
22 
23 #include <cstdlib>
24 #include <stdexcept>
25 #include <fstream>
26 
27 #include <TSystem.h>
28 #include <TObjString.h>
29 #include <TRegexp.h>
30 #include <TFile.h>
31 #include <TKey.h>
32 
33 #include "AliCDBLocal.h"
34 #include "AliCDBEntry.h"
35 #include "AliFileUtilities.h"
36 #include "AliLog.h"
37 using namespace std;
38 
39 ClassImp(AliCDBLocal)
40 
41 //_____________________________________________________________________________
43  fBaseDirectory(baseDir)
44 {
45  // constructor
46 
47  AliDebug(1, Form("fBaseDirectory = %s",fBaseDirectory.Data()));
48 
49  // check baseDire: trying to cd to baseDir; if it does not exist, create it
50  void* dir = gSystem->OpenDirectory(baseDir);
51  if (dir == NULL) {
52  if (gSystem->mkdir(baseDir, kTRUE)) {
53  AliError(Form("Can't open directory <%s>!", baseDir));
54  }
55 
56  } else {
57  AliDebug(2,Form("Folder <%s> found",fBaseDirectory.Data()));
58  gSystem->FreeDirectory(dir);
59  }
60  fType="local";
61  fBaseFolder = fBaseDirectory;
62 }
63 
64 //_____________________________________________________________________________
66 // destructor
67 
68 }
69 
70 
71 //_____________________________________________________________________________
72 Bool_t AliCDBLocal::FilenameToId(const char* filename, AliCDBRunRange& runRange,
73  Int_t& version, Int_t& subVersion) {
74  // build AliCDBId from filename numbers
75 
76  Ssiz_t mSize;
77 
78  // valid filename: Run#firstRun_#lastRun_v#version_s#subVersion.root
79  TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+.root$");
80  keyPattern.Index(filename, &mSize);
81  if (!mSize) {
82  AliDebug(2, Form("Bad filename <%s>.", filename));
83  return kFALSE;
84  }
85 
86  TString idString(filename);
87  idString.Resize(idString.Length() - sizeof(".root") + 1);
88 
89  TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
90 
91  TString firstRunString(((TObjString*) strArray->At(0))->GetString());
92  runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
93  runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
94 
95  TString verString(((TObjString*) strArray->At(2))->GetString());
96  version = atoi(verString.Data() + 1);
97 
98  TString subVerString(((TObjString*) strArray->At(3))->GetString());
99  subVersion = atoi(subVerString.Data() + 1);
100 
101  delete strArray;
102 
103  return kTRUE;
104 }
105 
106 
107 //_____________________________________________________________________________
108 Bool_t AliCDBLocal::IdToFilename(const AliCDBId& id, TString& filename) const {
109 // build file name from AliCDBId data (run range, version, subVersion)
110 
111  AliDebug(1, Form("fBaseDirectory = %s",fBaseDirectory.Data()));
112 
113  if (!id.GetAliCDBRunRange().IsValid()) {
114  AliDebug(2,Form("Invalid run range <%d, %d>.",
115  id.GetFirstRun(), id.GetLastRun()));
116  return kFALSE;
117  }
118 
119  if (id.GetVersion() < 0) {
120  AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
121  return kFALSE;
122  }
123 
124  if (id.GetSubVersion() < 0) {
125  AliDebug(2,Form("Invalid subversion <%d>.", id.GetSubVersion()));
126  return kFALSE;
127  }
128 
129  filename = Form("Run%d_%d_v%d_s%d.root", id.GetFirstRun(), id.GetLastRun(),
130  id.GetVersion(), id.GetSubVersion());
131 
132  filename.Prepend(fBaseDirectory +'/' + id.GetPath() + '/');
133 
134  return kTRUE;
135 }
136 
137 //_____________________________________________________________________________
139 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
140 
141  TString dirName = Form("%s/%s", fBaseDirectory.Data(), id.GetPath().Data());
142 
143  // go to the path; if directory does not exist, create it
144  void* dirPtr = gSystem->OpenDirectory(dirName);
145  if (!dirPtr) {
146  gSystem->mkdir(dirName, kTRUE);
147  dirPtr = gSystem->OpenDirectory(dirName);
148 
149  if (!dirPtr) {
150  AliError(Form("Can't create directory <%s>!",
151  dirName.Data()));
152  return kFALSE;
153  }
154  }
155 
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
161 
162  if (!id.HasVersion()) { // version not specified: look for highest version & subVersion
163 
164  while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
165 
166  TString aString(filename);
167  if (aString == "." || aString == "..") continue;
168 
169  if (!FilenameToId(filename, aRunRange, aVersion,
170  aSubVersion)) {
171  AliDebug(2,Form(
172  "Bad filename <%s>! I'll skip it.",
173  filename));
174  continue;
175  }
176 
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;
184  }
185 
186  id.SetVersion(lastVersion);
187  id.SetSubVersion(lastSubVersion + 1);
188 
189  } else { // version specified, look for highest subVersion only
190 
191  while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on the files
192 
193  TString aString(filename);
194  if (aString == "." || aString == "..") {
195  continue;
196  }
197 
198  if (!FilenameToId(filename, aRunRange, aVersion,
199  aSubVersion)) {
200  AliDebug(2,Form(
201  "Bad filename <%s>!I'll skip it.",
202  filename));
203  continue;
204  }
205 
206  if (aRunRange.Overlaps(id.GetAliCDBRunRange())
207  && aVersion == id.GetVersion()
208  && aSubVersion > lastSubVersion) {
209  lastSubVersion = aSubVersion;
210  lastRunRange = aRunRange;
211  }
212 
213  }
214 
215  id.SetSubVersion(lastSubVersion + 1);
216  }
217 
218  gSystem->FreeDirectory(dirPtr);
219 
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()));
225  return kFALSE;
226  }
227 
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));
234  }
235 
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));
240 
241  return kTRUE;
242 }
243 
244 
245 //_____________________________________________________________________________
247  // look for filename matching query (called by GetEntryId)
248 
249  // if querying for fRun and not specifying a version, look in the fValidFileIds list
250  if(!AliCDBManager::Instance()->GetCvmfsOcdbTag().IsNull() && query.GetFirstRun() == fRun && !query.HasVersion()) {
251  //if(query.GetFirstRun() == fRun && !query.HasVersion()) {
252  // get id from fValidFileIds
253  TIter iter(&fValidFileIds);
254 
255  AliCDBId *anIdPtr=0;
256  AliCDBId* result=0;
257 
258  while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
259  if(anIdPtr->GetPath() == query.GetPath()){
260  result = new AliCDBId(*anIdPtr);
261  break;
262  }
263  }
264  return result;
265  }
266 
267  // otherwise browse in the local filesystem CDB storage
268  TString dirName = Form("%s/%s", fBaseDirectory.Data(), query.GetPath().Data());
269 
270  void* dirPtr = gSystem->OpenDirectory(dirName);
271  if (!dirPtr) {
272  AliDebug(2,Form("Directory <%s> not found", (query.GetPath()).Data()));
273  AliDebug(2,Form("in DB folder %s", fBaseDirectory.Data()));
274  return NULL;
275  }
276 
277  const char* filename;
278  AliCDBId *result = new AliCDBId();
279  result->SetPath(query.GetPath());
280 
281  AliCDBRunRange aRunRange; // the runRange got from filename
282  Int_t aVersion, aSubVersion; // the version and subVersion got from filename
283 
284  if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
285 
286  while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
287 
288  TString aString(filename);
289  if (aString.BeginsWith('.')) continue;
290 
291  if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
292  // aRunRange, aVersion, aSubVersion filled from filename
293 
294  if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
295  // aRunRange contains requested run!
296 
297  AliDebug(1,Form("Filename %s matches\n",filename));
298 
299  if (result->GetVersion() < aVersion) {
300  result->SetVersion(aVersion);
301  result->SetSubVersion(aSubVersion);
302 
303  result->SetFirstRun(
304  aRunRange.GetFirstRun());
305  result->SetLastRun(
306  aRunRange.GetLastRun());
307 
308  } else if (result->GetVersion() == aVersion
309  && result->GetSubVersion()
310  < aSubVersion) {
311 
312  result->SetSubVersion(aSubVersion);
313 
314  result->SetFirstRun(
315  aRunRange.GetFirstRun());
316  result->SetLastRun(
317  aRunRange.GetLastRun());
318  } else if (result->GetVersion() == aVersion
319  && result->GetSubVersion() == aSubVersion){
320  AliError(Form("More than one object valid for run %d, version %d_%d!",
321  query.GetFirstRun(), aVersion, aSubVersion));
322  gSystem->FreeDirectory(dirPtr);
323  delete result;
324  return NULL;
325  }
326  }
327 
328  } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
329  result->SetVersion(query.GetVersion());
330 
331  while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
332 
333  TString aString(filename);
334  if (aString.BeginsWith('.')) continue;
335 
336  if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) continue;
337  // aRunRange, aVersion, aSubVersion filled from filename
338 
339  if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
340  // aRunRange contains requested run!
341 
342  if(query.GetVersion() != aVersion) continue;
343  // aVersion is requested version!
344 
345  if(result->GetSubVersion() == aSubVersion){
346  AliError(Form("More than one object valid for run %d, version %d_%d!",
347  query.GetFirstRun(), aVersion, aSubVersion));
348  gSystem->FreeDirectory(dirPtr);
349  delete result;
350  return NULL;
351  }
352  if( result->GetSubVersion() < aSubVersion) {
353 
354  result->SetSubVersion(aSubVersion);
355 
356  result->SetFirstRun(
357  aRunRange.GetFirstRun());
358  result->SetLastRun(
359  aRunRange.GetLastRun());
360  }
361  }
362 
363  } else { // both version and subversion specified
364 
365  //AliCDBId dataId(queryId.GetAliCDBPath(), -1, -1, -1, -1);
366  //Bool_t result;
367  while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
368 
369  TString aString(filename);
370  if (aString.BeginsWith('.')) continue;
371 
372  if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)){
373  AliDebug(5, Form("Could not make id from file: %s", filename));
374  continue;
375  }
376  // aRunRange, aVersion, aSubVersion filled from filename
377 
378  if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
379  // aRunRange contains requested run!
380 
381  if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion){
382  continue;
383  }
384  // aVersion and aSubVersion are requested version and subVersion!
385 
386  result->SetVersion(aVersion);
387  result->SetSubVersion(aSubVersion);
388  result->SetFirstRun(aRunRange.GetFirstRun());
389  result->SetLastRun(aRunRange.GetLastRun());
390  break;
391  }
392  }
393 
394  gSystem->FreeDirectory(dirPtr);
395 
396  return result;
397 }
398 
399 //_____________________________________________________________________________
401 // get AliCDBEntry from the storage (the CDB file matching the query is
402 // selected by GetEntryId and the contained AliCDBid is passed here)
403 
404  AliCDBId* dataId = GetEntryId(queryId);
405 
406  TString errMessage(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
407  if (!dataId || !dataId->IsSpecified()){
408  AliError(Form("No file found matching this id!"));
409  throw std::runtime_error(errMessage.Data());
410  return NULL;
411  }
412 
413  TString filename;
414  if (!IdToFilename(*dataId, filename)) {
415  AliError(Form("Bad data ID encountered!"));
416  delete dataId;
417  throw std::runtime_error(errMessage.Data());
418  return NULL;
419  }
420 
421  TFile file(filename, "READ"); // open file
422  if (!file.IsOpen()) {
423  AliError(Form("Can't open file <%s>!", filename.Data()));
424  delete dataId;
425  throw std::runtime_error(errMessage.Data());
426  return NULL;
427  }
428 
429  // get the only AliCDBEntry object from the file
430  // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
431 
432  AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file.Get("AliCDBEntry"));
433  if (!anEntry) {
434  AliError(Form("Bad storage data: No AliCDBEntry in file!"));
435  file.Close();
436  delete dataId;
437  throw std::runtime_error(errMessage.Data());
438  return NULL;
439  }
440 
441  AliCDBId& entryId = anEntry->GetId();
442 
443  // The object's Id are not reset during storage
444  // If object's Id runRange or version do not match with filename,
445  // it means that someone renamed file by hand. In this case a warning msg is issued.
446 
447  anEntry-> SetLastStorage("local");
448 
449  if(!entryId.IsEqual(dataId)){
450  AliWarning(Form("Mismatch between file name and object's Id!"));
451  AliWarning(Form("File name: %s", dataId->ToString().Data()));
452  AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
453  }
454 
455  // Check whether entry contains a TTree. In case load the tree in memory!
456  LoadTreeFromFile(anEntry);
457 
458  // close file, return retrieved entry
459  file.Close();
460  delete dataId;
461 
462  return anEntry;
463 }
464 
465 //_____________________________________________________________________________
467 // get AliCDBId from the storage
468 // Via GetId, select the CDB file matching the query and return
469 // the contained AliCDBId
470 
471  AliCDBId* dataId = 0;
472 
473  // look for a filename matching query requests (path, runRange, version, subVersion)
474  if (!queryId.HasVersion()) {
475  // if version is not specified, first check the selection criteria list
476  AliCDBId selectedId(queryId);
477  GetSelection(&selectedId);
478  dataId = GetId(selectedId);
479  } else {
480  dataId = GetId(queryId);
481  }
482 
483  if (dataId && !dataId->IsSpecified()) {
484  delete dataId;
485  return NULL;
486  }
487 
488  return dataId;
489 }
490 
491 //_____________________________________________________________________________
492 void AliCDBLocal::GetEntriesForLevel0(const char* level0,
493  const AliCDBId& queryId, TList* result) {
494  // multiple request (AliCDBStorage::GetAll)
495 
496  TString level0Dir = Form("%s/%s", fBaseDirectory.Data(), level0);
497 
498  void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
499  if (!level0DirPtr) {
500  AliDebug(2,Form("Can't open level0 directory <%s>!",
501  level0Dir.Data()));
502  return;
503  }
504 
505  const char* level1;
506  Long_t flag=0;
507  while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
508 
509  TString level1Str(level1);
510  // skip directories starting with a dot (".svn" and similar in old svn working copies)
511  if (level1Str.BeginsWith('.')) {
512  continue;
513  }
514 
515  TString fullPath = Form("%s/%s",level0Dir.Data(), level1);
516 
517  Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
518 
519  if(res){
520  AliDebug(2, Form("Error reading entry %s !",level1Str.Data()));
521  continue;
522  }
523  if(!(flag&2)) continue; // bit 1 of flag = directory!
524 
525  if (queryId.GetAliCDBPath().Level1Comprises(level1)) {
526  GetEntriesForLevel1(level0, level1, queryId, result);
527  }
528  }
529 
530  gSystem->FreeDirectory(level0DirPtr);
531 }
532 
533 //_____________________________________________________________________________
534 void AliCDBLocal::GetEntriesForLevel1(const char* level0, const char* level1,
535  const AliCDBId& queryId, TList* result) {
536  // multiple request (AliCDBStorage::GetAll)
537 
538  TString level1Dir = Form("%s/%s/%s", fBaseDirectory.Data(), level0,level1);
539 
540  void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
541  if (!level1DirPtr) {
542  AliDebug(2,Form("Can't open level1 directory <%s>!",
543  level1Dir.Data()));
544  return;
545  }
546 
547  const char* level2;
548  Long_t flag=0;
549  while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
550 
551  TString level2Str(level2);
552  // skip directories starting with a dot (".svn" and similar in old svn working copies)
553  if (level2Str.BeginsWith('.')) {
554  continue;
555  }
556 
557  TString fullPath = Form("%s/%s",level1Dir.Data(), level2);
558 
559  Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
560 
561  if(res){
562  AliDebug(2, Form("Error reading entry %s !",level2Str.Data()));
563  continue;
564  }
565  if(!(flag&2)) continue; // skip if not a directory
566 
567  if (queryId.GetAliCDBPath().Level2Comprises(level2)) {
568 
569  AliCDBPath entryPath(level0, level1, level2);
570 
571  AliCDBId entryId(entryPath, queryId.GetAliCDBRunRange(),
572  queryId.GetVersion(), queryId.GetSubVersion());
573 
574  // check filenames to see if any includes queryId.GetAliCDBRunRange()
575  void* level2DirPtr = gSystem->OpenDirectory(fullPath);
576  if (!level2DirPtr) {
577  AliDebug(2,Form("Can't open level2 directory <%s>!", fullPath.Data()));
578  return;
579  }
580  const char* level3;
581  Long_t file_flag=0;
582  while ((level3 = gSystem->GetDirEntry(level2DirPtr))) {
583  TString fileName(level3);
584  TString fullFileName = Form("%s/%s", fullPath.Data(), level3);
585 
586  Int_t file_res=gSystem->GetPathInfo(fullFileName.Data(), 0, (Long64_t*) 0, &file_flag, 0);
587 
588  if(file_res){
589  AliDebug(2, Form("Error reading entry %s !",level2Str.Data()));
590  continue;
591  }
592  if(file_flag)
593  continue; // it is not a regular file!
594 
595  // skip if result already contains an entry for this path
596  Bool_t alreadyLoaded = kFALSE;
597  Int_t nEntries = result->GetEntries();
598  for(int i=0; i<nEntries; i++){
599  AliCDBEntry *lEntry = (AliCDBEntry*) result->At(i);
600  AliCDBId lId = lEntry->GetId();
601  TString lPath = lId.GetPath();
602  if(lPath.EqualTo(entryPath.GetPath())){
603  alreadyLoaded = kTRUE;
604  break;
605  }
606  }
607  if (alreadyLoaded) continue;
608 
609  //skip filenames not matching the regex below
610  TRegexp re("^Run[0-9]+_[0-9]+_");
611  if(!fileName.Contains(re))
612  continue;
613  // Extract first- and last-run and version and subversion.
614  // This allows to avoid quering for a calibration path if we did not find a filename with
615  // run-range including the one specified in the query and
616  // with version, subversion matching the query
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();
627  AliCDBRunRange rr(firstRun,lastRun);
628  Int_t version = versionStr.Atoi();
629  Int_t subVersion = subvStr.Atoi();
630 
631  AliCDBEntry* anEntry = 0;
632  Bool_t versionOK = kTRUE, subVersionOK = kTRUE;
633  if ( queryId.HasVersion() && version!=queryId.GetVersion())
634  versionOK = kFALSE;
635  if ( queryId.HasSubVersion() && subVersion!=queryId.GetSubVersion())
636  subVersionOK = kFALSE;
637  if (rr.Comprises(queryId.GetAliCDBRunRange()) && versionOK && subVersionOK )
638  {
639  anEntry = GetEntry(entryId);
640  result->Add(anEntry);
641  }
642  }
643  }
644  }
645 
646  gSystem->FreeDirectory(level1DirPtr);
647 }
648 
649 //_____________________________________________________________________________
650 TList* AliCDBLocal::GetEntries(const AliCDBId& queryId) {
651 // multiple request (AliCDBStorage::GetAll)
652 
653  TList* result = new TList();
654  result->SetOwner();
655 
656  // if querying for fRun and not specifying a version, look in the fValidFileIds list
657  if(queryId.GetFirstRun() == fRun && !queryId.HasVersion()) {
658  // get id from fValidFileIds
659  TIter *iter = new TIter(&fValidFileIds);
660  TObjArray selectedIds;
661  selectedIds.SetOwner(1);
662 
663  // loop on list of valid Ids to select the right version to get.
664  // According to query and to the selection criteria list, version can be the highest or exact
665  AliCDBId* anIdPtr=0;
666  AliCDBId* dataId=0;
667  AliCDBPath queryPath = queryId.GetAliCDBPath();
668  while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
669  AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
670  if(!(queryPath.Comprises(thisCDBPath))){
671  continue;
672  }
673 
674  AliCDBId thisId(*anIdPtr);
675  dataId = GetId(thisId);
676  if(dataId)
677  selectedIds.Add(dataId);
678  }
679 
680  delete iter; iter=0;
681 
682  // selectedIds contains the Ids of the files matching all requests of query!
683  // All the objects are now ready to be retrieved
684  iter = new TIter(&selectedIds);
685  while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
686  AliCDBEntry* anEntry = GetEntry(*anIdPtr);
687  if(anEntry) result->Add(anEntry);
688  }
689  delete iter; iter=0;
690  return result;
691  }
692 
693  void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
694  if (!storageDirPtr) {
695  AliDebug(2,Form("Can't open storage directory <%s>",
696  fBaseDirectory.Data()));
697  return NULL;
698  }
699 
700  const char* level0;
701  Long_t flag=0;
702  while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
703 
704  TString level0Str(level0);
705  // skip directories starting with a dot (".svn" and similar in old svn working copies)
706  if (level0Str.BeginsWith('.')) {
707  continue;
708  }
709 
710  TString fullPath = Form("%s/%s",fBaseDirectory.Data(), level0);
711 
712  Int_t res=gSystem->GetPathInfo(fullPath.Data(), 0, (Long64_t*) 0, &flag, 0);
713 
714  if(res){
715  AliDebug(2, Form("Error reading entry %s !",level0Str.Data()));
716  continue;
717  }
718 
719  if(!(flag&2)) continue; // bit 1 of flag = directory!
720 
721  if (queryId.GetAliCDBPath().Level0Comprises(level0)) {
722  GetEntriesForLevel0(level0, queryId, result);
723  }
724  }
725 
726  gSystem->FreeDirectory(storageDirPtr);
727 
728  return result;
729 }
730 
731 //_____________________________________________________________________________
732 Bool_t AliCDBLocal::PutEntry(AliCDBEntry* entry, const char* mirrors) {
733 // put an AliCDBEntry object into the database
734 
735  AliCDBId& id = entry->GetId();
736 
737  // set version and subVersion for the entry to be stored
738  if (!PrepareId(id)) return kFALSE;
739 
740 
741  // build filename from entry's id
742  TString filename="";
743  if (!IdToFilename(id, filename)) {
744 
745  AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
746  return kFALSE;
747  }
748 
749  TString mirrorsString(mirrors);
750  if(!mirrorsString.IsNull())
751  AliWarning("AliCDBLocal storage cannot take mirror SEs into account. They will be ignored.");
752 
753  // open file
754  TFile file(filename, "CREATE");
755  if (!file.IsOpen()) {
756  AliError(Form("Can't open file <%s>!", filename.Data()));
757  return kFALSE;
758  }
759 
760  //SetTreeToFile(entry, &file);
761 
762  entry->SetVersion(id.GetVersion());
763  entry->SetSubVersion(id.GetSubVersion());
764 
765  // write object (key name: "AliCDBEntry")
766  Bool_t result = file.WriteTObject(entry, "AliCDBEntry");
767  if (!result) AliDebug(2,Form("Can't write entry to file: %s", filename.Data()));
768 
769  file.Close();
770  if(result) {
771  if(!(id.GetPath().Contains("SHUTTLE/STATUS")))
772  AliInfo(Form("CDB object stored into file %s",filename.Data()));
773  }
774 
775  return result;
776 }
777 
778 //_____________________________________________________________________________
780 
781  TString fullFileName(fileName);
782  fullFileName.Prepend(fBaseDirectory+'/');
783  TFile *file = TFile::Open(fullFileName);
784  if (!file) {
785  AliError(Form("Can't open selection file <%s>!", fullFileName.Data()));
786  return NULL;
787  }
788  file->cd();
789 
790  TList *list = new TList();
791  list->SetOwner();
792  int i=0;
793  TString keycycle;
794 
795  AliCDBId *id;
796  while(1){
797  i++;
798  keycycle = "AliCDBId;";
799  keycycle+=i;
800 
801  id = (AliCDBId*) file->Get(keycycle);
802  if(!id) break;
803  list->AddFirst(id);
804  }
805  file->Close(); delete file; file=0;
806  return list;
807 }
808 
809 //_____________________________________________________________________________
810 Bool_t AliCDBLocal::Contains(const char* path) const{
811 // check for path in storage's fBaseDirectory
812 
813  TString dirName = Form("%s/%s", fBaseDirectory.Data(), path);
814  Bool_t result=kFALSE;
815 
816  void* dirPtr = gSystem->OpenDirectory(dirName);
817  if (dirPtr) result=kTRUE;
818  gSystem->FreeDirectory(dirPtr);
819 
820  return result;
821 }
822 
823 //_____________________________________________________________________________
825 // Query the CDB for files valid for AliCDBStorage::fRun.
826 // Fills list fValidFileIds with AliCDBId objects extracted from CDB files
827 // present in the local storage.
828 // If fVersion was not set, fValidFileIds is filled with highest versions.
829 
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;
834  }
835  void* storageDirPtr = gSystem->OpenDirectory(fBaseDirectory);
836 
837  const char* level0;
838  while ((level0 = gSystem->GetDirEntry(storageDirPtr))) {
839 
840  TString level0Str(level0);
841  if (level0Str.BeginsWith(".")) {
842  continue;
843  }
844 
845  if (fPathFilter.Level0Comprises(level0)) {
846  TString level0Dir = Form("%s/%s",fBaseDirectory.Data(),level0);
847  void* level0DirPtr = gSystem->OpenDirectory(level0Dir);
848  const char* level1;
849  while ((level1 = gSystem->GetDirEntry(level0DirPtr))) {
850 
851  TString level1Str(level1);
852  if (level1Str.BeginsWith(".")) {
853  continue;
854  }
855 
856  if (fPathFilter.Level1Comprises(level1)) {
857  TString level1Dir = Form("%s/%s/%s",
858  fBaseDirectory.Data(),level0,level1);
859 
860  void* level1DirPtr = gSystem->OpenDirectory(level1Dir);
861  const char* level2;
862  while ((level2 = gSystem->GetDirEntry(level1DirPtr))) {
863 
864  TString level2Str(level2);
865  if (level2Str.BeginsWith(".")) {
866  continue;
867  }
868 
869  if (fPathFilter.Level2Comprises(level2)) {
870  TString dirName = Form("%s/%s/%s/%s", fBaseDirectory.Data(), level0, level1, level2);
871 
872  void* dirPtr = gSystem->OpenDirectory(dirName);
873 
874  const char* filename;
875 
876  AliCDBRunRange aRunRange; // the runRange got from filename
877  AliCDBRunRange hvRunRange; // the runRange of the highest version valid file
878  Int_t aVersion, aSubVersion; // the version and subVersion got from filename
879  Int_t highestV=-1, highestSubV=-1; // the highest version and subVersion for this calibration type
880 
881  while ((filename = gSystem->GetDirEntry(dirPtr))) { // loop on files
882 
883  TString aString(filename);
884  if (aString.BeginsWith(".")) continue;
885 
886  if (!FilenameToId(filename, aRunRange, aVersion, aSubVersion)) {
887  continue;
888  }
889 
890  AliCDBRunRange runrg(fRun, fRun);
891  if (!aRunRange.Comprises(runrg))
892  continue;
893 
894  // check to keep the highest version/subversion (in case of more than one)
895  if (aVersion > highestV) {
896  highestV = aVersion;
897  highestSubV = aSubVersion;
898  hvRunRange = aRunRange;
899  } else if (aVersion == highestV) {
900  if (aSubVersion > highestSubV) {
901  highestSubV = aSubVersion;
902  hvRunRange = aRunRange;
903  }
904  }
905  }
906  if(highestV >= 0){
907  AliCDBPath validPath(level0, level1, level2);
908  AliCDBId *validId = new AliCDBId(validPath, hvRunRange, highestV, highestSubV);
909  fValidFileIds.AddLast(validId);
910  }
911 
912  gSystem->FreeDirectory(dirPtr);
913  }
914  }
915  gSystem->FreeDirectory(level1DirPtr);
916  }
917  }
918  gSystem->FreeDirectory(level0DirPtr);
919  }
920  }
921  gSystem->FreeDirectory(storageDirPtr);
922 
923 }
924 
926 // //
927 // AliCDBLocal factory //
928 // //
930 
931 ClassImp(AliCDBLocalFactory)
932 
933 //_____________________________________________________________________________
934 Bool_t AliCDBLocalFactory::Validate(const char* dbString) {
935 // check if the string is valid local URI
936 
937  TRegexp dbPatternLocal("^local://.+$");
938 
939  return (TString(dbString).Contains(dbPatternLocal) || TString(dbString).BeginsWith("snapshot://folder="));
940 }
941 
942 //_____________________________________________________________________________
944 // create AliCDBLocalParam class from the URI string
945 
946  if (!Validate(dbString)) {
947  return NULL;
948  }
949 
950  TString checkSS(dbString);
951  if(checkSS.BeginsWith("snapshot://"))
952  {
953  TString snapshotPath("OCDB");
954  snapshotPath.Prepend(TString(gSystem->WorkingDirectory()) + '/');
955  checkSS.Remove(0,checkSS.First(':')+3);
956  return new AliCDBLocalParam(snapshotPath,checkSS);
957  }
958 
959  // if the string argument is not a snapshot URI, than it is a plain local URI
960  TString pathname(dbString + sizeof("local://") - 1);
961 
962  if(gSystem->ExpandPathName(pathname))
963  return NULL;
964 
965  if (pathname[0] != '/') {
966  pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
967  }
968  //pathname.Prepend("local://");
969 
970  return new AliCDBLocalParam(pathname);
971 }
972 
973 //_____________________________________________________________________________
975 // create AliCDBLocal storage instance from parameters
976 
977  if (AliCDBLocalParam::Class() == param->IsA()) {
978 
979  const AliCDBLocalParam* localParam =
980  (const AliCDBLocalParam*) param;
981 
982  return new AliCDBLocal(localParam->GetPath());
983  }
984 
985  return NULL;
986 }
987 //_____________________________________________________________________________
988 void AliCDBLocal::SetRetry(Int_t /* nretry */, Int_t /* initsec */) {
989 
990  // Function to set the exponential retry for putting entries in the OCDB
991 
992  AliInfo("This function sets the exponential retry for putting entries in the OCDB - to be used ONLY for AliCDBGrid --> returning without doing anything");
993  return;
994 }
995 
996 
997 
999 // //
1000 // AliCDBLocal Parameter class // //
1001 // //
1003 
1004 ClassImp(AliCDBLocalParam)
1005 
1006  //_____________________________________________________________________________
1008  AliCDBParam(),
1009  fDBPath()
1010 {
1011  // default constructor
1012 
1013 }
1014 
1015 //_____________________________________________________________________________
1017  AliCDBParam(),
1018  fDBPath(dbPath)
1019 {
1020  // constructor
1021 
1022  SetType("local");
1023  SetURI(TString("local://") + dbPath);
1024 }
1025 
1026 //_____________________________________________________________________________
1027 AliCDBLocalParam::AliCDBLocalParam(const char* dbPath, const char* uri):
1028  AliCDBParam(),
1029  fDBPath(dbPath)
1030 {
1031  // constructor
1032 
1033  SetType("local");
1034  SetURI(TString("alien://") + uri);
1035 }
1036 
1037 //_____________________________________________________________________________
1039  // destructor
1040 
1041 }
1042 
1043 //_____________________________________________________________________________
1045  // clone parameter
1046 
1047  return new AliCDBLocalParam(fDBPath);
1048 }
1049 
1050 //_____________________________________________________________________________
1051 ULong_t AliCDBLocalParam::Hash() const {
1052  // return Hash function
1053 
1054  return fDBPath.Hash();
1055 }
1056 
1057 //_____________________________________________________________________________
1058 Bool_t AliCDBLocalParam::IsEqual(const TObject* obj) const {
1059  // check if this object is equal to AliCDBParam obj
1060 
1061  if (this == obj) {
1062  return kTRUE;
1063  }
1064 
1065  if (AliCDBLocalParam::Class() != obj->IsA()) {
1066  return kFALSE;
1067  }
1068 
1069  AliCDBLocalParam* other = (AliCDBLocalParam*) obj;
1070 
1071  return fDBPath == other->fDBPath;
1072 }
1073 
void SetFirstRun(Int_t firstRun)
Definition: AliCDBId.h:50
void SetURI(const char *uri)
TFile * Open(const char *filename, Long64_t &nevents)
const TString & GetPath() const
Definition: AliCDBPath.h:38
void SetVersion(Int_t version)
Definition: AliCDBEntry.h:67
Bool_t HasSubVersion() const
Definition: AliCDBId.h:71
Bool_t Comprises(const AliCDBPath &other) const
Definition: AliCDBPath.cxx:250
virtual Bool_t Contains(const char *path) const
#define TObjArray
void SetPath(const char *path)
Definition: AliCDBId.h:44
Bool_t Level1Comprises(const TString &str) const
Definition: AliCDBPath.cxx:228
virtual AliCDBEntry * GetEntry(const AliCDBId &queryId)
const char * path
virtual AliCDBId * GetEntryId(const AliCDBId &queryId)
virtual AliCDBStorage * Create(const AliCDBParam *param)
Bool_t Overlaps(const AliCDBRunRange &other) const
void SetSubVersion(Int_t subVersion)
Definition: AliCDBId.h:62
const AliCDBPath & GetAliCDBPath() const
Definition: AliCDBId.h:39
virtual Bool_t IsEqual(const TObject *obj) const
Int_t GetVersion() const
Definition: AliCDBId.h:59
AliCDBId * GetId(const AliCDBId &query)
const AliCDBRunRange & GetAliCDBRunRange() const
Definition: AliCDBId.h:46
Bool_t Level2Comprises(const TString &str) const
Definition: AliCDBPath.cxx:239
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)
Definition: AliLog.h:541
Bool_t Level0Comprises(const TString &str) const
Definition: AliCDBPath.cxx:217
virtual TList * GetEntries(const AliCDBId &queryId)
virtual Bool_t IsEqual(const TObject *obj) const
virtual AliCDBParam * CloneParam() const
void SetVersion(Int_t version)
Definition: AliCDBId.h:61
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
Definition: AliCDBId.cxx:147
#define AliInfo(message)
Definition: AliLog.h:484
Int_t GetFirstRun() const
TString baseDir
Definition: UnitTest.C:38
virtual Bool_t IdToFilename(const AliCDBId &id, TString &filename) const
Int_t GetLastRun() const
Definition: AliCDBEntry.h:18
TString ToString() const
Definition: AliCDBId.cxx:163
virtual AliCDBParam * CreateParameter(const char *dbString)
Bool_t IsAnyRange() const
#define AliDebug(logLevel, message)
Definition: AliLog.h:300
void SetType(const char *type)
Bool_t HasVersion() const
Definition: AliCDBId.h:70
virtual ~AliCDBLocal()
Definition: AliCDBLocal.cxx:65
const TString & GetPath() const
Definition: AliCDBId.h:40
Bool_t IsSpecified() const
Definition: AliCDBId.h:68
virtual void SetRetry(Int_t, Int_t)
virtual ~AliCDBLocalParam()
#define AliError(message)
Definition: AliLog.h:591
AliCDBId & GetId()
Definition: AliCDBEntry.h:51
static AliCDBManager * Instance(TMap *entryCache=NULL, Int_t run=-1)
void res(Char_t i)
Definition: Resolution.C:2
void SetLastRun(Int_t lastRun)
Definition: AliCDBId.h:51
Bool_t Comprises(const AliCDBRunRange &other) const
Int_t GetFirstRun() const
Definition: AliCDBId.h:48
void GetEntriesForLevel1(const char *level0, const char *Level1, const AliCDBId &query, TList *result)
void SetSubVersion(Int_t subVersion)
Definition: AliCDBEntry.h:68
virtual TList * GetIdListFromFile(const char *fileName)
const TString & GetPath() const
Definition: AliCDBLocal.h:95
Bool_t FilenameToId(const char *filename, AliCDBRunRange &runRange, Int_t &version, Int_t &subVersion)
Definition: AliCDBLocal.cxx:72
void SetLastRun(Int_t lastRun)
virtual ULong_t Hash() const
Int_t GetSubVersion() const
Definition: AliCDBId.h:60