versatile_data/
operation.rs1use std::num::NonZeroU32;
2
3use futures::FutureExt;
4use hashbrown::HashMap;
5use idx_binary::AvltrieeUpdate;
6use uuid::Uuid;
7
8use crate::{Data, FieldName};
9
10#[derive(Clone, Copy, PartialEq, Debug)]
11pub enum Activity {
12 Inactive = 0,
13 Active = 1,
14}
15impl Default for Activity {
16 fn default() -> Self {
17 Self::Active
18 }
19}
20
21#[derive(Debug, Clone)]
22pub enum Term {
23 Default,
24 Overwrite(u64),
25}
26impl Default for Term {
27 fn default() -> Self {
28 Self::Default
29 }
30}
31
32pub fn create_uuid() -> u128 {
33 Uuid::new_v4().as_u128()
34}
35
36impl Data {
37 pub async fn insert(
39 &mut self,
40 activity: Activity,
41 term_begin: Term,
42 term_end: Term,
43 fields: HashMap<FieldName, Vec<u8>>,
44 ) -> NonZeroU32 {
45 let row = self.serial.next_row();
46 self.update(row, activity, term_begin, term_end, fields)
47 .await;
48 row
49 }
50
51 pub async fn update(
53 &mut self,
54 row: NonZeroU32,
55 activity: Activity,
56 term_begin: Term,
57 term_end: Term,
58 fields: HashMap<FieldName, Vec<u8>>,
59 ) {
60 for (key, _) in &fields {
61 if !self.fields.contains_key(key) {
62 self.create_field(key);
63 }
64 }
65 futures::future::join_all([
66 async {
67 futures::future::join_all(self.fields.iter_mut().filter_map(|(name, field)| {
68 fields
69 .get(&name.clone())
70 .map(|v| async { field.update(row, v) })
71 }))
72 .await;
73 }
74 .boxed_local(),
75 async {
76 if let Some(ref mut uuid) = self.uuid {
77 if uuid.node(row).is_none() {
78 uuid.update(row, &Uuid::new_v4().as_u128());
79 }
80 }
81 }
82 .boxed_local(),
83 async {
84 if let Some(ref mut f) = self.last_updated {
85 f.update(row, &Self::now());
86 }
87 }
88 .boxed_local(),
89 async {
90 if let Some(ref mut f) = self.activity {
91 f.update(row, &(activity as u8));
92 }
93 }
94 .boxed_local(),
95 async {
96 if let Some(ref mut f) = self.term_begin {
97 f.update(
98 row,
99 &if let Term::Overwrite(term) = term_begin {
100 term
101 } else {
102 Self::now()
103 },
104 );
105 }
106 }
107 .boxed_local(),
108 async {
109 if let Some(ref mut f) = self.term_end {
110 f.update(
111 row,
112 &if let Term::Overwrite(term) = term_end {
113 term
114 } else {
115 0
116 },
117 );
118 }
119 }
120 .boxed_local(),
121 ])
122 .await;
123 }
124
125 pub async fn delete(&mut self, row: NonZeroU32) {
127 futures::future::join(
128 futures::future::join(async { self.serial.delete(row) }, async {
129 futures::future::join_all(self.fields.iter_mut().map(|(_name, v)| async {
130 v.delete(row);
131 }))
132 .await
133 }),
134 async {
135 let mut futs = vec![];
136 if let Some(ref mut f) = self.uuid {
137 futs.push(
138 async {
139 f.delete(row);
140 }
141 .boxed_local(),
142 );
143 }
144 if let Some(ref mut f) = self.activity {
145 futs.push(
146 async {
147 f.delete(row);
148 }
149 .boxed_local(),
150 );
151 }
152 if let Some(ref mut f) = self.term_begin {
153 futs.push(
154 async {
155 f.delete(row);
156 }
157 .boxed_local(),
158 );
159 }
160 if let Some(ref mut f) = self.term_end {
161 futs.push(
162 async {
163 f.delete(row);
164 }
165 .boxed_local(),
166 );
167 }
168 if let Some(ref mut f) = self.last_updated {
169 futs.push(
170 async {
171 f.delete(row);
172 }
173 .boxed_local(),
174 );
175 }
176 futures::future::join_all(futs).await;
177 },
178 )
179 .await;
180 }
181}