Skip to content

Commit 251c5b5

Browse files
committed
feat(adapters-kv): fix code
Signed-off-by: owl <[email protected]>
1 parent 1c884db commit 251c5b5

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,25 +235,35 @@ impl<S: Adapter> Accessor for Backend<S> {
235235
}
236236

237237
async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
238-
match self.copy(from, to, OpCopy {}).await {
239-
Ok(_) => {
240-
let from = build_abs_path(&self.root, from);
241-
self.kv.delete(&from).await?;
242-
Ok(RpRename::default())
243-
}
244-
Err(e) => Err(e),
245-
}
238+
let from = build_abs_path(&self.root, from);
239+
let to = build_abs_path(&self.root, to);
240+
241+
let bs = match self.kv.get(&from).await? {
242+
// TODO: we can reuse the metadata in value to build content range.
243+
Some(bs) => bs,
244+
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
245+
};
246+
247+
self.kv.set(&to, &bs).await?;
248+
249+
self.kv.delete(&from).await?;
250+
Ok(RpRename::default())
246251
}
247252

248253
fn blocking_rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
249-
match self.blocking_copy(from, to, OpCopy {}) {
250-
Ok(_) => {
251-
let from = build_abs_path(&self.root, from);
252-
self.kv.blocking_delete(&from)?;
253-
Ok(RpRename::default())
254-
}
255-
Err(e) => Err(e),
256-
}
254+
let from = build_abs_path(&self.root, from);
255+
let to = build_abs_path(&self.root, to);
256+
257+
let bs = match self.kv.blocking_get(&from)? {
258+
// TODO: we can reuse the metadata in value to build content range.
259+
Some(bs) => bs,
260+
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
261+
};
262+
263+
self.kv.blocking_set(&to, &bs)?;
264+
265+
self.kv.blocking_delete(&from)?;
266+
Ok(RpRename::default())
257267
}
258268

259269
async fn copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,33 @@ impl<S: Adapter> Accessor for Backend<S> {
226226
}
227227

228228
async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
229-
match self.copy(from, to, OpCopy {}).await {
230-
Ok(_) => {
231-
let from = build_abs_path(&self.root, from);
232-
self.kv.delete(&from).await?;
233-
Ok(RpRename::default())
234-
}
235-
Err(e) => Err(e),
236-
}
229+
let from = build_abs_path(&self.root, from);
230+
let to = build_abs_path(&self.root, to);
231+
232+
let bs = match self.kv.get(&from).await? {
233+
// TODO: we can reuse the metadata in value to build content range.
234+
Some(bs) => bs,
235+
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
236+
};
237+
238+
self.kv.set(&to, bs).await?;
239+
self.kv.delete(&from).await?;
240+
Ok(RpRename::default())
237241
}
238242

239243
fn blocking_rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
240-
match self.blocking_copy(from, to, OpCopy {}) {
241-
Ok(_) => {
242-
let from = build_abs_path(&self.root, from);
243-
self.kv.blocking_delete(&from)?;
244-
Ok(RpRename::default())
245-
}
246-
Err(e) => Err(e),
247-
}
244+
let from = build_abs_path(&self.root, from);
245+
let to = build_abs_path(&self.root, to);
246+
247+
let bs = match self.kv.blocking_get(&from)? {
248+
// TODO: we can reuse the metadata in value to build content range.
249+
Some(bs) => bs,
250+
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
251+
};
252+
253+
self.kv.blocking_set(&to, bs)?;
254+
self.kv.blocking_delete(&from)?;
255+
Ok(RpRename::default())
248256
}
249257

250258
async fn copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {

0 commit comments

Comments
 (0)