Skip to content

Commit

Permalink
feat(services/moka, services/mini-moka): Add scan support (#2850)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLerxky authored Aug 11, 2023
1 parent dac7910 commit d380275
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
36 changes: 15 additions & 21 deletions core/src/services/mini_moka/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,7 @@ use crate::raw::adapters::typed_kv;
use crate::*;

/// [mini-moka](https://github.com/moka-rs/mini-moka) backend support.
///
/// # Capabilities
///
/// This service can be used to:
///
/// - [x] stat
/// - [x] read
/// - [x] write
/// - [x] create_dir
/// - [x] delete
/// - [ ] copy
/// - [ ] rename
/// - [ ] list
/// - [ ] ~~scan~~
/// - [ ] presign
/// - [ ] blocking
///
/// # Notes
///
/// To better assist you in choosing the right cache for your use case,
/// Here's a comparison table with [moka](https://github.com/moka-rs/moka#choosing-the-right-cache-for-your-use-case)
#[doc = include_str!("docs.md")]
#[derive(Default, Debug)]
pub struct MiniMokaBuilder {
/// Sets the max capacity of the cache.
Expand Down Expand Up @@ -167,6 +147,7 @@ impl typed_kv::Adapter for Adapter {
get: true,
set: true,
delete: true,
scan: true,
..Default::default()
},
)
Expand Down Expand Up @@ -202,4 +183,17 @@ impl typed_kv::Adapter for Adapter {

Ok(())
}

async fn scan(&self, path: &str) -> Result<Vec<String>> {
self.blocking_scan(path)
}

fn blocking_scan(&self, path: &str) -> Result<Vec<String>> {
let keys = self.inner.iter().map(|kv| kv.key().to_string());
if path.is_empty() {
Ok(keys.collect())
} else {
Ok(keys.filter(|k| k.starts_with(path)).collect())
}
}
}
20 changes: 20 additions & 0 deletions core/src/services/mini_moka/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Capabilities

This service can be used to:

- [x] stat
- [x] read
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [ ] list
- [x] scan
- [ ] presign
- [ ] blocking

## Notes

To better assist you in choosing the right cache for your use case,
Here's a comparison table with [moka](https://github.com/moka-rs/moka#choosing-the-right-cache-for-your-use-case)
31 changes: 15 additions & 16 deletions core/src/services/moka/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,7 @@ use crate::raw::adapters::typed_kv;
use crate::*;

/// [moka](https://github.com/moka-rs/moka) backend support.
///
/// # Capabilities
///
/// This service can be used to:
///
/// - [x] stat
/// - [x] read
/// - [x] write
/// - [x] create_dir
/// - [x] delete
/// - [ ] copy
/// - [ ] rename
/// - [ ] list
/// - [ ] ~~scan~~
/// - [ ] presign
/// - [ ] blocking
#[doc = include_str!("docs.md")]
#[derive(Default, Debug)]
pub struct MokaBuilder {
/// Name for this cache instance.
Expand Down Expand Up @@ -208,6 +193,7 @@ impl typed_kv::Adapter for Adapter {
get: true,
set: true,
delete: true,
scan: true,
..Default::default()
},
)
Expand Down Expand Up @@ -243,4 +229,17 @@ impl typed_kv::Adapter for Adapter {

Ok(())
}

async fn scan(&self, path: &str) -> Result<Vec<String>> {
self.blocking_scan(path)
}

fn blocking_scan(&self, path: &str) -> Result<Vec<String>> {
let keys = self.inner.iter().map(|kv| kv.0.to_string());
if path.is_empty() {
Ok(keys.collect())
} else {
Ok(keys.filter(|k| k.starts_with(path)).collect())
}
}
}
15 changes: 15 additions & 0 deletions core/src/services/moka/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Capabilities

This service can be used to:

- [x] stat
- [x] read
- [x] write
- [x] create_dir
- [x] delete
- [ ] copy
- [ ] rename
- [ ] list
- [x] scan
- [ ] presign
- [ ] blocking

0 comments on commit d380275

Please sign in to comment.