revision.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. use crate::core::document::default::initial_delta;
  2. use bytes::Bytes;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use lib_ot::rich_text::RichTextDelta;
  5. use std::{convert::TryFrom, fmt::Formatter, ops::RangeInclusive};
  6. #[derive(PartialEq, Eq, Clone, Default, ProtoBuf)]
  7. pub struct Revision {
  8. #[pb(index = 1)]
  9. pub base_rev_id: i64,
  10. #[pb(index = 2)]
  11. pub rev_id: i64,
  12. #[pb(index = 3)]
  13. pub delta_data: Vec<u8>,
  14. #[pb(index = 4)]
  15. pub md5: String,
  16. #[pb(index = 5)]
  17. pub doc_id: String,
  18. #[pb(index = 6)]
  19. pub ty: RevType,
  20. #[pb(index = 7)]
  21. pub user_id: String,
  22. }
  23. impl std::convert::From<Vec<u8>> for Revision {
  24. fn from(data: Vec<u8>) -> Self {
  25. let bytes = Bytes::from(data);
  26. Revision::try_from(bytes).unwrap()
  27. }
  28. }
  29. impl Revision {
  30. pub fn is_empty(&self) -> bool { self.base_rev_id == self.rev_id }
  31. pub fn pair_rev_id(&self) -> (i64, i64) { (self.base_rev_id, self.rev_id) }
  32. #[allow(dead_code)]
  33. pub fn is_initial(&self) -> bool { self.rev_id == 0 }
  34. pub fn initial_revision(user_id: &str, doc_id: &str, ty: RevType) -> Self {
  35. let delta_data = initial_delta().to_bytes();
  36. let md5 = format!("{:x}", md5::compute(&delta_data));
  37. Revision::new(doc_id, 0, 0, delta_data, ty, user_id, md5)
  38. }
  39. pub fn new(
  40. doc_id: &str,
  41. base_rev_id: i64,
  42. rev_id: i64,
  43. delta_data: Bytes,
  44. ty: RevType,
  45. user_id: &str,
  46. md5: String,
  47. ) -> Revision {
  48. let doc_id = doc_id.to_owned();
  49. let delta_data = delta_data.to_vec();
  50. let base_rev_id = base_rev_id;
  51. let rev_id = rev_id;
  52. let user_id = user_id.to_owned();
  53. if base_rev_id != 0 {
  54. debug_assert!(base_rev_id != rev_id);
  55. }
  56. Self {
  57. base_rev_id,
  58. rev_id,
  59. delta_data,
  60. md5,
  61. doc_id,
  62. ty,
  63. user_id,
  64. }
  65. }
  66. }
  67. impl std::fmt::Debug for Revision {
  68. fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
  69. let _ = f.write_fmt(format_args!("doc_id {}, ", self.doc_id))?;
  70. let _ = f.write_fmt(format_args!("base_rev_id {}, ", self.base_rev_id))?;
  71. let _ = f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?;
  72. match RichTextDelta::from_bytes(&self.delta_data) {
  73. Ok(delta) => {
  74. let _ = f.write_fmt(format_args!("delta {:?}", delta.to_json()))?;
  75. },
  76. Err(e) => {
  77. let _ = f.write_fmt(format_args!("delta {:?}", e))?;
  78. },
  79. }
  80. Ok(())
  81. }
  82. }
  83. #[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
  84. pub struct RepeatedRevision {
  85. #[pb(index = 1)]
  86. pub items: Vec<Revision>,
  87. }
  88. impl std::ops::Deref for RepeatedRevision {
  89. type Target = Vec<Revision>;
  90. fn deref(&self) -> &Self::Target { &self.items }
  91. }
  92. impl std::ops::DerefMut for RepeatedRevision {
  93. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.items }
  94. }
  95. impl RepeatedRevision {
  96. pub fn into_inner(self) -> Vec<Revision> { self.items }
  97. }
  98. #[derive(Clone, Debug, ProtoBuf, Default)]
  99. pub struct RevId {
  100. #[pb(index = 1)]
  101. pub value: i64,
  102. }
  103. impl AsRef<i64> for RevId {
  104. fn as_ref(&self) -> &i64 { &self.value }
  105. }
  106. impl std::convert::From<RevId> for i64 {
  107. fn from(rev_id: RevId) -> Self { rev_id.value }
  108. }
  109. impl std::convert::From<i64> for RevId {
  110. fn from(value: i64) -> Self { RevId { value } }
  111. }
  112. impl std::fmt::Display for RevId {
  113. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!("{}", self.value)) }
  114. }
  115. #[derive(Debug, ProtoBuf_Enum, Clone, Eq, PartialEq)]
  116. pub enum RevType {
  117. Local = 0,
  118. Remote = 1,
  119. }
  120. impl RevType {
  121. pub fn is_local(&self) -> bool { self == &RevType::Local }
  122. }
  123. impl std::default::Default for RevType {
  124. fn default() -> Self { RevType::Local }
  125. }
  126. #[derive(Debug, Clone, Default, ProtoBuf)]
  127. pub struct RevisionRange {
  128. #[pb(index = 1)]
  129. pub doc_id: String,
  130. #[pb(index = 2)]
  131. pub start: i64,
  132. #[pb(index = 3)]
  133. pub end: i64,
  134. }
  135. impl RevisionRange {
  136. pub fn len(&self) -> i64 {
  137. debug_assert!(self.end >= self.start);
  138. if self.end >= self.start {
  139. self.end - self.start + 1
  140. } else {
  141. 0
  142. }
  143. }
  144. pub fn is_empty(&self) -> bool { self.end == self.start }
  145. pub fn iter(&self) -> RangeInclusive<i64> {
  146. debug_assert!(self.start != self.end);
  147. RangeInclusive::new(self.start, self.end)
  148. }
  149. }
  150. #[inline]
  151. pub fn md5<T: AsRef<[u8]>>(data: T) -> String {
  152. let md5 = format!("{:x}", md5::compute(data));
  153. md5
  154. }
  155. #[derive(Debug, Clone, Eq, PartialEq)]
  156. pub enum RevState {
  157. StateLocal = 0,
  158. Ack = 1,
  159. }