opendal_core/services/foundationdb/
core.rs1use 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}