Adding the possibility to specify SEs for mirroring when putting an object on AliEn
[u/mrichter/AliRoot.git] / STEER / CDB / AliCDBDump.cxx
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
16 /////////////////////////////////////////////////////////////////////
17 //                                                                 //
18 //  class AliCDBDump                                               //
19 //  access class to a DataBase in a dump storage (single file)     //
20 //                                                                 //
21 /////////////////////////////////////////////////////////////////////
22
23 #include <cstdlib>
24 #include <TSystem.h>
25 #include <TKey.h>
26 #include <TFile.h>
27 #include <TRegexp.h>
28 #include <TObjString.h>
29 #include <TList.h>
30
31 #include "AliCDBDump.h"
32 #include "AliCDBEntry.h"
33 #include "AliLog.h"
34
35 ClassImp(AliCDBDump)
36
37 //_____________________________________________________________________________
38 AliCDBDump::AliCDBDump(const char* dbFile, Bool_t readOnly):
39 fFile(NULL), fReadOnly(readOnly) {
40 // constructor
41
42         // opening file
43         fFile = TFile::Open(dbFile, fReadOnly ? "READ" : "UPDATE");     
44         if (!fFile) {
45                 AliError(Form("Can't open file <%s>!" , dbFile));
46         } else {
47                 AliDebug(2,Form("File <%s> opened",dbFile));
48                 if(fReadOnly) AliDebug(2,Form("in read-only mode"));
49         }
50
51         fType="dump";
52         fBaseFolder = dbFile;
53 }
54
55 //_____________________________________________________________________________
56 AliCDBDump::~AliCDBDump() {
57 // destructor
58
59         if (fFile) {
60                 fFile->Close();
61                 delete fFile;
62         }
63 }
64
65
66 //_____________________________________________________________________________
67 Bool_t AliCDBDump::KeyNameToId(const char* keyname, AliCDBRunRange& runRange,
68         Int_t& version, Int_t& subVersion) {
69 // build AliCDBId from keyname numbers
70
71         Ssiz_t mSize;
72
73         // valid keyname: Run#firstRun_#lastRun_v#version_s#subVersion.root
74         TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s[0-9]+$");
75         keyPattern.Index(keyname, &mSize);
76         if (!mSize) {
77                 AliDebug(2,Form("Bad keyname <%s>.", keyname));
78                 return kFALSE;
79         }
80
81         TObjArray* strArray = (TObjArray*) TString(keyname).Tokenize("_");
82
83         TString firstRunString(((TObjString*) strArray->At(0))->GetString());
84         runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
85         runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
86         
87         TString verString(((TObjString*) strArray->At(2))->GetString());
88         version = atoi(verString.Data() + 1);
89
90         TString subVerString(((TObjString*) strArray->At(3))->GetString());
91         subVersion = atoi(subVerString.Data() + 1);
92
93         delete strArray;
94
95         return kTRUE;
96 }
97
98 //_____________________________________________________________________________
99 Bool_t AliCDBDump::IdToKeyName(const AliCDBRunRange& runRange, Int_t version,
100         Int_t subVersion, TString& keyname) {
101 // build key name from AliCDBId data (run range, version, subVersion)
102
103         if (!runRange.IsValid()) {
104                 AliDebug(2,Form("Invalid run range <%d, %d>.",
105                         runRange.GetFirstRun(), runRange.GetLastRun()));
106                 return kFALSE;
107         }
108
109         if (version < 0) {
110                 AliDebug(2,Form("Invalid version <%d>.", version));
111                 return kFALSE;
112         }
113
114         if (subVersion < 0) {
115                 AliDebug(2,Form("Invalid subversion <%d>.", subVersion));
116                 return kFALSE;
117         }
118     
119         keyname += "Run";
120         keyname += runRange.GetFirstRun();
121         keyname += "_";
122         keyname += runRange.GetLastRun();
123         keyname += "_v";
124         keyname += version;
125         keyname += "_s";
126         keyname += subVersion;
127
128         return kTRUE;
129 }
130
131 //_____________________________________________________________________________
132 Bool_t AliCDBDump::MkDir(const TString& path) {
133 // descend into TDirectory, making TDirectories if they don't exist 
134         TObjArray* strArray = (TObjArray*) path.Tokenize("/");
135         
136         TIter iter(strArray);
137         TObjString* str;
138
139         while ((str = (TObjString*) iter.Next())) {
140                 
141                 TString dirName(str->GetString());
142                 if (!dirName.Length()) {
143                         continue;
144                 }
145
146                 if (gDirectory->cd(dirName)) {
147                         continue;
148                 }
149
150                 TDirectory* aDir = gDirectory->mkdir(dirName, "");
151                 if (!aDir) {
152                         AliError(Form("Can't create directory <%s>!", 
153                                         dirName.Data()));
154                         delete strArray;
155
156                         return kFALSE;
157                 }
158
159                 aDir->cd();
160         }       
161         
162         delete strArray;
163
164         return kTRUE;
165 }
166
167 //_____________________________________________________________________________
168 Bool_t AliCDBDump::PrepareId(AliCDBId& id) {
169 // prepare id (version, subVersion) of the object that will be stored (called by PutEntry)
170
171         AliCDBRunRange aRunRange; // the runRange got from filename 
172         AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
173         Int_t aVersion, aSubVersion; // the version subVersion got from filename
174         Int_t lastVersion = 0, lastSubVersion = -1; // highest version and subVersion found
175
176         
177         TIter iter(gDirectory->GetListOfKeys());
178         TKey* key;
179
180         if (!id.HasVersion()) { // version not specified: look for highest version & subVersion 
181                                 
182                 while ((key = (TKey*) iter.Next())) { // loop on keys
183
184                         const char* keyName = key->GetName();
185         
186                         if (!KeyNameToId(keyName, aRunRange, aVersion, 
187                            aSubVersion)) {
188                                 AliDebug(2,Form(
189                                         "Bad keyname <%s>!I'll skip it.", keyName));
190                                 continue;
191                         }
192                         
193                         if (!aRunRange.Overlaps(id.GetAliCDBRunRange())) continue;
194                         if(aVersion < lastVersion) continue;
195                         if(aVersion > lastVersion) lastSubVersion = -1;
196                         if(aSubVersion < lastSubVersion) continue;
197                         lastVersion = aVersion;
198                         lastSubVersion = aSubVersion;
199                         lastRunRange = aRunRange;
200                 }
201
202                 id.SetVersion(lastVersion);
203                 id.SetSubVersion(lastSubVersion + 1);
204
205         } else { // version specified, look for highest subVersion only
206                 
207                 while ((key = (TKey*) iter.Next())) { // loop on the keys
208
209                         const char* keyName = key->GetName();
210         
211                         if (!KeyNameToId(keyName, aRunRange, aVersion, 
212                            aSubVersion)) {
213                                 AliDebug(2,Form(
214                                         "Bad keyname <%s>!I'll skip it.", keyName));
215                                 continue;
216                         }
217
218                         if (aRunRange.Overlaps(id.GetAliCDBRunRange()) 
219                                 && aVersion == id.GetVersion()
220                                 && aSubVersion > lastSubVersion) {
221                                 lastSubVersion = aSubVersion;
222                                 lastRunRange = aRunRange;
223                         }
224         
225                 }
226                 
227                 id.SetSubVersion(lastSubVersion + 1);
228         }
229
230         TString lastStorage = id.GetLastStorage();
231         if(lastStorage.Contains(TString("grid"), TString::kIgnoreCase) &&
232            id.GetSubVersion() > 0 ){
233                 AliError(Form("Grid to Dump Storage error! local object with version v%d_s%d found:",id.GetVersion(), id.GetSubVersion()-1));
234                 AliError(Form("This object has been already transferred from Grid (check v%d_s0)!",id.GetVersion()));
235                 return kFALSE;
236         }
237
238         if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) &&
239            id.GetSubVersion() > 0 ){
240                 AliDebug(2, Form("A NEW object is being stored with version v%d_s%d",
241                                         id.GetVersion(),id.GetSubVersion()));
242                 AliDebug(2, Form("and it will hide previously stored object with v%d_s%d!",
243                                         id.GetVersion(),id.GetSubVersion()-1));
244         }
245
246         if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(& id.GetAliCDBRunRange()))) 
247                 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d_s%d)",
248                         lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), 
249                         id.GetVersion(), id.GetSubVersion()-1));
250
251         return kTRUE;
252
253 }
254
255 // //_____________________________________________________________________________
256 // Bool_t AliCDBDump::GetId(const AliCDBId& query, AliCDBId& result) {
257 // // look for filename matching query (called by GetEntry)
258 // 
259 // 
260 //         AliCDBRunRange aRunRange; // the runRange got from filename
261 //         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
262 // 
263 //      TIter iter(gDirectory->GetListOfKeys());
264 //      TKey* key;
265 // 
266 //      if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
267 // 
268 //                 while ((key = (TKey*) iter.Next())) { // loop on the keys
269 // 
270 //                      if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
271 //                         // aRunRange, aVersion, aSubVersion filled from filename
272 // 
273 //                      if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
274 //                      // aRunRange contains requested run!
275 // 
276 //                      if (result.GetVersion() < aVersion) {
277 //                              result.SetVersion(aVersion);
278 //                              result.SetSubVersion(aSubVersion);
279 // 
280 //                              result.SetFirstRun(
281 //                                      aRunRange.GetFirstRun());
282 //                              result.SetLastRun(
283 //                                      aRunRange.GetLastRun());
284 // 
285 //                      } else if (result.GetVersion() == aVersion
286 //                              && result.GetSubVersion()
287 //                                      < aSubVersion) {
288 // 
289 //                              result.SetSubVersion(aSubVersion);
290 // 
291 //                              result.SetFirstRun(
292 //                                      aRunRange.GetFirstRun());
293 //                              result.SetLastRun(
294 //                                      aRunRange.GetLastRun());
295 //                      } else if (result.GetVersion() == aVersion
296 //                              && result.GetSubVersion() == aSubVersion){
297 //                                      AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
298 //                                      query.GetFirstRun(), aVersion, aSubVersion));
299 //                              result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
300 //                              return kFALSE;
301 //                              }
302 //              }
303 // 
304 //      } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
305 // 
306 //              result.SetVersion(query.GetVersion());
307 // 
308 //                 while ((key = (TKey*) iter.Next())) { // loop on the keys
309 // 
310 //                      if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
311 //                         // aRunRange, aVersion, aSubVersion filled from filename
312 // 
313 //                        if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
314 //                      // aRunRange contains requested run!
315 //                      
316 //                      if(query.GetVersion() != aVersion) continue;
317 //                      // aVersion is requested version!
318 // 
319 //                      if(result.GetSubVersion() == aSubVersion){
320 //                                      AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
321 //                                      query.GetFirstRun(), aVersion, aSubVersion));
322 //                              result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
323 //                              return kFALSE;
324 //                      }
325 //                      if( result.GetSubVersion() < aSubVersion) {
326 // 
327 //                                 result.SetSubVersion(aSubVersion);
328 // 
329 //                                 result.SetFirstRun(
330 //                                      aRunRange.GetFirstRun());
331 //                                 result.SetLastRun(
332 //                                      aRunRange.GetLastRun());
333 //                      }
334 //                 }
335 // 
336 //      } else { // both version and subversion specified
337 // 
338 //                 while ((key = (TKey*) iter.Next())) { // loop on the keys
339 // 
340 //                      if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
341 //                         // aRunRange, aVersion, aSubVersion filled from filename
342 // 
343 //                      if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
344 //                      // aRunRange contains requested run!
345 // 
346 //                      if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
347 //                      // aVersion and aSubVersion are requested version and subVersion!
348 // 
349 //                      if(result.GetVersion() == aVersion && result.GetSubVersion() == aSubVersion){
350 //                                      AliDebug(2,Form("More than one object valid for run %d, version %d_%d!",
351 //                                      query.GetFirstRun(), aVersion, aSubVersion));
352 //                              result.SetRunRange(-1,-1); result.SetVersion(-1); result.SetSubVersion(-1);
353 //                              return kFALSE; 
354 //                      }
355 //                      result.SetVersion(aVersion);
356 //                      result.SetSubVersion(aSubVersion);
357 //                      result.SetFirstRun(aRunRange.GetFirstRun());
358 //                      result.SetLastRun(aRunRange.GetLastRun());
359 // 
360 //              }
361 //      }
362 // 
363 //      return kTRUE;
364 // }
365
366 //_____________________________________________________________________________
367 AliCDBId* AliCDBDump::GetId(const AliCDBId& query) {
368 // look for filename matching query (called by GetEntry)
369
370
371         AliCDBRunRange aRunRange; // the runRange got from filename
372         Int_t aVersion, aSubVersion; // the version and subVersion got from filename
373
374         TIter iter(gDirectory->GetListOfKeys());
375         TKey* key;
376
377         AliCDBId* result = new AliCDBId();
378         result->SetPath(query.GetPath());
379
380         if (!query.HasVersion()) { // neither version and subversion specified -> look for highest version and subVersion
381
382                 while ((key = (TKey*) iter.Next())) { // loop on the keys
383                         
384                         if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
385                         // aRunRange, aVersion, aSubVersion filled from filename
386
387                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
388                         // aRunRange contains requested run!
389
390                         if (result->GetVersion() < aVersion) {
391                                 result->SetVersion(aVersion);
392                                 result->SetSubVersion(aSubVersion);
393
394                                 result->SetFirstRun(
395                                         aRunRange.GetFirstRun());
396                                 result->SetLastRun(
397                                         aRunRange.GetLastRun());
398
399                         } else if (result->GetVersion() == aVersion
400                                 && result->GetSubVersion()
401                                         < aSubVersion) {
402
403                                 result->SetSubVersion(aSubVersion);
404
405                                 result->SetFirstRun(
406                                         aRunRange.GetFirstRun());
407                                 result->SetLastRun(
408                                         aRunRange.GetLastRun());
409                         } else if (result->GetVersion() == aVersion
410                                 && result->GetSubVersion() == aSubVersion){
411                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
412                                         query.GetFirstRun(), aVersion, aSubVersion));
413                                 delete result;
414                                 return NULL;
415                                 }
416                 }
417
418         } else if (!query.HasSubVersion()) { // version specified but not subversion -> look for highest subVersion
419
420                 result->SetVersion(query.GetVersion());
421
422                 while ((key = (TKey*) iter.Next())) { // loop on the keys
423
424                         if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
425                         // aRunRange, aVersion, aSubVersion filled from filename
426
427                        if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue; 
428                         // aRunRange contains requested run!
429                         
430                         if(query.GetVersion() != aVersion) continue;
431                         // aVersion is requested version!
432
433                         if(result->GetSubVersion() == aSubVersion){
434                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
435                                         query.GetFirstRun(), aVersion, aSubVersion));
436                                 delete result;
437                                 return NULL;
438                         }
439                         if( result->GetSubVersion() < aSubVersion) {
440
441                                 result->SetSubVersion(aSubVersion);
442
443                                 result->SetFirstRun(
444                                         aRunRange.GetFirstRun());
445                                 result->SetLastRun(
446                                         aRunRange.GetLastRun());
447                         } 
448                 }
449
450         } else { // both version and subversion specified
451
452                 while ((key = (TKey*) iter.Next())) { // loop on the keys
453                         
454                         if (!KeyNameToId(key->GetName(), aRunRange, aVersion, aSubVersion)) continue;
455                         // aRunRange, aVersion, aSubVersion filled from filename
456
457                         if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
458                         // aRunRange contains requested run!
459
460                         if(query.GetVersion() != aVersion || query.GetSubVersion() != aSubVersion) continue;
461                         // aVersion and aSubVersion are requested version and subVersion!
462
463                         if(result->GetVersion() == aVersion && result->GetSubVersion() == aSubVersion){
464                                 AliError(Form("More than one object valid for run %d, version %d_%d!",
465                                         query.GetFirstRun(), aVersion, aSubVersion));
466                                 delete result;
467                                 return NULL;
468                         }
469                         result->SetVersion(aVersion);
470                         result->SetSubVersion(aSubVersion);
471                         result->SetFirstRun(aRunRange.GetFirstRun());
472                         result->SetLastRun(aRunRange.GetLastRun());
473                         
474                 }
475         }
476
477         return result;
478 }
479
480 //_____________________________________________________________________________
481 AliCDBEntry* AliCDBDump::GetEntry(const AliCDBId& queryId) {
482 // get AliCDBEntry from the database
483
484         TDirectory::TContext context(gDirectory, fFile);
485
486         if (!(fFile && fFile->IsOpen())) {
487                 AliError("AliCDBDump storage is not initialized properly");
488                 return NULL;
489         }
490
491         if (!gDirectory->cd(queryId.GetPath())) {
492                 return NULL;
493         }
494
495         AliCDBId *dataId = GetEntryId(queryId);
496
497         if (!dataId || !dataId->IsSpecified()) {
498                 if(dataId) delete dataId;
499                 return NULL;
500         }
501
502         TString keyname;
503         if (!IdToKeyName(dataId->GetAliCDBRunRange(), dataId->GetVersion(),
504                 dataId->GetSubVersion(), keyname)) {
505                 AliDebug(2,Form("Bad ID encountered! Subnormal error!"));
506                 delete dataId;
507                 return NULL;
508         }
509
510         // get the only AliCDBEntry object from the file
511         // the object in the file is an AliCDBEntry entry named keyname
512         // keyName = Run#firstRun_#lastRun_v#version_s#subVersion
513
514         TObject* anObject = gDirectory->Get(keyname);
515         if (!anObject) {
516                 AliDebug(2,Form("Bad storage data: NULL entry object!"));
517                 delete dataId;
518                 return NULL;
519         } 
520
521         if (AliCDBEntry::Class() != anObject->IsA()) {
522                 AliDebug(2,Form("Bad storage data: Invalid entry object!"));
523                 delete dataId;
524                 return NULL;
525         }
526
527         ((AliCDBEntry*) anObject)->SetLastStorage("dump");
528
529         delete dataId;
530         return (AliCDBEntry*) anObject;
531 }
532
533 //_____________________________________________________________________________
534 AliCDBId* AliCDBDump::GetEntryId(const AliCDBId& queryId) {
535 // get AliCDBEntry from the database
536
537         TDirectory::TContext context(gDirectory, fFile);
538
539         if (!(fFile && fFile->IsOpen())) {
540                 AliError("AliCDBDump storage is not initialized properly");
541                 return NULL;
542         }
543
544         if (!gDirectory->cd(queryId.GetPath())) {
545                 return NULL;
546         }
547
548         AliCDBId* dataId = 0;
549
550         // look for a filename matching query requests (path, runRange, version, subVersion)
551         if (!queryId.HasVersion()) {
552                 // if version is not specified, first check the selection criteria list
553                 AliCDBId selectedId(queryId);
554                 GetSelection(&selectedId);
555                 dataId = GetId(queryId);
556         } else {
557                 dataId = GetId(queryId);
558         }
559
560         if (dataId && !dataId->IsSpecified()) {
561                 delete dataId;
562                 return NULL;
563         }
564
565         return dataId;
566 }
567
568 //_____________________________________________________________________________
569 void AliCDBDump::GetEntriesForLevel0(const AliCDBId& queryId, TList* result) {
570 // multiple request (AliCDBStorage::GetAll)
571
572         TDirectory* saveDir = gDirectory;
573
574         TIter iter(gDirectory->GetListOfKeys());
575         TKey* key;
576
577         while ((key = (TKey*) iter.Next())) {
578
579                 TString keyNameStr(key->GetName());
580                 if (queryId.GetAliCDBPath().Level1Comprises(keyNameStr)) {
581                         gDirectory->cd(keyNameStr);
582                         GetEntriesForLevel1(queryId, result);
583                         
584                         saveDir->cd();
585                 }
586         }
587 }
588
589 //_____________________________________________________________________________
590 void AliCDBDump::GetEntriesForLevel1(const AliCDBId& queryId, TList* result) {
591 // multiple request (AliCDBStorage::GetAll)
592
593         TIter iter(gDirectory->GetListOfKeys());
594         TKey* key;
595
596         TDirectory* level0Dir = (TDirectory*) gDirectory->GetMother();
597
598         while ((key = (TKey*) iter.Next())) {
599
600                 TString keyNameStr(key->GetName());
601                 if (queryId.GetAliCDBPath().Level2Comprises(keyNameStr)) {
602                         
603                         AliCDBPath aPath(level0Dir->GetName(), 
604                                         gDirectory->GetName(), keyNameStr);
605                         AliCDBId anId(aPath, queryId.GetAliCDBRunRange(), 
606                                         queryId.GetVersion(), -1);
607
608                         AliCDBEntry* anEntry = GetEntry(anId);
609                         if (anEntry) {
610                                 result->Add(anEntry);
611                         }
612         
613                 }
614         }
615
616 }
617
618                         
619 //_____________________________________________________________________________
620 TList* AliCDBDump::GetEntries(const AliCDBId& queryId) {
621 // multiple request (AliCDBStorage::GetAll)
622
623         TDirectory::TContext context(gDirectory, fFile);
624
625         if (!(fFile && fFile->IsOpen())) {
626                 AliError("AliCDBDump storage is not initialized properly");
627                 return NULL;
628         }
629
630         TList* result = new TList();
631         result->SetOwner();
632
633         TIter iter(gDirectory->GetListOfKeys());
634         TKey* key;
635
636         while ((key = (TKey*) iter.Next())) {
637                 
638                 TString keyNameStr(key->GetName());
639                 if (queryId.GetAliCDBPath().Level0Comprises(keyNameStr)) {
640                         gDirectory->cd(keyNameStr);
641                         GetEntriesForLevel0(queryId, result);
642
643                         fFile->cd();
644                 }
645         }
646
647         return result;
648 }
649
650 //_____________________________________________________________________________
651 Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry, const char* mirrors) {
652 // put an AliCDBEntry object into the database
653
654         TDirectory::TContext context(gDirectory, fFile);
655
656         if (!(fFile && fFile->IsOpen())) {
657                 AliError("AliCDBDump storage is not initialized properly");
658                 return kFALSE;
659         }
660
661         if (fReadOnly) {
662                 AliError("AliCDBDump storage is read only!");
663                 return kFALSE;
664         }
665
666         TString mirrorsString(mirrors);
667         if(!mirrorsString.IsNull())
668                 AliError("AliCDBDump storage cannot take mirrors into account.");
669
670         AliCDBId& id = entry->GetId();
671         
672         if (!gDirectory->cd(id.GetPath())) {
673                 if (!MkDir(id.GetPath())) {
674                         AliError(Form("Can't open directory <%s>!", 
675                                         id.GetPath().Data()));
676                         return kFALSE;
677                 }
678         }
679
680         // set version and subVersion for the entry to be stored
681         if (!PrepareId(id)) {
682                 return kFALSE;          
683         }
684
685         // build keyname from entry's id
686         TString keyname;
687         if (!IdToKeyName(id.GetAliCDBRunRange(), id.GetVersion(), id.GetSubVersion(), keyname)) {
688                 AliError("Invalid ID encountered! Subnormal error!");
689                 return kFALSE;
690         }       
691
692         // write object (key name: Run#firstRun_#lastRun_v#version_s#subVersion)
693         Bool_t result = gDirectory->WriteTObject(entry, keyname);
694         if (!result) {
695                 AliError(Form("Can't write entry to file: %s",
696                                 fFile->GetName()));
697         }
698
699         if(result) {
700                 AliInfo(Form("CDB object stored into file %s",fFile->GetName()));
701                 AliInfo(Form("TDirectory/key name: %s/%s",id.GetPath().Data(),keyname.Data()));
702         }
703
704         return result;
705 }
706 //_____________________________________________________________________________
707 TList* AliCDBDump::GetIdListFromFile(const char* fileName){
708
709         TString turl(fileName);
710         if (turl[0] != '/') {
711                 turl.Prepend(TString(gSystem->WorkingDirectory()) + '/');
712         }
713         TFile *file = TFile::Open(turl);
714         if (!file) {
715                 AliError(Form("Can't open selection file <%s>!", turl.Data()));
716                 return NULL;
717         }
718         file->cd();
719
720         TList *list = new TList();
721         list->SetOwner();
722         int i=0;
723         TString keycycle;
724         
725         AliCDBId *id;
726         while(1){
727                 i++;
728                 keycycle = "AliCDBId;";
729                 keycycle+=i;
730                 
731                 id = (AliCDBId*) file->Get(keycycle);
732                 if(!id) break;
733                 list->AddFirst(id);
734         }
735         file->Close(); delete file; file=0;     
736         return list;
737 }
738
739 //_____________________________________________________________________________
740 Bool_t AliCDBDump::Contains(const char* path) const{
741 // check for path in storage
742
743         TDirectory::TContext context(gDirectory, fFile);
744         if (!(fFile && fFile->IsOpen())) {
745                 AliError("AliCDBDump storage is not initialized properly");
746                 return kFALSE;
747         }
748         
749         return gDirectory->cd(path);
750
751 }
752
753 //_____________________________________________________________________________
754 void AliCDBDump::QueryValidFiles()
755 {
756 // Query the CDB for files valid for AliCDBStorage::fRun
757 // fills list fValidFileIds with AliCDBId objects created from file name
758
759         AliError("Not yet (and maybe never) implemented");
760 }
761
762 //_____________________________________________________________________________
763 Bool_t AliCDBDump::IdToFilename(const AliCDBId& /*id*/, TString& /*filename*/) const {
764 // build file name from AliCDBId (path, run range, version) and fDBFolder
765
766         AliError("Not implemented");
767         return kFALSE;
768 }
769
770 //_____________________________________________________________________________
771 Int_t AliCDBDump::GetLatestVersion(const char* path, Int_t run){
772 // get last version found in the database valid for run and path
773
774         AliCDBPath aCDBPath(path);
775         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
776                 AliError(Form("Invalid path in request: %s", path));
777                 return -1;
778         }
779
780         AliCDBId query(path, run, run, -1, -1);
781         AliCDBId *dataId = GetId(query);
782
783         if(!dataId) return -1;
784         Int_t version = dataId->GetVersion();
785
786         delete dataId;
787         return version;
788 }
789
790 //_____________________________________________________________________________
791 Int_t AliCDBDump::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
792 // get last version found in the database valid for run and path
793
794         AliCDBPath aCDBPath(path);
795         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
796                 AliError(Form("Invalid path in request: %s", path));
797                 return -1;
798         }
799
800         AliCDBId query(path, run, run, version, -1);
801         AliCDBId *dataId = GetId(query);
802
803         if(!dataId) return -1;
804
805         Int_t subVersion = dataId->GetSubVersion();
806
807         delete dataId;
808         return subVersion;
809
810 }
811
812 //_____________________________________________________________________________
813 void AliCDBDump::SetRetry(Int_t /* nretry */, Int_t /* initsec */) {
814
815         // Function to set the exponential retry for putting entries in the OCDB
816
817         AliInfo("This function sets the exponential retry for putting entries in the OCDB - to be used ONLY for AliCDBGrid --> returning without doing anything");
818         return;
819
820
821 /////////////////////////////////////////////////////////////////////////////////////////////////
822 //                                                                                             //
823 // AliCDBDump factory                                                                          //
824 //                                                                                             //
825 /////////////////////////////////////////////////////////////////////////////////////////////////
826
827 ClassImp(AliCDBDumpFactory)
828
829 //_____________________________________________________________________________
830 Bool_t AliCDBDumpFactory::Validate(const char* dbString) {
831 // check if the string is valid dump URI
832
833         TRegexp dbPattern("^dump://.+$");
834
835         return TString(dbString).Contains(dbPattern);
836 }
837
838 //_____________________________________________________________________________
839 AliCDBParam* AliCDBDumpFactory::CreateParameter(const char* dbString) {
840 // create AliCDBDumpParam class from the URI string
841
842         if (!Validate(dbString)) {
843                 return NULL;
844         }
845
846         TString pathname(dbString + sizeof("dump://") - 1);
847
848         Bool_t readOnly;
849
850         if (pathname.Contains(TRegexp(";ReadOnly$"))) {
851                 pathname.Resize(pathname.Length() - sizeof(";ReadOnly") + 1);
852                 readOnly = kTRUE;
853         } else {
854                 readOnly = kFALSE;
855         }
856
857         gSystem->ExpandPathName(pathname);
858         
859         if (pathname[0] != '/') {
860                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
861         }
862
863         return new AliCDBDumpParam(pathname, readOnly);
864 }
865
866 //_____________________________________________________________________________
867 AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
868 // create AliCDBDump storage instance from parameters
869
870         if (AliCDBDumpParam::Class() == param->IsA()) {
871
872                 const AliCDBDumpParam* dumpParam = 
873                         (const AliCDBDumpParam*) param;
874
875                 return new AliCDBDump(dumpParam->GetPath(),
876                                 dumpParam->IsReadOnly());
877         }
878
879         return NULL;
880 }
881
882 /////////////////////////////////////////////////////////////////////////////////////////////////
883 //                                                                                             //
884 // AliCDBDump parameter class                                                                  //
885 //                                                                                             //
886 /////////////////////////////////////////////////////////////////////////////////////////////////
887
888 ClassImp(AliCDBDumpParam)
889
890 //_____________________________________________________________________________
891 AliCDBDumpParam::AliCDBDumpParam():
892 fDBPath(), fReadOnly(kFALSE)
893 {
894 // default constructor
895
896 }
897
898 //_____________________________________________________________________________
899 AliCDBDumpParam::AliCDBDumpParam(const char* dbPath, Bool_t readOnly):
900         fDBPath(dbPath), fReadOnly(readOnly)
901 {
902 // constructor
903
904         TString uri;
905         uri += "dump://";
906         uri += dbPath;
907
908         if (fReadOnly) {
909                 uri += ";ReadOnly";
910         }
911         
912         SetURI(uri);
913         SetType("dump");
914 }
915
916 //_____________________________________________________________________________
917 AliCDBDumpParam::~AliCDBDumpParam() {
918 // destructor
919
920 }
921
922 //_____________________________________________________________________________
923 AliCDBParam* AliCDBDumpParam::CloneParam() const {
924 // clone parameter
925
926         return new AliCDBDumpParam(fDBPath, fReadOnly);
927 }
928
929 //_____________________________________________________________________________
930 ULong_t AliCDBDumpParam::Hash() const {
931 // return Hash function
932
933         return fDBPath.Hash();
934 }
935
936 //_____________________________________________________________________________
937 Bool_t AliCDBDumpParam::IsEqual(const TObject* obj) const {
938 // check if this object is equal to AliCDBParam obj
939         
940         if (this == obj) {
941                 return kTRUE;
942         }
943
944         if (AliCDBDumpParam::Class() != obj->IsA()) {
945                 return kFALSE;
946         }
947
948         AliCDBDumpParam* other = (AliCDBDumpParam*) obj;
949
950         return fDBPath == other->fDBPath;
951 }