Skip to content

Commit 95ff8e2

Browse files
authored
feat(adapters-kv): add rename and copy support to kv adapters (#2513)
* fix(binding/zig): fix zig binding CI failed Signed-off-by: owl <[email protected]> * feat(adapters-kv): add rename and copy support to kv adapters Signed-off-by: owl <[email protected]> --------- Signed-off-by: owl <[email protected]>
1 parent 374e404 commit 95ff8e2

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

bindings/zig/test/bdd.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ test "Opendal BDD test" {
6262
.len = std.mem.len(testkit.content),
6363
};
6464
const code = opendal.c.opendal_operator_blocking_write(testkit.p, testkit.path, data);
65-
try testing.expectEqual(code, @enumToInt(Code.OK));
65+
try testing.expectEqual(code, @intFromEnum(Code.OK));
6666

6767
// The blocking file "test" should exist
6868
var e: opendal.c.opendal_result_is_exist = opendal.c.opendal_operator_is_exist(testkit.p, testkit.path);
69-
try testing.expectEqual(e.code, @enumToInt(Code.OK));
69+
try testing.expectEqual(e.code, @intFromEnum(Code.OK));
7070
try testing.expect(e.is_exist);
7171

7272
// The blocking file "test" entry mode must be file
7373
var s: opendal.c.opendal_result_stat = opendal.c.opendal_operator_stat(testkit.p, testkit.path);
74-
try testing.expectEqual(s.code, @enumToInt(Code.OK));
74+
try testing.expectEqual(s.code, @intFromEnum(Code.OK));
7575
var meta: [*c]opendal.c.opendal_metadata = s.meta;
7676
try testing.expect(opendal.c.opendal_metadata_is_file(meta));
7777

@@ -82,7 +82,7 @@ test "Opendal BDD test" {
8282
// The blocking file "test" must have content "Hello, World!"
8383
var r: opendal.c.opendal_result_read = opendal.c.opendal_operator_blocking_read(testkit.p, testkit.path);
8484
defer opendal.c.opendal_bytes_free(r.data);
85-
try testing.expect(r.code == @enumToInt(Code.OK));
85+
try testing.expect(r.code == @intFromEnum(Code.OK));
8686
try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
8787

8888
var count: usize = 0;

core/src/raw/adapters/typed_kv/backend.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,58 @@ impl<S: Adapter> Accessor for Backend<S> {
221221

222222
Ok((RpList::default(), pager))
223223
}
224+
225+
async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
226+
match self.copy(from, to, OpCopy {}).await {
227+
Ok(_) => {
228+
let from = build_abs_path(&self.root, from);
229+
self.kv.delete(&from).await?;
230+
Ok(RpRename::default())
231+
}
232+
Err(e) => Err(e),
233+
}
234+
}
235+
236+
fn blocking_rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
237+
match self.blocking_copy(from, to, OpCopy {}) {
238+
Ok(_) => {
239+
let from = build_abs_path(&self.root, from);
240+
self.kv.blocking_delete(&from)?;
241+
Ok(RpRename::default())
242+
}
243+
Err(e) => Err(e),
244+
}
245+
}
246+
247+
async fn copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {
248+
let from = build_abs_path(&self.root, from);
249+
let to = build_abs_path(&self.root, to);
250+
251+
let bs = match self.kv.get(&from).await? {
252+
// TODO: we can reuse the metadata in value to build content range.
253+
Some(bs) => bs,
254+
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
255+
};
256+
257+
self.kv.set(&to, bs).await?;
258+
259+
Ok(RpCopy::default())
260+
}
261+
262+
fn blocking_copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {
263+
let from = build_abs_path(&self.root, from);
264+
let to = build_abs_path(&self.root, to);
265+
266+
let bs = match self.kv.blocking_get(&from)? {
267+
// TODO: we can reuse the metadata in value to build content range.
268+
Some(bs) => bs,
269+
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
270+
};
271+
272+
self.kv.blocking_set(&to, bs)?;
273+
274+
Ok(RpCopy::default())
275+
}
224276
}
225277

226278
impl<S> Backend<S>

0 commit comments

Comments
 (0)