MongoDB C++ Driver  mongocxx-3.2.0
uploader.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 <bsoncxx/document/value.hpp>
22 #include <bsoncxx/stdx/optional.hpp>
23 #include <bsoncxx/stdx/string_view.hpp>
24 #include <bsoncxx/types/value.hpp>
25 #include <bsoncxx/view_or_value.hpp>
26 #include <mongocxx/collection.hpp>
27 #include <mongocxx/result/gridfs/upload.hpp>
28 #include <mongocxx/stdx.hpp>
29 
30 #include <mongocxx/config/prelude.hpp>
31 
32 namespace mongocxx {
33 MONGOCXX_INLINE_NAMESPACE_BEGIN
34 namespace gridfs {
35 
39 class MONGOCXX_API uploader {
40  public:
46  uploader() noexcept;
47 
51  uploader(uploader&&) noexcept;
52 
56  uploader& operator=(uploader&&) noexcept;
57 
58  uploader(const uploader&) = delete;
59 
60  uploader& operator=(const uploader&) = delete;
61 
65  ~uploader();
66 
70  explicit operator bool() const noexcept;
71 
90  void write(const std::uint8_t* bytes, std::size_t length);
91 
104  result::gridfs::upload close();
105 
114  void abort();
115 
122  std::int32_t chunk_size() const;
123 
124  private:
125  friend class bucket;
126 
127  //
128  // Constructs a new uploader stream.
129  //
130  // @param id
131  // The id of the GridFS file being uploaded.
132  //
133  // @param files
134  // The files collection of the bucket receiving the file.
135  //
136  // @param chunks
137  // The chunks collection of the bucket receiving the file.
138  //
139  // @param chunk_size
140  // The size in bytes of the chunks being uploaded.
141  //
142  // @param metadata
143  // Optional metadata field of the files collection document.
144  //
145  MONGOCXX_PRIVATE uploader(bsoncxx::types::value id,
146  stdx::string_view filename,
147  collection files,
148  collection chunks,
149  std::int32_t chunk_size,
150  stdx::optional<bsoncxx::document::view_or_value> metadata = {});
151 
152  MONGOCXX_PRIVATE void finish_chunk();
153  MONGOCXX_PRIVATE void flush_chunks();
154 
155  class MONGOCXX_PRIVATE impl;
156 
157  MONGOCXX_PRIVATE impl& _get_impl();
158  MONGOCXX_PRIVATE const impl& _get_impl() const;
159 
160  std::unique_ptr<impl> _impl;
161 };
162 
163 } // namespace gridfs
164 MONGOCXX_INLINE_NAMESPACE_END
165 } // namespace mongocxx
Top level namespace for the MongoDB C++ driver.
Definition: bulk_write.hpp:22
Class used to upload a GridFS file.
Definition: uploader.hpp:39
Class representing a GridFS bucket.
Definition: bucket.hpp:62
Class representing the result of a GridFS upload operation.
Definition: upload.hpp:28
A variant that can contain any BSON type.
Definition: value.hpp:37
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:84