Belle II Software  release-06-00-14
SeqFile Class Reference

A class to manage I/O for a chain of blocked files. More...

#include <SeqFile.h>

Collaboration diagram for SeqFile:

Public Member Functions

 SeqFile (const std::string &filename, const std::string &rwflag, char *streamerinfo=nullptr, int streamerinfo_size=0, bool filenameIsPattern=false)
 Constructor. More...
 
 ~SeqFile ()
 Destructor.
 
 SeqFile (const SeqFile &)=delete
 No copying.
 
SeqFileoperator= (const SeqFile &)=delete
 No assignment.
 
int status () const
 Returns status after constructor call. More...
 
int write (const char *buf)
 Write a record to a file. More...
 
int read (char *buf, int max)
 Read a record from a file. More...
 

Private Member Functions

void openFile (std::string filename, bool readonly)
 actually open the file
 

Private Attributes

std::string m_filename
 Name of the opened file.
 
std::string m_filenamePattern
 Pattern for creating the file from the sequence number.
 
int m_fd { -1}
 file descriptor.
 
int m_nb {0}
 when saving a file, the total number of bytes written, 0 when reading.
 
int m_nfile {0}
 file counter, starting at 0 (files are split after c_MaxFileSize bytes).
 
bool m_compressed {false}
 is file gzipped compressed?
 
std::unique_ptr< std::ios > m_stream
 pointer to the filtering input or output stream
 
char * m_streamerinfo
 StreamerInfo.
 
int m_streamerinfo_size
 size(bytes) of StreamerInfo
 

Static Private Attributes

static const int c_MaxFileSize {512000000 * 4}
 maximal size of one file (in Bytes).
 

Detailed Description

A class to manage I/O for a chain of blocked files.

Definition at line 22 of file SeqFile.h.

Constructor & Destructor Documentation

◆ SeqFile()

SeqFile ( const std::string &  filename,
const std::string &  rwflag,
char *  streamerinfo = nullptr,
int  streamerinfo_size = 0,
bool  filenameIsPattern = false 
)

Constructor.

Parameters
filenamename of the file
rwflagshould probably be r or rw
streamerinfoString containing the streamer info
streamerinfo_sizeSize of the string containing the streamer info
filenameIsPatternif true interpret the filename as a boost::format pattern which takes the sequence number as argument instead of producing .sroot-N files

Definition at line 25 of file SeqFile.cc.

26  :
27  m_filename(filename)
28 {
29  if (filename.empty()) {
30  B2ERROR("SeqFile: Empty filename given");
31  return;
32  }
33  bool readonly = rwflag.find('w') == std::string::npos;
34  // is the file already compressed?
35  m_compressed = filename.size() > 3 && filename.compare(filename.size() - 3, 3, ".gz") == 0;
36  // strip .gz suffix to add it at the end automatically and correctly for subsequent files
37  if (m_compressed) {
38  m_filename = filename.substr(0, filename.size() - 3);
39  }
40  // check if we want different naming scheme using boost::format
41  if (filenameIsPattern) {
43  try {
44  m_filename = (boost::format(m_filenamePattern) % m_nfile).str();
45  } catch (std::exception& e) {
46  B2FATAL("Cannot use filename pattern" << m_filenamePattern << ": " << e.what());
47  }
48  }
49 
50  // Store StreamerInfo 2017.5.8
51  m_streamerinfo = nullptr;
53  if (streamerinfo != nullptr && streamerinfo_size > 0) {
54  m_streamerinfo_size = streamerinfo_size;
55  m_streamerinfo = new char[ m_streamerinfo_size ];
56  memcpy(m_streamerinfo, streamerinfo, m_streamerinfo_size);
57  }
58 
59  // open the file
60  openFile(m_filename, readonly);
61  // if that fails and it's not already assumed to be compressed try again adding .gz to the name
62  if (m_fd < 0 && !m_compressed) {
63  B2WARNING("SeqFile: error opening '" << filename << "': " << strerror(errno)
64  << ", trying again with '.gz'");
65  m_compressed = true;
66  openFile(m_filename, readonly);
67  }
68  // is the file open now?
69  if (m_fd < 0) {
70  B2ERROR("SeqFile: error opening '" << filename << "': " << strerror(errno));
71  } else {
72  B2INFO("SeqFile: " << m_filename << " opened (fd=" << m_fd << ")");
73  }
74 
75 }
std::string m_filenamePattern
Pattern for creating the file from the sequence number.
Definition: SeqFile.h:60
void openFile(std::string filename, bool readonly)
actually open the file
Definition: SeqFile.cc:77
int m_streamerinfo_size
size(bytes) of StreamerInfo
Definition: SeqFile.h:71
bool m_compressed
is file gzipped compressed?
Definition: SeqFile.h:64
std::string m_filename
Name of the opened file.
Definition: SeqFile.h:59
int m_fd
file descriptor.
Definition: SeqFile.h:61
int m_nfile
file counter, starting at 0 (files are split after c_MaxFileSize bytes).
Definition: SeqFile.h:63
char * m_streamerinfo
StreamerInfo.
Definition: SeqFile.h:68

Member Function Documentation

◆ read()

int read ( char *  buf,
int  max 
)

Read a record from a file.

The record length is returned.

Definition at line 170 of file SeqFile.cc.

◆ status()

int status ( ) const

Returns status after constructor call.

If success, fd is returned. If not, -1

Definition at line 131 of file SeqFile.cc.

◆ write()

int write ( const char *  buf)

Write a record to a file.

First word of the record should contain number of words.

Definition at line 136 of file SeqFile.cc.


The documentation for this class was generated from the following files: