Fixes for bug #52499: Field polarities inconsistiency
[u/mrichter/AliRoot.git] / STEER / 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 <%s>.", 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) {
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         AliCDBId& id = entry->GetId();
667         
668         if (!gDirectory->cd(id.GetPath())) {
669                 if (!MkDir(id.GetPath())) {
670                         AliError(Form("Can't open directory <%s>!", 
671                                         id.GetPath().Data()));
672                         return kFALSE;
673                 }
674         }
675
676         // set version and subVersion for the entry to be stored
677         if (!PrepareId(id)) {
678                 return kFALSE;          
679         }
680
681         // build keyname from entry's id
682         TString keyname;
683         if (!IdToKeyName(id.GetAliCDBRunRange(), id.GetVersion(), id.GetSubVersion(), keyname)) {
684                 AliError("Invalid ID encountered! Subnormal error!");
685                 return kFALSE;
686         }       
687
688         // write object (key name: Run#firstRun_#lastRun_v#version_s#subVersion)
689         Bool_t result = gDirectory->WriteTObject(entry, keyname);
690         if (!result) {
691                 AliError(Form("Can't write entry to file: %s",
692                                 fFile->GetName()));
693         }
694
695         if(result) {
696                 AliInfo(Form("CDB object stored into file %s",fFile->GetName()));
697                 AliInfo(Form("TDirectory/key name: %s/%s",id.GetPath().Data(),keyname.Data()));
698         }
699
700         return result;
701 }
702 //_____________________________________________________________________________
703 TList* AliCDBDump::GetIdListFromFile(const char* fileName){
704
705         TString turl(fileName);
706         if (turl[0] != '/') {
707                 turl.Prepend(TString(gSystem->WorkingDirectory()) + '/');
708         }
709         TFile *file = TFile::Open(turl);
710         if (!file) {
711                 AliError(Form("Can't open selection file <%s>!", turl.Data()));
712                 return NULL;
713         }
714         file->cd();
715
716         TList *list = new TList();
717         list->SetOwner();
718         int i=0;
719         TString keycycle;
720         
721         AliCDBId *id;
722         while(1){
723                 i++;
724                 keycycle = "AliCDBId;";
725                 keycycle+=i;
726                 
727                 id = (AliCDBId*) file->Get(keycycle);
728                 if(!id) break;
729                 list->AddFirst(id);
730         }
731         file->Close(); delete file; file=0;     
732         return list;
733 }
734
735 //_____________________________________________________________________________
736 Bool_t AliCDBDump::Contains(const char* path) const{
737 // check for path in storage
738
739         TDirectory::TContext context(gDirectory, fFile);
740         if (!(fFile && fFile->IsOpen())) {
741                 AliError("AliCDBDump storage is not initialized properly");
742                 return kFALSE;
743         }
744         
745         return gDirectory->cd(path);
746
747 }
748
749 //_____________________________________________________________________________
750 void AliCDBDump::QueryValidFiles()
751 {
752 // Query the CDB for files valid for AliCDBStorage::fRun
753 // fills list fValidFileIds with AliCDBId objects created from file name
754
755         AliError("Not yet (and maybe never) implemented");
756 }
757
758 //_____________________________________________________________________________
759 Bool_t AliCDBDump::IdToFilename(const AliCDBId& /*id*/, TString& /*filename*/) const {
760 // build file name from AliCDBId (path, run range, version) and fDBFolder
761
762         AliError("Not implemented");
763         return kFALSE;
764 }
765
766 //_____________________________________________________________________________
767 Int_t AliCDBDump::GetLatestVersion(const char* path, Int_t run){
768 // get last version found in the database valid for run and path
769
770         AliCDBPath aCDBPath(path);
771         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
772                 AliError(Form("Invalid path in request: %s", path));
773                 return -1;
774         }
775
776         AliCDBId query(path, run, run, -1, -1);
777         AliCDBId *dataId = GetId(query);
778
779         if(!dataId) return -1;
780         Int_t version = dataId->GetVersion();
781
782         delete dataId;
783         return version;
784 }
785
786 //_____________________________________________________________________________
787 Int_t AliCDBDump::GetLatestSubVersion(const char* path, Int_t run, Int_t version){
788 // get last version found in the database valid for run and path
789
790         AliCDBPath aCDBPath(path);
791         if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
792                 AliError(Form("Invalid path in request: %s", path));
793                 return -1;
794         }
795
796         AliCDBId query(path, run, run, version, -1);
797         AliCDBId *dataId = GetId(query);
798
799         if(!dataId) return -1;
800
801         Int_t subVersion = dataId->GetSubVersion();
802
803         delete dataId;
804         return subVersion;
805
806 }
807
808
809 /////////////////////////////////////////////////////////////////////////////////////////////////
810 //                                                                                             //
811 // AliCDBDump factory                                                                          //
812 //                                                                                             //
813 /////////////////////////////////////////////////////////////////////////////////////////////////
814
815 ClassImp(AliCDBDumpFactory)
816
817 //_____________________________________________________________________________
818 Bool_t AliCDBDumpFactory::Validate(const char* dbString) {
819 // check if the string is valid dump URI
820
821         TRegexp dbPattern("^dump://.+$");
822
823         return TString(dbString).Contains(dbPattern);
824 }
825
826 //_____________________________________________________________________________
827 AliCDBParam* AliCDBDumpFactory::CreateParameter(const char* dbString) {
828 // create AliCDBDumpParam class from the URI string
829
830         if (!Validate(dbString)) {
831                 return NULL;
832         }
833
834         TString pathname(dbString + sizeof("dump://") - 1);
835
836         Bool_t readOnly;
837
838         if (pathname.Contains(TRegexp(";ReadOnly$"))) {
839                 pathname.Resize(pathname.Length() - sizeof(";ReadOnly") + 1);
840                 readOnly = kTRUE;
841         } else {
842                 readOnly = kFALSE;
843         }
844
845         gSystem->ExpandPathName(pathname);
846         
847         if (pathname[0] != '/') {
848                 pathname.Prepend(TString(gSystem->WorkingDirectory()) + '/');
849         }
850
851         return new AliCDBDumpParam(pathname, readOnly);
852 }
853
854 //_____________________________________________________________________________
855 AliCDBStorage* AliCDBDumpFactory::Create(const AliCDBParam* param) {
856 // create AliCDBDump storage instance from parameters
857
858         if (AliCDBDumpParam::Class() == param->IsA()) {
859
860                 const AliCDBDumpParam* dumpParam = 
861                         (const AliCDBDumpParam*) param;
862
863                 return new AliCDBDump(dumpParam->GetPath(),
864                                 dumpParam->IsReadOnly());
865         }
866
867         return NULL;
868 }
869
870 /////////////////////////////////////////////////////////////////////////////////////////////////
871 //                                                                                             //
872 // AliCDBDump parameter class                                                                  //
873 //                                                                                             //
874 /////////////////////////////////////////////////////////////////////////////////////////////////
875
876 ClassImp(AliCDBDumpParam)
877
878 //_____________________________________________________________________________
879 AliCDBDumpParam::AliCDBDumpParam():
880 fDBPath(), fReadOnly(kFALSE)
881 {
882 // default constructor
883
884 }
885
886 //_____________________________________________________________________________
887 AliCDBDumpParam::AliCDBDumpParam(const char* dbPath, Bool_t readOnly):
888         fDBPath(dbPath), fReadOnly(readOnly)
889 {
890 // constructor
891
892         TString uri;
893         uri += "dump://";
894         uri += dbPath;
895
896         if (fReadOnly) {
897                 uri += ";ReadOnly";
898         }
899         
900         SetURI(uri);
901         SetType("dump");
902 }
903
904 //_____________________________________________________________________________
905 AliCDBDumpParam::~AliCDBDumpParam() {
906 // destructor
907
908 }
909
910 //_____________________________________________________________________________
911 AliCDBParam* AliCDBDumpParam::CloneParam() const {
912 // clone parameter
913
914         return new AliCDBDumpParam(fDBPath, fReadOnly);
915 }
916
917 //_____________________________________________________________________________
918 ULong_t AliCDBDumpParam::Hash() const {
919 // return Hash function
920
921         return fDBPath.Hash();
922 }
923
924 //_____________________________________________________________________________
925 Bool_t AliCDBDumpParam::IsEqual(const TObject* obj) const {
926 // check if this object is equal to AliCDBParam obj
927         
928         if (this == obj) {
929                 return kTRUE;
930         }
931
932         if (AliCDBDumpParam::Class() != obj->IsA()) {
933                 return kFALSE;
934         }
935
936         AliCDBDumpParam* other = (AliCDBDumpParam*) obj;
937
938         return fDBPath == other->fDBPath;
939 }