Fix data access method and solve import page
This commit is contained in:
1 parent
532640058e
commit
9a7578727b
2 files changed
+35
-41
No files matched your search
+11
-11
@@ -245,7 +245,7 @@ pub struct DataRepositoryInner<T: Clone + EntityData, D: DbLayer<T>> {
|
||||
cache: Vec<T>,
|
||||
temporal: Vec<T>,
|
||||
next_temporal: u32,
|
||||
connection: Arc<RwLock<SqliteConnection>>,
|
||||
connection: Arc<Mutex<SqliteConnection>>,
|
||||
data_layer: D,
|
||||
query_manger: QueryManger<T, Self, D>,
|
||||
version: u16,
|
||||
@@ -253,7 +253,7 @@ pub struct DataRepositoryInner<T: Clone + EntityData, D: DbLayer<T>> {
|
||||
}
|
||||
|
||||
impl<T: Clone + EntityData, D: DbLayer<T> + Default> DataRepository<T, D> {
|
||||
pub fn new(connection: Arc<RwLock<SqliteConnection>>) -> DataRepository<T, D> {
|
||||
pub fn new(connection: Arc<Mutex<SqliteConnection>>) -> DataRepository<T, D> {
|
||||
Self{
|
||||
inner: Arc::new(RwLock::new(DataRepositoryInner{
|
||||
cache: vec![],
|
||||
@@ -428,7 +428,7 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
||||
|
||||
fn create_now(&mut self, mut new: T) -> Result<EntityHandle<T>, NotificationData> {
|
||||
self.data_layer
|
||||
.add_entity(&mut new, &self.connection.read())?;
|
||||
.add_entity(&mut new, &self.connection.lock())?;
|
||||
let index = self.cache.len();
|
||||
let handle = EntityHandle::new(new.id(), index, true);
|
||||
self.cache.push(new);
|
||||
@@ -440,12 +440,12 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
||||
if entity.real() {
|
||||
let data = self.get_ref(entity).unwrap();
|
||||
self.data_layer
|
||||
.write_entity(data, &self.connection.read())?;
|
||||
.write_entity(data, &self.connection.lock())?;
|
||||
} 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.read())?;
|
||||
.add_entity(&mut data, &self.connection.lock())?;
|
||||
entity.set_real(true);
|
||||
entity.id = data.id();
|
||||
entity.set_index(self.cache.len());
|
||||
@@ -461,7 +461,7 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
||||
fn delete(&mut self, mut handle: EntityHandle<T>) -> MaybeError {
|
||||
let vec = if handle.real() {
|
||||
self.data_layer
|
||||
.delete_entity(handle.id, &self.connection.read())?;
|
||||
.delete_entity(handle.id, &self.connection.lock())?;
|
||||
&mut self.cache
|
||||
} else {
|
||||
&mut self.temporal
|
||||
@@ -495,7 +495,7 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
|
||||
|
||||
fn init(&mut self) -> MaybeError {
|
||||
self.data_layer
|
||||
.load_all(&mut self.cache, &self.connection.read())?;
|
||||
.load_all(&mut self.cache, &self.connection.lock())?;
|
||||
self.version += 1;
|
||||
Ok(())
|
||||
}
|
||||
@@ -538,7 +538,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
|
||||
for DataRepositoryInner<T, D>
|
||||
{
|
||||
fn create_now_many(&mut self, entities: &mut [T]) -> Result<Vec<EntityHandle<T>>, NotificationData> {
|
||||
self.data_layer.add_many_entities(entities, &mut self.connection.write())?;
|
||||
self.data_layer.add_many_entities(entities, &mut self.connection.lock())?;
|
||||
let len = self.cache.len();
|
||||
let handles = entities.iter().enumerate().map(|(index, entity)| {EntityHandle::new(entity.id(), index + len, true)}).collect();
|
||||
self.cache.extend(entities.to_vec());
|
||||
@@ -574,7 +574,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
|
||||
});
|
||||
|
||||
self.data_layer
|
||||
.add_many_entities(&mut temp_values, &mut self.connection.write())?;
|
||||
.add_many_entities(&mut temp_values, &mut self.connection.lock())?;
|
||||
|
||||
let start = self.cache.len();
|
||||
|
||||
@@ -592,7 +592,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
|
||||
let h = handle;
|
||||
let idx = fast_position_of(self.cache.as_slice(), h).unwrap();
|
||||
self.data_layer
|
||||
.write_entity(&self.cache[idx], &self.connection.read())?;
|
||||
.write_entity(&self.cache[idx], &self.connection.lock())?;
|
||||
}
|
||||
self.shifts += 1;
|
||||
|
||||
@@ -616,7 +616,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
|
||||
!remove
|
||||
});
|
||||
self.data_layer
|
||||
.delete_many_entities(cached.as_slice(), &mut self.connection.write())?;
|
||||
.delete_many_entities(cached.as_slice(), &mut self.connection.lock())?;
|
||||
let len = cached.len();
|
||||
cell = 0;
|
||||
self.cache.retain(|x| {
|
||||
|
||||
Reference in new issue
Block a user