@@ -8,7 +8,9 @@ class G3MultiFileWriter : public G3Module {
88public:
99 G3MultiFileWriter (boost::python::object filename,
1010 size_t size_limit,
11- boost::python::object divide_on = boost::python::object());
11+ boost::python::object divide_on = boost::python::object(),
12+ size_t buffersize=1024 *1024 );
13+
1214 void Process (G3FramePtr frame, std::deque<G3FramePtr> &out);
1315 std::string CurrentFile () { return current_filename_; }
1416private:
@@ -18,26 +20,26 @@ class G3MultiFileWriter : public G3Module {
1820 boost::python::object filename_callback_;
1921 std::string current_filename_;
2022 size_t size_limit_;
23+ size_t buffersize_;
2124
2225 std::vector<G3Frame::FrameType> always_break_on_;
2326 boost::python::object newfile_callback_;
2427
25- std::shared_ptr<std:: ostream> stream_;
28+ std::ostream stream_;
2629 std::vector<G3FramePtr> metadata_cache_;
2730 int seqno;
2831
2932 SET_LOGGER (" G3MultiFileWriter" );
3033};
3134
3235G3MultiFileWriter::G3MultiFileWriter (boost::python::object filename,
33- size_t size_limit, boost::python::object divide_on)
34- : size_limit_(size_limit), seqno(0 )
36+ size_t size_limit, boost::python::object divide_on, size_t buffersize )
37+ : size_limit_(size_limit), buffersize_(buffersize), stream_( nullptr ), seqno(0 )
3538{
3639 boost::python::extract<std::string> fstr (filename);
3740
3841 if (fstr.check ()) {
3942 filename_ = fstr ();
40- g3_check_output_path (filename_);
4143
4244 if (snprintf (NULL , 0 , filename_.c_str (), 0 ) < 0 )
4345 log_fatal (" Cannot format filename. Should be "
@@ -77,10 +79,10 @@ G3MultiFileWriter::CheckNewFile(G3FramePtr frame)
7779{
7880 // If we are already saving data, check file size. Otherwise, open
7981 // a new file unconditionally.
80- if (stream_ != nullptr ) {
82+ if (stream_) {
8183 bool start_new_ = false ;
8284
83- if (g3_ostream_count ( stream_) > size_limit_)
85+ if (( size_t ) stream_. tellp ( ) > size_limit_)
8486 start_new_ = true ;
8587
8688 if (newfile_callback_.ptr () != Py_None &&
@@ -95,7 +97,7 @@ G3MultiFileWriter::CheckNewFile(G3FramePtr frame)
9597 return false ;
9698 }
9799
98- stream_.reset ();
100+ stream_.flush ();
99101
100102 std::string filename;
101103 if (filename_ != " " ) {
@@ -111,12 +113,10 @@ G3MultiFileWriter::CheckNewFile(G3FramePtr frame)
111113 } else {
112114 filename = boost::python::extract<std::string>(
113115 filename_callback_ (frame, seqno++))();
114-
115- g3_check_output_path (filename);
116116 }
117117
118118 current_filename_ = filename;
119- stream_ = g3_ostream_to_path (filename, false , true );
119+ g3_ostream_to_path (stream_, filename, false , buffersize_ );
120120
121121 for (auto i = metadata_cache_.begin (); i != metadata_cache_.end (); i++)
122122 (*i)->saves (stream_);
@@ -129,7 +129,7 @@ void G3MultiFileWriter::Process(G3FramePtr frame, std::deque<G3FramePtr> &out)
129129 bool new_file (false ), meta_cached (false );
130130
131131 if (frame->type == G3Frame::EndProcessing) {
132- stream_.reset ();
132+ stream_.flush ();
133133 goto done;
134134 }
135135
@@ -186,8 +186,8 @@ PYBINDINGS("core") {
186186 " python callable as divide_on. This callable will be passed each "
187187 " frame in turn. If it returns True (or something with positive "
188188 " truth-value), a new file will be started at that frame." ,
189- init<object, size_t , optional<object> >((arg (" filename" ),
190- arg (" size_limit" ), arg (" divide_on" )=object ())))
189+ init<object, size_t , optional<object, size_t > >((arg (" filename" ),
190+ arg (" size_limit" ), arg (" divide_on" )=object (), arg ( " buffersize " )= 1024 * 1024 )))
191191 .def_readonly (" current_file" , &G3MultiFileWriter::CurrentFile)
192192 .def_readonly (" __g3module__" , true )
193193 ;
0 commit comments