Turi Create  4.0
cache_stream_source.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_SOURCE_HPP
7 #define CACHE_STREAM_SOURCE_HPP
8 
9 #include <iostream>
10 #include <boost/iostreams/stream.hpp>
11 #include <core/storage/fileio/general_fstream_source.hpp>
12 #include <core/storage/fileio/fixed_size_cache_manager.hpp>
13 
14 namespace turi {
15 namespace fileio_impl {
16 
17 /**
18  * \internal
19  *
20  * A boost::iostreams::input_seekable concept implemented using cache_block as the underlying
21  * source device.
22  */
24 
25  typedef fileio::cache_id_type cache_id_type;
26 
27  public:
28  typedef char char_type;
29  struct category: public boost::iostreams::device_tag,
30  boost::iostreams::closable_tag,
31  boost::iostreams::multichar_tag,
32  boost::iostreams::input_seekable {};
33 
34  /**
35  * Construct the source from a cache_id.
36  *
37  * Intialize the underlying datasources, either the in memory array
38  * or the on disk cache file.
39  */
40  explicit cache_stream_source(cache_id_type cache_id);
41 
42  /**
43  * Attempts to read bufsize bytes into the buffer provided.
44  * Returns the actual number of bytes read.
45  */
46  std::streamsize read(char* c, std::streamsize bufsize);
47 
48  /**
49  * Closes all file handles.
50  */
51  void close();
52 
53  /**
54  * Returns true if the stream is opened.
55  */
56  bool is_open() const;
57 
58  /**
59  * Seeks to a different location. Will fail on compressed files.
60  */
61  std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
62 
63  /**
64  * Returns the file size of the opened file.
65  * Returns (size_t)(-1) if there is no file opened, or if there is an
66  * error obtaining the file size.
67  */
68  size_t file_size() const;
69 
70  /**
71  * Returns the underlying stream object. The underlying stream object
72  * is an boost array_source if the cache is in memory, and is a
73  * some other stream object otherwise.
74  */
75  std::shared_ptr<std::istream> get_underlying_stream();
76 
77  private:
78  char* in_array;
79  size_t array_size;
80  size_t array_cur_pos;
81  std::shared_ptr<fileio::cache_block> in_block;
82  std::shared_ptr<general_fstream_source> in_file;
83 };
84 
85 
86 } // end of fileio
87 } // end of turicreate
88 
89 #endif
std::streamsize read(char *c, std::streamsize bufsize)
std::shared_ptr< std::istream > get_underlying_stream()
cache_stream_source(cache_id_type cache_id)
std::streampos seek(std::streamoff off, std::ios_base::seekdir way)