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