revision.rs 4.3 KB

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