opendal/services/hdfs_native/
writer.rs

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
18use hdfs_native::file::FileWriter;
19
20use crate::raw::*;
21use crate::services::hdfs_native::error::parse_hdfs_error;
22use crate::*;
23pub struct HdfsNativeWriter {
24    f: FileWriter,
25    size: u64,
26}
27
28impl HdfsNativeWriter {
29    pub fn new(f: FileWriter, initial_size: u64) -> Self {
30        HdfsNativeWriter {
31            f,
32            size: initial_size,
33        }
34    }
35}
36
37impl oio::Write for HdfsNativeWriter {
38    async fn write(&mut self, mut buf: Buffer) -> Result<()> {
39        let len = buf.len() as u64;
40
41        for bs in buf.by_ref() {
42            self.f.write(bs).await.map_err(parse_hdfs_error)?;
43        }
44
45        self.size += len;
46        Ok(())
47    }
48
49    async fn close(&mut self) -> Result<Metadata> {
50        self.f.close().await.map_err(parse_hdfs_error)?;
51
52        Ok(Metadata::default().with_content_length(self.size))
53    }
54
55    async fn abort(&mut self) -> Result<()> {
56        Err(Error::new(
57            ErrorKind::Unsupported,
58            "HdfsNativeWriter doesn't support abort",
59        ))
60    }
61}