revision.rs 5.3 KB

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