martin-g commented on code in PR #139:
URL: https://github.com/apache/avro-rs/pull/139#discussion_r1979339739


##########
avro/src/ser_direct.rs:
##########
@@ -0,0 +1,2049 @@
+// 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 std::{borrow::Cow, io::Write};
+
+use serde::ser;
+
+use crate::{
+    encode::{encode_int, encode_long},
+    error::Error,
+    schema::{Name, NamesRef, Namespace, RecordSchema, Schema},
+};
+
+const RECORD_FIELD_INIT_BUFFER_SIZE: usize = 64;
+const COLLECTION_SERIALIZER_ITEM_LIMIT: usize = 1024;
+const COLLECTION_SERIALIZER_DEFAULT_INIT_ITEM_CAPACITY: usize = 32;
+const SINGLE_VALUE_INIT_BUFFER_SIZE: usize = 128;
+
+pub struct DirectSerializeSeq<'a, 's, W: Write> {
+    ser: &'a mut DirectSerializer<'s, W>,
+    item_schema: &'s Schema,
+    item_buffer_size: usize,
+    item_buffers: Vec<Vec<u8>>,
+    bytes_written: usize,
+}
+
+impl<'a, 's, W: Write> DirectSerializeSeq<'a, 's, W> {
+    fn new(
+        ser: &'a mut DirectSerializer<'s, W>,
+        item_schema: &'s Schema,
+        len: Option<usize>,
+    ) -> DirectSerializeSeq<'a, 's, W> {
+        DirectSerializeSeq {
+            ser,
+            item_schema,
+            item_buffer_size: SINGLE_VALUE_INIT_BUFFER_SIZE,
+            item_buffers: Vec::with_capacity(
+                
len.unwrap_or(COLLECTION_SERIALIZER_DEFAULT_INIT_ITEM_CAPACITY),
+            ),
+            bytes_written: 0,
+        }
+    }
+
+    fn write_buffered_items(&mut self) -> Result<(), Error> {
+        if !self.item_buffers.is_empty() {
+            self.bytes_written +=
+                encode_long(self.item_buffers.len() as i64, &mut 
self.ser.writer)?;
+            for item in self.item_buffers.drain(..) {
+                self.bytes_written += self
+                    .ser
+                    .writer
+                    .write(item.as_slice())
+                    .map_err(Error::WriteBytes)?;
+            }
+        }
+
+        Ok(())
+    }
+
+    fn serialize_element<T: ser::Serialize>(&mut self, value: &T) -> 
Result<(), Error> {
+        let mut item_buffer: Vec<u8> = 
Vec::with_capacity(self.item_buffer_size);
+        let mut item_ser = DirectSerializer::new(
+            &mut item_buffer,
+            self.item_schema,
+            self.ser.names,
+            self.ser.enclosing_namespace.clone(),
+        );
+        value.serialize(&mut item_ser)?;
+
+        self.item_buffer_size = std::cmp::max(self.item_buffer_size, 
item_buffer.len() + 16);
+
+        self.item_buffers.push(item_buffer);
+
+        if self.item_buffers.len() > COLLECTION_SERIALIZER_ITEM_LIMIT {
+            self.write_buffered_items()?;
+        }
+
+        Ok(())
+    }
+
+    fn end(mut self) -> Result<usize, Error> {
+        self.write_buffered_items()?;
+        self.bytes_written += 
self.ser.writer.write(&[0u8]).map_err(Error::WriteBytes)?;

Review Comment:
   What is the idea behind `write(&[0u8])` ?



-- 
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