opendal/raw/adapters/kv/
api.rs1use std::fmt::Debug;
19use std::future::ready;
20use std::ops::DerefMut;
21
22use futures::Future;
23
24use crate::Capability;
25use crate::Scheme;
26use crate::raw::*;
27use crate::*;
28
29pub trait Scan: Send + Sync + Unpin {
31 fn next(&mut self) -> impl Future<Output = Result<Option<String>>> + MaybeSend;
35}
36
37impl Scan for () {
39 async fn next(&mut self) -> Result<Option<String>> {
40 Ok(None)
41 }
42}
43
44#[allow(dead_code)]
46pub struct ScanStdIter<I>(I);
47
48#[cfg(any(feature = "services-rocksdb", feature = "services-sled"))]
49impl<I> ScanStdIter<I>
50where
51 I: Iterator<Item = Result<String>> + Unpin + Send + Sync,
52{
53 pub(crate) fn new(inner: I) -> Self {
55 Self(inner)
56 }
57}
58
59impl<I> Scan for ScanStdIter<I>
60where
61 I: Iterator<Item = Result<String>> + Unpin + Send + Sync,
62{
63 async fn next(&mut self) -> Result<Option<String>> {
64 self.0.next().transpose()
65 }
66}
67
68pub type Scanner = Box<dyn ScanDyn>;
70
71pub trait ScanDyn: Unpin + Send + Sync {
72 fn next_dyn(&mut self) -> BoxedFuture<'_, Result<Option<String>>>;
73}
74
75impl<T: Scan + ?Sized> ScanDyn for T {
76 fn next_dyn(&mut self) -> BoxedFuture<'_, Result<Option<String>>> {
77 Box::pin(self.next())
78 }
79}
80
81impl<T: ScanDyn + ?Sized> Scan for Box<T> {
82 async fn next(&mut self) -> Result<Option<String>> {
83 self.deref_mut().next_dyn().await
84 }
85}
86
87pub trait Adapter: Send + Sync + Debug + Unpin + 'static {
91 type Scanner: Scan;
93
94 fn info(&self) -> Info;
96
97 fn get(&self, path: &str) -> impl Future<Output = Result<Option<Buffer>>> + MaybeSend;
101
102 fn set(&self, path: &str, value: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
104
105 fn delete(&self, path: &str) -> impl Future<Output = Result<()>> + MaybeSend;
109
110 fn scan(&self, path: &str) -> impl Future<Output = Result<Self::Scanner>> + MaybeSend {
112 let _ = path;
113
114 ready(Err(Error::new(
115 ErrorKind::Unsupported,
116 "kv adapter doesn't support this operation",
117 )
118 .with_operation("kv::Adapter::scan")))
119 }
120
121 fn append(&self, path: &str, value: &[u8]) -> impl Future<Output = Result<()>> + MaybeSend {
123 let _ = path;
124 let _ = value;
125
126 ready(Err(Error::new(
127 ErrorKind::Unsupported,
128 "kv adapter doesn't support this operation",
129 )
130 .with_operation("kv::Adapter::append")))
131 }
132}
133
134pub struct Info {
136 scheme: Scheme,
137 name: String,
138 capabilities: Capability,
139}
140
141impl Info {
142 pub fn new(scheme: Scheme, name: &str, capabilities: Capability) -> Self {
144 Self {
145 scheme,
146 name: name.to_string(),
147 capabilities,
148 }
149 }
150
151 pub fn scheme(&self) -> Scheme {
153 self.scheme
154 }
155
156 pub fn name(&self) -> &str {
158 &self.name
159 }
160
161 pub fn capabilities(&self) -> Capability {
163 self.capabilities
164 }
165}