aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-06-02 23:46:28 +0200
committerAlex Auvolat <alex@adnab.me>2022-06-02 23:46:28 +0200
commit6ebb54cddb3ac21f7384720b8336995a8889d20d (patch)
tree8dcf03aaf2e674e3b527d8d6287e783f5c8d2a89
parent0e4f33688745b2ffe71a80fdf0de44a1163d4f2d (diff)
downloadgarage-6ebb54cddb3ac21f7384720b8336995a8889d20d.tar.gz
garage-6ebb54cddb3ac21f7384720b8336995a8889d20d.zip
Just refactor a bit
-rw-r--r--src/db/sqlite_adapter.rs116
1 files changed, 45 insertions, 71 deletions
diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs
index 0516f2b7..605a61d0 100644
--- a/src/db/sqlite_adapter.rs
+++ b/src/db/sqlite_adapter.rs
@@ -1,6 +1,5 @@
use core::ops::Bound;
-use std::cell::Cell;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::ptr::NonNull;
@@ -133,41 +132,8 @@ impl IDb for SqliteDb {
) -> Result<ValueIter<'a>> {
let tree = self.get_tree(tree)?;
- let mut sql = format!("SELECT k, v FROM {}", tree);
- let mut params: Vec<Vec<u8>> = vec![];
-
- match low {
- Bound::Included(b) => {
- sql.push_str(" WHERE k >= ?1");
- params.push(b.to_vec());
- }
- Bound::Excluded(b) => {
- sql.push_str(" WHERE k > ?1");
- params.push(b.to_vec());
- }
- Bound::Unbounded => (),
- };
-
- match high {
- Bound::Included(b) => {
- if !params.is_empty() {
- sql.push_str(" AND k <= ?2");
- } else {
- sql.push_str(" WHERE k <= ?1");
- }
- params.push(b.to_vec());
- }
- Bound::Excluded(b) => {
- if !params.is_empty() {
- sql.push_str(" AND k < ?2");
- } else {
- sql.push_str(" WHERE k < ?1");
- }
- params.push(b.to_vec());
- }
- Bound::Unbounded => (),
- }
- sql.push_str(" ORDER BY k ASC");
+ let (bounds_sql, params) = bounds_sql(low, high);
+ let sql = format!("SELECT k, v FROM {} {} ORDER BY k ASC", tree, bounds_sql);
let params = params
.iter()
@@ -187,41 +153,8 @@ impl IDb for SqliteDb {
) -> Result<ValueIter<'a>> {
let tree = self.get_tree(tree)?;
- let mut sql = format!("SELECT k, v FROM {}", tree);
- let mut params: Vec<Vec<u8>> = vec![];
-
- match low {
- Bound::Included(b) => {
- sql.push_str(" WHERE k >= ?1");
- params.push(b.to_vec());
- }
- Bound::Excluded(b) => {
- sql.push_str(" WHERE k > ?1");
- params.push(b.to_vec());
- }
- Bound::Unbounded => (),
- };
-
- match high {
- Bound::Included(b) => {
- if !params.is_empty() {
- sql.push_str(" AND k <= ?2");
- } else {
- sql.push_str(" WHERE k <= ?1");
- }
- params.push(b.to_vec());
- }
- Bound::Excluded(b) => {
- if !params.is_empty() {
- sql.push_str(" AND k < ?2");
- } else {
- sql.push_str(" WHERE k < ?1");
- }
- params.push(b.to_vec());
- }
- Bound::Unbounded => (),
- }
- sql.push_str(" ORDER BY k DESC");
+ let (bounds_sql, params) = bounds_sql(low, high);
+ let sql = format!("SELECT k, v FROM {} {} ORDER BY k DESC", tree, bounds_sql);
let params = params
.iter()
@@ -429,3 +362,44 @@ impl<'a> Iterator for DbValueIteratorPin<'a> {
Some(Ok((k.into(), v.into())))
}
}
+
+// ----
+
+fn bounds_sql<'r>(low: Bound<&'r [u8]>, high: Bound<&'r [u8]>) -> (String, Vec<Vec<u8>>) {
+ let mut sql = String::new();
+ let mut params: Vec<Vec<u8>> = vec![];
+
+ match low {
+ Bound::Included(b) => {
+ sql.push_str(" WHERE k >= ?1");
+ params.push(b.to_vec());
+ }
+ Bound::Excluded(b) => {
+ sql.push_str(" WHERE k > ?1");
+ params.push(b.to_vec());
+ }
+ Bound::Unbounded => (),
+ };
+
+ match high {
+ Bound::Included(b) => {
+ if !params.is_empty() {
+ sql.push_str(" AND k <= ?2");
+ } else {
+ sql.push_str(" WHERE k <= ?1");
+ }
+ params.push(b.to_vec());
+ }
+ Bound::Excluded(b) => {
+ if !params.is_empty() {
+ sql.push_str(" AND k < ?2");
+ } else {
+ sql.push_str(" WHERE k < ?1");
+ }
+ params.push(b.to_vec());
+ }
+ Bound::Unbounded => (),
+ }
+
+ (sql, params)
+}