-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve docs and add build() method to {Null,Boolean,}BufferBuilder
#9155
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
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 |
|---|---|---|
|
|
@@ -21,11 +21,28 @@ use std::ops::Range; | |
|
|
||
| /// Builder for [`BooleanBuffer`] | ||
| /// | ||
| /// Builds a packed buffer of bits representing boolean values. Each bit in the | ||
| /// buffer corresponds to a boolean value, | ||
| /// | ||
| /// # See Also | ||
| /// | ||
| /// * [`NullBuffer`] for building [`BooleanBuffer`]s for representing nulls | ||
|
Contributor
Author
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. NullBufferBuilder is the correct reference so I fixed that |
||
| /// * [`NullBufferBuilder`] for building [`BooleanBuffer`]s for representing nulls | ||
| /// * [`BufferBuilder`] for building [`Buffer`]s | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// # use arrow_buffer::builder::BooleanBufferBuilder; | ||
| /// let mut builder = BooleanBufferBuilder::new(10); | ||
| /// builder.append(true); | ||
| /// builder.append(false); | ||
| /// builder.append_n(3, true); // append 3 trues | ||
| /// let buffer = builder.build(); | ||
| /// assert_eq!(buffer.len(), 5); // 5 bits appended | ||
| /// assert_eq!(buffer.values(), &[0b00011101_u8]); // packed bits | ||
| ///``` | ||
| /// | ||
| /// [`NullBuffer`]: crate::NullBuffer | ||
| /// [`BufferBuilder`]: crate::builder::BufferBuilder | ||
| /// [`NullBufferBuilder`]: crate::builder::NullBufferBuilder | ||
| #[derive(Debug)] | ||
| pub struct BooleanBufferBuilder { | ||
| buffer: MutableBuffer, | ||
|
|
@@ -247,14 +264,24 @@ impl BooleanBufferBuilder { | |
| self.buffer.as_slice_mut() | ||
| } | ||
|
|
||
| /// Creates a [`BooleanBuffer`] | ||
| /// Resets this builder and returns a [`BooleanBuffer`]. | ||
| /// | ||
| /// Use [`Self::build`] when you don't need to reuse this builder. | ||
| #[inline] | ||
| pub fn finish(&mut self) -> BooleanBuffer { | ||
| let buf = std::mem::replace(&mut self.buffer, MutableBuffer::new(0)); | ||
| let len = std::mem::replace(&mut self.len, 0); | ||
| BooleanBuffer::new(buf.into(), 0, len) | ||
| } | ||
|
|
||
| /// Builds a [`BooleanBuffer`] without resetting the builder. | ||
| /// | ||
| /// This consumes the builder. Use [`Self::finish`] to reuse it. | ||
| #[inline] | ||
| pub fn build(self) -> BooleanBuffer { | ||
|
Contributor
Author
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. most builders have a |
||
| BooleanBuffer::new(self.buffer.into(), 0, self.len) | ||
| } | ||
|
|
||
| /// Builds the [BooleanBuffer] without resetting the builder. | ||
| pub fn finish_cloned(&self) -> BooleanBuffer { | ||
| BooleanBuffer::new(Buffer::from_slice_ref(self.as_slice()), 0, self.len) | ||
|
|
@@ -285,7 +312,7 @@ impl From<BooleanBufferBuilder> for Buffer { | |
| impl From<BooleanBufferBuilder> for BooleanBuffer { | ||
| #[inline] | ||
| fn from(builder: BooleanBufferBuilder) -> Self { | ||
| BooleanBuffer::new(builder.buffer.into(), 0, builder.len) | ||
| builder.build() | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,22 @@ | |
|
|
||
| use crate::{BooleanBufferBuilder, MutableBuffer, NullBuffer}; | ||
|
|
||
| /// Builder for creating [`NullBuffer`] | ||
| /// Builder for creating [`NullBuffer`]s (bitmaps indicating validity/nulls). | ||
| /// | ||
| /// # See also | ||
| /// * [`BooleanBufferBuilder`] for a lower-level bitmap builder. | ||
| /// * [`Self::allocated_size`] for the current memory allocated by the builder. | ||
| /// | ||
| /// # Performance | ||
| /// | ||
| /// This builder only materializes the buffer when we append `false`. | ||
| /// If you only append `true`s to the builder, what you get will be | ||
| /// `None` when calling [`finish`](#method.finish). | ||
| /// This builder only materializes the buffer when null values (`false`) are | ||
|
Contributor
Author
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. drive by wording cleanup |
||
| /// appended. If you only append non-null, (`true`) to the builder, no buffer is | ||
| /// allocated and [`build`](#method.build) or [`finish`](#method.finish) return | ||
| /// `None`. | ||
| /// | ||
| /// This optimization is **very** important for the performance as it avoids | ||
| /// allocating memory for the null buffer when there are no nulls. | ||
| /// | ||
| /// See [`Self::allocated_size`] to get the current memory allocated by the builder. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// # use arrow_buffer::NullBufferBuilder; | ||
|
|
@@ -193,11 +196,20 @@ impl NullBufferBuilder { | |
| } | ||
| } | ||
|
|
||
| /// Builds the null buffer and resets the builder. | ||
| /// Returns `None` if the builder only contains `true`s. | ||
| /// Builds the [`NullBuffer`] and resets the builder. | ||
| /// | ||
| /// Returns `None` if the builder only contains `true`s. Use [`Self::build`] | ||
| /// when you don't need to reuse this builder. | ||
| pub fn finish(&mut self) -> Option<NullBuffer> { | ||
| self.len = 0; | ||
| Some(NullBuffer::new(self.bitmap_builder.take()?.finish())) | ||
| Some(NullBuffer::new(self.bitmap_builder.take()?.build())) | ||
| } | ||
|
|
||
| /// Builds the [`NullBuffer`] without resetting the builder. | ||
| /// | ||
| /// This consumes the builder. Use [`Self::finish`] to reuse it. | ||
| pub fn build(self) -> Option<NullBuffer> { | ||
| self.bitmap_builder.map(NullBuffer::from) | ||
| } | ||
|
|
||
| /// Builds the [NullBuffer] without resetting the builder. | ||
|
|
@@ -238,9 +250,7 @@ impl NullBufferBuilder { | |
| .map(|b| b.capacity() / 8) | ||
| .unwrap_or(0) | ||
| } | ||
| } | ||
|
|
||
| impl NullBufferBuilder { | ||
| /// Return the number of bits in the buffer. | ||
| pub fn len(&self) -> usize { | ||
| self.bitmap_builder.as_ref().map_or(self.len, |b| b.len()) | ||
|
|
||
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.
adding some comments here to help navigate the maze of builders available