136 lines
3.5 KiB
Rust
Raw Normal View History

2021-12-13 22:46:35 +08:00
use crate::services::doc::revision::RevisionRecord;
2021-09-23 13:15:35 +08:00
use diesel::sql_types::Integer;
2021-12-11 13:47:16 +08:00
use flowy_collaboration::util::md5;
2021-12-13 22:46:35 +08:00
use flowy_database::schema::rev_table;
2021-12-07 22:32:34 +08:00
use lib_ot::revision::{RevId, RevState, RevType, Revision};
2021-09-23 13:15:35 +08:00
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
2021-09-26 16:39:57 +08:00
#[table_name = "rev_table"]
pub(crate) struct RevTable {
id: i32,
pub(crate) doc_id: String,
2021-09-23 15:49:10 +08:00
pub(crate) base_rev_id: i64,
pub(crate) rev_id: i64,
2021-09-23 13:15:35 +08:00
pub(crate) data: Vec<u8>,
2021-12-08 14:17:40 +08:00
pub(crate) state: RevTableState,
2021-09-26 16:39:57 +08:00
pub(crate) ty: RevTableType,
2021-09-23 13:15:35 +08:00
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, FromSqlRow, AsExpression)]
#[repr(i32)]
#[sql_type = "Integer"]
2021-12-08 14:17:40 +08:00
pub enum RevTableState {
Local = 0,
Acked = 1,
2021-09-23 13:15:35 +08:00
}
2021-12-08 14:17:40 +08:00
impl std::default::Default for RevTableState {
fn default() -> Self { RevTableState::Local }
2021-09-23 13:15:35 +08:00
}
2021-12-08 14:17:40 +08:00
impl std::convert::From<i32> for RevTableState {
2021-09-23 13:15:35 +08:00
fn from(value: i32) -> Self {
match value {
2021-12-08 14:17:40 +08:00
0 => RevTableState::Local,
1 => RevTableState::Acked,
2021-09-23 13:15:35 +08:00
o => {
2021-09-26 16:39:57 +08:00
log::error!("Unsupported rev state {}, fallback to RevState::Local", o);
2021-12-08 14:17:40 +08:00
RevTableState::Local
2021-09-23 13:15:35 +08:00
},
}
}
}
2021-12-07 22:32:34 +08:00
2021-12-08 14:17:40 +08:00
impl RevTableState {
2021-09-23 13:15:35 +08:00
pub fn value(&self) -> i32 { *self as i32 }
}
2021-12-08 14:17:40 +08:00
impl_sql_integer_expression!(RevTableState);
2021-09-26 16:39:57 +08:00
2021-12-08 14:17:40 +08:00
impl std::convert::From<RevTableState> for RevState {
fn from(s: RevTableState) -> Self {
2021-12-07 22:32:34 +08:00
match s {
2021-12-14 15:31:44 +08:00
RevTableState::Local => RevState::StateLocal,
2021-12-08 14:17:40 +08:00
RevTableState::Acked => RevState::Acked,
}
}
}
impl std::convert::From<RevState> for RevTableState {
fn from(s: RevState) -> Self {
match s {
2021-12-14 15:31:44 +08:00
RevState::StateLocal => RevTableState::Local,
2021-12-08 14:17:40 +08:00
RevState::Acked => RevTableState::Acked,
2021-12-07 22:32:34 +08:00
}
}
}
2021-12-13 22:46:35 +08:00
pub(crate) fn mk_revision_record_from_table(user_id: &str, table: RevTable) -> RevisionRecord {
2021-12-09 22:28:11 +08:00
let md5 = md5(&table.data);
2021-12-13 22:46:35 +08:00
let revision = Revision {
2021-12-09 22:28:11 +08:00
base_rev_id: table.base_rev_id,
rev_id: table.rev_id,
delta_data: table.data,
md5,
doc_id: table.doc_id,
ty: table.ty.into(),
user_id: user_id.to_owned(),
2021-12-13 22:46:35 +08:00
};
RevisionRecord {
revision,
state: table.state.into(),
}
}
2021-11-27 19:19:41 +08:00
impl std::convert::From<RevType> for RevTableType {
fn from(ty: RevType) -> Self {
match ty {
RevType::Local => RevTableType::Local,
RevType::Remote => RevTableType::Remote,
}
}
}
impl std::convert::From<RevTableType> for RevType {
fn from(ty: RevTableType) -> Self {
match ty {
RevTableType::Local => RevType::Local,
RevTableType::Remote => RevType::Remote,
}
}
}
2021-09-26 16:39:57 +08:00
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, FromSqlRow, AsExpression)]
#[repr(i32)]
#[sql_type = "Integer"]
pub enum RevTableType {
Local = 0,
Remote = 1,
}
impl std::default::Default for RevTableType {
fn default() -> Self { RevTableType::Local }
}
2021-09-23 13:15:35 +08:00
2021-09-26 16:39:57 +08:00
impl std::convert::From<i32> for RevTableType {
fn from(value: i32) -> Self {
match value {
0 => RevTableType::Local,
1 => RevTableType::Remote,
o => {
log::error!("Unsupported rev type {}, fallback to RevTableType::Local", o);
RevTableType::Local
},
}
}
}
impl RevTableType {
pub fn value(&self) -> i32 { *self as i32 }
}
impl_sql_integer_expression!(RevTableType);
2021-09-23 13:15:35 +08:00
2021-12-18 18:35:45 +08:00
pub struct RevChangeset {
pub(crate) doc_id: String,
2021-10-02 21:35:06 +08:00
pub(crate) rev_id: RevId,
2021-12-08 14:17:40 +08:00
pub(crate) state: RevTableState,
2021-09-26 16:39:57 +08:00
}