This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new d38027533 feat(services/moka, services/mini-moka): Add scan support 
(#2850)
d38027533 is described below

commit d380275332bc4bdbb6ecb84b5c3fdd552e858df3
Author: JLer <[email protected]>
AuthorDate: Fri Aug 11 16:34:32 2023 +0800

    feat(services/moka, services/mini-moka): Add scan support (#2850)
---
 core/src/services/mini_moka/backend.rs | 36 ++++++++++++++--------------------
 core/src/services/mini_moka/docs.md    | 20 +++++++++++++++++++
 core/src/services/moka/backend.rs      | 31 ++++++++++++++---------------
 core/src/services/moka/docs.md         | 15 ++++++++++++++
 4 files changed, 65 insertions(+), 37 deletions(-)

diff --git a/core/src/services/mini_moka/backend.rs 
b/core/src/services/mini_moka/backend.rs
index 942fe909f..0703fd4de 100644
--- a/core/src/services/mini_moka/backend.rs
+++ b/core/src/services/mini_moka/backend.rs
@@ -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.
@@ -167,6 +147,7 @@ impl typed_kv::Adapter for Adapter {
                 get: true,
                 set: true,
                 delete: true,
+                scan: true,
                 ..Default::default()
             },
         )
@@ -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())
+        }
+    }
 }
diff --git a/core/src/services/mini_moka/docs.md 
b/core/src/services/mini_moka/docs.md
new file mode 100644
index 000000000..3a4f75720
--- /dev/null
+++ b/core/src/services/mini_moka/docs.md
@@ -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)
diff --git a/core/src/services/moka/backend.rs 
b/core/src/services/moka/backend.rs
index f6b2a8fc3..4b9081630 100644
--- a/core/src/services/moka/backend.rs
+++ b/core/src/services/moka/backend.rs
@@ -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.
@@ -208,6 +193,7 @@ impl typed_kv::Adapter for Adapter {
                 get: true,
                 set: true,
                 delete: true,
+                scan: true,
                 ..Default::default()
             },
         )
@@ -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())
+        }
+    }
 }
diff --git a/core/src/services/moka/docs.md b/core/src/services/moka/docs.md
new file mode 100644
index 000000000..89546b9c9
--- /dev/null
+++ b/core/src/services/moka/docs.md
@@ -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

Reply via email to