Skip to content

Commit

Permalink
fix clippy and simplify the set interface of kv
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrchen committed Nov 10, 2021
1 parent 755a8eb commit 588304c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
9 changes: 7 additions & 2 deletions 26_kv/src/storage/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ impl Storage for MemTable {
Ok(table.get(key).map(|v| v.value().clone()))
}

fn set(&self, table: &str, key: String, value: Value) -> Result<Option<Value>, KvError> {
fn set(
&self,
table: &str,
key: impl Into<String>,
value: impl Into<Value>,
) -> Result<Option<Value>, KvError> {
let table = self.get_or_create_table(table);
Ok(table.insert(key, value))
Ok(table.insert(key.into(), value.into()))
}

fn contains(&self, table: &str, key: &str) -> Result<bool, KvError> {
Expand Down
19 changes: 12 additions & 7 deletions 26_kv/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ pub trait Storage {
/// 从一个 HashTable 里获取一个 key 的 value
fn get(&self, table: &str, key: &str) -> Result<Option<Value>, KvError>;
/// 从一个 HashTable 里设置一个 key 的 value,返回旧的 value
fn set(&self, table: &str, key: String, value: Value) -> Result<Option<Value>, KvError>;
fn set(
&self,
table: &str,
key: impl Into<String>,
value: impl Into<Value>,
) -> Result<Option<Value>, KvError>;
/// 查看 HashTable 中是否有 key
fn contains(&self, table: &str, key: &str) -> Result<bool, KvError>;
/// 从 HashTable 中删除一个 key
Expand Down Expand Up @@ -94,10 +99,10 @@ mod tests {

fn test_basi_interface(store: impl Storage) {
// 第一次 set 会创建 table,插入 key 并返回 None(之前没值)
let v = store.set("t1", "hello".into(), "world".into());
let v = store.set("t1", "hello", "world");
assert!(v.unwrap().is_none());
// 再次 set 同样的 key 会更新,并返回之前的值
let v1 = store.set("t1", "hello".into(), "world1".into());
let v1 = store.set("t1", "hello", "world1");
assert_eq!(v1, Ok(Some("world".into())));

// get 存在的 key 会得到最新的值
Expand All @@ -123,8 +128,8 @@ mod tests {
}

fn test_get_all(store: impl Storage) {
store.set("t2", "k1".into(), "v1".into()).unwrap();
store.set("t2", "k2".into(), "v2".into()).unwrap();
store.set("t2", "k1", "v1").unwrap();
store.set("t2", "k2", "v2").unwrap();
let mut data = store.get_all("t2").unwrap();
data.sort_by(|a, b| a.partial_cmp(b).unwrap());
assert_eq!(
Expand All @@ -137,8 +142,8 @@ mod tests {
}

fn test_get_iter(store: impl Storage) {
store.set("t2", "k1".into(), "v1".into()).unwrap();
store.set("t2", "k2".into(), "v2".into()).unwrap();
store.set("t2", "k1", "v1").unwrap();
store.set("t2", "k2", "v2").unwrap();
let mut data: Vec<_> = store.get_iter("t2").unwrap().collect();
data.sort_by(|a, b| a.partial_cmp(b).unwrap());
assert_eq!(
Expand Down
10 changes: 8 additions & 2 deletions 26_kv/src/storage/sleddb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ impl Storage for SledDb {
flip(result)
}

fn set(&self, table: &str, key: String, value: Value) -> Result<Option<Value>, KvError> {
fn set(
&self,
table: &str,
key: impl Into<String>,
value: impl Into<Value>,
) -> Result<Option<Value>, KvError> {
let key = key.into();
let name = SledDb::get_full_key(table, &key);
let data: Vec<u8> = value.try_into()?;
let data: Vec<u8> = value.into().try_into()?;

let result = self.0.insert(name, data)?.map(|v| v.as_ref().try_into());
flip(result)
Expand Down
2 changes: 0 additions & 2 deletions 31_ffi/bzlib_sys/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(unknown_lints)]
#![allow(clippy::all)]
/* automatically generated by rust-bindgen 0.59.1 */

pub const BZ_RUN: u32 = 0;
Expand Down
1 change: 1 addition & 0 deletions 31_ffi/bzlib_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use anyhow::{anyhow, Result};
use std::mem;

#[allow(clippy::all)]
mod bindings;

pub use bindings::*;
Expand Down

0 comments on commit 588304c

Please sign in to comment.