mod.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. pub mod version_1;
  2. mod version_2;
  3. use std::sync::Arc;
  4. pub use version_1::{app_sql::*, trash_sql::*, v1_impl::V1Transaction, view_sql::*, workspace_sql::*};
  5. use crate::module::WorkspaceDatabase;
  6. use flowy_core_data_model::entities::{
  7. app::App,
  8. prelude::RepeatedTrash,
  9. trash::Trash,
  10. view::View,
  11. workspace::Workspace,
  12. };
  13. use flowy_error::{FlowyError, FlowyResult};
  14. pub trait FlowyCorePersistenceTransaction {
  15. fn create_workspace(&self, user_id: &str, workspace: Workspace) -> FlowyResult<()>;
  16. fn read_workspaces(&self, user_id: &str, workspace_id: Option<String>) -> FlowyResult<Vec<Workspace>>;
  17. fn update_workspace(&self, changeset: WorkspaceChangeset) -> FlowyResult<()>;
  18. fn delete_workspace(&self, workspace_id: &str) -> FlowyResult<()>;
  19. fn create_app(&self, app: App) -> FlowyResult<()>;
  20. fn update_app(&self, changeset: AppChangeset) -> FlowyResult<()>;
  21. fn read_app(&self, app_id: &str) -> FlowyResult<App>;
  22. fn read_workspace_apps(&self, workspace_id: &str) -> FlowyResult<Vec<App>>;
  23. fn delete_app(&self, app_id: &str) -> FlowyResult<App>;
  24. fn create_view(&self, view: View) -> FlowyResult<()>;
  25. fn read_view(&self, view_id: &str) -> FlowyResult<View>;
  26. fn read_views(&self, belong_to_id: &str) -> FlowyResult<Vec<View>>;
  27. fn update_view(&self, changeset: ViewChangeset) -> FlowyResult<()>;
  28. fn delete_view(&self, view_id: &str) -> FlowyResult<()>;
  29. fn create_trash(&self, trashes: Vec<Trash>) -> FlowyResult<()>;
  30. fn read_all_trash(&self) -> FlowyResult<RepeatedTrash>;
  31. fn delete_all_trash(&self) -> FlowyResult<()>;
  32. fn read_trash(&self, trash_id: &str) -> FlowyResult<Trash>;
  33. fn delete_trash(&self, trash_ids: Vec<String>) -> FlowyResult<()>;
  34. }
  35. pub struct FlowyCorePersistence {
  36. database: Arc<dyn WorkspaceDatabase>,
  37. }
  38. impl FlowyCorePersistence {
  39. pub fn new(database: Arc<dyn WorkspaceDatabase>) -> Self { Self { database } }
  40. pub fn begin_transaction<F, O>(&self, f: F) -> FlowyResult<O>
  41. where
  42. F: for<'a> FnOnce(Box<dyn FlowyCorePersistenceTransaction + 'a>) -> FlowyResult<O>,
  43. {
  44. //[[immediate_transaction]]
  45. // https://sqlite.org/lang_transaction.html
  46. // IMMEDIATE cause the database connection to start a new write immediately,
  47. // without waiting for a write statement. The BEGIN IMMEDIATE might fail
  48. // with SQLITE_BUSY if another write transaction is already active on another
  49. // database connection.
  50. //
  51. // EXCLUSIVE is similar to IMMEDIATE in that a write transaction is started
  52. // immediately. EXCLUSIVE and IMMEDIATE are the same in WAL mode, but in
  53. // other journaling modes, EXCLUSIVE prevents other database connections from
  54. // reading the database while the transaction is underway.
  55. let conn = self.database.db_connection()?;
  56. conn.immediate_transaction::<_, FlowyError, _>(|| f(Box::new(V1Transaction(&conn))))
  57. }
  58. }