database.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use crate::entities::{SignInResponse, SignUpResponse, UpdateUserProfileParams, UserProfilePB};
  2. use flowy_database::ConnectionPool;
  3. use flowy_database::{schema::user_table, DBConnection, Database};
  4. use flowy_error::{ErrorCode, FlowyError};
  5. use lazy_static::lazy_static;
  6. use parking_lot::RwLock;
  7. use std::path::PathBuf;
  8. use std::{collections::HashMap, sync::Arc, time::Duration};
  9. pub struct UserDB {
  10. db_dir: String,
  11. }
  12. impl UserDB {
  13. pub fn new(db_dir: &str) -> Self {
  14. Self {
  15. db_dir: db_dir.to_owned(),
  16. }
  17. }
  18. fn open_user_db_if_need(&self, user_id: &str) -> Result<Arc<ConnectionPool>, FlowyError> {
  19. if user_id.is_empty() {
  20. return Err(ErrorCode::UserIdIsEmpty.into());
  21. }
  22. if let Some(database) = DB_MAP.read().get(user_id) {
  23. return Ok(database.get_pool());
  24. }
  25. let mut write_guard = DB_MAP.write();
  26. // The Write guard acquire exclusive access that will guarantee the user db only initialize once.
  27. match write_guard.get(user_id) {
  28. None => {}
  29. Some(database) => return Ok(database.get_pool()),
  30. }
  31. let mut dir = PathBuf::new();
  32. dir.push(&self.db_dir);
  33. dir.push(user_id);
  34. let dir = dir.to_str().unwrap().to_owned();
  35. tracing::trace!("open user db {} at path: {}", user_id, dir);
  36. let db = flowy_database::init(&dir).map_err(|e| {
  37. log::error!("open user: {} db failed, {:?}", user_id, e);
  38. FlowyError::internal().context(e)
  39. })?;
  40. let pool = db.get_pool();
  41. write_guard.insert(user_id.to_owned(), db);
  42. drop(write_guard);
  43. Ok(pool)
  44. }
  45. pub(crate) fn close_user_db(&self, user_id: &str) -> Result<(), FlowyError> {
  46. match DB_MAP.try_write_for(Duration::from_millis(300)) {
  47. None => Err(FlowyError::internal().context("Acquire write lock to close user db failed")),
  48. Some(mut write_guard) => {
  49. write_guard.remove(user_id);
  50. Ok(())
  51. }
  52. }
  53. }
  54. pub(crate) fn get_connection(&self, user_id: &str) -> Result<DBConnection, FlowyError> {
  55. let conn = self.get_pool(user_id)?.get()?;
  56. Ok(conn)
  57. }
  58. pub(crate) fn get_pool(&self, user_id: &str) -> Result<Arc<ConnectionPool>, FlowyError> {
  59. let pool = self.open_user_db_if_need(user_id)?;
  60. Ok(pool)
  61. }
  62. }
  63. lazy_static! {
  64. static ref DB_MAP: RwLock<HashMap<String, Database>> = RwLock::new(HashMap::new());
  65. }
  66. #[derive(Clone, Default, Queryable, Identifiable, Insertable)]
  67. #[table_name = "user_table"]
  68. pub struct UserTable {
  69. pub(crate) id: String,
  70. pub(crate) name: String,
  71. pub(crate) token: String,
  72. pub(crate) email: String,
  73. pub(crate) workspace: String, // deprecated
  74. pub(crate) icon_url: String,
  75. }
  76. impl UserTable {
  77. pub fn new(id: String, name: String, email: String, token: String) -> Self {
  78. Self {
  79. id,
  80. name,
  81. email,
  82. token,
  83. icon_url: "".to_owned(),
  84. workspace: "".to_owned(),
  85. }
  86. }
  87. pub fn set_workspace(mut self, workspace: String) -> Self {
  88. self.workspace = workspace;
  89. self
  90. }
  91. }
  92. impl std::convert::From<SignUpResponse> for UserTable {
  93. fn from(resp: SignUpResponse) -> Self {
  94. UserTable::new(resp.user_id, resp.name, resp.email, resp.token)
  95. }
  96. }
  97. impl std::convert::From<SignInResponse> for UserTable {
  98. fn from(resp: SignInResponse) -> Self {
  99. UserTable::new(resp.user_id, resp.name, resp.email, resp.token)
  100. }
  101. }
  102. impl std::convert::From<UserTable> for UserProfilePB {
  103. fn from(table: UserTable) -> Self {
  104. UserProfilePB {
  105. id: table.id,
  106. email: table.email,
  107. name: table.name,
  108. token: table.token,
  109. icon_url: table.icon_url,
  110. }
  111. }
  112. }
  113. #[derive(AsChangeset, Identifiable, Default, Debug)]
  114. #[table_name = "user_table"]
  115. pub struct UserTableChangeset {
  116. pub id: String,
  117. pub workspace: Option<String>, // deprecated
  118. pub name: Option<String>,
  119. pub email: Option<String>,
  120. pub icon_url: Option<String>,
  121. }
  122. impl UserTableChangeset {
  123. pub fn new(params: UpdateUserProfileParams) -> Self {
  124. UserTableChangeset {
  125. id: params.id,
  126. workspace: None,
  127. name: params.name,
  128. email: params.email,
  129. icon_url: params.icon_url,
  130. }
  131. }
  132. }