opendal/services/rocksdb/
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 rocksdb::DB;
21
22use super::ROCKSDB_SCHEME;
23use super::config::RocksdbConfig;
24use super::core::*;
25use super::deleter::RocksdbDeleter;
26use super::lister::RocksdbLister;
27use super::writer::RocksdbWriter;
28use crate::raw::*;
29use crate::*;
30
31/// RocksDB service support.
32#[doc = include_str!("docs.md")]
33#[derive(Debug, Default)]
34pub struct RocksdbBuilder {
35    pub(super) config: RocksdbConfig,
36}
37
38impl RocksdbBuilder {
39    /// Set the path to the rocksdb data directory. Will create if not exists.
40    pub fn datadir(mut self, path: &str) -> Self {
41        self.config.datadir = Some(path.into());
42        self
43    }
44
45    /// set the working directory, all operations will be performed under it.
46    ///
47    /// default: "/"
48    pub fn root(mut self, root: &str) -> Self {
49        self.config.root = if root.is_empty() {
50            None
51        } else {
52            Some(root.to_string())
53        };
54
55        self
56    }
57}
58
59impl Builder for RocksdbBuilder {
60    type Config = RocksdbConfig;
61
62    fn build(self) -> Result<impl Access> {
63        let path = self.config.datadir.ok_or_else(|| {
64            Error::new(ErrorKind::ConfigInvalid, "datadir is required but not set")
65                .with_context("service", ROCKSDB_SCHEME)
66        })?;
67        let db = DB::open_default(&path).map_err(|e| {
68            Error::new(ErrorKind::ConfigInvalid, "open default transaction db")
69                .with_context("service", ROCKSDB_SCHEME)
70                .with_context("datadir", path)
71                .set_source(e)
72        })?;
73
74        let root = normalize_root(&self.config.root.unwrap_or_default());
75
76        Ok(RocksdbBackend::new(RocksdbCore { db: Arc::new(db) }).with_normalized_root(root))
77    }
78}
79
80/// Backend for rocksdb services.
81#[derive(Clone, Debug)]
82pub struct RocksdbBackend {
83    core: Arc<RocksdbCore>,
84    root: String,
85    info: Arc<AccessorInfo>,
86}
87
88impl RocksdbBackend {
89    pub fn new(core: RocksdbCore) -> Self {
90        let info = AccessorInfo::default();
91        info.set_scheme(ROCKSDB_SCHEME)
92            .set_name(&core.db.path().to_string_lossy())
93            .set_root("/")
94            .set_native_capability(Capability {
95                read: true,
96                stat: true,
97                write: true,
98                write_can_empty: true,
99                delete: true,
100                list: true,
101                list_with_recursive: true,
102                shared: false,
103                ..Default::default()
104            });
105
106        Self {
107            core: Arc::new(core),
108            root: "/".to_string(),
109            info: Arc::new(info),
110        }
111    }
112
113    fn with_normalized_root(mut self, root: String) -> Self {
114        self.info.set_root(&root);
115        self.root = root;
116        self
117    }
118}
119
120impl Access for RocksdbBackend {
121    type Reader = Buffer;
122    type Writer = RocksdbWriter;
123    type Lister = oio::HierarchyLister<RocksdbLister>;
124    type Deleter = oio::OneShotDeleter<RocksdbDeleter>;
125
126    fn info(&self) -> Arc<AccessorInfo> {
127        self.info.clone()
128    }
129
130    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
131        let p = build_abs_path(&self.root, path);
132
133        if p == build_abs_path(&self.root, "") {
134            Ok(RpStat::new(Metadata::new(EntryMode::DIR)))
135        } else {
136            let bs = self.core.get(&p)?;
137            match bs {
138                Some(bs) => Ok(RpStat::new(
139                    Metadata::new(EntryMode::FILE).with_content_length(bs.len() as u64),
140                )),
141                None => Err(Error::new(ErrorKind::NotFound, "kv not found in rocksdb")),
142            }
143        }
144    }
145
146    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
147        let p = build_abs_path(&self.root, path);
148        let bs = match self.core.get(&p)? {
149            Some(bs) => bs,
150            None => {
151                return Err(Error::new(ErrorKind::NotFound, "kv not found in rocksdb"));
152            }
153        };
154        Ok((RpRead::new(), bs.slice(args.range().to_range_as_usize())))
155    }
156
157    async fn write(&self, path: &str, _: OpWrite) -> Result<(RpWrite, Self::Writer)> {
158        let p = build_abs_path(&self.root, path);
159        let writer = RocksdbWriter::new(self.core.clone(), p);
160        Ok((RpWrite::new(), writer))
161    }
162
163    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
164        let deleter = RocksdbDeleter::new(self.core.clone(), self.root.clone());
165        Ok((RpDelete::default(), oio::OneShotDeleter::new(deleter)))
166    }
167
168    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
169        let p = build_abs_path(&self.root, path);
170        let lister = RocksdbLister::new(self.core.clone(), self.root.clone(), p)?;
171        Ok((
172            RpList::default(),
173            oio::HierarchyLister::new(lister, path, args.recursive()),
174        ))
175    }
176}