record.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. use blake3;
  2. use otils::Max;
  3. use std::cmp::Ordering;
  4. #[derive(Clone, Debug, PartialEq, PartialOrd)]
  5. pub enum RecordType {
  6. User,
  7. Fetch,
  8. Send,
  9. Dummy,
  10. }
  11. #[derive(Debug)]
  12. pub struct Record {
  13. pub uid: i64,
  14. pub idx: u32,
  15. pub map: u8,
  16. pub rec_type: RecordType,
  17. pub mark: u16,
  18. pub last_fetch: u32,
  19. pub last_send: u32,
  20. pub data: u64,
  21. pub _dum: [u64; 12],
  22. }
  23. impl Record {
  24. pub fn new(uid: i64, type_rec: RecordType, data: u64, map: u8, idx: u32) -> Self {
  25. Record {
  26. uid,
  27. idx,
  28. map,
  29. rec_type: type_rec,
  30. mark: 0,
  31. last_fetch: 0,
  32. last_send: 0,
  33. data,
  34. _dum: [0; 12],
  35. }
  36. }
  37. pub fn send(uid: i64, message: u64) -> Self {
  38. Record::new(uid, RecordType::Send, message, 0, 0)
  39. }
  40. pub fn fetch(uid: i64, volume: u64) -> Self {
  41. Record::new(uid, RecordType::Fetch, volume, 0, 0)
  42. }
  43. pub fn is_user_store(&self) -> bool {
  44. self.rec_type == RecordType::User
  45. }
  46. pub fn is_fetch(&self) -> bool {
  47. self.rec_type == RecordType::Fetch
  48. }
  49. pub fn is_send(&self) -> bool {
  50. self.rec_type == RecordType::Send
  51. }
  52. }
  53. pub struct IndexRecord(pub Record);
  54. impl IndexRecord {
  55. pub fn new(uid: i64, rec_type: RecordType) -> Self {
  56. IndexRecord(Record::new(uid, rec_type, 0, 0, 0))
  57. }
  58. pub fn dummy_fetches(&self) -> Vec<Self> {
  59. (0..self.0.data)
  60. .map(|_| IndexRecord::new(self.0.uid, RecordType::Fetch))
  61. .collect()
  62. }
  63. pub fn get_idx(&self, idx: u32) -> u32 {
  64. let mut hasher = blake3::Hasher::new();
  65. hasher.update(&self.0.uid.to_ne_bytes());
  66. hasher.update(&idx.to_ne_bytes());
  67. let hash = hasher.finalize();
  68. u32::from_ne_bytes(<[u8; 4]>::try_from(&hash.as_bytes()[0..4]).unwrap())
  69. }
  70. pub fn is_request(&self) -> bool {
  71. self.0.rec_type != RecordType::User
  72. }
  73. pub fn is_updated_user_store(&self) -> bool {
  74. self.0.mark == 1 && self.0.uid != i64::MAX
  75. }
  76. pub fn set_user_store(&mut self) {
  77. self.0.rec_type = RecordType::User;
  78. }
  79. }
  80. impl PartialEq for IndexRecord {
  81. fn eq(&self, other: &Self) -> bool {
  82. self.0.uid == other.0.uid && self.0.rec_type == other.0.rec_type
  83. }
  84. }
  85. impl PartialOrd for IndexRecord {
  86. fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
  87. let uid_ord = self.0.uid.partial_cmp(&other.0.uid);
  88. let type_ord = self.0.rec_type.partial_cmp(&other.0.rec_type);
  89. match uid_ord {
  90. Some(Ordering::Equal) => type_ord,
  91. x => x,
  92. }
  93. }
  94. }
  95. impl Max for IndexRecord {
  96. fn maximum() -> Self {
  97. IndexRecord(Record::new(i64::MAX, RecordType::Dummy, 0, 0, 0))
  98. }
  99. }
  100. pub struct SubmapRecord(pub Record);
  101. impl SubmapRecord {
  102. pub fn dummy_send(num_requests: usize, map: u8) -> Vec<Self> {
  103. (0..num_requests)
  104. .map(|_| SubmapRecord(Record::new(0, RecordType::Dummy, 0, map, u32::MAX)))
  105. .collect()
  106. }
  107. pub fn dummy_fetch(num_requests: usize, map: u8) -> Vec<Self> {
  108. (0..num_requests)
  109. .map(|_| SubmapRecord(Record::new(0, RecordType::Fetch, 0, map, u32::MAX)))
  110. .collect()
  111. }
  112. }
  113. impl PartialEq for SubmapRecord {
  114. fn eq(&self, other: &Self) -> bool {
  115. self.0.map == other.0.map && self.0.rec_type == other.0.rec_type
  116. }
  117. }
  118. impl PartialOrd for SubmapRecord {
  119. fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
  120. let map_ord = self.0.map.partial_cmp(&other.0.map);
  121. let idx_ord = self.0.idx.partial_cmp(&other.0.idx);
  122. match map_ord {
  123. Some(Ordering::Equal) => idx_ord,
  124. x => x,
  125. }
  126. }
  127. }
  128. impl Max for SubmapRecord {
  129. fn maximum() -> Self {
  130. SubmapRecord(Record::new(0, RecordType::Dummy, 0, u8::MAX, 0))
  131. }
  132. }