Belle II Software development
SeqFile Class Reference

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

#include <SeqFile.h>

Public Member Functions

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

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,
const 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 (m_filename.empty()) {
30 B2ERROR("SeqFile: Empty filename given");
31 return;
32 }
33 bool readonly = rwflag.find('w') == std::string::npos;
34 // CAF use URL input style and add a prefix that need to be removed for sroot files
35 if (m_filename.compare(0, 7, "file://") == 0) {
36 m_filename = m_filename.substr(7, m_filename.size() - 7);
37 }
38 // is the file already compressed?
39 m_compressed = m_filename.size() > 3 && m_filename.compare(m_filename.size() - 3, 3, ".gz") == 0;
40 // strip .gz suffix to add it at the end automatically and correctly for subsequent files
41 if (m_compressed) {
42 m_filename.resize(m_filename.size() - 3);
43 }
44 // check if we want different naming scheme using boost::format
45 if (filenameIsPattern) {
47 try {
48 m_filename = (boost::format(m_filenamePattern) % m_nfile).str();
49 } catch (std::exception& e) {
50 B2FATAL("Cannot use filename pattern" << m_filenamePattern << ": " << e.what());
51 }
52 }
53
54 m_streamerinfo = nullptr;
56 if (streamerinfo != nullptr && streamerinfo_size > 0) {
57 m_streamerinfo_size = streamerinfo_size;
59 memcpy(m_streamerinfo, streamerinfo, m_streamerinfo_size);
60 }
61
62 // open the file
63 openFile(m_filename, readonly);
64 // if that fails and it's not already assumed to be compressed try again adding .gz to the name
65 if (m_fd < 0 && !m_compressed) {
66 B2WARNING("SeqFile: error opening '" << m_filename << "': " << strerror(errno)
67 << ", trying again with '.gz'");
68 m_compressed = true;
69 openFile(m_filename, readonly);
70 }
71 // is the file open now?
72 if (m_fd < 0) {
73 B2ERROR("SeqFile: error opening '" << m_filename << "': " << strerror(errno));
74 } else {
75 B2INFO("SeqFile: " << m_filename << " opened (fd=" << m_fd << ")");
76 }
77
78}
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:80
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

◆ ~SeqFile()

~SeqFile ( )

Destructor.

Definition at line 124 of file SeqFile.cc.

125{
126 if (m_streamerinfo != nullptr) delete m_streamerinfo;
127 B2INFO("Closing SeqFile " << m_nfile);
128 //closed automatically by m_stream.
129}

Member Function Documentation

◆ openFile()

void openFile ( std::string filename,
bool readonly )
private

actually open the file

Definition at line 80 of file SeqFile.cc.

81{
82
83 // add compression suffix if file is supposed to be compressed
84 if (m_compressed) filename += ".gz";
85 if (!readonly) {
86 //open file in create mode and set stream correctly
87 m_fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664);
88 auto filter = new io::filtering_ostream();
89 if (m_compressed) filter->push(io::gzip_compressor());
90 filter->push(io::file_descriptor_sink(m_fd, io::close_handle));
91 filter->exceptions(ios_base::badbit | ios_base::failbit);
92 m_stream.reset(filter);
93
94 if (m_streamerinfo == nullptr || m_streamerinfo_size <= 0) {
95 // If you want to use SeqFile for non-sroot file type, please skip this B2FATAL
96 B2FATAL("Invalid size of StreamerInfo : " << m_streamerinfo_size << "bytes");
97 } else {
98 auto* out = dynamic_cast<std::ostream*>(m_stream.get());
99 if (!out) {
100 B2FATAL("SeqFile::write() called on a file opened in read mode");
101 }
102 try {
104 B2INFO("Wrote StreamerInfo at the begenning of the file. : " << m_streamerinfo_size << "bytes");
105 } catch (ios_base::failure& e) {
106
107 B2ERROR("SeqFile::openFile() error: " << e.what() << ", " << strerror(errno));
108 }
109 }
110
111 } else {
112 //open file in read mode and set stream correctly
113 m_fd = open(filename.c_str(), O_RDONLY);
114 auto filter = new io::filtering_istream();
115 if (m_compressed) filter->push(io::gzip_decompressor());
116 filter->push(io::file_descriptor_source(m_fd, io::close_handle));
117 filter->exceptions(ios_base::badbit | ios_base::failbit);
118 m_stream.reset(filter);
119 }
120 // reset number of written bytes (does not include streamerinfo )
121 m_nb = 0;
122}
int m_nb
when saving a file, the total number of bytes written, 0 when reading.
Definition SeqFile.h:62
std::unique_ptr< std::ios > m_stream
pointer to the filtering input or output stream
Definition SeqFile.h:65
std::map< ExpRun, std::pair< double, double > > filter(const std::map< ExpRun, std::pair< double, double > > &runs, double cut, std::map< ExpRun, std::pair< double, double > > &runsRemoved)
filter events to remove runs shorter than cut, it stores removed runs in runsRemoved
Definition Splitter.cc:38

◆ 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.

171{
172 // cast stream object
173 auto* in = dynamic_cast<std::istream*>(m_stream.get());
174 if (!in) {
175 B2FATAL("SeqFile::read() called on a file opened in write mode");
176 }
177 //trigger eof if there's nothing left int the file. Could throw an error on decompress failure
178 try {
179 in->peek();
180 } catch (ios_base::failure& e) {
181 B2ERROR("SeqFile::read() cannot read file: " << e.what());
182 return -1;
183 }
184 //ok, now we can open the next file reliably
185 if (in->eof()) {
186 // EOF of current file, search for next file
187 m_nfile++;
188 auto nextfile = m_filename + '-' + std::to_string(m_nfile);
189 if (!m_filenamePattern.empty()) {
190 nextfile = (boost::format(m_filenamePattern) % m_nfile).str();
191 }
192 openFile(nextfile, true);
193 if (m_fd < 0) return 0; // End of all files
194 // update the stream pointer
195 in = dynamic_cast<std::istream*>(m_stream.get());
196 B2INFO("SeqFile::read() opened '" << nextfile << "'");
197 }
198 try {
199 // Obtain new header
200 in->read(buf, sizeof(int));
201 } catch (ios_base::failure& e) {
202 B2ERROR("SeqFile::read() " << e.what() << ": couldn't read next record size");
203 return -1;
204 }
205 // Normal processing, extract the record size from the first 4 bytes
206 int recsize = *reinterpret_cast<const int*>(buf);
207 if (recsize > size) {
208 B2ERROR("SeqFile::read() buffer too small, need at least " << recsize << " bytes");
209 return -1;
210 }
211 try {
212 in->read(buf + sizeof(int), recsize - sizeof(int));
213 } catch (ios_base::failure& e) {
214 B2ERROR("SeqFile::read() " << e.what() << ": could only read " << in->gcount() << " bytes, expected " << recsize);
215 return -1;
216 }
217 return recsize;
218}

◆ 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.

132{
133 return m_fd;
134}

◆ 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.

137{
138 // cast stream object
139 auto* out = dynamic_cast<std::ostream*>(m_stream.get());
140 if (!out) {
141 B2FATAL("SeqFile::write() called on a file opened in read mode");
142 }
143 int insize = *reinterpret_cast<const int*>(buf); // nbytes in the buffer at the beginning
144 if (insize + m_nb >= c_MaxFileSize && m_filename != "/dev/null") {
145 B2INFO("SeqFile: previous file closed (size=" << m_nb << " bytes)");
146 m_nfile++;
147 auto file = m_filename + '-' + std::to_string(m_nfile);
148 if (!m_filenamePattern.empty()) {
149 file = (boost::format(m_filenamePattern) % m_nfile).str();
150 }
151 openFile(file, false);
152 if (m_fd < 0) {
153 B2FATAL("SeqFile::write() error opening file '" << file << "': " << strerror(errno));
154 }
155 m_nb = 0;
156 B2INFO("SeqFile::write() opened '" << file << "'");
157 // update stream pointer since we reopened the file
158 out = dynamic_cast<std::ostream*>(m_stream.get());
159 }
160 try {
161 out->write(buf, insize);
162 m_nb += insize;
163 return insize;
164 } catch (ios_base::failure& e) {
165 B2ERROR("SeqFile::write() error: " << e.what() << ", " << strerror(errno));
166 return 0;
167 }
168}
static const int c_MaxFileSize
maximal size of one file (in Bytes).
Definition SeqFile.h:57

Member Data Documentation

◆ c_MaxFileSize

const int c_MaxFileSize {512000000 * 4}
staticprivate

maximal size of one file (in Bytes).

Definition at line 57 of file SeqFile.h.

57{512000000 * 4};

◆ m_compressed

bool m_compressed {false}
private

is file gzipped compressed?

Definition at line 64 of file SeqFile.h.

64{false};

◆ m_fd

int m_fd { -1}
private

file descriptor.

Definition at line 61 of file SeqFile.h.

61{ -1};

◆ m_filename

std::string m_filename
private

Name of the opened file.

Definition at line 59 of file SeqFile.h.

◆ m_filenamePattern

std::string m_filenamePattern
private

Pattern for creating the file from the sequence number.

Definition at line 60 of file SeqFile.h.

◆ m_nb

int m_nb {0}
private

when saving a file, the total number of bytes written, 0 when reading.

Definition at line 62 of file SeqFile.h.

62{0};

◆ m_nfile

int m_nfile {0}
private

file counter, starting at 0 (files are split after c_MaxFileSize bytes).

Definition at line 63 of file SeqFile.h.

63{0};

◆ m_stream

std::unique_ptr<std::ios> m_stream
private

pointer to the filtering input or output stream

Definition at line 65 of file SeqFile.h.

◆ m_streamerinfo

char* m_streamerinfo
private

StreamerInfo.

Definition at line 68 of file SeqFile.h.

◆ m_streamerinfo_size

int m_streamerinfo_size
private

size(bytes) of StreamerInfo

Definition at line 71 of file SeqFile.h.


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