opendal_core/services/foundationdb/
core.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 foundationdb::Database;
22use foundationdb::api::NetworkAutoStop;
23
24use super::FOUNDATIONDB_SCHEME;
25use crate::*;
26
27#[derive(Clone)]
28pub struct FoundationdbCore {
29    pub db: Arc<Database>,
30    pub _network: Arc<NetworkAutoStop>,
31}
32
33impl Debug for FoundationdbCore {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("FoundationdbCore").finish_non_exhaustive()
36    }
37}
38
39impl FoundationdbCore {
40    pub async fn get(&self, path: &str) -> Result<Option<Buffer>> {
41        let transaction = self.db.create_trx().expect("Unable to create transaction");
42
43        match transaction.get(path.as_bytes(), false).await {
44            Ok(slice) => match slice {
45                Some(data) => Ok(Some(Buffer::from(data.to_vec()))),
46                None => Err(Error::new(
47                    ErrorKind::NotFound,
48                    "foundationdb: key not found",
49                )),
50            },
51            Err(_) => Err(Error::new(
52                ErrorKind::NotFound,
53                "foundationdb: key not found",
54            )),
55        }
56    }
57
58    pub async fn set(&self, path: &str, value: Buffer) -> Result<()> {
59        let transaction = self.db.create_trx().expect("Unable to create transaction");
60
61        transaction.set(path.as_bytes(), &value.to_vec());
62
63        match transaction.commit().await {
64            Ok(_) => Ok(()),
65            Err(e) => Err(parse_transaction_commit_error(e)),
66        }
67    }
68
69    pub async fn delete(&self, path: &str) -> Result<()> {
70        let transaction = self.db.create_trx().expect("Unable to create transaction");
71        transaction.clear(path.as_bytes());
72
73        match transaction.commit().await {
74            Ok(_) => Ok(()),
75            Err(e) => Err(parse_transaction_commit_error(e)),
76        }
77    }
78}
79
80fn parse_transaction_commit_error(e: foundationdb::TransactionCommitError) -> Error {
81    Error::new(ErrorKind::Unexpected, e.to_string().as_str())
82        .with_context("service", FOUNDATIONDB_SCHEME)
83}