KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
DMSAvailableRunsFile.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Mon Jul 20 11:33:43 2009
2 //Author: John Frankland
3 
4 #include "DMSAvailableRunsFile.h"
5 #include "KVDMS.h"
6 #include "KVDataRepository.h"
7 #include "KVDataSet.h"
8 #include "KVDBRun.h"
9 #include "KVUniqueNameList.h"
10 #include "TClass.h"
11 
12 //macro converting octal filemode to decimal value
13 //to convert e.g. 664 (=u+rw, g+rw, o+r) use CHMODE(6,6,4)
14 #define CHMODE(u,g,o) ((u << 6) + (g << 3) + o)
15 
16 using namespace std;
17 
19 
20 
21 
22 
26 {
27  // Default constructor
28 }
29 
30 
31 
34 
37 {
38  // Constructor with data-type
39 }
40 
41 
42 
45 
47 {
48  // Destructor
49 }
50 
51 
52 
53 
65 
66 void DMSAvailableRunsFile::Update(Bool_t no_existing_file)
67 {
68  //Examine the contents of the DMS container corresponding to this datatype
69  //for parent dataset fDataSet.
70  //For each file found which was not already in the list of available runs and
71  //which corresponds to a run in the database gDataBase,
72  //we add an entry to the available runlist file:
73  // [run number]|[date of modification]|[name of file]
74  // For "old" runs we keep the existing informations (including KV version & username)
75  //
76  // When no_existing_file=kTRUE we are making an available runs file
77  // for the first time. There is no pre-existing file.
78 
80 
81  if (!no_existing_file) {
82  // read all existing informations
83  ReadFile();
84  //use "lockfile" to make sure nobody else tries to modify available_runs file
85  //while we are working on it
86  if (!runlist_lock.Lock(runlist.Data())) return;
87  }
88  //open temporary file
89  TString tmp_file_path(GetFileName());
90  ofstream tmp_file;
91  KVBase::OpenTempFile(tmp_file_path, tmp_file);
92 
93  KVDataRepository* repository = GetDataSet()->GetRepository();
94  //Info("Update","repository : %s",repository->IsA()->GetName());
95  cout << endl << "Updating runlist : " << flush;
96  //get directory listing from repository
97  KVUniqueNameList* dir_list =
98  repository->GetDirectoryListing(GetDataSet(), GetDataType());
99  if (!dir_list) {
100  //Info("Update","Sort au milieu");
101  return;
102  }
103  TIter next(dir_list);
104  DMSFile_t* objs;
105  //progress bar
106  Int_t ntot = dir_list->GetSize();
107  Int_t n5pc = TMath::Max(ntot / 20, 1);
108  Int_t ndone = 0;
109  KVNumberList lnewruns; //list containing run with a valid file but not store in database
110  while ((objs = (DMSFile_t*) next())) { // loop over all entries in directory
111 
112  //Info("Update","objs->GetName()=%s",objs->GetName());
113  Int_t run_num;
114  //is this the correct name of a run in the repository ?
115  if ((run_num = IsRunFileName(objs->GetName()))) {
116 
117  KVDatime filedate;
118  KVDBRun* run = GetDataSet()->GetDataBase()->GetDBRun(run_num);
119  if (run) {
120  //runfile exists in repository
121  //check in case it is possible to extract a date from the name of the file
122  //the file may be much older than its DMS modtime (=date of DMS import)
123  if (!ExtractDateFromFileName(objs->GetName(), filedate))
124  filedate = objs->GetModTime();
125 
126  if (!no_existing_file) {
127  // was there already an entry for exactly the same file in the previous file ?
128  Int_t occIdx = 0;
129  KVNameValueList* prevEntry = RunHasFileWithDateAndName(run->GetNumber(), objs->GetName(), filedate, occIdx);
130  if (prevEntry) {
131  // copy infos of previous entry
132  tmp_file << run->GetNumber() << '|' << filedate.AsSQLString() << '|' << objs->GetName();
133  if (prevEntry->HasParameter(Form("KVVersion[%d]", occIdx))) {
134  tmp_file << "|" << prevEntry->GetStringValue(Form("KVVersion[%d]", occIdx)) << "|" << prevEntry->GetStringValue(Form("Username[%d]", occIdx));
135  }
136  tmp_file << endl;
137  }
138  else {
139  // New Entry - write in temporary runlist file '[run number]|[date of modification]|[name of file]
140  tmp_file << run->GetNumber() << '|' << filedate.AsSQLString() << '|' << objs->GetName() << endl;
141  }
142  }
143  else { // no previous existing file
144  // New Entry in a new file - write in temporary runlist file '[run number]|[date of modification]|[name of file]
145  tmp_file << run->GetNumber() << '|' << filedate.AsSQLString() << '|' << objs->GetName() << endl;
146  }
147  }
148  else {
149  lnewruns.Add(run_num);
150  filedate = objs->GetModTime();
151  tmp_file << run_num << '|' << filedate.AsSQLString() << '|' << objs->GetName() << endl;
152  }
153  }
154 
155  ndone++;
156  if (!(ndone % n5pc))
157  cout << '>' << flush;
158  }
159 
160  cout << " DONE" << endl;
161 
162  // If runs not present in the database were found, emit an informational message
163  if (lnewruns.GetNValues())
164  Info("Update", "Found %d runs not in database :\n\tlist:%s", lnewruns.GetNValues(), lnewruns.AsString());
165 
166  delete dir_list;
167  //close temp file
168  tmp_file.close();
169 
170  if (CheckDirectoryForAvailableRunsFile()) {
171  if (no_existing_file) {
172  //use "lockfile" to make sure nobody else tries to modify available_runs file
173  //while we are working on it
174  if (!runlist_lock.Lock(runlist.Data())) return;
175  }
176 
177  //copy temporary file to KVFiles directory, overwrite previous
178  gSystem->CopyFile(tmp_file_path, runlist, kTRUE);
179  //set access permissions to 664
180  gSystem->Chmod(runlist.Data(), CHMODE(6, 6, 4));
181  }
182  //remove lockfile
183  runlist_lock.Release();
184 
185  //delete temp file
186  gSystem->Unlink(tmp_file_path);
187 }
188 
189 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
bool Bool_t
int type
char * Form(const char *fmt,...)
Handles lists of available runs using Data Management Systems.
virtual ~DMSAvailableRunsFile()
Destructor.
DMSAvailableRunsFile()
Default constructor.
virtual void Update(Bool_t no_existing_file=kFALSE)
KVDatime GetModTime() const
Definition: KVDMS.h:30
Handles lists of available runs for different datasets and types of data.
Bool_t ExtractDateFromFileName(const Char_t *name, KVDatime &date)
const Char_t * GetDataType() const
const Char_t * GetFileName() const
const KVDataSet * GetDataSet() const
KVLockfile runlist_lock
for locking runlist file
KVNameValueList * RunHasFileWithDateAndName(Int_t run, const Char_t *filename, TDatime modtime, Int_t &OccNum)
const Char_t * GetFullPathToAvailableRunsFile() const
Int_t IsRunFileName(const Char_t *filename)
static void OpenTempFile(TString &base, std::ofstream &fp)
Definition: KVBase.cpp:790
virtual Int_t GetNumber() const
Definition: KVDBRecord.h:72
Description of an experimental run in database ,.
Definition: KVDBRun.h:35
Base class for managing repositories of experimental data.
virtual KVUniqueNameList * GetDirectoryListing(const KVDataSet *dataset, const Char_t *datatype="", const Char_t *subdir="")
Manage an experimental dataset corresponding to a given experiment or campaign.
Definition: KVDataSet.h:207
KVDataRepository * GetRepository() const
Get pointer to data repository in which dataset is stored.
Definition: KVDataSet.cpp:1398
KVExpDB * GetDataBase(Option_t *opt="") const
Definition: KVDataSet.cpp:286
Extension of TDatime to handle various useful date formats.
Definition: KVDatime.h:32
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:74
Bool_t Lock(const Char_t *filename="")
Definition: KVLockfile.cpp:165
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
const Char_t * GetStringValue(const Char_t *name) const
Bool_t HasParameter(const Char_t *name) const
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:83
void Add(Int_t)
Add value 'n' to the list.
virtual Int_t GetSize() const
Optimised list in which named objects can only be placed once.
const char * AsSQLString() const
virtual const char * GetName() const
const char * Data() const
Double_t Max(Double_t a, Double_t b)