opendal/services/hdfs_native/
config.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::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::HDFS_NATIVE_SCHEME;
24use super::backend::HdfsNativeBuilder;
25
26/// Config for HdfsNative services support.
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct HdfsNativeConfig {
31    /// work dir of this backend
32    pub root: Option<String>,
33    /// name_node of this backend
34    pub name_node: Option<String>,
35    /// enable the append capacity
36    pub enable_append: bool,
37}
38
39impl Debug for HdfsNativeConfig {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("HdfsNativeConfig")
42            .field("root", &self.root)
43            .field("name_node", &self.name_node)
44            .field("enable_append", &self.enable_append)
45            .finish_non_exhaustive()
46    }
47}
48
49impl crate::Configurator for HdfsNativeConfig {
50    type Builder = HdfsNativeBuilder;
51
52    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
53        let authority = uri.authority().ok_or_else(|| {
54            crate::Error::new(crate::ErrorKind::ConfigInvalid, "uri authority is required")
55                .with_context("service", HDFS_NATIVE_SCHEME)
56        })?;
57
58        let mut map = uri.options().clone();
59        map.insert("name_node".to_string(), format!("hdfs://{authority}"));
60
61        if let Some(root) = uri.root() {
62            if !root.is_empty() {
63                map.insert("root".to_string(), root.to_string());
64            }
65        }
66
67        Self::from_iter(map)
68    }
69
70    fn into_builder(self) -> Self::Builder {
71        HdfsNativeBuilder { config: self }
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78    use crate::Configurator;
79    use crate::types::OperatorUri;
80
81    #[test]
82    fn from_uri_sets_name_node_and_root() {
83        let uri = OperatorUri::new(
84            "hdfs-native://namenode:9000/user/project",
85            Vec::<(String, String)>::new(),
86        )
87        .unwrap();
88
89        let cfg = HdfsNativeConfig::from_uri(&uri).unwrap();
90        assert_eq!(cfg.name_node.as_deref(), Some("hdfs://namenode:9000"));
91        assert_eq!(cfg.root.as_deref(), Some("user/project"));
92    }
93}