Fix data access method and solve import page

This commit is contained in:
micialware committed 2026-09-04 11:37:19 +03:00
1 parent 532640058e
commit 9a7578727b
2 files changed
+35 -41

No files matched your search

+11 -11
View File
@@ -245,7 +245,7 @@ pub struct DataRepositoryInner<T: Clone + EntityData, D: DbLayer<T>> {
cache: Vec<T>, cache: Vec<T>,
temporal: Vec<T>, temporal: Vec<T>,
next_temporal: u32, next_temporal: u32,
connection: Arc<RwLock<SqliteConnection>>, connection: Arc<Mutex<SqliteConnection>>,
data_layer: D, data_layer: D,
query_manger: QueryManger<T, Self, D>, query_manger: QueryManger<T, Self, D>,
version: u16, 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> { 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{ Self{
inner: Arc::new(RwLock::new(DataRepositoryInner{ inner: Arc::new(RwLock::new(DataRepositoryInner{
cache: vec![], 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> { fn create_now(&mut self, mut new: T) -> Result<EntityHandle<T>, NotificationData> {
self.data_layer self.data_layer
.add_entity(&mut new, &self.connection.read())?; .add_entity(&mut new, &self.connection.lock())?;
let index = self.cache.len(); let index = self.cache.len();
let handle = EntityHandle::new(new.id(), index, true); let handle = EntityHandle::new(new.id(), index, true);
self.cache.push(new); self.cache.push(new);
@@ -440,12 +440,12 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
if entity.real() { if entity.real() {
let data = self.get_ref(entity).unwrap(); let data = self.get_ref(entity).unwrap();
self.data_layer self.data_layer
.write_entity(data, &self.connection.read())?; .write_entity(data, &self.connection.lock())?;
} else { } else {
let index = fast_position_of(self.temporal.as_slice(), entity).unwrap(); let index = fast_position_of(self.temporal.as_slice(), entity).unwrap();
let mut data = self.temporal.remove(index); let mut data = self.temporal.remove(index);
self.data_layer self.data_layer
.add_entity(&mut data, &self.connection.read())?; .add_entity(&mut data, &self.connection.lock())?;
entity.set_real(true); entity.set_real(true);
entity.id = data.id(); entity.id = data.id();
entity.set_index(self.cache.len()); 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 { fn delete(&mut self, mut handle: EntityHandle<T>) -> MaybeError {
let vec = if handle.real() { let vec = if handle.real() {
self.data_layer self.data_layer
.delete_entity(handle.id, &self.connection.read())?; .delete_entity(handle.id, &self.connection.lock())?;
&mut self.cache &mut self.cache
} else { } else {
&mut self.temporal &mut self.temporal
@@ -495,7 +495,7 @@ impl<T: EntityData + Clone, D: DbLayer<T>> Repository<T, D> for DataRepositoryIn
fn init(&mut self) -> MaybeError { fn init(&mut self) -> MaybeError {
self.data_layer self.data_layer
.load_all(&mut self.cache, &self.connection.read())?; .load_all(&mut self.cache, &self.connection.lock())?;
self.version += 1; self.version += 1;
Ok(()) Ok(())
} }
@@ -538,7 +538,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
for DataRepositoryInner<T, D> for DataRepositoryInner<T, D>
{ {
fn create_now_many(&mut self, entities: &mut [T]) -> Result<Vec<EntityHandle<T>>, NotificationData> { 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 len = self.cache.len();
let handles = entities.iter().enumerate().map(|(index, entity)| {EntityHandle::new(entity.id(), index + len, true)}).collect(); let handles = entities.iter().enumerate().map(|(index, entity)| {EntityHandle::new(entity.id(), index + len, true)}).collect();
self.cache.extend(entities.to_vec()); self.cache.extend(entities.to_vec());
@@ -574,7 +574,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
}); });
self.data_layer 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(); let start = self.cache.len();
@@ -592,7 +592,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
let h = handle; let h = handle;
let idx = fast_position_of(self.cache.as_slice(), h).unwrap(); let idx = fast_position_of(self.cache.as_slice(), h).unwrap();
self.data_layer self.data_layer
.write_entity(&self.cache[idx], &self.connection.read())?; .write_entity(&self.cache[idx], &self.connection.lock())?;
} }
self.shifts += 1; self.shifts += 1;
@@ -616,7 +616,7 @@ impl<T: EntityData + Clone, D: BatchedDbLayer<T> + DbLayer<T>> BatchedRepository
!remove !remove
}); });
self.data_layer 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(); let len = cached.len();
cell = 0; cell = 0;
self.cache.retain(|x| { self.cache.retain(|x| {
+24 -30
View File
@@ -432,18 +432,29 @@ impl ImportState {
info!("{}", group.name); info!("{}", group.name);
Task::perform( Task::perform(
async move { async move {
let words_list = spawn_blocking(move || { spawn_blocking(move || {
let temp_file_path = temp_import_path(); let temp_file_path = temp_import_path();
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?; let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
let import_list = get_words_of_group(&connection, group.id)?; let import_list = get_words_of_group(&connection, group.id)?;
let mut words_list = Vec::with_capacity(import_list.len()); let mut words_list = Vec::with_capacity(import_list.len());
let map_indices = Self::get_mapping_indices(&group); let map_indices = Self::get_mapping_indices(&group);
let group_handle;
{
let group_entity = WordGroup {
id: 0.into(),
name: group.name.clone(),
};
let mut groups_repo = state.word_groups.write();
group_handle = groups_repo.create_now(group_entity)?;
}
let group_id = group_handle.id;
for import in import_list { for import in import_list {
let mut word = WordData::new(); let mut word = WordData::new();
word.tags = import.tags.trim().replace(" ", ", "); word.tags = import.tags.trim().replace(" ", ", ");
word.group_id = group_id;
for (dest, indices) in &map_indices { for (dest, indices) in &map_indices {
let collected_string = Self::collect_strings( let collected_string = Self::collect_strings(
&import.fields, &import.fields,
@@ -470,37 +481,20 @@ impl ImportState {
words_list.push(word); words_list.push(word);
} }
Ok::<_, NotificationData>(words_list) let mut words_repo = state.dictionary.write();
let mut index = 0;
let total_len = words_list.len() as f32 / 1024.0;
for words in &mut words_list.chunks_mut(1024) {
words_repo.create_now_many(words)?;
index += 1;
let mut progress = progress.lock();
*progress = (index as f32 / total_len).min(1.0);
}
Ok(())
}) })
.await .await
.unwrap()?; .unwrap()
let group_handle = {
let group_entity = WordGroup {
id: 0.into(),
name: group.name.clone(),
};
let mut groups_repo = state.word_groups.write();
groups_repo.create_now(group_entity)?
};
let group_id = group_handle.id;
let mut words_list = words_list;
for word in &mut words_list {
word.group_id = group_id;
}
let mut words_repo = state.dictionary.write();
let mut index = 0;
let total_len = words_list.len() as f32 / 1024.0;
for words in &mut words_list.chunks_mut(1024) {
words_repo.create_now_many(words)?;
index += 1;
let mut progress = progress.lock();
*progress = (index as f32 / total_len).min(1.0);
}
Ok(())
}, },
|r: Result<(), NotificationData>| match r { |r: Result<(), NotificationData>| match r {
Ok(_) => RootMessage::Import(ImportFinished), Ok(_) => RootMessage::Import(ImportFinished),