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