Turi Create  4.0
cache_stream_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 CACHE_STREAM_SINK_HPP
7 #define CACHE_STREAM_SINK_HPP
8 
9 #include <iostream>
10 #include <memory>
11 #include <boost/iostreams/stream.hpp>
12 #include <core/storage/fileio/general_fstream_sink.hpp>
13 #include <core/storage/fileio/fixed_size_cache_manager.hpp>
14 
15 namespace turi {
16 namespace fileio_impl {
17 
18 /**
19  * \internal
20  *
21  * A boost::iostreams::sink concept implemented using cache_block as the underlying
22  * sink device.
23  */
25 
26  typedef fileio::cache_id_type cache_id_type;
27 
28  public:
29  typedef char char_type;
30  struct category: public boost::iostreams::sink_tag,
31  boost::iostreams::closable_tag,
32  boost::iostreams::multichar_tag {};
33 
34  /**
35  * Construct the sink from a cache_id.
36  *
37  * Intialize the underlying data sink, either the in memory array
38  * or the on disk cache file.
39  */
40  explicit cache_stream_sink(cache_id_type cache_id);
41 
42  /// Destructor. CLoses the stream.
44 
45  /**
46  * Attempts to write bufsize bytes into the stream from the buffer.
47  * Returns the actual number of bytes written. Returns -1 on failure.
48  */
49  std::streamsize write(const char* c, std::streamsize bufsize);
50 
51  /**
52  * Returns true if the file is opened
53  */
54  bool is_open() const;
55 
56  /**
57  * Closes all file handles
58  */
59  void close();
60 
61  /**
62  * Seeks to a different location. Will fail on compressed files.
63  */
64  std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
65 
66  /**
67  * Returns true if the stream is good. See std::ios_base
68  */
69  bool good() const;
70 
71  /**
72  * Returns true if the stream is bad. See std::ios_base
73  */
74  bool bad() const;
75 
76  /**
77  * Returns true if a stream operation failed. See std::ios_base
78  */
79  bool fail() const;
80 
81 
82  private:
83  fileio::fixed_size_cache_manager& cache_manager;
84  std::shared_ptr<fileio::cache_block> out_block;
85  std::shared_ptr<general_fstream_sink> out_file;
86 };
87 
88 
89 } // end of fileio
90 } // end of turicreate
91 
92 #endif
std::streamsize write(const char *c, std::streamsize bufsize)
cache_stream_sink(cache_id_type cache_id)
std::streampos seek(std::streamoff off, std::ios_base::seekdir way)
~cache_stream_sink()
Destructor. CLoses the stream.