You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/02/21 20:53:05 UTC

[avro] branch branch-1.11 updated: [Rust][interop] Write directly in the .avro file

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new 6c14b21  [Rust][interop] Write directly in the .avro file
6c14b21 is described below

commit 6c14b21b51cab705d1e8953000b63d109d74a7a4
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Mon Feb 21 22:51:04 2022 +0200

    [Rust][interop] Write directly in the .avro file
    
    Don't write in memory (vec<u8>) and then dump it in the file.
    
    Use generics for the user metadata methods.
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    (cherry picked from commit 70223760d8d29c1e21ca224df452f9f0cb801cbd)
---
 lang/rust/examples/generate_interop_data.rs | 23 ++++++++++++-----------
 lang/rust/examples/test_interop_data.rs     |  7 ++++---
 2 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/lang/rust/examples/generate_interop_data.rs b/lang/rust/examples/generate_interop_data.rs
index 7aa0358..64b10ba 100644
--- a/lang/rust/examples/generate_interop_data.rs
+++ b/lang/rust/examples/generate_interop_data.rs
@@ -21,6 +21,7 @@ use apache_avro::{
     Codec, Writer,
 };
 use std::{collections::HashMap, io::BufWriter};
+use std::io::Write;
 use strum::IntoEnumIterator;
 
 fn create_datum(schema: &Schema) -> Record {
@@ -77,12 +78,6 @@ fn main() -> anyhow::Result<()> {
     let schema = Schema::parse_str(schema_str.as_str())?;
 
     for codec in Codec::iter() {
-        let mut writer = Writer::with_codec(&schema, BufWriter::new(Vec::new()), codec);
-        write_user_metadata(&mut writer)?;
-
-        let datum = create_datum(&schema);
-        writer.append(datum)?;
-        let bytes = writer.into_inner()?;
 
         let codec_name = <&str>::from(codec);
         let suffix = if codec_name == "null" {
@@ -91,16 +86,22 @@ fn main() -> anyhow::Result<()> {
             format!("_{}", codec_name)
         };
 
-        std::fs::write(
-            format!("../../build/interop/data/rust{}.avro", suffix),
-            bytes.into_inner()?,
-        )?;
+        let file_name = format!("../../build/interop/data/rust{}.avro", suffix);
+        let output_file = std::fs::File::create(&file_name)?;
+
+        let mut writer = Writer::with_codec(&schema, BufWriter::new(output_file), codec);
+        write_user_metadata(&mut writer)?;
+
+        let datum = create_datum(&schema);
+        writer.append(datum)?;
+        writer.flush()?;
+        println!("Wrote {}", file_name);
     }
 
     Ok(())
 }
 
-fn write_user_metadata(writer: &mut Writer<BufWriter<Vec<u8>>>) -> anyhow::Result<()> {
+fn write_user_metadata<W: Write>(writer: &mut Writer<BufWriter<W>>) -> anyhow::Result<()> {
     writer.add_user_metadata("user_metadata".to_string(), b"someByteArray")?;
 
     Ok(())
diff --git a/lang/rust/examples/test_interop_data.rs b/lang/rust/examples/test_interop_data.rs
index 08934dc..c20ac25 100644
--- a/lang/rust/examples/test_interop_data.rs
+++ b/lang/rust/examples/test_interop_data.rs
@@ -16,7 +16,8 @@
 // under the License.
 
 use apache_avro::Reader;
-use std::{collections::HashMap, ffi::OsStr, fs::File, io::BufReader};
+use std::{collections::HashMap, ffi::OsStr, io::BufReader};
+use std::io::Read;
 
 fn main() -> anyhow::Result<()> {
     let mut expected_user_metadata: HashMap<String, Vec<u8>> = HashMap::new();
@@ -64,8 +65,8 @@ fn main() -> anyhow::Result<()> {
     }
 }
 
-fn test_user_metadata(
-    reader: &Reader<BufReader<&File>>,
+fn test_user_metadata<R: Read>(
+    reader: &Reader<BufReader<R>>,
     expected_user_metadata: &HashMap<String, Vec<u8>>,
 ) {
     let user_metadata = reader.user_metadata();