-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathbackend.rs
More file actions
304 lines (268 loc) · 8.79 KB
/
backend.rs
File metadata and controls
304 lines (268 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::fmt::Debug;
use std::sync::Arc;
use foyer::HybridCache;
use log::debug;
use super::FOYER_SCHEME;
use super::FoyerKey;
use super::FoyerValue;
use super::config::FoyerConfig;
use super::core::FoyerCore;
use super::deleter::FoyerDeleter;
use super::writer::FoyerWriter;
use opendal_core::raw::*;
use opendal_core::*;
/// [foyer](https://github.com/foyer-rs/foyer) backend support.
#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct FoyerBuilder {
pub(super) config: FoyerConfig,
pub(super) cache: Option<Arc<HybridCache<FoyerKey, FoyerValue>>>,
}
impl Debug for FoyerBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FoyerBuilder")
.field("config", &self.config)
.finish_non_exhaustive()
}
}
impl FoyerBuilder {
/// Create a new [`FoyerBuilder`] with default settings.
///
/// The cache will be lazily initialized when first accessed if not provided via [`Self::cache`].
///
/// # Example
///
/// ```no_run
/// use opendal_service_foyer::Foyer;
///
/// let builder = Foyer::new()
/// .memory(64 * 1024 * 1024)
/// .root("/cache");
/// ```
pub fn new() -> Self {
Self {
..Default::default()
}
}
/// Set the name of this cache instance.
pub fn name(mut self, name: &str) -> Self {
if !name.is_empty() {
self.config.name = Some(name.to_owned());
}
self
}
/// Set a pre-built [`foyer::HybridCache`] instance.
///
/// If provided, this cache will be used directly. Otherwise, a cache will be
/// lazily initialized using the configured memory size.
///
/// # Example
///
/// ```no_run
/// use opendal_service_foyer::Foyer;
/// use foyer::{HybridCacheBuilder, Engine};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let cache = HybridCacheBuilder::new()
/// .memory(64 * 1024 * 1024)
/// .storage(Engine::Large(Default::default()))
/// .build()
/// .await?;
///
/// let builder = Foyer::new().cache(cache);
/// # Ok(())
/// # }
/// ```
pub fn cache(mut self, cache: HybridCache<FoyerKey, FoyerValue>) -> Self {
self.cache = Some(Arc::new(cache));
self
}
/// Set the root path of this backend.
///
/// All operations will be relative to this root path.
pub fn root(mut self, path: &str) -> Self {
self.config.root = if path.is_empty() {
None
} else {
Some(path.to_string())
};
self
}
/// Set the memory capacity in bytes for the cache.
///
/// This is used when the cache is lazily initialized (i.e., when no pre-built cache
/// is provided via [`Self::cache`]).
///
/// Default is 1 GiB (1024 * 1024 * 1024 bytes).
pub fn memory(mut self, size: usize) -> Self {
self.config.memory = Some(size);
self
}
/// Set the disk cache directory path.
///
/// Enables hybrid cache with disk storage. When memory cache is full, data will
/// be persisted to this directory.
pub fn disk_path(mut self, path: &str) -> Self {
self.config.disk_path = if path.is_empty() {
None
} else {
Some(path.to_string())
};
self
}
/// Set the disk cache total capacity in bytes.
///
/// Only used when `disk_path` is set.
pub fn disk_capacity(mut self, size: usize) -> Self {
self.config.disk_capacity = Some(size);
self
}
/// Set the individual cache file size in bytes.
///
/// Default is 1 MiB (1024 * 1024 bytes).
/// Only used when `disk_path` is set.
pub fn disk_file_size(mut self, size: usize) -> Self {
self.config.disk_file_size = Some(size);
self
}
/// Set the recovery mode when starting the cache.
///
/// Valid values: "none" (default), "quiet", "strict".
/// - "none": Don't recover from disk
/// - "quiet": Recover and skip errors
/// - "strict": Recover and panic on errors
pub fn recover_mode(mut self, mode: &str) -> Self {
if !mode.is_empty() {
self.config.recover_mode = Some(mode.to_string());
}
self
}
/// Set the number of shards for concurrent access.
///
/// Default is 1. Higher values improve concurrency but increase overhead.
pub fn shards(mut self, count: usize) -> Self {
self.config.shards = Some(count);
self
}
}
impl Builder for FoyerBuilder {
type Config = FoyerConfig;
fn build(self) -> Result<impl Access> {
debug!("backend build started: {:?}", &self);
let root = normalize_root(
self.config
.root
.clone()
.unwrap_or_else(|| "/".to_string())
.as_str(),
);
let mut core = FoyerCore::new(self.config.clone());
if let Some(cache) = self.cache {
core = core.with_cache(cache.clone());
}
debug!("backend build finished: {:?}", self.config);
Ok(FoyerBackend::new(core).with_normalized_root(root))
}
}
#[derive(Debug, Clone)]
pub struct FoyerBackend {
core: Arc<FoyerCore>,
root: String,
info: Arc<AccessorInfo>,
}
impl FoyerBackend {
fn new(core: FoyerCore) -> Self {
let info = AccessorInfo::default();
info.set_scheme(FOYER_SCHEME);
info.set_name(core.name().unwrap_or("foyer"));
info.set_root("/");
info.set_native_capability(Capability {
read: true,
write: true,
write_can_empty: true,
delete: true,
stat: true,
shared: true,
..Default::default()
});
Self {
core: Arc::new(core),
root: "/".to_string(),
info: Arc::new(info),
}
}
fn with_normalized_root(mut self, root: String) -> Self {
self.info.set_root(&root);
self.root = root;
self
}
}
impl Access for FoyerBackend {
type Reader = Buffer;
type Writer = FoyerWriter;
type Lister = ();
type Deleter = oio::OneShotDeleter<FoyerDeleter>;
fn info(&self) -> Arc<AccessorInfo> {
self.info.clone()
}
async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
let p = build_abs_path(&self.root, path);
if p == build_abs_path(&self.root, "") {
Ok(RpStat::new(Metadata::new(EntryMode::DIR)))
} else {
match self.core.get(&p).await? {
Some(bs) => Ok(RpStat::new(
Metadata::new(EntryMode::FILE).with_content_length(bs.len() as u64),
)),
None => Err(Error::new(ErrorKind::NotFound, "key not found in foyer")),
}
}
}
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
let p = build_abs_path(&self.root, path);
let buffer = match self.core.get(&p).await? {
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "key not found in foyer")),
};
let buffer = if args.range().is_full() {
buffer
} else {
let range = args.range();
let start = range.offset() as usize;
let end = match range.size() {
Some(size) => (range.offset() + size) as usize,
None => buffer.len(),
};
buffer.slice(start..end.min(buffer.len()))
};
Ok((RpRead::new(), buffer))
}
async fn write(&self, path: &str, _: OpWrite) -> Result<(RpWrite, Self::Writer)> {
let p = build_abs_path(&self.root, path);
Ok((RpWrite::new(), FoyerWriter::new(self.core.clone(), p)))
}
async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
Ok((
RpDelete::default(),
oio::OneShotDeleter::new(FoyerDeleter::new(self.core.clone(), self.root.clone())),
))
}
}
#[cfg(test)]
mod tests {}