Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapters-kv): add rename and copy support to non typed kv adapters #2515

Merged
merged 8 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions core/src/raw/adapters/kv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ impl<S: Adapter> Accessor for Backend<S> {
cap.delete = true;
}

if cap.read && cap.write {
cap.copy = true;
}

if cap.read && cap.write && cap.delete {
cap.rename = true;
}

if cap.list {
cap.list_without_delimiter = true;
}
Expand Down Expand Up @@ -230,6 +238,68 @@ impl<S: Adapter> Accessor for Backend<S> {

Ok((RpList::default(), pager))
}

async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
let from = build_abs_path(&self.root, from);
let to = build_abs_path(&self.root, to);

let bs = match self.kv.get(&from).await? {
// TODO: we can reuse the metadata in value to build content range.
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
};

self.kv.set(&to, &bs).await?;

self.kv.delete(&from).await?;
Ok(RpRename::default())
}

fn blocking_rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
let from = build_abs_path(&self.root, from);
let to = build_abs_path(&self.root, to);

let bs = match self.kv.blocking_get(&from)? {
// TODO: we can reuse the metadata in value to build content range.
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
};

self.kv.blocking_set(&to, &bs)?;

self.kv.blocking_delete(&from)?;
Ok(RpRename::default())
}

async fn copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {
let from = build_abs_path(&self.root, from);
let to = build_abs_path(&self.root, to);

let bs = match self.kv.get(&from).await? {
// TODO: we can reuse the metadata in value to build content range.
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
};

self.kv.set(&to, &bs).await?;

Ok(RpCopy::default())
}

fn blocking_copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {
let from = build_abs_path(&self.root, from);
let to = build_abs_path(&self.root, to);

let bs = match self.kv.blocking_get(&from)? {
// TODO: we can reuse the metadata in value to build content range.
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
};

self.kv.blocking_set(&to, &bs)?;

Ok(RpCopy::default())
}
}

impl<S> Backend<S>
Expand Down
48 changes: 32 additions & 16 deletions core/src/raw/adapters/typed_kv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ impl<S: Adapter> Accessor for Backend<S> {
cap.list_without_delimiter = true;
}

if cap.read && cap.write {
cap.copy = true;
}

if cap.read && cap.write && cap.delete {
cap.rename = true;
}

cap.blocking = true;

am
Expand Down Expand Up @@ -223,25 +231,33 @@ impl<S: Adapter> Accessor for Backend<S> {
}

async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
match self.copy(from, to, OpCopy {}).await {
Ok(_) => {
let from = build_abs_path(&self.root, from);
self.kv.delete(&from).await?;
Ok(RpRename::default())
}
Err(e) => Err(e),
}
let from = build_abs_path(&self.root, from);
let to = build_abs_path(&self.root, to);

let bs = match self.kv.get(&from).await? {
// TODO: we can reuse the metadata in value to build content range.
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
};

self.kv.set(&to, bs).await?;
self.kv.delete(&from).await?;
Ok(RpRename::default())
}

fn blocking_rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
match self.blocking_copy(from, to, OpCopy {}) {
Ok(_) => {
let from = build_abs_path(&self.root, from);
self.kv.blocking_delete(&from)?;
Ok(RpRename::default())
}
Err(e) => Err(e),
}
let from = build_abs_path(&self.root, from);
let to = build_abs_path(&self.root, to);

let bs = match self.kv.blocking_get(&from)? {
// TODO: we can reuse the metadata in value to build content range.
Some(bs) => bs,
None => return Err(Error::new(ErrorKind::NotFound, "kv doesn't have this path")),
};

self.kv.blocking_set(&to, bs)?;
self.kv.blocking_delete(&from)?;
Ok(RpRename::default())
}

async fn copy(&self, from: &str, to: &str, _: OpCopy) -> Result<RpCopy> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/services/dashmap/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This service can be used to:
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [x] copy
- [x] rename
- [ ] list
- [x] scan
- [ ] presign
Expand Down
4 changes: 2 additions & 2 deletions core/src/services/memory/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This service can be used to:
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [x] copy
- [x] rename
- [ ] list
- [x] scan
- [ ] presign
Expand Down
4 changes: 2 additions & 2 deletions core/src/services/redis/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This service can be used to:
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [x] copy
- [x] rename
- [ ] ~~list~~
- [ ] scan
- [ ] ~~presign~~
Expand Down
4 changes: 2 additions & 2 deletions core/src/services/rocksdb/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This service can be used to:
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [x] copy
- [x] rename
- [ ] ~~list~~
- [x] scan
- [ ] ~~presign~~
Expand Down
4 changes: 2 additions & 2 deletions core/src/services/sled/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This service can be used to:
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [x] copy
- [x] rename
- [ ] ~~list~~
- [x] scan
- [ ] ~~presign~~
Expand Down