opendal/services/rocksdb/
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::backend::RocksdbBuilder;
24
25/// Config for Rocksdb Service.
26#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct RocksdbConfig {
30    /// The path to the rocksdb data directory.
31    pub datadir: Option<String>,
32    /// the working directory of the service. Can be "/path/to/dir"
33    ///
34    /// default is "/"
35    pub root: Option<String>,
36}
37
38impl crate::Configurator for RocksdbConfig {
39    type Builder = RocksdbBuilder;
40
41    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
42        let mut map = uri.options().clone();
43
44        if let Some(path) = uri.root() {
45            if !path.is_empty() {
46                map.entry("datadir".to_string())
47                    .or_insert_with(|| format!("/{path}"));
48            }
49        }
50
51        Self::from_iter(map)
52    }
53
54    fn into_builder(self) -> Self::Builder {
55        RocksdbBuilder { config: self }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use crate::Configurator;
63    use crate::types::OperatorUri;
64
65    #[test]
66    fn from_uri_sets_datadir_and_root() {
67        let uri = OperatorUri::new(
68            "rocksdb:///var/db?root=namespace",
69            Vec::<(String, String)>::new(),
70        )
71        .unwrap();
72
73        let cfg = RocksdbConfig::from_uri(&uri).unwrap();
74        assert_eq!(cfg.datadir.as_deref(), Some("/var/db"));
75        assert_eq!(cfg.root.as_deref(), Some("namespace"));
76    }
77}