MongoDB C++ Driver  mongocxx-3.10.2
downloader.hpp
1 // Copyright 2017 MongoDB Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 
21 #include <mongocxx/gridfs/bucket-fwd.hpp>
22 #include <mongocxx/gridfs/downloader-fwd.hpp>
23 
24 #include <bsoncxx/document/value.hpp>
25 #include <bsoncxx/document/view.hpp>
26 #include <bsoncxx/stdx/optional.hpp>
27 #include <bsoncxx/types/bson_value/view.hpp>
28 #include <mongocxx/cursor.hpp>
29 #include <mongocxx/stdx.hpp>
30 
31 #include <mongocxx/config/prelude.hpp>
32 
33 namespace mongocxx {
34 namespace v_noabi {
35 namespace gridfs {
36 
41  std::int32_t chunks_offset = 0;
42  std::int32_t bytes_offset = 0;
43 };
44 
48 class downloader {
49  public:
55  downloader() noexcept;
56 
60  downloader(downloader&&) noexcept;
61 
65  downloader& operator=(downloader&&) noexcept;
66 
67  downloader(const downloader&) = delete;
68 
69  downloader& operator=(const downloader&) = delete;
70 
75 
80  explicit operator bool() const noexcept;
81 
102  std::size_t read(std::uint8_t* buffer, std::size_t length);
103 
109  void close();
110 
117  std::int32_t chunk_size() const;
118 
125  std::int64_t file_length() const;
126 
133  bsoncxx::v_noabi::document::view files_document() const;
134 
135  private:
136  friend ::mongocxx::v_noabi::gridfs::bucket;
137 
138  //
139  // Constructs a new downloader stream.
140  //
141  // @param chunks
142  // The cursor to read the chunks of the file from. It must have a value if the length of the
143  // file is non-zero.
144  //
145  // @param start
146  // The offset from which to start reading the chunks of the file.
147  //
148  // @param chunk_size
149  // The expected size of a chunk in bytes.
150  //
151  // @param file_len
152  // The expected size of the file in bytes.
153  //
154  // @param files_doc
155  // The files collection document of the file being downloaded.
156  //
157  MONGOCXX_PRIVATE downloader(stdx::optional<cursor> chunks,
159  std::int32_t chunk_size,
160  std::int64_t file_len,
161  bsoncxx::v_noabi::document::value files_doc);
162 
163  MONGOCXX_PRIVATE void fetch_chunk();
164 
165  class MONGOCXX_PRIVATE impl;
166 
167  MONGOCXX_PRIVATE impl& _get_impl();
168  MONGOCXX_PRIVATE const impl& _get_impl() const;
169 
170  std::unique_ptr<impl> _impl;
171 };
172 
173 } // namespace gridfs
174 } // namespace v_noabi
175 } // namespace mongocxx
176 
177 // CXX-2770: missing include of postlude header.
178 #if defined(MONGOCXX_TEST_MACRO_GUARDS_FIX_MISSING_POSTLUDE)
179 #include <mongocxx/config/postlude.hpp>
180 #endif
Class representing a pointer to the result set of a query on a MongoDB server.
Definition: cursor.hpp:42
Class representing a GridFS bucket.
Definition: bucket.hpp:63
Class used to download a GridFS file.
Definition: downloader.hpp:48
downloader() noexcept
Default constructs a downloader object.
bsoncxx::v_noabi::document::view files_document() const
Gets the files collection document of the file being downloaded.
std::int32_t chunk_size() const
Gets the chunk size of the file being downloaded.
void close()
Closes the downloader stream.
std::size_t read(std::uint8_t *buffer, std::size_t length)
Reads a specified number of bytes from the GridFS file being downloaded.
std::int64_t file_length() const
Gets the length of the file being downloaded.
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19
The top-level namespace for mongocxx library entities.
Definition: bulk_write-fwd.hpp:19
Class used to specify the offset from which to start reading the chunks of the file.
Definition: downloader.hpp:40