EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
DataFile.h
1 // -*- c++ -*-
2 #if !defined(_DATAFILE_H_)
3 #define _DATAFILE_H_
4 
5 // system
6 #include <stdio.h>
7 
8 // tools
9 #include "tools/Filename.h"
10 #include "tools/AutoPtr.h"
11 #include "tools/AutoFilePtr.h"
12 
13 // storage
14 #include "storage/CtrlInfo.h"
15 #include "storage/RawValue.h"
16 
19 
32 class DataFile {
33 
34 public:
35 
37  static const uint32_t cookie = 0x41444631;
38 
56  static DataFile *reference(const stdString &dirname,
57  const stdString &basename, bool for_write);
58 
61 
63  size_t refCount() const
64  { return ref_count; }
65 
68 
70  bool isTaggedFile() { return is_tagged_file; }
71 
73  //
75  void release();
76 
78  bool is_writable() const
79  { return for_write; }
80 
84  FileOffset getSize() const;
85 
92  void reopen();
93 
95  static void show_all_sizes();
96 
111  static size_t clear_cache();
112 
123  static void close_all();
124 
126  const stdString &getFilename() { return filename; }
127 
129  const stdString &getDirname () { return dirname; }
130 
132  const stdString &getBasename() { return basename; }
133 
139 
141  size_t getHeaderSize(const stdString &name,
142  DbrType dbr_type, DbrCount dbr_count,
143  size_t num_samples);
144 
152  class DataHeader *addHeader(const stdString &name,
153  DbrType dbr_type, DbrCount dbr_count,
154  double period, size_t num_samples);
155 
162  void addCtrlInfo(const CtrlInfo &info, FileOffset &offset);
163 
165  void write(const CtrlInfo& ctrlInfo, FileOffset offset);
166 
168  void read(CtrlInfo& ctrlInfo, FileOffset offset);
169 
170 private:
171 
172  friend class DataHeader;
173  friend class DataWriter;
174  friend class RawDataReaderEA3;
175  // Attach DataFile to disk file of given name.
176  // Existing file is opened, otherwise new one is created.
177  // Unlike the reference() call, the constructor
178  // expexts a normalized dirname/basename/filename.
179  DataFile(const stdString &dirname,
180  const stdString &basename,
181  const stdString &filename, bool for_write);
182 
183  // Close file.
184  ~DataFile();
185 
186  // prohibit assignment or implicit copy:
187  // (these are not implemented, use reference() !)
188  DataFile(const DataFile &other);
189  DataFile &operator = (const DataFile &other);
190 
191  // The current data file
192  AutoFilePtr file;
193  size_t ref_count;
194  bool for_write;
195  bool is_tagged_file;
196  stdString filename;
197  stdString dirname;
198  stdString basename;
199 };
200 
202 
206 class DataHeader {
207 
208 public:
209 
211 
216 
218  ~DataHeader();
219 
220  enum // Scott Meyers' "enum hack":
221  { FilenameLength = 40 };
222 
223  // NOTE: For now, the layout of the following must not
224  // change because it defines the on-disk layout (except for byte order)!
225 
227  struct DataHeaderData {
232  uint32_t num_samples;
234  uint32_t buf_size;
235  uint32_t buf_free;
238  uint8_t pad[4];
239  double period;
240  epicsTimeStamp begin_time;
241  epicsTimeStamp next_file_time;
242  epicsTimeStamp end_time;
243  char prev_file[FilenameLength];
244  char next_file[FilenameLength];
245  } data;
246 
249 
251 
255 
257  void clear();
258 
260  bool isValid();
261 
263  size_t available();
264 
266  size_t capacity();
267 
270  void read(FileOffset offset);
271 
274  void write() const;
275 
277  bool read_next();
278 
280  bool read_prev();
281 
283  void set_prev(const stdString &basename, FileOffset offset);
284 
286  void set_next(const stdString &basename, FileOffset offset);
287 
289  void show(FILE *f, bool full_detail);
290 
291 private:
292  bool get_prev_next(const char *name, FileOffset new_offset);
293 };
294 
296 
297 #endif // !defined(_DATAFILE_H_)
DataFile * reference()
Add reference to current DataFile.
struct DataHeader::DataHeaderData data
header data
Binary Data File.
Definition: DataFile.h:32
size_t available()
Returns number of unused samples in buffer.
unsigned short DbrType
DbrType is used to hold dbr_time_xxx types.
Definition: RawValue.h:24
DbrType dbr_type
ca type of data
Definition: DataFile.h:236
epicsTimeStamp end_time
last time stamp in this buffer
Definition: DataFile.h:242
Meta-information for values: Units, limits, etc .
Definition: CtrlInfo.h:75
DataHeader(DataFile *datafile)
Create header for given data file.
static void close_all()
Close all data files.
void write(const CtrlInfo &ctrlInfo, FileOffset offset)
Write ctrlInfo.
uint32_t buf_free
remaining bytes in this buffer
Definition: DataFile.h:235
FileOffset getSize() const
Get current file size in bytes.
void read(FileOffset offset)
Read (and convert) from offset in current DataFile, updating offset.
void addCtrlInfo(const CtrlInfo &info, FileOffset &offset)
Add CtrlInfo to the data file.
size_t refCount() const
Get reference count.
Definition: DataFile.h:63
bool read_prev()
Read the previous data header.
~DataHeader()
Destructor releases the current DataFile.
static size_t clear_cache()
Close as many data files as possible.
void read(CtrlInfo &ctrlInfo, FileOffset offset)
Read ctrlInfo.
The header data.
Definition: DataFile.h:227
Writes data to storage.
Definition: DataWriter.h:21
bool isValid()
Is offset valid?
FileOffset curr_offset
rel. offs. from data header to free entry
Definition: DataFile.h:231
const stdString & getDirname()
Get directory name of data file.
Definition: DataFile.h:129
DbrCount dbr_count
array dimension of this data type
Definition: DataFile.h:237
FileOffset prev_offset
abs. offs. of data header in prev buffer
Definition: DataFile.h:230
double period
period at which the channel is archived (secs)
Definition: DataFile.h:239
bool is_writable() const
Returns true if DataFile is writable.
Definition: DataFile.h:78
FileOffset next_offset
abs. offs. of data header in next buffer
Definition: DataFile.h:229
static void show_all_sizes()
Show all size.
unsigned short DbrCount
DbrCount is used to hold the array size of CA channels.
Definition: RawValue.h:27
uint32_t FileOffset
FileOffset is used as a system independent type for, well, offsets into files.
Definition: StorageTypes.h:13
epicsTimeStamp next_file_time
first time stamp of data in the next file
Definition: DataFile.h:241
class DataHeader * addHeader(const stdString &name, DbrType dbr_type, DbrCount dbr_count, double period, size_t num_samples)
Add a new DataHeader to the file.
void set_prev(const stdString &basename, FileOffset offset)
Helper to set data.prev_file/offset.
bool is_new_file
Indicates if this file was newly created.
Definition: DataFile.h:67
Auto-close FILE pointer wrapper.
Definition: AutoFilePtr.h:15
FileOffset ctrl_info_offset
abs. offset to CtrlInfo
Definition: DataFile.h:233
void clear()
Fill the data with zeroes, invalidate the data.
char prev_file[FilenameLength]
basename for prev. buffer
Definition: DataFile.h:243
Used by DataFile.
Definition: DataFile.h:206
static const uint32_t cookie
== &#39;ADF1&#39;, Archive Data File 1
Definition: DataFile.h:37
FileOffset dir_offset
offset of the old directory entry
Definition: DataFile.h:228
void set_next(const stdString &basename, FileOffset offset)
Helper to set data.next_file/offset.
uint8_t pad[4]
to align double period...
Definition: DataFile.h:238
uint32_t num_samples
number of samples in this buffer
Definition: DataFile.h:232
bool read_next()
Read the next data header.
bool isTaggedFile()
Does the file follow the 2-1-1 format w/ cookie and tags?
Definition: DataFile.h:70
void release()
De-reference a data file (Call instead of delete).
uint32_t buf_size
disk space alloc. for this buffer including sizeof(DataHeader)
Definition: DataFile.h:234
void show(FILE *f, bool full_detail)
Print data.
FileOffset offset
Offset in current file for this header.
Definition: DataFile.h:254
class DataHeader * getHeader(FileOffset offset)
Read header at given offset.
void reopen()
Closes and re-opens a DataFile.
epicsTimeStamp begin_time
first time stamp of data in this file
Definition: DataFile.h:240
const stdString & getBasename()
Get base name of data file.
Definition: DataFile.h:132
DataFile * datafile
The currently used DataFile (DataHeader handles ref and release).
Definition: DataFile.h:248
void write() const
Convert and write to current offset in current DataFile.
An implementation of the DataReader for raw data.
Definition: RawDataReaderEA3.h:22
const stdString & getFilename()
Get full name of data file.
Definition: DataFile.h:126
char next_file[FilenameLength]
basename for next buffer
Definition: DataFile.h:244
size_t getHeaderSize(const stdString &name, DbrType dbr_type, DbrCount dbr_count, size_t num_samples)
Get size of a header with given parameters.
size_t capacity()
Returns max. number of values in buffer.