feat: add entity state to speed up catalog persist function

This commit is contained in:
2025-09-30 17:54:53 +02:00
parent 8cf8e36b2a
commit 97e54e7891
8 changed files with 21 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ pub fn run(path: &path::Path, entity_class: &str) -> Vec<Entity> {
for entity in result {
let mut entity = entity.unwrap();
sqlite::load::attributes(&connection, &mut entity);
entity.state = EntityState::Loaded;
entities.push(entity);
}
entities

View File

@@ -14,6 +14,7 @@ pub fn run(path: &path::Path, entity_id: &str) -> Option<Entity> {
let mut entity = result.unwrap();
if let Some(ref mut entity) = entity {
sqlite::load::attributes(&connection, entity);
entity.state = EntityState::Loaded;
}
entity
}

View File

@@ -37,6 +37,7 @@ fn write_attribute(attribute: &Attribute, entity: &Entity, transaction: &rusqlit
fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) {
let entity_id = [&entity.id];
let entity_values = (&entity.id, &entity.class);
println!("Writing: {:?}", entity_values);
let _ = transaction.execute(DELETE_ENTITY_STATEMENT, entity_id);
let _ = transaction.execute(INSERT_ENTITY_STATEMENT, entity_values);
for (_key, attribute) in entity.attributes.iter() {
@@ -47,7 +48,11 @@ fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) {
pub fn run(path: &path::Path, catalog: &Catalog) {
let mut connection = Connection::open(path).unwrap();
let transaction = connection.transaction().unwrap();
for (_key, entity) in catalog.items.iter() {
for (_key, entity) in catalog
.items
.iter()
.filter(|item| item.1.state == EntityState::New)
{
write_entity(entity, &transaction);
}
let _ = transaction.commit();

View File

@@ -10,6 +10,7 @@ mod tst;
pub use crate::str::attribute::O as Attribute;
pub use crate::str::catalog::O as Catalog;
pub use crate::str::entity::O as Entity;
pub use crate::str::entity_state::E as EntityState;
pub use crate::str::value::E as Value;
pub use crate::trt::eav::T as EAV;

View File

@@ -18,12 +18,14 @@ impl O {
sqlite::init::db(path);
}
pub fn insert(&mut self, object: impl EAV) {
let entity = object.into();
let mut entity = object.into();
entity.state = EntityState::New;
self.items.insert(entity.id.clone(), entity);
}
pub fn insert_many(&mut self, objects: Vec<impl EAV>) {
for object in objects {
let entity = object.into();
let mut entity = object.into();
entity.state = EntityState::New;
self.items.insert(entity.id.clone(), entity);
}
}

View File

@@ -3,6 +3,7 @@ use crate::*;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct O {
pub id: String,
pub state: EntityState,
pub class: String,
pub attributes: std::collections::HashMap<String, Attribute>,
}

View File

@@ -0,0 +1,6 @@
#[derive(Debug, Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
pub enum E {
#[default]
New,
Loaded,
}

View File

@@ -1,4 +1,5 @@
pub mod attribute;
pub mod catalog;
pub mod entity;
pub mod entity_state;
pub mod value;