-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-43949: [C++] io::BufferedInput: Fixing the invalid state with SetBufferSize #44387
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -51,6 +51,7 @@ class BufferedBase { | |||||||||
return !is_open_; | ||||||||||
} | ||||||||||
|
||||||||||
// Reset the `buffer_` to a new buffer of size `buffer_size_` | ||||||||||
Status ResetBuffer() { | ||||||||||
if (!buffer_) { | ||||||||||
// On first invocation, or if the buffer has been released, we allocate a | ||||||||||
|
@@ -284,17 +285,30 @@ class BufferedInputStream::Impl : public BufferedBase { | |||||||||
|
||||||||||
// Resize internal read buffer. Note that the internal buffer-size | ||||||||||
// should be not larger than the raw_read_bound_. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
// | ||||||||||
// SetBufferSize will not change the buffer_pos_ and bytes_buffered_. | ||||||||||
Comment on lines
+288
to
+289
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
Status SetBufferSize(int64_t new_buffer_size) { | ||||||||||
if (new_buffer_size <= 0) { | ||||||||||
return Status::Invalid("Buffer size should be positive"); | ||||||||||
} | ||||||||||
if ((buffer_pos_ + bytes_buffered_) >= new_buffer_size) { | ||||||||||
return Status::Invalid("Cannot shrink read buffer if buffered data remains"); | ||||||||||
return Status::Invalid( | ||||||||||
"Cannot shrink read buffer if buffered data remains, new_buffer_size:", | ||||||||||
new_buffer_size, ", buffer_pos:", buffer_pos_, | ||||||||||
", bytes_buffered:", bytes_buffered_, ", buffer_size:", buffer_size_); | ||||||||||
} | ||||||||||
if (raw_read_bound_ >= 0) { | ||||||||||
// No need to reserve space for more than the total remaining number of bytes. | ||||||||||
new_buffer_size = std::min(new_buffer_size, | ||||||||||
bytes_buffered_ + (raw_read_bound_ - raw_read_total_)); | ||||||||||
if (bytes_buffered_ == 0) { | ||||||||||
// Special case: we can not keep the current buffer because it does not | ||||||||||
// contains any required data. | ||||||||||
new_buffer_size = std::min(new_buffer_size, raw_read_bound_ - raw_read_total_); | ||||||||||
} else { | ||||||||||
// We should keeping the current buffer because it contains data that | ||||||||||
// can be read. | ||||||||||
new_buffer_size = | ||||||||||
std::min(new_buffer_size, buffer_size_ + (raw_read_bound_ - raw_read_total_)); | ||||||||||
Comment on lines
+309
to
+310
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Isn't |
||||||||||
} | ||||||||||
} | ||||||||||
return ResizeBuffer(new_buffer_size); | ||||||||||
} | ||||||||||
|
@@ -350,7 +364,7 @@ class BufferedInputStream::Impl : public BufferedBase { | |||||||||
} | ||||||||||
|
||||||||||
Status DoBuffer() { | ||||||||||
// Fill buffer | ||||||||||
// Refill the buffer from the raw stream with `buffer_size_` bytes. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
if (!buffer_) { | ||||||||||
RETURN_NOT_OK(ResetBuffer()); | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep consistent variable names in the comment across this file.