revision.rs 4.6 KB

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