@@ -246,6 +246,55 @@ void OpenObjectMultipleRangedRead(google::cloud::storage::AsyncClient& client,
246246 std::cout << " The ranges contain " << count << " newlines\n " ;
247247}
248248
249+ void OpenObjectWithInitialReadRanges (
250+ google::cloud::storage::AsyncClient& client,
251+ std::vector<std::string> const & argv) {
252+ // ! [open-object-initial-read-ranges]
253+ // [START storage_open_object_initial_read_ranges]
254+ namespace gcs = google::cloud::storage;
255+
256+ // Helper coroutine to count newlines returned by an AsyncReader.
257+ auto count_newlines =
258+ [](gcs::AsyncReader reader,
259+ gcs::AsyncToken token) -> google::cloud::future<std::uint64_t > {
260+ std::uint64_t count = 0 ;
261+ while (token.valid ()) {
262+ auto [payload, t] = (co_await reader.Read (std::move (token))).value ();
263+ token = std::move (t);
264+ for (auto const & buffer : payload.contents ()) {
265+ count += std::count (buffer.begin (), buffer.end (), ' \n ' );
266+ }
267+ }
268+ co_return count;
269+ };
270+
271+ auto coro =
272+ [&count_newlines](
273+ gcs::AsyncClient& client, std::string bucket_name,
274+ std::string object_name) -> google::cloud::future<std::uint64_t > {
275+ gcs::AsyncClient::InitialReadRanges config;
276+ config.initial_ranges = {{0 , 1024 }, {1024 , 1024 }};
277+
278+ auto descriptor =
279+ (co_await client.Open (gcs::BucketName (std::move (bucket_name)),
280+ std::move (object_name), std::move (config)))
281+ .value ();
282+
283+ auto [r1, t1] = descriptor.Read (0 , 1024 );
284+ auto [r2, t2] = descriptor.Read (1024 , 1024 );
285+
286+ auto c1 = count_newlines (std::move (r1), std::move (t1));
287+ auto c2 = count_newlines (std::move (r2), std::move (t2));
288+ co_return (co_await std::move (c1)) + (co_await std::move (c2));
289+ };
290+ // [END storage_open_object_initial_read_ranges]
291+ // ! [open-object-initial-read-ranges]
292+ // The example is easier to test and run if we call the coroutine and block
293+ // until it completes.
294+ auto const count = coro (client, argv.at (0 ), argv.at (1 )).get ();
295+ std::cout << " The pre-warmed ranges contain " << count << " newlines\n " ;
296+ }
297+
249298void OpenObjectReadFullObject (google::cloud::storage::AsyncClient& client,
250299 std::vector<std::string> const & argv) {
251300 // ! [open-object-read-full-object]
@@ -997,6 +1046,11 @@ void OpenObjectMultipleRangedRead(google::cloud::storage::AsyncClient&,
9971046 std::cerr << " AsyncClient::Open() example requires coroutines\n " ;
9981047}
9991048
1049+ void OpenObjectWithInitialReadRanges (google::cloud::storage::AsyncClient&,
1050+ std::vector<std::string> const &) {
1051+ std::cerr << " AsyncClient::Open() example requires coroutines\n " ;
1052+ }
1053+
10001054void OpenMultipleObjectsRangedRead (google::cloud::storage::AsyncClient&,
10011055 std::vector<std::string> const &) {
10021056 std::cerr << " AsyncClient::Open() example requires coroutines\n " ;
@@ -1306,6 +1360,10 @@ void AutoRun(std::vector<std::string> const& argv) {
13061360 << std::endl;
13071361 OpenObjectMultipleRangedRead (client, {bucket_name, composed_name});
13081362
1363+ std::cout << " Running the OpenObjectWithInitialReadRanges() example"
1364+ << std::endl;
1365+ OpenObjectWithInitialReadRanges (client, {bucket_name, composed_name});
1366+
13091367 std::cout << " Running the OpenMultipleObjectsRangedRead() example"
13101368 << std::endl;
13111369 auto const multi_read_o1 =
@@ -1563,6 +1621,8 @@ int main(int argc, char* argv[]) try {
15631621 OpenObjectSingleRangedRead),
15641622 make_entry (" open-object-multiple-ranged-read" , {},
15651623 OpenObjectMultipleRangedRead),
1624+ make_entry (" open-object-initial-read-ranges" , {},
1625+ OpenObjectWithInitialReadRanges),
15661626 make_entry (" open-object-read-full-object" , {}, OpenObjectReadFullObject),
15671627 make_entry (" open-multiple-objects-ranged-read" ,
15681628 {" <object-name-1>" , " <object-name-2>" , " <object-name-3>" },
0 commit comments