diff options
author | Alex Auvolat <alex@adnab.me> | 2022-06-02 23:43:32 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-06-02 23:43:32 +0200 |
commit | 0e4f33688745b2ffe71a80fdf0de44a1163d4f2d (patch) | |
tree | e8c977f00ade7d741777f59e65c689b9572b8385 /src | |
parent | c439cb11a908ee5405ed0a3a721e9c8c0e299ad2 (diff) | |
download | garage-0e4f33688745b2ffe71a80fdf0de44a1163d4f2d.tar.gz garage-0e4f33688745b2ffe71a80fdf0de44a1163d4f2d.zip |
Horrible implementation of range for sqlite
Diffstat (limited to 'src')
-rw-r--r-- | src/db/sqlite_adapter.rs | 94 |
1 files changed, 92 insertions, 2 deletions
diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs index bed72e6b..0516f2b7 100644 --- a/src/db/sqlite_adapter.rs +++ b/src/db/sqlite_adapter.rs @@ -132,7 +132,52 @@ impl IDb for SqliteDb { high: Bound<&'r [u8]>, ) -> Result<ValueIter<'a>> { let tree = self.get_tree(tree)?; - unimplemented!(); + + 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 params = params + .iter() + .map(|x| x as &dyn rusqlite::ToSql) + .collect::<Vec<_>>(); + DbValueIterator::new::<&[&dyn rusqlite::ToSql]>( + self.db.lock().unwrap(), + &sql, + params.as_ref(), + ) } fn range_rev<'a, 'r>( &'a self, @@ -141,7 +186,52 @@ impl IDb for SqliteDb { high: Bound<&'r [u8]>, ) -> Result<ValueIter<'a>> { let tree = self.get_tree(tree)?; - unimplemented!(); + + 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 params = params + .iter() + .map(|x| x as &dyn rusqlite::ToSql) + .collect::<Vec<_>>(); + DbValueIterator::new::<&[&dyn rusqlite::ToSql]>( + self.db.lock().unwrap(), + &sql, + params.as_ref(), + ) } // ---- |