revision.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use crate::util::md5;
  2. use bytes::Bytes;
  3. use serde::{Deserialize, Serialize};
  4. use std::{convert::TryFrom, fmt::Formatter, ops::RangeInclusive};
  5. #[derive(PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
  6. pub struct Revision {
  7. pub base_rev_id: i64,
  8. pub rev_id: i64,
  9. pub bytes: Vec<u8>,
  10. pub md5: String,
  11. pub object_id: String,
  12. }
  13. impl std::convert::From<Vec<u8>> for Revision {
  14. fn from(data: Vec<u8>) -> Self {
  15. let bytes = Bytes::from(data);
  16. Revision::try_from(bytes).unwrap()
  17. }
  18. }
  19. impl std::convert::TryFrom<Bytes> for Revision {
  20. type Error = serde_json::Error;
  21. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  22. serde_json::from_slice(&bytes)
  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. Ok(())
  62. }
  63. }
  64. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  65. pub struct RevisionRange {
  66. pub start: i64,
  67. pub end: i64,
  68. }
  69. impl std::fmt::Display for RevisionRange {
  70. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  71. f.write_fmt(format_args!("[{},{}]", self.start, self.end))
  72. }
  73. }
  74. impl RevisionRange {
  75. pub fn len(&self) -> u64 {
  76. debug_assert!(self.end >= self.start);
  77. if self.end >= self.start {
  78. (self.end - self.start + 1) as u64
  79. } else {
  80. 0
  81. }
  82. }
  83. pub fn is_empty(&self) -> bool {
  84. self.end == self.start
  85. }
  86. pub fn iter(&self) -> RangeInclusive<i64> {
  87. // debug_assert!(self.start != self.end);
  88. RangeInclusive::new(self.start, self.end)
  89. }
  90. pub fn to_rev_ids(&self) -> Vec<i64> {
  91. self.iter().collect::<Vec<_>>()
  92. }
  93. }