Turi Create  4.0
general_fstream_sink.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef FILEIO_GENERAL_FSTREAM_SINK_HPP
7 #define FILEIO_GENERAL_FSTREAM_SINK_HPP
8 #include <memory>
9 #include <iostream>
10 #include <fstream>
11 #include <boost/iostreams/stream.hpp>
12 #include <core/storage/fileio/union_fstream.hpp>
13 #include <core/storage/fileio/fileio_constants.hpp>
14 #include <boost/iostreams/filter/gzip.hpp>
15 namespace turi {
16 namespace fileio_impl {
17 
18 /**
19  * \ingroup fileio
20  * \internal
21  * Implements a general file stream sink device which wraps the
22  * union_fstream, and provides automatic gzip decompression capabilities.
23  *
24  * The general_fstream_sink is NOT thread-safe.
25  */
27 
28  /// The sink device must be copyable; thus the shared_ptr.
29  std::shared_ptr<union_fstream> out_file;
30  /// The sink device must be copyable; thus the shared_ptr.
31  std::shared_ptr<boost::iostreams::gzip_compressor> compressor;
32 
33  /// The underlying stream inside the in_file (std stream or hdfs stream)
34  std::shared_ptr<std::ostream> underlying_stream;
35 
36  /// Set by the constructor. whether it is gzip compressed.
37  bool is_gzip_compressed = false;
38 
39  /// Filename that was opened
40  std::string sanitized_filename;
41 
42  public:
43  typedef char char_type;
44  struct category: public boost::iostreams::sink_tag,
45  boost::iostreams::closable_tag,
46  boost::iostreams::multichar_tag,
47  boost::iostreams::optimally_buffered_tag {};
48 
49  /**
50  * Constructs a fstream sink which write to a file. This file can be
51  * of any protocol supported by the union_fstream, and may also be
52  * gzip compressed. Gzip compression detection is automatic based on the file
53  * extension. (Whether it ends in .gz)
54  */
55  explicit general_fstream_sink(std::string file);
56 
57  /**
58  * Constructs a fstream sink which writes to a file. This file can be
59  * of any protocol supported by the union_fstream, and may also be
60  * gzip compressed. Gzip compression detection is not performed, and the
61  * gzip_compressed flag is used to enable/disable gzip decompression.
62  */
63  general_fstream_sink(std::string file, bool gzip_compressed);
64 
65  /**
66  * Default copy constructor. copied object shares handles with the original
67  * object. Required because boost streams requires devices to be copyable.
68  * This should really not be used otherwise.
69  */
71 
72  /**
73  * Default move constructor
74  */
76 
77 
78  /// Destructor. Closes the file
80 
81 
82  inline std::streamsize optimal_buffer_size() const {
84  }
85 
86  /**
87  * Returns true if the file is opened
88  */
89  bool is_open() const;
90 
91  /**
92  * Attempts to write bufsize bytes into the stream from the buffer.
93  * Returns the actual number of bytes read. Returns -1 on failure.
94  */
95  std::streamsize write(const char* c, std::streamsize bufsize);
96 
97  /**
98  * Closes all file handles
99  */
100  void close();
101 
102  /**
103  * Returns true if the stream is good. See std::ios_base
104  */
105  bool good() const;
106 
107  /**
108  * Returns true if the stream is bad. See std::ios_base
109  */
110  bool bad() const;
111 
112  /**
113  * Returns true if a stream operation failed. See std::ios_base
114  */
115  bool fail() const;
116 
117  /**
118  * Returns the number of physical bytes written so far. This is an estimate,
119  * especially if the file is gzip compressed.
120  * Returns (size_t)(-1) if there is no file opened.
121  */
122  size_t get_bytes_written() const;
123 
124  private:
125  /**
126  * The constructors redirect to this function to open the stream.
127  */
128  void open_file(std::string file, bool gzip_compressed);
129 };
130 
131 } // namespace fileio_impl
132 } // namespace turi
133 #endif
std::streamsize write(const char *c, std::streamsize bufsize)
~general_fstream_sink()
Destructor. Closes the file.
size_t FILEIO_WRITER_BUFFER_SIZE