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(
48 feature = "services-cloudflare-kv",
49 feature = "services-etcd",
50 feature = "services-rocksdb",
51 feature = "services-sled"
52))]
53impl<I> ScanStdIter<I>
54where
55 I: Iterator<Item = Result<String>> + Unpin + Send + Sync,
56{
57 pub(crate) fn new(inner: I) -> Self {
59 Self(inner)
60 }
61}
62
63impl<I> Scan for ScanStdIter<I>
64where
65 I: Iterator<Item = Result<String>> + Unpin + Send + Sync,
66{
67 async fn next(&mut self) -> Result<Option<String>> {
68 self.0.next().transpose()
69 }
70}
71
72pub type Scanner = Box<dyn ScanDyn>;
74
75pub trait ScanDyn: Unpin + Send + Sync {
76 fn next_dyn(&mut self) -> BoxedFuture<Result<Option<String>>>;
77}
78
79impl<T: Scan + ?Sized> ScanDyn for T {
80 fn next_dyn(&mut self) -> BoxedFuture<Result<Option<String>>> {
81 Box::pin(self.next())
82 }
83}
84
85impl<T: ScanDyn + ?Sized> Scan for Box<T> {
86 async fn next(&mut self) -> Result<Option<String>> {
87 self.deref_mut().next_dyn().await
88 }
89}
90
91pub trait Adapter: Send + Sync + Debug + Unpin + 'static {
95 type Scanner: Scan;
97
98 fn info(&self) -> Info;
100
101 fn get(&self, path: &str) -> impl Future<Output = Result<Option<Buffer>>> + MaybeSend;
105
106 fn set(&self, path: &str, value: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
108
109 fn delete(&self, path: &str) -> impl Future<Output = Result<()>> + MaybeSend;
113
114 fn scan(&self, path: &str) -> impl Future<Output = Result<Self::Scanner>> + MaybeSend {
116 let _ = path;
117
118 ready(Err(Error::new(
119 ErrorKind::Unsupported,
120 "kv adapter doesn't support this operation",
121 )
122 .with_operation("kv::Adapter::scan")))
123 }
124
125 fn append(&self, path: &str, value: &[u8]) -> impl Future<Output = Result<()>> + MaybeSend {
127 let _ = path;
128 let _ = value;
129
130 ready(Err(Error::new(
131 ErrorKind::Unsupported,
132 "kv adapter doesn't support this operation",
133 )
134 .with_operation("kv::Adapter::append")))
135 }
136}
137
138pub struct Info {
140 scheme: Scheme,
141 name: String,
142 capabilities: Capability,
143}
144
145impl Info {
146 pub fn new(scheme: Scheme, name: &str, capabilities: Capability) -> Self {
148 Self {
149 scheme,
150 name: name.to_string(),
151 capabilities,
152 }
153 }
154
155 pub fn scheme(&self) -> Scheme {
157 self.scheme
158 }
159
160 pub fn name(&self) -> &str {
162 &self.name
163 }
164
165 pub fn capabilities(&self) -> Capability {
167 self.capabilities
168 }
169}