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