From ea214d8ac0c39ad890b5b81aa9a6df0239eb2b40 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sat, 29 Aug 2026 23:10:57 +0300 Subject: [PATCH] Change connection type from Mutex to RwLock --- src/data/repositories.rs | 53 ++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/src/data/repositories.rs b/src/data/repositories.rs index 7bb6cb0..59ed59e 100644 --- a/src/data/repositories.rs +++ b/src/data/repositories.rs @@ -43,6 +43,7 @@ where T: EntityData, { pub(crate) fn handle_save(&self, old: EntityHandle, new: &mut EntityHandle, data: &T) { + return; if old.real() && new.real() { return; } @@ -57,6 +58,7 @@ where } } pub(crate) fn handle_delete(&self, handle: &EntityHandle, data: &T) { + return; let affected = self.get_affected_queries(data); for q in affected { let mut query = self.queries[q].lock(); @@ -189,10 +191,10 @@ pub struct DataRepositoryInner> cache: Vec, temporal: Vec, next_temporal: u32, - connection: Arc>, + connection: Arc>, data_layer: D, query_manger: QueryManger, - version: u16 + version: u16, } impl> Deref for DataRepository { @@ -203,7 +205,7 @@ impl> Deref for DataRepository< } } -impl> DataRepository { +impl> DataRepositoryInner { pub fn query_par( &self, rule: Box bool + Send + Sync>, @@ -211,9 +213,7 @@ impl> DataRepository { where T: Send + Sync, { - let inner = self.inner.read(); - - let mut cache: Vec> = inner + let mut cache: Vec> = self .cache .par_iter() .enumerate() @@ -222,8 +222,7 @@ impl> DataRepository { .collect(); cache.extend( - inner - .temporal + self.temporal .iter() .enumerate() .filter(|(_, entity)| rule(entity)) @@ -246,9 +245,7 @@ impl> DataRepository { where T: Send + Sync, { - let inner = self.inner.read(); - - let mut cache: Vec> = inner + let mut cache: Vec> = self .cache .iter() .enumerate() @@ -257,8 +254,7 @@ impl> DataRepository { .collect(); cache.extend( - inner - .temporal + self.temporal .iter() .enumerate() .filter(|(_, entity)| rule(entity)) @@ -324,12 +320,12 @@ impl> Repository for DataRepositoryIn if entity.real() { let data = self.get_ref(entity).unwrap(); self.data_layer - .write_entity(data, &self.connection.lock())?; + .write_entity(data, &self.connection.read())?; } else { let index = fast_position_of(self.temporal.as_slice(), entity).unwrap(); let mut data = self.temporal.remove(index); self.data_layer - .add_entity(&mut data, &self.connection.lock())?; + .add_entity(&mut data, &self.connection.read())?; entity.set_real(true); entity.id = data.id(); entity.set_index(self.cache.len()); @@ -344,7 +340,7 @@ impl> Repository for DataRepositoryIn fn delete(&mut self, mut handle: EntityHandle) -> MaybeError { let vec = if handle.real() { self.data_layer - .delete_entity(handle.id, &self.connection.lock())?; + .delete_entity(handle.id, &self.connection.read())?; &mut self.cache } else { &mut self.temporal @@ -361,7 +357,7 @@ impl> Repository for DataRepositoryIn fn init(&mut self) -> MaybeError { self.data_layer - .load_all(&mut self.cache, &self.connection.lock())?; + .load_all(&mut self.cache, &self.connection.read())?; self.version += 1; Ok(()) } @@ -388,7 +384,7 @@ impl + DbLayer> BatchedRepository } }); - let mut values = Vec::with_capacity(temp.len()); + let mut temp_values = Vec::with_capacity(temp.len()); let mut cell = 0; let len = temp.len(); self.temporal.retain(|x| { @@ -399,32 +395,31 @@ impl + DbLayer> BatchedRepository let remove = x.id() == find; if remove { cell += 1; - values.push(x.clone()); + temp_values.push(x.clone()); } !remove }); self.data_layer - .add_many_entity(&mut values, &self.connection.lock())?; + .add_many_entity(&mut temp_values, &self.connection.read())?; let start = self.cache.len(); - for (i, (handle, data)) in temp.into_iter().zip(&values).enumerate() { + for (i, (handle, data)) in temp.into_iter().zip(&mut temp_values).enumerate() { let old = handle.clone(); - let h = handle; - h.id = self.cache[start + i].id(); - h.set_index(start + i); - h.set_real(true); - self.query_manger.handle_save(old, h, data); + handle.id = data.id(); + handle.set_index(start + i); + handle.set_real(true); + self.query_manger.handle_save(old, handle, data); } - self.cache.extend(values); + self.cache.extend(temp_values); for handle in cached.into_iter() { let h = handle; let idx = fast_position_of(self.cache.as_slice(), h).unwrap(); self.data_layer - .write_entity(&self.cache[idx], &self.connection.lock())?; + .write_entity(&self.cache[idx], &self.connection.read())?; } Ok(()) @@ -447,7 +442,7 @@ impl + DbLayer> BatchedRepository !remove }); self.data_layer - .delete_many(cached.as_slice(), &self.connection.lock())?; + .delete_many(cached.as_slice(), &self.connection.read())?; let len = cached.len(); cell = 0; self.cache.retain(|x| {