@@ -34,17 +34,17 @@ ObjectDescriptorImpl::ObjectDescriptorImpl(
3434 OpenStreamFactory make_stream,
3535 google::storage::v2::BidiReadObjectSpec read_object_spec,
3636 std::shared_ptr<OpenStream> stream, Options options)
37- : resume_policy_ (std::move(resume_policy)),
37+ : resume_policy_prototype_ (std::move(resume_policy)),
3838 make_stream_ (std::move(make_stream)),
3939 read_object_spec_(std::move(read_object_spec)),
40- options_(std::move(options)),
41- streams_{ std::move (stream)} {
42- AddNewActiveRanges ( );
40+ options_(std::move(options)) {
41+ streams_. push_back (
42+ Stream{ std::move (stream), {}, resume_policy_prototype_-> clone ()} );
4343}
4444
4545ObjectDescriptorImpl::~ObjectDescriptorImpl () {
4646 for (auto const & stream : streams_) {
47- stream->Cancel ();
47+ stream. stream ->Cancel ();
4848 }
4949}
5050
@@ -55,7 +55,7 @@ void ObjectDescriptorImpl::Start(
5555
5656void ObjectDescriptorImpl::Cancel () {
5757 for (auto const & stream : streams_) {
58- stream->Cancel ();
58+ stream. stream ->Cancel ();
5959 }
6060}
6161
@@ -75,12 +75,11 @@ void ObjectDescriptorImpl::MakeSubsequentStream() {
7575
7676 *request.mutable_read_object_spec () = read_object_spec_;
7777 auto stream_result = make_stream_ (std::move (request)).get ();
78- auto stream = std::move (stream_result->stream );
7978
8079 std::unique_lock<std::mutex> lk (mu_);
81- active_stream_ = streams_.size ();
82- streams_. push_back ( std::move (stream));
83- AddNewActiveRanges (lk );
80+ streams_.push_back (Stream{ std::move (stream_result-> stream ),
81+ {},
82+ resume_policy_prototype_-> clone ()} );
8483 lk.unlock ();
8584 OnRead (std::move (stream_result->first_response ));
8685}
@@ -99,7 +98,7 @@ ObjectDescriptorImpl::Read(ReadParams p) {
9998
10099 std::unique_lock<std::mutex> lk (mu_);
101100 auto const id = ++read_id_generator_;
102- active_ranges_[active_stream_] .emplace (id, range);
101+ streams_. back (). active_ranges .emplace (id, range);
103102 auto & read_range = *next_request_.add_read_ranges ();
104103 read_range.set_read_id (id);
105104 read_range.set_read_offset (p.start );
@@ -176,7 +175,8 @@ void ObjectDescriptorImpl::OnRead(
176175
177176void ObjectDescriptorImpl::CleanupDoneRanges (
178177 std::unique_lock<std::mutex> const &) {
179- auto & active_ranges = active_ranges_[active_stream_];
178+ if (streams_.empty ()) return ;
179+ auto & active_ranges = streams_.back ().active_ranges ;
180180 for (auto i = active_ranges.begin (); i != active_ranges.end ();) {
181181 if (i->second ->IsDone ()) {
182182 i = active_ranges.erase (i);
@@ -207,10 +207,6 @@ void ObjectDescriptorImpl::OnFinish(Status const& status) {
207207 for (auto const & kv : copy) {
208208 kv.second ->OnFinish (status);
209209 }
210- CancelStream (streams_[active_stream_]);
211- streams_.erase (streams_.begin () + active_stream_);
212- active_ranges_.erase (active_ranges_.begin () + active_stream_);
213- active_stream_ = streams_.size ();
214210}
215211
216212void ObjectDescriptorImpl::Resume (google::rpc::Status const & proto_status) {
@@ -220,7 +216,7 @@ void ObjectDescriptorImpl::Resume(google::rpc::Status const& proto_status) {
220216 ApplyRedirectErrors (read_object_spec_, proto_status);
221217 auto request = google::storage::v2::BidiReadObjectRequest{};
222218 *request.mutable_read_object_spec () = read_object_spec_;
223- for (auto const & kv : active_ranges_[active_stream_] ) {
219+ for (auto const & kv : streams_. back (). active_ranges ) {
224220 auto range = kv.second ->RangeForResume (kv.first );
225221 if (!range) continue ;
226222 *request.add_read_ranges () = *std::move (range);
@@ -235,9 +231,9 @@ void ObjectDescriptorImpl::Resume(google::rpc::Status const& proto_status) {
235231void ObjectDescriptorImpl::OnResume (StatusOr<OpenStreamResult> result) {
236232 if (!result) return OnFinish (std::move (result).status ());
237233 std::unique_lock<std::mutex> lk (mu_);
238- active_stream_ = streams_.size ();
239- streams_. push_back ( std::move (result-> stream ));
240- AddNewActiveRanges (lk );
234+ streams_.push_back (Stream{ std::move (result-> stream ),
235+ {},
236+ resume_policy_prototype_-> clone ()} );
241237 // TODO(#15105) - this should be done without release the lock.
242238 Flush (std::move (lk));
243239 OnRead (std::move (result->first_response ));
@@ -257,9 +253,8 @@ bool ObjectDescriptorImpl::IsResumable(
257253 CleanupDoneRanges (std::unique_lock<std::mutex>(mu_));
258254 return true ;
259255 }
260-
261- return resume_policy_->OnFinish (status) ==
262- storage_experimental::ResumePolicy::kContinue ;
256+ return streams_.back ().resume_policy ->OnFinish (status) ==
257+ storage_experimental::ResumePolicy::kContinue ;
263258}
264259
265260GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
0 commit comments