123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- // Copyright 2017 MongoDB Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #pragma once
- #include <istream>
- #include <memory>
- #include <ostream>
- #include <bsoncxx/document/view_or_value.hpp>
- #include <bsoncxx/stdx/string_view.hpp>
- #include <bsoncxx/types/bson_value/view.hpp>
- #include <mongocxx/cursor.hpp>
- #include <mongocxx/gridfs/downloader.hpp>
- #include <mongocxx/gridfs/uploader.hpp>
- #include <mongocxx/options/find.hpp>
- #include <mongocxx/options/gridfs/bucket.hpp>
- #include <mongocxx/options/gridfs/upload.hpp>
- #include <mongocxx/result/gridfs/upload.hpp>
- #include <mongocxx/stdx.hpp>
- #include <mongocxx/config/prelude.hpp>
- namespace mongocxx {
- MONGOCXX_INLINE_NAMESPACE_BEGIN
- class database;
- namespace gridfs {
- ///
- /// Class representing a GridFS bucket.
- ///
- /// A GridFS bucket is used to store files that may be too large to store in a single document due
- /// to the 16 MB limit. The bucket comprises of two collections, `<bucketname>.files` and
- /// `<bucketname>.chunks.` A file written to a GridFS bucket will be serialized into zero or more
- /// chunk documents stored in the `<bucketname>.chunks` collection, and one document will be stored
- /// in the `<bucketname>.files` collection containing the information about the file. Users should
- /// not modify these collections directly.
- ///
- /// Example of how obtain the default GridFS bucket for a given database:
- /// @code
- /// mongocxx::client mongo_client{mongocxx::uri{}};
- /// auto gridfs_bucket = mongo_client["database"].gridfs_bucket();
- /// @endcode
- ///
- /// See also the method documentation for `mongocxx::database::gridfs_bucket()`.
- ///
- /// @see http://www.mongodb.org/display/DOCS/GridFS
- ///
- class MONGOCXX_API bucket {
- public:
- ///
- /// Default constructs a bucket object. The bucket is equivalent to the state of a moved from
- /// bucket. The only valid actions to take with a default constructed bucket are to assign to
- /// it, or destroy it.
- ///
- bucket() noexcept;
- ///
- /// Move constructs a bucket.
- ///
- bucket(bucket&&) noexcept;
- ///
- /// Move assigns a bucket.
- ///
- bucket& operator=(bucket&&) noexcept;
- ///
- /// Copy constructs a bucket.
- ///
- bucket(const bucket&);
- ///
- /// Copy assigns a bucket.
- ///
- bucket& operator=(const bucket&);
- ///
- /// Destroys a bucket.
- ///
- ~bucket();
- ///
- /// Returns true if the bucket is valid, meaning it was not default constructed or moved from.
- ///
- explicit operator bool() const noexcept;
- ///
- /// @{
- ///
- /// Opens a gridfs::uploader to create a new GridFS file. The id of the file will be
- /// automatically generated as an ObjectId.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @return
- /// A stream for writing to the GridFS file.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- uploader open_upload_stream(stdx::string_view filename,
- const options::gridfs::upload& options = {});
- ///
- /// Opens a gridfs::uploader to create a new GridFS file. The id of the file will be
- /// automatically generated as an ObjectId.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the upload. The client session must
- /// remain valid for the lifetime of the uploader.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @return
- /// A stream for writing to the GridFS file.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- uploader open_upload_stream(const client_session& session,
- stdx::string_view filename,
- const options::gridfs::upload& options = {});
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Opens a gridfs::uploader to create a new GridFS file.
- ///
- /// @param id
- /// The unique id of the file being uploaded.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @return
- /// The gridfs::uploader to which the GridFS file should be written.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- uploader open_upload_stream_with_id(bsoncxx::types::bson_value::view id,
- stdx::string_view filename,
- const options::gridfs::upload& options = {});
- ///
- /// Opens a gridfs::uploader to create a new GridFS file.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the upload. The client session must
- /// remain valid for the lifetime of the uploader.
- ///
- /// @param id
- /// The unique id of the file being uploaded.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @return
- /// The gridfs::uploader to which the GridFS file should be written.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- uploader open_upload_stream_with_id(const client_session& session,
- bsoncxx::types::bson_value::view id,
- stdx::string_view filename,
- const options::gridfs::upload& options = {});
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Creates a new GridFS file by uploading bytes from an input stream. The id of the file will
- /// be automatically generated as an ObjectId.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param source
- /// The non-null stream from which the GridFS file should be read. The exception mask on
- /// `source` will be cleared of `eofbit` and set for `failbit` and `badbit`.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @return
- /// The id of the uploaded file.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::bulk_write_exception
- /// if an error occurs when writing chunk data or file metadata to the database.
- ///
- /// @throws std::ios_base::failure
- /// if reading from `source` fails. Any exception thrown during the execution of
- /// `source::read()` will be re-thrown.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
- /// size.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- result::gridfs::upload upload_from_stream(stdx::string_view filename,
- std::istream* source,
- const options::gridfs::upload& options = {});
- ///
- /// Creates a new GridFS file by uploading bytes from an input stream. The id of the file will
- /// be automatically generated as an ObjectId.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the upload.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param source
- /// The non-null stream from which the GridFS file should be read. The exception mask on
- /// `source` will be cleared of `eofbit` and set for `failbit` and `badbit`.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @return
- /// The id of the uploaded file.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::bulk_write_exception
- /// if an error occurs when writing chunk data or file metadata to the database.
- ///
- /// @throws std::ios_base::failure
- /// if reading from `source` fails. Any exception thrown during the execution of
- /// `source::read()` will be re-thrown.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
- /// size.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- result::gridfs::upload upload_from_stream(const client_session& session,
- stdx::string_view filename,
- std::istream* source,
- const options::gridfs::upload& options = {});
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Creates a new GridFS file with a user-supplied unique id by uploading bytes from an input
- /// stream.
- ///
- /// @param id
- /// A unique id for the file being uploaded.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param source
- /// The non-null stream from which the GridFS file should be read. The exception mask on
- /// `source` will be cleared of `eofbit` and set for `failbit` and `badbit`.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::bulk_write_exception
- /// if an error occurs when writing chunk data or file metadata to the database.
- ///
- /// @throws std::ios_base::failure
- /// if reading from `source` fails. Any exception thrown during the execution of
- /// `source::read()` will be re-thrown.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
- /// size.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- void upload_from_stream_with_id(bsoncxx::types::bson_value::view id,
- stdx::string_view filename,
- std::istream* source,
- const options::gridfs::upload& options = {});
- ///
- /// Creates a new GridFS file with a user-supplied unique id by uploading bytes from an input
- /// stream.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the upload.
- ///
- /// @param id
- /// A unique id for the file being uploaded.
- ///
- /// @param filename
- /// The name of the file to be uploaded. A bucket can contain multiple files with the same
- /// name.
- ///
- /// @param source
- /// The non-null stream from which the GridFS file should be read. The exception mask on
- /// `source` will be cleared of `eofbit` and set for `failbit` and `badbit`.
- ///
- /// @param options
- /// Optional arguments; see options::gridfs::upload.
- ///
- /// @note
- /// If this GridFS bucket does not already exist in the database, it will be implicitly
- /// created and initialized with GridFS indexes.
- ///
- /// @throws mongocxx::logic_error if `options` are invalid.
- ///
- /// @throws mongocxx::bulk_write_exception
- /// if an error occurs when writing chunk data or file metadata to the database.
- ///
- /// @throws std::ios_base::failure
- /// if reading from `source` fails. Any exception thrown during the execution of
- /// `source::read()` will be re-thrown.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
- /// size.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- /// @throws mongocxx::operation_exception if an error occurs when building GridFS indexes.
- ///
- void upload_from_stream_with_id(const client_session& session,
- bsoncxx::types::bson_value::view id,
- stdx::string_view filename,
- std::istream* source,
- const options::gridfs::upload& options = {});
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Opens a gridfs::downloader to read a GridFS file.
- ///
- /// @param id
- /// The id of the file to read.
- ///
- /// @return
- /// The gridfs::downloader from which the GridFS file should be read.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the requested file does not exist, or if the requested file has been corrupted.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- downloader open_download_stream(bsoncxx::types::bson_value::view id);
- ///
- /// Opens a gridfs::downloader to read a GridFS file.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the download. The client session must
- /// remain valid for the lifetime of the downloader.
- ///
- /// @param id
- /// The id of the file to read.
- ///
- /// @return
- /// The gridfs::downloader from which the GridFS file should be read.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the requested file does not exist, or if the requested file has been corrupted.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files collection for this bucket.
- ///
- downloader open_download_stream(const client_session& session,
- bsoncxx::types::bson_value::view id);
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Downloads the contents of a stored GridFS file from the bucket and writes it to a stream.
- ///
- /// @param id
- /// The id of the file to read.
- ///
- /// @param destination
- /// The non-null stream to which the GridFS file should be written.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the requested file does not exist, or if the requested file has been corrupted.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files or chunks collections for this bucket.
- ///
- /// @throws std::ios_base::failure
- /// if writing to `destination` fails. In addition, if `destination::exceptions()` is set for
- /// `badbit`, any exception thrown during execution of `destination::write()` will be
- /// re-thrown.
- ///
- void download_to_stream(bsoncxx::types::bson_value::view id, std::ostream* destination);
- ///
- /// Downloads the contents of a stored GridFS file from the bucket and writes it to a stream.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the download.
- ///
- /// @param id
- /// The id of the file to read.
- ///
- /// @param destination
- /// The non-null stream to which the GridFS file should be written.
- ///
- /// @throws mongocxx::gridfs_exception
- /// if the requested file does not exist, or if the requested file has been corrupted.
- ///
- /// @throws mongocxx::query_exception
- /// if an error occurs when reading from the files or chunks collections for this bucket.
- ///
- /// @throws std::ios_base::failure
- /// if writing to `destination` fails. In addition, if `destination::exceptions()` is set for
- /// `badbit`, any exception thrown during execution of `destination::write()` will be
- /// re-thrown.
- ///
- void download_to_stream(const client_session& session,
- bsoncxx::types::bson_value::view id,
- std::ostream* destination);
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Deletes a GridFS file from the bucket.
- ///
- /// @param id
- /// The id of the file to be deleted.
- ///
- /// @throws mongocxx::gridfs_exception if the requested file does not exist.
- ///
- /// @throws mongocxx::bulk_write_exception
- /// if an error occurs when removing file data or chunk data from the database.
- ///
- void delete_file(bsoncxx::types::bson_value::view id);
- ///
- /// Deletes a GridFS file from the bucket.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the delete.
- ///
- /// @param id
- /// The id of the file to be deleted.
- ///
- /// @throws mongocxx::gridfs_exception if the requested file does not exist.
- ///
- /// @throws mongocxx::bulk_write_exception
- /// if an error occurs when removing file data or chunk data from the database.
- ///
- void delete_file(const client_session& session, bsoncxx::types::bson_value::view id);
- ///
- /// @}
- ///
- ///
- /// @{
- ///
- /// Finds the documents in the files collection of the bucket which match the provided filter.
- ///
- /// @param filter
- /// Document view representing a document that should match the query.
- ///
- /// @param options
- /// Optional arguments; see options::find.
- ///
- /// @return
- /// A mongocxx::cursor with the results. If the query fails, the cursor throws
- /// mongocxx::query_exception when the returned cursor is iterated.
- ///
- /// @throws mongocxx::logic_error if the options are invalid, or if the unsupported option
- /// modifiers "$query" or "$explain" are used.
- ///
- /// @see mongocxx::collection::find.
- ///
- cursor find(bsoncxx::document::view_or_value filter, const options::find& options = {});
- ///
- /// Finds the documents in the files collection of the bucket which match the provided filter.
- ///
- /// @param session
- /// The mongocxx::client_session with which to perform the query. The client session must
- /// remain valid for the lifetime of the cursor.
- ///
- /// @param filter
- /// Document view representing a document that should match the query.
- ///
- /// @param options
- /// Optional arguments; see options::find.
- ///
- /// @return
- /// A mongocxx::cursor with the results. If the query fails, the cursor throws
- /// mongocxx::query_exception when the returned cursor is iterated.
- ///
- /// @throws mongocxx::logic_error if the options are invalid, or if the unsupported option
- /// modifiers "$query" or "$explain" are used.
- ///
- /// @see mongocxx::collection::find.
- ///
- cursor find(const client_session& session,
- bsoncxx::document::view_or_value filter,
- const options::find& options = {});
- ///
- /// @}
- ///
- ///
- /// Gets the name of the GridFS bucket.
- ///
- /// @return
- /// The name of the GridFS bucket.
- ///
- stdx::string_view bucket_name() const;
- private:
- friend class mongocxx::database;
- // Constructs a new GridFS bucket. Throws if options are invalid.
- MONGOCXX_PRIVATE bucket(const database& db, const options::gridfs::bucket& options);
- MONGOCXX_PRIVATE void create_indexes_if_nonexistent(const client_session* session);
- MONGOCXX_PRIVATE uploader _open_upload_stream_with_id(const client_session* session,
- bsoncxx::types::bson_value::view id,
- stdx::string_view filename,
- const options::gridfs::upload& options);
- MONGOCXX_PRIVATE void _upload_from_stream_with_id(const client_session* session,
- bsoncxx::types::bson_value::view id,
- stdx::string_view filename,
- std::istream* source,
- const options::gridfs::upload& options);
- MONGOCXX_PRIVATE downloader _open_download_stream(const client_session* session,
- bsoncxx::types::bson_value::view id);
- MONGOCXX_PRIVATE void _download_to_stream(const client_session* session,
- bsoncxx::types::bson_value::view id,
- std::ostream* destination);
- MONGOCXX_PRIVATE void _delete_file(const client_session* session,
- bsoncxx::types::bson_value::view id);
- class MONGOCXX_PRIVATE impl;
- MONGOCXX_PRIVATE impl& _get_impl();
- MONGOCXX_PRIVATE const impl& _get_impl() const;
- std::unique_ptr<impl> _impl;
- };
- } // namespace gridfs
- MONGOCXX_INLINE_NAMESPACE_END
- } // namespace mongocxx
- #include <mongocxx/config/postlude.hpp>
|