opendal/services/hdfs_native/
backend.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 std::sync::Arc;
19
20use log::debug;
21
22use super::HDFS_NATIVE_SCHEME;
23use super::config::HdfsNativeConfig;
24use super::core::HdfsNativeCore;
25use super::deleter::HdfsNativeDeleter;
26use super::error::parse_hdfs_error;
27use super::lister::HdfsNativeLister;
28use super::reader::HdfsNativeReader;
29use super::writer::HdfsNativeWriter;
30use crate::raw::*;
31use crate::*;
32
33/// [Hadoop Distributed File System (HDFS™)](https://hadoop.apache.org/) support.
34/// Using [Native Rust HDFS client](https://github.com/Kimahriman/hdfs-native).
35#[doc = include_str!("docs.md")]
36#[derive(Debug, Default)]
37pub struct HdfsNativeBuilder {
38    pub(super) config: HdfsNativeConfig,
39}
40
41impl HdfsNativeBuilder {
42    /// Set root of this backend.
43    ///
44    /// All operations will happen under this root.
45    pub fn root(mut self, root: &str) -> Self {
46        self.config.root = if root.is_empty() {
47            None
48        } else {
49            Some(root.to_string())
50        };
51
52        self
53    }
54
55    /// Set name_node of this backend.
56    ///
57    /// Valid format including:
58    ///
59    /// - `default`: using the default setting based on hadoop config.
60    /// - `hdfs://127.0.0.1:9000`: connect to hdfs cluster.
61    pub fn name_node(mut self, name_node: &str) -> Self {
62        if !name_node.is_empty() {
63            // Trim trailing `/` so that we can accept `http://127.0.0.1:9000/`
64            self.config.name_node = Some(name_node.trim_end_matches('/').to_string())
65        }
66
67        self
68    }
69
70    /// Enable append capacity of this backend.
71    ///
72    /// This should be disabled when HDFS runs in non-distributed mode.
73    pub fn enable_append(mut self, enable_append: bool) -> Self {
74        self.config.enable_append = enable_append;
75        self
76    }
77}
78
79impl Builder for HdfsNativeBuilder {
80    type Config = HdfsNativeConfig;
81
82    fn build(self) -> Result<impl Access> {
83        debug!("backend build started: {:?}", &self);
84
85        let name_node = match &self.config.name_node {
86            Some(v) => v,
87            None => {
88                return Err(Error::new(ErrorKind::ConfigInvalid, "name_node is empty")
89                    .with_context("service", HDFS_NATIVE_SCHEME));
90            }
91        };
92
93        let root = normalize_root(&self.config.root.unwrap_or_default());
94        debug!("backend use root {root}");
95
96        let client = hdfs_native::ClientBuilder::new()
97            .with_url(name_node)
98            .build()
99            .map_err(parse_hdfs_error)?;
100
101        // need to check if root dir exists, create if not
102        Ok(HdfsNativeBackend {
103            core: Arc::new(HdfsNativeCore {
104                info: {
105                    let am = AccessorInfo::default();
106                    am.set_scheme(HDFS_NATIVE_SCHEME)
107                        .set_root(&root)
108                        .set_native_capability(Capability {
109                            stat: true,
110
111                            read: true,
112
113                            write: true,
114                            write_can_append: self.config.enable_append,
115
116                            create_dir: true,
117                            delete: true,
118
119                            list: true,
120
121                            rename: true,
122
123                            shared: true,
124
125                            ..Default::default()
126                        });
127
128                    am.into()
129                },
130                root,
131                client: Arc::new(client),
132                enable_append: self.config.enable_append,
133            }),
134        })
135    }
136}
137
138// #[inline]
139// fn tmp_file_of(path: &str) -> String {
140//     let name = get_basename(path);
141//     let uuid = Uuid::new_v4().to_string();
142
143//     format!("{name}.{uuid}")
144// }
145
146/// Backend for hdfs-native services.
147#[derive(Debug, Clone)]
148pub struct HdfsNativeBackend {
149    core: Arc<HdfsNativeCore>,
150}
151
152impl Access for HdfsNativeBackend {
153    type Reader = HdfsNativeReader;
154    type Writer = HdfsNativeWriter;
155    type Lister = Option<HdfsNativeLister>;
156    type Deleter = oio::OneShotDeleter<HdfsNativeDeleter>;
157
158    fn info(&self) -> Arc<AccessorInfo> {
159        self.core.info.clone()
160    }
161
162    async fn create_dir(&self, path: &str, _args: OpCreateDir) -> Result<RpCreateDir> {
163        self.core.hdfs_create_dir(path).await?;
164        Ok(RpCreateDir::default())
165    }
166
167    async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
168        let m = self.core.hdfs_stat(path).await?;
169        Ok(RpStat::new(m))
170    }
171
172    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
173        let (f, offset, size) = self.core.hdfs_read(path, &args).await?;
174
175        let r = HdfsNativeReader::new(f, offset as _, size as _);
176
177        Ok((RpRead::new(), r))
178    }
179
180    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
181        let (f, initial_size) = self.core.hdfs_write(path, &args).await?;
182
183        Ok((RpWrite::new(), HdfsNativeWriter::new(f, initial_size)))
184    }
185
186    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
187        Ok((
188            RpDelete::default(),
189            oio::OneShotDeleter::new(HdfsNativeDeleter::new(Arc::clone(&self.core))),
190        ))
191    }
192
193    async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Lister)> {
194        match self.core.hdfs_list(path).await? {
195            Some((p, current_path)) => Ok((
196                RpList::default(),
197                Some(HdfsNativeLister::new(
198                    &self.core.root,
199                    &self.core.client,
200                    &p,
201                    current_path,
202                )),
203            )),
204            None => Ok((RpList::default(), None)),
205        }
206    }
207
208    async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
209        self.core.hdfs_rename(from, to).await?;
210        Ok(RpRename::default())
211    }
212}