From ad33b4f089594fe134db3e4e127295f5c8986665 Mon Sep 17 00:00:00 2001 From: jihuayu <8042833@qq.com> Date: Wed, 14 Feb 2024 14:35:49 +0800 Subject: [PATCH 1/4] add sqlite scan function --- core/src/services/sqlite/backend.rs | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/core/src/services/sqlite/backend.rs b/core/src/services/sqlite/backend.rs index f108aa9680af..520ffc5b2567 100644 --- a/core/src/services/sqlite/backend.rs +++ b/core/src/services/sqlite/backend.rs @@ -263,6 +263,7 @@ impl kv::Adapter for Adapter { write: true, delete: true, blocking: true, + list: true, ..Default::default() }, ) @@ -335,6 +336,44 @@ impl kv::Adapter for Adapter { statement.execute([path]).map_err(parse_rusqlite_error)?; Ok(()) } + + /// Scan a key prefix to get all keys that start with this key. + async fn scan(&self, path: &str) -> Result> { + let this = self.clone(); + let path = path.to_string(); + + task::spawn_blocking(move || this.blocking_scan(&path)) + .await + .map_err(new_task_join_error)? + } + + /// Scan a key prefix to get all keys that start with this key + /// in blocking way. + fn blocking_scan(&self, path: &str) -> Result> { + let conn = self.pool.get().map_err(parse_r2d2_error)?; + + let query = format!( + "SELECT {} FROM {} WHERE `{}` like $1", + self.key_field, self.table, self.key_field + ); + let mut statement = conn.prepare(&query).map_err(parse_rusqlite_error)?; + let param = format!("{}%", path); + + let result = statement.query([param]); + + match result { + Ok(mut rows) => { + let mut keys: Vec = Vec::new(); + while let Some(row) = rows.next().map_err(parse_rusqlite_error)? { + let item = row.get(0).map_err(parse_rusqlite_error)?; + keys.push(item); + } + Ok(keys) + } + Err(rusqlite::Error::QueryReturnedNoRows) => Ok(vec![]), + Err(err) => Err(parse_rusqlite_error(err)), + } + } } fn parse_rusqlite_error(err: rusqlite::Error) -> Error { From 22640755ab8a56dfd39ab3bb7464b0aa04996171 Mon Sep 17 00:00:00 2001 From: jihuayu <8042833@qq.com> Date: Wed, 14 Feb 2024 14:36:26 +0800 Subject: [PATCH 2/4] update --- core/src/services/sqlite/backend.rs | 3 --- core/src/services/sqlite/docs.md | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/core/src/services/sqlite/backend.rs b/core/src/services/sqlite/backend.rs index 520ffc5b2567..86d2e446bc6c 100644 --- a/core/src/services/sqlite/backend.rs +++ b/core/src/services/sqlite/backend.rs @@ -337,7 +337,6 @@ impl kv::Adapter for Adapter { Ok(()) } - /// Scan a key prefix to get all keys that start with this key. async fn scan(&self, path: &str) -> Result> { let this = self.clone(); let path = path.to_string(); @@ -347,8 +346,6 @@ impl kv::Adapter for Adapter { .map_err(new_task_join_error)? } - /// Scan a key prefix to get all keys that start with this key - /// in blocking way. fn blocking_scan(&self, path: &str) -> Result> { let conn = self.pool.get().map_err(parse_r2d2_error)?; diff --git a/core/src/services/sqlite/docs.md b/core/src/services/sqlite/docs.md index ac349ace8c19..e8dad8185312 100644 --- a/core/src/services/sqlite/docs.md +++ b/core/src/services/sqlite/docs.md @@ -9,9 +9,7 @@ This service can be used to: - [x] delete - [ ] copy - [ ] rename -- [ ] ~~list~~ -- [ ] scan -- [ ] ~~presign~~ +- [x] list - [ ] blocking ## Configuration From b879e19dae2a526c183c5bed9f51ea608611b62a Mon Sep 17 00:00:00 2001 From: jihuayu <8042833@qq.com> Date: Wed, 14 Feb 2024 15:03:09 +0800 Subject: [PATCH 3/4] update --- core/src/services/sqlite/backend.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/core/src/services/sqlite/backend.rs b/core/src/services/sqlite/backend.rs index 86d2e446bc6c..6bff3a909303 100644 --- a/core/src/services/sqlite/backend.rs +++ b/core/src/services/sqlite/backend.rs @@ -348,15 +348,13 @@ impl kv::Adapter for Adapter { fn blocking_scan(&self, path: &str) -> Result> { let conn = self.pool.get().map_err(parse_r2d2_error)?; - let query = format!( - "SELECT {} FROM {} WHERE `{}` like $1", - self.key_field, self.table, self.key_field + "SELECT {} FROM {} WHERE `{}` LIKE $1 and `{}` <> $2", + self.key_field, self.table, self.key_field, self.key_field ); let mut statement = conn.prepare(&query).map_err(parse_rusqlite_error)?; - let param = format!("{}%", path); - - let result = statement.query([param]); + let like_param = format!("{}%", path); + let result = statement.query(params![like_param,path]); match result { Ok(mut rows) => { From 1d5b14d0ba0e5561c90ed0b1e121f8329283c76a Mon Sep 17 00:00:00 2001 From: jihuayu <8042833@qq.com> Date: Wed, 14 Feb 2024 15:15:50 +0800 Subject: [PATCH 4/4] format --- core/src/services/sqlite/backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/services/sqlite/backend.rs b/core/src/services/sqlite/backend.rs index 6bff3a909303..5e83651b1742 100644 --- a/core/src/services/sqlite/backend.rs +++ b/core/src/services/sqlite/backend.rs @@ -354,7 +354,7 @@ impl kv::Adapter for Adapter { ); let mut statement = conn.prepare(&query).map_err(parse_rusqlite_error)?; let like_param = format!("{}%", path); - let result = statement.query(params![like_param,path]); + let result = statement.query(params![like_param, path]); match result { Ok(mut rows) => {