kv.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. use crate::kv::schema::{kv_table, kv_table::dsl, KV_SQL};
  2. use ::diesel::{query_dsl::*, ExpressionMethods};
  3. use diesel::{Connection, SqliteConnection};
  4. use lazy_static::lazy_static;
  5. use lib_sqlite::{DBConnection, Database, PoolConfig};
  6. use std::{collections::HashMap, path::Path, sync::RwLock};
  7. macro_rules! impl_get_func {
  8. (
  9. $func_name:ident,
  10. $get_method:ident=>$target:ident
  11. ) => {
  12. #[allow(dead_code)]
  13. pub fn $func_name(k: &str) -> Option<$target> {
  14. match KV::get(k) {
  15. Ok(item) => item.$get_method,
  16. Err(_) => None,
  17. }
  18. }
  19. };
  20. }
  21. macro_rules! impl_set_func {
  22. ($func_name:ident,$set_method:ident,$key_type:ident) => {
  23. #[allow(dead_code)]
  24. pub fn $func_name(key: &str, value: $key_type) {
  25. let mut item = KeyValue::new(key);
  26. item.$set_method = Some(value);
  27. match KV::set(item) {
  28. Ok(_) => {}
  29. Err(e) => {
  30. log::error!("{:?}", e)
  31. }
  32. };
  33. }
  34. };
  35. }
  36. const DB_NAME: &str = "kv.db";
  37. lazy_static! {
  38. static ref KV_HOLDER: RwLock<KV> = RwLock::new(KV::new());
  39. }
  40. pub struct KV {
  41. database: Option<Database>,
  42. cache: HashMap<String, KeyValue>,
  43. }
  44. impl KV {
  45. fn new() -> Self {
  46. KV {
  47. database: None,
  48. cache: HashMap::new(),
  49. }
  50. }
  51. fn set(value: KeyValue) -> Result<(), String> {
  52. log::trace!("[KV]: set value: {:?}", value);
  53. update_cache(value.clone());
  54. let _ = diesel::replace_into(kv_table::table)
  55. .values(&value)
  56. .execute(&*(get_connection()?))
  57. .map_err(|e| format!("KV set error: {:?}", e))?;
  58. Ok(())
  59. }
  60. fn get(key: &str) -> Result<KeyValue, String> {
  61. if let Some(value) = read_cache(key) {
  62. return Ok(value);
  63. }
  64. let conn = get_connection()?;
  65. let value = dsl::kv_table
  66. .filter(kv_table::key.eq(key))
  67. .first::<KeyValue>(&*conn)
  68. .map_err(|e| format!("KV get error: {:?}", e))?;
  69. update_cache(value.clone());
  70. Ok(value)
  71. }
  72. #[allow(dead_code)]
  73. pub fn remove(key: &str) -> Result<(), String> {
  74. log::debug!("remove key: {}", key);
  75. match KV_HOLDER.write() {
  76. Ok(mut guard) => {
  77. guard.cache.remove(key);
  78. }
  79. Err(e) => log::error!("Require write lock failed: {:?}", e),
  80. };
  81. let conn = get_connection()?;
  82. let sql = dsl::kv_table.filter(kv_table::key.eq(key));
  83. let _ = diesel::delete(sql)
  84. .execute(&*conn)
  85. .map_err(|e| format!("KV remove error: {:?}", e))?;
  86. Ok(())
  87. }
  88. pub fn init(root: &str) -> Result<(), String> {
  89. if !Path::new(root).exists() {
  90. return Err(format!("Init KVStore failed. {} not exists", root));
  91. }
  92. let pool_config = PoolConfig::default();
  93. let database = Database::new(root, DB_NAME, pool_config).unwrap();
  94. let conn = database.get_connection().unwrap();
  95. SqliteConnection::execute(&*conn, KV_SQL).unwrap();
  96. let mut store = KV_HOLDER
  97. .write()
  98. .map_err(|e| format!("KVStore write failed: {:?}", e))?;
  99. store.database = Some(database);
  100. Ok(())
  101. }
  102. pub fn get_bool(key: &str) -> bool {
  103. match KV::get(key) {
  104. Ok(item) => item.bool_value.unwrap_or(false),
  105. Err(_) => false,
  106. }
  107. }
  108. impl_set_func!(set_str, str_value, String);
  109. impl_set_func!(set_bool, bool_value, bool);
  110. impl_set_func!(set_int, int_value, i64);
  111. impl_set_func!(set_float, float_value, f64);
  112. impl_get_func!(get_str,str_value=>String);
  113. impl_get_func!(get_int,int_value=>i64);
  114. impl_get_func!(get_float,float_value=>f64);
  115. }
  116. fn read_cache(key: &str) -> Option<KeyValue> {
  117. match KV_HOLDER.read() {
  118. Ok(guard) => guard.cache.get(key).cloned(),
  119. Err(e) => {
  120. log::error!("Require read lock failed: {:?}", e);
  121. None
  122. }
  123. }
  124. }
  125. fn update_cache(value: KeyValue) {
  126. match KV_HOLDER.write() {
  127. Ok(mut guard) => {
  128. guard.cache.insert(value.key.clone(), value);
  129. }
  130. Err(e) => log::error!("Require write lock failed: {:?}", e),
  131. };
  132. }
  133. fn get_connection() -> Result<DBConnection, String> {
  134. match KV_HOLDER.read() {
  135. Ok(store) => {
  136. let conn = store
  137. .database
  138. .as_ref()
  139. .expect("KVStore is not init")
  140. .get_connection()
  141. .map_err(|e| format!("KVStore error: {:?}", e))?;
  142. Ok(conn)
  143. }
  144. Err(e) => {
  145. let msg = format!("KVStore get connection failed: {:?}", e);
  146. log::error!("{:?}", msg);
  147. Err(msg)
  148. }
  149. }
  150. }
  151. #[derive(Clone, Debug, Default, Queryable, Identifiable, Insertable, AsChangeset)]
  152. #[table_name = "kv_table"]
  153. #[primary_key(key)]
  154. pub struct KeyValue {
  155. pub key: String,
  156. pub str_value: Option<String>,
  157. pub int_value: Option<i64>,
  158. pub float_value: Option<f64>,
  159. pub bool_value: Option<bool>,
  160. }
  161. impl KeyValue {
  162. pub fn new(key: &str) -> Self {
  163. KeyValue {
  164. key: key.to_string(),
  165. ..Default::default()
  166. }
  167. }
  168. }
  169. #[cfg(test)]
  170. mod tests {
  171. use crate::kv::KV;
  172. #[test]
  173. fn kv_store_test() {
  174. let dir = "./temp/";
  175. if !std::path::Path::new(dir).exists() {
  176. std::fs::create_dir_all(dir).unwrap();
  177. }
  178. KV::init(dir).unwrap();
  179. KV::set_str("1", "hello".to_string());
  180. assert_eq!(KV::get_str("1").unwrap(), "hello");
  181. assert_eq!(KV::get_str("2"), None);
  182. KV::set_bool("1", true);
  183. assert!(KV::get_bool("1"));
  184. assert!(!KV::get_bool("2"));
  185. }
  186. }