|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::mem::take; |
| 19 | + |
| 20 | +use arrow_array::RecordBatch; |
| 21 | +use async_trait::async_trait; |
| 22 | + |
| 23 | +use crate::spec::DataFile; |
| 24 | +use crate::writer::base_writer::data_file_writer::DataFileWriter; |
| 25 | +use crate::writer::file_writer::FileWriterBuilder; |
| 26 | +use crate::writer::{IcebergWriter, IcebergWriterBuilder}; |
| 27 | +use crate::{Error, ErrorKind, Result}; |
| 28 | + |
| 29 | +#[async_trait] |
| 30 | +pub trait RollingFileWriter: IcebergWriter { |
| 31 | + fn should_roll(&mut self, input_size: u64) -> bool; |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Clone)] |
| 35 | +pub struct RollingDataFileWriterBuilder<B: FileWriterBuilder> { |
| 36 | + inner_builder: B, |
| 37 | + target_size: u64, |
| 38 | +} |
| 39 | + |
| 40 | +impl<B: FileWriterBuilder> RollingDataFileWriterBuilder<B> { |
| 41 | + pub fn new(inner_builder: B, target_size: u64) -> Self { |
| 42 | + Self { |
| 43 | + inner_builder, |
| 44 | + target_size, |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[async_trait] |
| 50 | +impl<B: FileWriterBuilder> IcebergWriterBuilder for RollingDataFileWriterBuilder<B> { |
| 51 | + type R = RollingDataFileWriter<B>; |
| 52 | + |
| 53 | + async fn build(self) -> Result<Self::R> { |
| 54 | + Ok(RollingDataFileWriter { |
| 55 | + inner: None, |
| 56 | + inner_builder: self.inner_builder, |
| 57 | + target_size: self.target_size, |
| 58 | + written_size: 0, |
| 59 | + data_files: vec![], |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub struct RollingDataFileWriter<B: FileWriterBuilder> { |
| 65 | + inner: Option<DataFileWriter<B::R>>, |
| 66 | + inner_builder: B, |
| 67 | + target_size: u64, |
| 68 | + written_size: u64, |
| 69 | + data_files: Vec<DataFile>, |
| 70 | +} |
| 71 | + |
| 72 | +#[async_trait] |
| 73 | +impl<B: FileWriterBuilder> IcebergWriter for RollingDataFileWriter<B> { |
| 74 | + async fn write(&mut self, input: RecordBatch) -> Result<()> { |
| 75 | + let input_size = input.get_array_memory_size() as u64; |
| 76 | + if self.should_roll(input_size) { |
| 77 | + if let Some(mut inner) = self.inner.take() { |
| 78 | + // close the current writer, roll to a new file |
| 79 | + self.data_files.extend(inner.close().await?); |
| 80 | + } |
| 81 | + |
| 82 | + // clear bytes written |
| 83 | + self.written_size = 0; |
| 84 | + } |
| 85 | + |
| 86 | + if self.inner.is_none() { |
| 87 | + // start a new writer |
| 88 | + self.inner = Some(self.inner_builder.clone().build().await?); |
| 89 | + } |
| 90 | + |
| 91 | + // write the input and count bytes written |
| 92 | + let Some(writer) = self.inner.as_mut() else { |
| 93 | + return Err(Error::new( |
| 94 | + ErrorKind::Unexpected, |
| 95 | + "Writer is not initialized!", |
| 96 | + )); |
| 97 | + }; |
| 98 | + writer.write(input).await?; |
| 99 | + self.written_size += input_size; |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + async fn close(&mut self) -> Result<Vec<DataFile>> { |
| 104 | + Ok(take(&mut self.data_files)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<B: FileWriterBuilder> RollingFileWriter for RollingDataFileWriter<B> { |
| 109 | + fn should_roll(&mut self, input_size: u64) -> bool { |
| 110 | + self.written_size + input_size > self.target_size |
| 111 | + } |
| 112 | +} |
0 commit comments