Skip to content

Commit 6a0218e

Browse files
committed
CXX-1314 Switch order of pointer/length parameters to gridfs::uploader::write and gridfs::downloader::read
1 parent 7653032 commit 6a0218e

File tree

10 files changed

+25
-25
lines changed

10 files changed

+25
-25
lines changed

examples/mongocxx/gridfs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int main() {
4545

4646
// Write 50 bytes to the file.
4747
for (auto i = 0; i < 5; ++i) {
48-
uploader.write(10, bytes);
48+
uploader.write(bytes, 10);
4949
}
5050

5151
auto result = uploader.close();
@@ -62,7 +62,7 @@ int main() {
6262
auto buffer_size = std::min(file_length, static_cast<std::int64_t>(downloader.chunk_size()));
6363
auto buffer = make_unique<std::uint8_t[]>(buffer_size);
6464

65-
while (auto length_read = downloader.read(buffer_size, buffer.get())) {
65+
while (auto length_read = downloader.read(buffer.get(), buffer_size)) {
6666
bytes_counter += length_read;
6767

6868
// Do something with the contents of buffer.

src/mongocxx/gridfs/bucket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void bucket::upload_from_stream_with_id(bsoncxx::types::value id,
154154

155155
while (!source->eof()) {
156156
source->read(reinterpret_cast<char*>(buffer.get()), static_cast<std::size_t>(chunk_size));
157-
upload_stream.write(source->gcount(), buffer.get());
157+
upload_stream.write(buffer.get(), source->gcount());
158158
}
159159

160160
upload_stream.close();
@@ -209,7 +209,7 @@ void bucket::download_to_stream(bsoncxx::types::value id, std::ostream* destinat
209209
std::size_t bytes_read;
210210

211211
while ((bytes_read =
212-
download_stream.read(static_cast<std::size_t>(chunk_size), buffer.get())) != 0) {
212+
download_stream.read(buffer.get(), static_cast<std::size_t>(chunk_size))) != 0) {
213213
destination->write(reinterpret_cast<char*>(buffer.get()), bytes_read);
214214
}
215215

src/mongocxx/gridfs/downloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ downloader::operator bool() const noexcept {
4242
return static_cast<bool>(_impl);
4343
}
4444

45-
std::size_t downloader::read(std::size_t length_requested, std::uint8_t* buffer) {
45+
std::size_t downloader::read(std::uint8_t* buffer, std::size_t length_requested) {
4646
if (_get_impl().closed) {
4747
throw logic_error{error_code::k_gridfs_stream_not_open};
4848
}

src/mongocxx/gridfs/downloader.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ class MONGOCXX_API downloader {
7171
///
7272
/// Reads a specified number of bytes from the GridFS file being downloaded.
7373
///
74-
/// @param length
75-
/// The number of bytes to read from the file.
76-
///
7774
/// @param buffer
7875
/// A pointer to a buffer to store the bytes read from the file.
7976
///
77+
/// @param length
78+
/// The number of bytes to read from the file.
79+
///
8080
/// @return
8181
/// The number of bytes actually read. If zero, the downloader has reached the end of the
8282
/// file.
@@ -88,7 +88,7 @@ class MONGOCXX_API downloader {
8888
/// @throws mongocxx::query_exception
8989
/// if an error occurs when reading chunk data from the database for the requested file.
9090
///
91-
std::size_t read(std::size_t length, std::uint8_t* buffer);
91+
std::size_t read(std::uint8_t* buffer, std::size_t length);
9292

9393
///
9494
/// Closes the downloader stream.

src/mongocxx/gridfs/uploader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ uploader::operator bool() const noexcept {
6868
return static_cast<bool>(_impl);
6969
}
7070

71-
void uploader::write(std::size_t length, const std::uint8_t* bytes) {
71+
void uploader::write(const std::uint8_t* bytes, std::size_t length) {
7272
if (_get_impl().closed) {
7373
throw logic_error{error_code::k_gridfs_stream_not_open};
7474
}

src/mongocxx/gridfs/uploader.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ class MONGOCXX_API uploader {
7272
///
7373
/// Writes a specified number of bytes to a GridFS file.
7474
///
75-
/// @param length
76-
/// The number of bytes to write.
77-
///
7875
/// @param bytes
7976
/// A pointer to the bytes to write.
8077
///
78+
/// @param length
79+
/// The number of bytes to write.
80+
///
8181
/// @throws mongocxx::logic_error if the upload stream was already closed.
8282
///
8383
/// @throws mongocxx::bulk_write_exception
@@ -87,7 +87,7 @@ class MONGOCXX_API uploader {
8787
/// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
8888
/// size.
8989
///
90-
void write(std::size_t length, const std::uint8_t* bytes);
90+
void write(const std::uint8_t* bytes, std::size_t length);
9191

9292
///
9393
/// Closes the uploader stream.

src/mongocxx/test/gridfs/bucket.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TEST_CASE("downloading throws error when chunks document is corrupt", "[gridfs::
297297
auto downloader_read_one = [&downloader]() {
298298
stdx::optional<std::uint8_t> result;
299299
std::uint8_t byte;
300-
std::size_t bytes_downloaded = downloader.read(1, &byte);
300+
std::size_t bytes_downloaded = downloader.read(&byte, 1);
301301
if (bytes_downloaded > 0) {
302302
result = byte;
303303
}
@@ -407,7 +407,7 @@ TEST_CASE("mongocxx::gridfs::downloader::read with arbitrary sizes", "[gridfs::d
407407
std::size_t total_bytes_read = 0;
408408
auto downloader = bucket.open_download_stream(bsoncxx::types::value{id});
409409

410-
while (std::size_t bytes_read = downloader.read(read_size, buffer.data())) {
410+
while (std::size_t bytes_read = downloader.read(buffer.data(), read_size)) {
411411
std::vector<std::uint8_t> expected_bytes{expected.data() + total_bytes_read,
412412
expected.data() + total_bytes_read + bytes_read};
413413
std::vector<std::uint8_t> actual_bytes{buffer.data(), buffer.data() + bytes_read};
@@ -510,7 +510,7 @@ TEST_CASE("mongocxx::gridfs::uploader::write with arbitrary sizes", "[gridfs::up
510510
std::min(static_cast<std::int64_t>(write_size),
511511
file_length - static_cast<std::int64_t>(bytes_written)));
512512

513-
uploader.write(actual_write_size, bytes.data() + bytes_written);
513+
uploader.write(bytes.data() + bytes_written, actual_write_size);
514514
bytes_written += actual_write_size;
515515
}
516516

@@ -565,15 +565,15 @@ TEST_CASE("gridfs upload/download round trip", "[gridfs::uploader] [gridfs::down
565565
std::array<std::uint8_t, 100> downloaded_bytes;
566566

567567
auto uploader = bucket.open_upload_stream("file");
568-
uploader.write(100, uploaded_bytes.data());
568+
uploader.write(uploaded_bytes.data(), 100);
569569
auto result = uploader.close();
570570

571571
auto downloader = bucket.open_download_stream(result.id());
572-
auto bytes_read = downloader.read(100, downloaded_bytes.data());
572+
auto bytes_read = downloader.read(downloaded_bytes.data(), 100);
573573
REQUIRE(bytes_read == 100);
574574

575575
std::uint8_t c;
576-
REQUIRE(downloader.read(1, &c) == 0);
576+
REQUIRE(downloader.read(&c, 1) == 0);
577577

578578
REQUIRE(uploaded_bytes == downloaded_bytes);
579579
}

src/mongocxx/test/gridfs/downloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ TEST_CASE("mongocxx::gridfs::downloader default constructor makes invalid downlo
2626
gridfs::downloader downloader;
2727
REQUIRE(!downloader);
2828
std::uint8_t c;
29-
REQUIRE_THROWS_AS(downloader.read(1, &c), logic_error);
29+
REQUIRE_THROWS_AS(downloader.read(&c, 1), logic_error);
3030
}

src/mongocxx/test/gridfs/uploader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ TEST_CASE("mongocxx::gridfs::uploader default constructor makes invalid uploader
2626
gridfs::uploader uploader;
2727
REQUIRE(!uploader);
2828
std::uint8_t c = 0x0;
29-
REQUIRE_THROWS_AS(uploader.write(1, &c), logic_error);
29+
REQUIRE_THROWS_AS(uploader.write(&c, 1), logic_error);
3030
}

src/mongocxx/test/spec/gridfs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void test_download(database db,
223223

224224
// Otherwise, an error should occur when reading from the stream.
225225
gridfs::downloader downloader = bucket.open_download_stream(id);
226-
REQUIRE_THROWS_AS(downloader.read(length, actual.get()), std::exception);
226+
REQUIRE_THROWS_AS(downloader.read(actual.get(), length), std::exception);
227227

228228
return;
229229
}
@@ -233,7 +233,7 @@ void test_download(database db,
233233

234234
gridfs::downloader downloader = bucket.open_download_stream(id);
235235

236-
std::int64_t actual_size = downloader.read(length, actual.get());
236+
std::int64_t actual_size = downloader.read(actual.get(), length);
237237
REQUIRE(actual_size == length);
238238

239239
// The GridFS spec specifies the expected binary data in the form of { $hex: "<hexadecimal
@@ -283,7 +283,7 @@ void test_upload(database db,
283283
std::string hex = source["$hex"].get_utf8().value.to_string();
284284
std::basic_string<std::uint8_t> source_bytes = test_util::convert_hex_string_to_bytes(hex);
285285

286-
uploader.write(source_bytes.size(), source_bytes.data());
286+
uploader.write(source_bytes.data(), source_bytes.size());
287287
result::gridfs::upload upload_result = uploader.close();
288288
auto id = upload_result.id();
289289

0 commit comments

Comments
 (0)