68 lines
1.6 KiB
Rust
Raw Normal View History

2021-09-23 15:49:10 +08:00
use crate::entities::doc::Revision;
2021-09-23 13:15:35 +08:00
use diesel::sql_types::Integer;
use flowy_database::schema::op_table;
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
#[table_name = "op_table"]
2021-09-23 15:49:10 +08:00
#[primary_key(rev_id)]
2021-09-23 13:15:35 +08:00
pub(crate) struct OpTable {
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,
pub(crate) state: OpState,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, FromSqlRow, AsExpression)]
#[repr(i32)]
#[sql_type = "Integer"]
pub enum OpState {
Local = 0,
Sending = 1,
Acked = 2,
}
impl std::default::Default for OpState {
fn default() -> Self { OpState::Local }
}
impl std::convert::From<i32> for OpState {
fn from(value: i32) -> Self {
match value {
0 => OpState::Local,
1 => OpState::Sending,
2 => OpState::Acked,
o => {
log::error!("Unsupported view type {}, fallback to ViewType::Docs", o);
OpState::Local
},
}
}
}
impl OpState {
pub fn value(&self) -> i32 { *self as i32 }
}
impl_sql_integer_expression!(OpState);
#[derive(AsChangeset, Identifiable, Default, Debug)]
#[table_name = "op_table"]
2021-09-23 15:49:10 +08:00
#[primary_key(rev_id)]
2021-09-23 13:15:35 +08:00
pub(crate) struct OpChangeset {
2021-09-23 15:49:10 +08:00
pub(crate) rev_id: i64,
2021-09-23 13:15:35 +08:00
pub(crate) state: Option<OpState>,
}
2021-09-23 15:49:10 +08:00
impl std::convert::Into<OpTable> for Revision {
fn into(self) -> OpTable {
OpTable {
base_rev_id: self.base_rev_id,
rev_id: self.rev_id,
data: self.delta,
md5: self.md5,
state: OpState::Local,
}
}
}