revision.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. use crate::services::util::md5;
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use flowy_ot::core::Delta;
  4. use std::fmt::Formatter;
  5. #[derive(Debug, ProtoBuf_Enum, Clone, Eq, PartialEq)]
  6. pub enum RevType {
  7. Local = 0,
  8. Remote = 1,
  9. }
  10. impl RevType {
  11. pub fn is_local(&self) -> bool { self == &RevType::Local }
  12. }
  13. impl std::default::Default for RevType {
  14. fn default() -> Self { RevType::Local }
  15. }
  16. // [[i64 to bytes]]
  17. // use byteorder::{BigEndian, ReadBytesExt};
  18. // use std::{io::Cursor};
  19. // impl std::convert::TryFrom<Bytes> for RevId {
  20. // type Error = DocError;
  21. //
  22. // fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  23. // // let mut wtr = vec![];
  24. // // let _ = wtr.write_i64::<BigEndian>(revision.rev_id);
  25. //
  26. // let mut rdr = Cursor::new(bytes);
  27. // match rdr.read_i64::<BigEndian>() {
  28. // Ok(rev_id) => Ok(RevId(rev_id)),
  29. // Err(e) => Err(DocError::internal().context(e)),
  30. // }
  31. // }
  32. // }
  33. #[derive(Clone, Debug, ProtoBuf, Default)]
  34. pub struct RevId {
  35. #[pb(index = 1)]
  36. pub value: i64,
  37. }
  38. impl AsRef<i64> for RevId {
  39. fn as_ref(&self) -> &i64 { &self.value }
  40. }
  41. impl std::convert::Into<i64> for RevId {
  42. fn into(self) -> i64 { self.value }
  43. }
  44. impl std::convert::From<i64> for RevId {
  45. fn from(value: i64) -> Self { RevId { value } }
  46. }
  47. impl std::fmt::Display for RevId {
  48. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!("{}", self.value)) }
  49. }
  50. #[derive(PartialEq, Eq, Clone, Default, ProtoBuf)]
  51. pub struct Revision {
  52. #[pb(index = 1)]
  53. pub base_rev_id: i64,
  54. #[pb(index = 2)]
  55. pub rev_id: i64,
  56. #[pb(index = 3)]
  57. pub delta_data: Vec<u8>,
  58. #[pb(index = 4)]
  59. pub md5: String,
  60. #[pb(index = 5)]
  61. pub doc_id: String,
  62. #[pb(index = 6)]
  63. pub ty: RevType,
  64. }
  65. impl std::fmt::Debug for Revision {
  66. fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
  67. let _ = f.write_fmt(format_args!("doc_id {}, ", self.doc_id))?;
  68. let _ = f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?;
  69. match Delta::from_bytes(&self.delta_data) {
  70. Ok(delta) => {
  71. let _ = f.write_fmt(format_args!("delta {:?}", delta.to_json()))?;
  72. },
  73. Err(e) => {
  74. let _ = f.write_fmt(format_args!("delta {:?}", e))?;
  75. },
  76. }
  77. Ok(())
  78. }
  79. }
  80. impl Revision {
  81. pub fn new<T1: Into<i64>, T2: Into<i64>>(
  82. base_rev_id: T1,
  83. rev_id: T2,
  84. delta_data: Vec<u8>,
  85. doc_id: &str,
  86. ty: RevType,
  87. ) -> Revision {
  88. let md5 = md5(&delta_data);
  89. let doc_id = doc_id.to_owned();
  90. Self {
  91. base_rev_id: base_rev_id.into(),
  92. rev_id: rev_id.into(),
  93. delta_data,
  94. md5,
  95. doc_id,
  96. ty,
  97. }
  98. }
  99. }
  100. #[derive(Debug, Clone, Default, ProtoBuf)]
  101. pub struct RevisionRange {
  102. #[pb(index = 1)]
  103. pub doc_id: String,
  104. #[pb(index = 2)]
  105. pub from_rev_id: i64,
  106. #[pb(index = 3)]
  107. pub to_rev_id: i64,
  108. }