alamb commented on code in PR #5481:
URL: https://github.com/apache/arrow-rs/pull/5481#discussion_r1522285749


##########
arrow-array/src/builder/generic_bytes_view_builder.rs:
##########
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::builder::ArrayBuilder;
+use crate::types::{BinaryViewType, ByteViewType, StringViewType};
+use crate::{ArrayRef, GenericByteViewArray};
+use arrow_buffer::{Buffer, BufferBuilder, NullBufferBuilder, ScalarBuffer};
+use arrow_data::ByteView;
+use std::any::Any;
+use std::marker::PhantomData;
+use std::sync::Arc;
+
+const DEFAULT_BLOCK_SIZE: u32 = 8 * 1024;
+
+/// A builder for [`GenericByteViewArray`]
+///
+/// See [`Self::append_value`] for the allocation strategy
+pub struct GenericByteViewBuilder<T: ByteViewType + ?Sized> {
+    views_builder: BufferBuilder<u128>,
+    null_buffer_builder: NullBufferBuilder,
+    completed: Vec<Buffer>,
+    in_progress: Vec<u8>,
+    block_size: u32,
+    phantom: PhantomData<T>,
+}
+
+impl<T: ByteViewType + ?Sized> GenericByteViewBuilder<T> {
+    /// Creates a new [`GenericByteViewBuilder`].
+    pub fn new() -> Self {
+        Self::with_capacity(1024)
+    }
+
+    /// Creates a new [`GenericByteViewBuilder`] with space for `capacity` 
strings
+    pub fn with_capacity(capacity: usize) -> Self {
+        Self {
+            views_builder: BufferBuilder::new(capacity),
+            null_buffer_builder: NullBufferBuilder::new(capacity),
+            completed: vec![],
+            in_progress: vec![],
+            block_size: DEFAULT_BLOCK_SIZE,
+            phantom: Default::default(),
+        }
+    }
+
+    /// Override the minimum size of buffers to allocate for string data
+    pub fn with_block_size(self, block_size: u32) -> Self {
+        Self { block_size, ..self }
+    }
+
+    /// Appends a value into the builder
+    ///
+    /// # Panics
+    ///
+    /// Panics if
+    /// - String buffer count exceeds `u32::MAX`
+    /// - String length exceeds `u32::MAX`
+    #[inline]
+    pub fn append_value(&mut self, value: impl AsRef<T::Native>) {
+        let v: &[u8] = value.as_ref().as_ref();
+        let length: u32 = v.len().try_into().unwrap();
+        if length <= 12 {
+            let mut view_buffer = [0; 16];
+            view_buffer[0..4].copy_from_slice(&length.to_le_bytes());
+            view_buffer[4..4 + v.len()].copy_from_slice(v);
+            self.views_builder.append(u128::from_le_bytes(view_buffer));
+            self.null_buffer_builder.append_non_null();
+            return;
+        }
+
+        let required_cap = self.in_progress.len() + v.len();
+        if self.in_progress.capacity() < required_cap {

Review Comment:
   Then I don't understand the difference between capacity and block_size, 
though maybe I lost some subtely
   
   At the very least I think  we should document the behavior of the two 
settings
   
   I would expect:
   1. `capacity` to act like `Vec::with_capacity` that gives me a way to 
pre-declare how much buffer I want to pre-allocate, but if I put more data in 
then the internal allocation will grow
   2. `block_size` to act as the target size for the variable length buffer -- 
when the buffer would grow larger than `block_size` a new buffer is allocated
   
   I don't think this is what the code does -- so I think we should clarify 
what it does do



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to