Xuanwo commented on code in PR #275: URL: https://github.com/apache/iceberg-rust/pull/275#discussion_r1557940399
########## crates/iceberg/src/writer/base_writer/data_file_writer.rs: ########## @@ -0,0 +1,323 @@ +// 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. + +//! This module provide `DataFileWriter`. + +use crate::spec::{DataContentType, DataFile, Struct}; +use crate::writer::file_writer::FileWriter; +use crate::writer::CurrentFileStatus; +use crate::writer::{file_writer::FileWriterBuilder, IcebergWriter, IcebergWriterBuilder}; +use crate::Result; +use arrow_array::RecordBatch; +use itertools::Itertools; + +/// Builder for `DataFileWriter`. +#[derive(Clone)] +pub struct DataFileWriterBuilder<B: FileWriterBuilder> { + inner: B, +} + +impl<B: FileWriterBuilder> DataFileWriterBuilder<B> { + /// Create a new `DataFileWriterBuilder` using a `FileWriterBuilder`. + pub fn new(inner: B) -> Self { + Self { inner } + } +} + +/// Config for `DataFileWriter`. +pub struct DataFileWriterConfig { + partition_value: Struct, +} + +impl DataFileWriterConfig { + /// Create a new `DataFileWriterConfig` with partition value. + pub fn new(partition_value: Option<Struct>) -> Self { + Self { + partition_value: partition_value.unwrap_or(Struct::empty()), + } + } +} + +#[async_trait::async_trait] +impl<B: FileWriterBuilder> IcebergWriterBuilder for DataFileWriterBuilder<B> { + type R = DataFileWriter<B>; + type C = DataFileWriterConfig; + + async fn build(self, config: Self::C) -> Result<Self::R> { + Ok(DataFileWriter { + inner_writer: self.inner.clone().build().await?, + builder: self.inner, + partition_value: config.partition_value, + }) + } +} + +/// A writer write data is within one spec/partition. +pub struct DataFileWriter<B: FileWriterBuilder> { + builder: B, + inner_writer: B::R, + partition_value: Struct, +} + +#[async_trait::async_trait] +impl<B: FileWriterBuilder> IcebergWriter for DataFileWriter<B> { + async fn write(&mut self, batch: RecordBatch) -> Result<()> { + self.inner_writer.write(&batch).await + } + + async fn flush(&mut self) -> Result<Vec<DataFile>> { + let writer = std::mem::replace(&mut self.inner_writer, self.builder.clone().build().await?); Review Comment: I'm thinking about the error handling cases. If user hits error during flush, they can't retry it and will lost all existing writing data. How about pushing the writer back if it flush failed? User can decide whether abort the whole writing process or retry this flush. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org