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