docs(storage): add samples for PrecomputedChecksumsOption - #16308
docs(storage): add samples for PrecomputedChecksumsOption#16308v-pratap wants to merge 3 commits into
Conversation
Use the new PrecomputedChecksumsOption instead of UseCrc32cValueOption in the async upload sample.
Added and examples to demonstrate how to provide precomputed hashes to synchronous client calls.
There was a problem hiding this comment.
Code Review
This pull request updates the Google Cloud Storage C++ examples to demonstrate the use of precomputed checksums when inserting and writing objects. It replaces the deprecated UseCrc32cValueOption with PrecomputedChecksumsOption in async samples and introduces new synchronous examples (InsertObjectWithChecksum and WriteObjectWithChecksum). The review feedback highlights a misleading use of std::move on a const reference string parameter in InsertObjectWithChecksum, which could also lead to an order-of-evaluation bug if the parameter is ever refactored to be passed by value.
| StatusOr<gcs::ObjectMetadata> object_metadata = client.InsertObject( | ||
| bucket_name, object_name, std::move(contents), | ||
| google::cloud::Options{}.set<gcs::PrecomputedChecksumsOption>( | ||
| gcs::PrecomputedChecksums{ | ||
| gcs::ComputeCrc32cChecksum(contents), ""})); |
There was a problem hiding this comment.
Using std::move(contents) on a std::string const& is misleading because it does not actually move the string (it results in a copy). More importantly, if contents is ever refactored to be passed by value (e.g., std::string contents) to allow a true move, this code introduces a severe order-of-evaluation bug. Because the order of evaluation of function arguments is indeterminately sequenced, std::move(contents) could be evaluated before gcs::ComputeCrc32cChecksum(contents), causing the checksum to be computed on an empty, moved-from string.
To prevent this, compute the checksum first and store it in a local variable, then pass contents without the misleading std::move.
auto checksum = gcs::ComputeCrc32cChecksum(contents);
StatusOr<gcs::ObjectMetadata> object_metadata = client.InsertObject(
bucket_name, object_name, contents,
google::cloud::Options{}.set<gcs::PrecomputedChecksumsOption>(
gcs::PrecomputedChecksums{std::move(checksum), ""}));- Remove empty string explicitly passed for MD5 hash in precomputed checksums. - Add an example demonstrating .
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16308 +/- ##
==========================================
+ Coverage 92.29% 92.31% +0.01%
==========================================
Files 2226 2226
Lines 208573 208851 +278
==========================================
+ Hits 192504 192792 +288
+ Misses 16069 16059 -10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| [](gcs::Client client, std::string const& bucket_name, | ||
| std::string const& object_name, std::string const& contents) { | ||
| try { | ||
| client.InsertObject( |
There was a problem hiding this comment.
client.InsertObject(...) returns a StatusOr<ObjectMetadata>, and it does not throw exceptions by default when an API error occurs. Because the returned StatusOr is ignored, the bad checksum error will be silently dropped, and the catch block will never execute.
| [](gcs::Client client, std::string const& bucket_name, | ||
| std::string const& object_name, std::string const& contents) { | ||
| StatusOr<gcs::ObjectMetadata> object_metadata = client.InsertObject( | ||
| bucket_name, object_name, std::move(contents), |
There was a problem hiding this comment.
Calling std::move on a const reference doesn't move anything; it forces a copy. This appears to be inherited from the original InsertObject snippet, but it is an anti-pattern, so let's avoid it. Here and below.
This PR updates the examples to demonstrate how to use
PrecomputedChecksumsOption.UseCrc32cValueOptioninstorage_async_samples.ccwithPrecomputedChecksumsOption.InsertObjectWithChecksumandWriteObjectWithChecksumtostorage_object_samples.ccto demonstrate providing precomputed hashes to synchronous client calls.These changes are a follow-up to the previously merged PR that introduced
PrecomputedChecksumsOption.