feat: add entity state to speed up catalog persist function
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>,
|
||||
}
|
||||
|
||||
6
01.workspace/heave/src/str/entity_state.rs
Normal file
6
01.workspace/heave/src/str/entity_state.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#[derive(Debug, Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||
pub enum E {
|
||||
#[default]
|
||||
New,
|
||||
Loaded,
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod attribute;
|
||||
pub mod catalog;
|
||||
pub mod entity;
|
||||
pub mod entity_state;
|
||||
pub mod value;
|
||||
|
||||
Reference in New Issue
Block a user