revision.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. use crate::services::util::md5;
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use flowy_ot::core::Delta;
  4. #[derive(Debug, ProtoBuf_Enum, Clone, Eq, PartialEq)]
  5. pub enum RevType {
  6. Local = 0,
  7. Remote = 1,
  8. }
  9. impl std::default::Default for RevType {
  10. fn default() -> Self { RevType::Local }
  11. }
  12. #[derive(Clone, Default, ProtoBuf)]
  13. pub struct Revision {
  14. #[pb(index = 1)]
  15. pub base_rev_id: i64,
  16. #[pb(index = 2)]
  17. pub rev_id: i64,
  18. #[pb(index = 3)]
  19. pub delta_data: Vec<u8>,
  20. #[pb(index = 4)]
  21. pub md5: String,
  22. #[pb(index = 5)]
  23. pub doc_id: String,
  24. #[pb(index = 6)]
  25. pub ty: RevType,
  26. }
  27. impl std::fmt::Debug for Revision {
  28. fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
  29. f.write_fmt(format_args!("doc_id {}, ", self.doc_id));
  30. f.write_fmt(format_args!("rev_id {}, ", self.rev_id));
  31. match Delta::from_bytes(&self.delta_data) {
  32. Ok(delta) => {
  33. f.write_fmt(format_args!("delta {:?}", delta.to_json()));
  34. },
  35. Err(e) => {
  36. f.write_fmt(format_args!("delta {:?}", e));
  37. },
  38. }
  39. Ok(())
  40. }
  41. }
  42. impl Revision {
  43. pub fn new(base_rev_id: i64, rev_id: i64, delta_data: Vec<u8>, doc_id: &str, ty: RevType) -> Revision {
  44. let md5 = md5(&delta_data);
  45. let doc_id = doc_id.to_owned();
  46. Self {
  47. base_rev_id,
  48. rev_id,
  49. delta_data,
  50. md5,
  51. doc_id,
  52. ty,
  53. }
  54. }
  55. }
  56. #[derive(Debug, Clone, Default, ProtoBuf)]
  57. pub struct RevisionRange {
  58. #[pb(index = 1)]
  59. pub doc_id: String,
  60. #[pb(index = 2)]
  61. pub from_rev_id: i64,
  62. #[pb(index = 3)]
  63. pub to_rev_id: i64,
  64. }