105 lines
2.7 KiB
Rust
Raw Normal View History

2021-09-26 16:39:57 +08:00
use crate::entities::doc::{RevType, Revision};
2021-09-23 13:15:35 +08:00
use diesel::sql_types::Integer;
2021-09-26 16:39:57 +08:00
use flowy_database::schema::rev_table;
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"]
#[primary_key(doc_id)]
2021-09-26 16:39:57 +08:00
pub(crate) struct RevTable {
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>,
pub(crate) md5: String,
2021-09-26 16:39:57 +08:00
pub(crate) state: RevState,
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-09-26 16:39:57 +08:00
pub enum RevState {
Local = 0,
Acked = 1,
2021-09-23 13:15:35 +08:00
}
2021-09-26 16:39:57 +08:00
impl std::default::Default for RevState {
fn default() -> Self { RevState::Local }
2021-09-23 13:15:35 +08:00
}
2021-09-26 16:39:57 +08:00
impl std::convert::From<i32> for RevState {
2021-09-23 13:15:35 +08:00
fn from(value: i32) -> Self {
match value {
2021-09-26 16:39:57 +08:00
0 => RevState::Local,
1 => RevState::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);
RevState::Local
2021-09-23 13:15:35 +08:00
},
}
}
}
2021-09-26 16:39:57 +08:00
impl RevState {
2021-09-23 13:15:35 +08:00
pub fn value(&self) -> i32 { *self as i32 }
}
2021-09-26 16:39:57 +08:00
impl_sql_integer_expression!(RevState);
#[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
#[derive(AsChangeset, Identifiable, Default, Debug)]
2021-09-26 16:39:57 +08:00
#[table_name = "rev_table"]
#[primary_key(doc_id)]
2021-09-26 16:39:57 +08:00
pub(crate) struct RevChangeset {
pub(crate) doc_id: String,
2021-09-23 15:49:10 +08:00
pub(crate) rev_id: i64,
2021-09-26 16:39:57 +08:00
pub(crate) state: Option<RevState>,
2021-09-23 13:15:35 +08:00
}
2021-09-23 15:49:10 +08:00
2021-09-26 16:39:57 +08:00
impl std::convert::Into<RevTable> for Revision {
fn into(self) -> RevTable {
RevTable {
doc_id: self.doc_id,
2021-09-23 15:49:10 +08:00
base_rev_id: self.base_rev_id,
rev_id: self.rev_id,
data: self.delta,
md5: self.md5,
2021-09-26 16:39:57 +08:00
state: RevState::Local,
ty: rev_ty_to_rev_state(self.ty),
2021-09-23 15:49:10 +08:00
}
}
}
2021-09-26 16:39:57 +08:00
fn rev_ty_to_rev_state(ty: RevType) -> RevTableType {
match ty {
RevType::Local => RevTableType::Local,
RevType::Remote => RevTableType::Remote,
}
}