opendal/services/persy/
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::fmt::Debug;
19use std::sync::Arc;
20
21use super::config::PersyConfig;
22use super::core::*;
23use super::deleter::PersyDeleter;
24use super::writer::PersyWriter;
25use crate::raw::*;
26use crate::*;
27
28/// persy service support.
29#[doc = include_str!("docs.md")]
30#[derive(Default, Debug)]
31pub struct PersyBuilder {
32    pub(super) config: PersyConfig,
33}
34
35impl PersyBuilder {
36    /// Set the path to the persy data directory. Will create if not exists.
37    pub fn datafile(mut self, path: &str) -> Self {
38        self.config.datafile = Some(path.into());
39        self
40    }
41
42    /// Set the name of the persy segment. Will create if not exists.
43    pub fn segment(mut self, path: &str) -> Self {
44        self.config.segment = Some(path.into());
45        self
46    }
47
48    /// Set the name of the persy index. Will create if not exists.
49    pub fn index(mut self, path: &str) -> Self {
50        self.config.index = Some(path.into());
51        self
52    }
53}
54
55impl Builder for PersyBuilder {
56    type Config = PersyConfig;
57
58    fn build(self) -> Result<impl Access> {
59        let datafile_path = self.config.datafile.ok_or_else(|| {
60            Error::new(ErrorKind::ConfigInvalid, "datafile is required but not set")
61                .with_context("service", Scheme::Persy)
62        })?;
63
64        let segment_name = self.config.segment.ok_or_else(|| {
65            Error::new(ErrorKind::ConfigInvalid, "segment is required but not set")
66                .with_context("service", Scheme::Persy)
67        })?;
68
69        let segment = segment_name.clone();
70
71        let index_name = self.config.index.ok_or_else(|| {
72            Error::new(ErrorKind::ConfigInvalid, "index is required but not set")
73                .with_context("service", Scheme::Persy)
74        })?;
75
76        let index = index_name.clone();
77
78        let persy = persy::OpenOptions::new()
79            .create(true)
80            .prepare_with(move |p| init(p, &segment_name, &index_name))
81            .open(&datafile_path)
82            .map_err(|e| {
83                Error::new(ErrorKind::ConfigInvalid, "open db")
84                    .with_context("service", Scheme::Persy)
85                    .with_context("datafile", datafile_path.clone())
86                    .set_source(e)
87            })?;
88
89        // This function will only be called on database creation
90        fn init(
91            persy: &persy::Persy,
92            segment_name: &str,
93            index_name: &str,
94        ) -> Result<(), Box<dyn std::error::Error>> {
95            let mut tx = persy.begin()?;
96
97            if !tx.exists_segment(segment_name)? {
98                tx.create_segment(segment_name)?;
99            }
100            if !tx.exists_index(index_name)? {
101                tx.create_index::<String, persy::PersyId>(index_name, persy::ValueMode::Replace)?;
102            }
103
104            let prepared = tx.prepare()?;
105            prepared.commit()?;
106
107            Ok(())
108        }
109
110        Ok(PersyBackend::new(PersyCore {
111            datafile: datafile_path,
112            segment,
113            index,
114            persy,
115        }))
116    }
117}
118
119/// Backend for persy services.
120#[derive(Clone, Debug)]
121pub struct PersyBackend {
122    core: Arc<PersyCore>,
123    root: String,
124    info: Arc<AccessorInfo>,
125}
126
127impl PersyBackend {
128    pub fn new(core: PersyCore) -> Self {
129        let info = AccessorInfo::default();
130        info.set_scheme(Scheme::Persy.into_static());
131        info.set_name(&core.datafile);
132        info.set_root("/");
133        info.set_native_capability(Capability {
134            read: true,
135            stat: true,
136            write: true,
137            write_can_empty: true,
138            delete: true,
139            shared: false,
140            ..Default::default()
141        });
142
143        Self {
144            core: Arc::new(core),
145            root: "/".to_string(),
146            info: Arc::new(info),
147        }
148    }
149}
150
151impl Access for PersyBackend {
152    type Reader = Buffer;
153    type Writer = PersyWriter;
154    type Lister = ();
155    type Deleter = oio::OneShotDeleter<PersyDeleter>;
156
157    fn info(&self) -> Arc<AccessorInfo> {
158        self.info.clone()
159    }
160
161    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
162        let p = build_abs_path(&self.root, path);
163
164        if p == build_abs_path(&self.root, "") {
165            Ok(RpStat::new(Metadata::new(EntryMode::DIR)))
166        } else {
167            let bs = self.core.get(&p)?;
168            match bs {
169                Some(bs) => Ok(RpStat::new(
170                    Metadata::new(EntryMode::FILE).with_content_length(bs.len() as u64),
171                )),
172                None => Err(Error::new(ErrorKind::NotFound, "kv not found in persy")),
173            }
174        }
175    }
176
177    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
178        let p = build_abs_path(&self.root, path);
179        let bs = match self.core.get(&p)? {
180            Some(bs) => bs,
181            None => {
182                return Err(Error::new(ErrorKind::NotFound, "kv not found in persy"));
183            }
184        };
185        Ok((RpRead::new(), bs.slice(args.range().to_range_as_usize())))
186    }
187
188    async fn write(&self, path: &str, _: OpWrite) -> Result<(RpWrite, Self::Writer)> {
189        let p = build_abs_path(&self.root, path);
190        Ok((RpWrite::new(), PersyWriter::new(self.core.clone(), p)))
191    }
192
193    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
194        Ok((
195            RpDelete::default(),
196            oio::OneShotDeleter::new(PersyDeleter::new(self.core.clone(), self.root.clone())),
197        ))
198    }
199
200    async fn list(&self, path: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
201        let _ = build_abs_path(&self.root, path);
202        Ok((RpList::default(), ()))
203    }
204}