use std::fmt::Display;
use crate::{
database::transactions::Transaction,
direct_access::repository_factory,
entities::Table,
event::{DirectAccessEntity, EntityEvent, Event, EventBuffer, Origin},
snapshot::EntityTreeSnapshot,
types::EntityId,
};
use crate::direct_access::document::DocumentRelationshipField;
use crate::error::RepositoryError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TableRelationshipField {
Cells,
}
impl Display for TableRelationshipField {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
pub trait TableTable {
fn create(&mut self, entity: &Table) -> Result<Table, RepositoryError>;
fn create_multi(&mut self, entities: &[Table]) -> Result<Vec<Table>, RepositoryError>;
fn get(&self, id: &EntityId) -> Result<Option<Table>, RepositoryError>;
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Table>>, RepositoryError>;
fn get_all(&self) -> Result<Vec<Table>, RepositoryError>;
fn update(&mut self, entity: &Table) -> Result<Table, RepositoryError>;
fn update_multi(&mut self, entities: &[Table]) -> Result<Vec<Table>, RepositoryError>;
fn update_with_relationships(&mut self, entity: &Table) -> Result<Table, RepositoryError>;
fn update_with_relationships_multi(
&mut self,
entities: &[Table],
) -> Result<Vec<Table>, RepositoryError>;
fn remove(&mut self, id: &EntityId) -> Result<(), RepositoryError>;
fn remove_multi(&mut self, ids: &[EntityId]) -> Result<(), RepositoryError>;
fn get_relationship(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationship_many(
&self,
ids: &[EntityId],
field: &TableRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError>;
fn get_relationship_count(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<usize, RepositoryError>;
fn get_relationship_in_range(
&self,
id: &EntityId,
field: &TableRelationshipField,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationships_from_right_ids(
&self,
field: &TableRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError>;
fn set_relationship_multi(
&mut self,
field: &TableRelationshipField,
relationships: Vec<(EntityId, Vec<EntityId>)>,
) -> Result<(), RepositoryError>;
fn set_relationship(
&mut self,
id: &EntityId,
field: &TableRelationshipField,
right_ids: &[EntityId],
) -> Result<(), RepositoryError>;
fn move_relationship_ids(
&mut self,
id: &EntityId,
field: &TableRelationshipField,
ids_to_move: &[EntityId],
new_index: i32,
) -> Result<Vec<EntityId>, RepositoryError>;
}
pub trait TableTableRO {
fn get(&self, id: &EntityId) -> Result<Option<Table>, RepositoryError>;
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Table>>, RepositoryError>;
fn get_all(&self) -> Result<Vec<Table>, RepositoryError>;
fn get_relationship(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationship_many(
&self,
ids: &[EntityId],
field: &TableRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError>;
fn get_relationship_count(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<usize, RepositoryError>;
fn get_relationship_in_range(
&self,
id: &EntityId,
field: &TableRelationshipField,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationships_from_right_ids(
&self,
field: &TableRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError>;
}
pub struct TableRepository<'a> {
redb_table: Box<dyn TableTable + 'a>,
transaction: &'a Transaction,
}
impl<'a> TableRepository<'a> {
pub fn new(redb_table: Box<dyn TableTable + 'a>, transaction: &'a Transaction) -> Self {
TableRepository {
redb_table,
transaction,
}
}
pub fn create_orphan(
&mut self,
event_buffer: &mut EventBuffer,
entity: &Table,
) -> Result<Table, RepositoryError> {
let new = self.redb_table.create(entity)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Created)),
ids: vec![new.id],
data: None,
});
Ok(new)
}
pub fn create_orphan_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Table],
) -> Result<Vec<Table>, RepositoryError> {
let new_entities = self.redb_table.create_multi(entities)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Created)),
ids: new_entities.iter().map(|e| e.id).collect(),
data: None,
});
Ok(new_entities)
}
pub fn create(
&mut self,
event_buffer: &mut EventBuffer,
entity: &Table,
owner_id: EntityId,
index: i32,
) -> Result<Table, RepositoryError> {
let new = self.redb_table.create(entity)?;
let created_id = new.id;
let mut relationship_ids = self.get_relationships_from_owner(&owner_id)?;
if index >= 0 && (index as usize) < relationship_ids.len() {
relationship_ids.insert(index as usize, created_id);
} else {
relationship_ids.push(created_id);
}
self.set_relationships_in_owner(event_buffer, &owner_id, &relationship_ids)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Created)),
ids: vec![created_id],
data: None,
});
Ok(new)
}
pub fn create_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Table],
owner_id: EntityId,
index: i32,
) -> Result<Vec<Table>, RepositoryError> {
let new_entities = self.redb_table.create_multi(entities)?;
let created_ids: Vec<EntityId> = new_entities.iter().map(|e| e.id).collect();
let mut relationship_ids = self.get_relationships_from_owner(&owner_id)?;
if index >= 0 && (index as usize) < relationship_ids.len() {
for (i, id) in created_ids.iter().enumerate() {
relationship_ids.insert(index as usize + i, *id);
}
} else {
relationship_ids.extend(created_ids.iter());
}
self.set_relationships_in_owner(event_buffer, &owner_id, &relationship_ids)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Created)),
ids: created_ids,
data: None,
});
Ok(new_entities)
}
pub fn get(&self, id: &EntityId) -> Result<Option<Table>, RepositoryError> {
self.redb_table.get(id)
}
pub fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Table>>, RepositoryError> {
self.redb_table.get_multi(ids)
}
pub fn get_all(&self) -> Result<Vec<Table>, RepositoryError> {
self.redb_table.get_all()
}
pub fn update(
&mut self,
event_buffer: &mut EventBuffer,
entity: &Table,
) -> Result<Table, RepositoryError> {
let updated = self.redb_table.update(entity)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: vec![updated.id],
data: None,
});
Ok(updated)
}
pub fn update_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Table],
) -> Result<Vec<Table>, RepositoryError> {
let updated = self.redb_table.update_multi(entities)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: updated.iter().map(|e| e.id).collect(),
data: None,
});
Ok(updated)
}
pub fn update_with_relationships(
&mut self,
event_buffer: &mut EventBuffer,
entity: &Table,
) -> Result<Table, RepositoryError> {
let updated = self.redb_table.update_with_relationships(entity)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: vec![updated.id],
data: None,
});
Ok(updated)
}
pub fn update_with_relationships_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Table],
) -> Result<Vec<Table>, RepositoryError> {
let updated = self.redb_table.update_with_relationships_multi(entities)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: updated.iter().map(|e| e.id).collect(),
data: None,
});
Ok(updated)
}
pub fn remove(
&mut self,
event_buffer: &mut EventBuffer,
id: &EntityId,
) -> Result<(), RepositoryError> {
let entity = match self.redb_table.get(id)? {
Some(e) => e,
None => return Ok(()),
};
let cells = entity.cells.clone();
repository_factory::write::create_table_cell_repository(self.transaction)
.remove_multi(event_buffer, &cells)?;
let affected_owner_ids: Vec<EntityId> = {
let owner_repo =
repository_factory::write::create_document_repository(self.transaction);
owner_repo
.get_relationships_from_right_ids(&DocumentRelationshipField::Tables, &[*id])?
.into_iter()
.map(|(owner_id, _)| owner_id)
.collect()
};
let mut owner_rel_before: std::collections::HashMap<EntityId, Vec<EntityId>> =
std::collections::HashMap::new();
for owner_id in &affected_owner_ids {
owner_rel_before.insert(*owner_id, self.get_relationships_from_owner(owner_id)?);
}
self.redb_table.remove(id)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Removed)),
ids: vec![*id],
data: None,
});
for owner_id in &affected_owner_ids {
if let Some(rel_ids) = owner_rel_before.get(owner_id) {
let updated: Vec<EntityId> =
rel_ids.iter().copied().filter(|rid| *rid != *id).collect();
self.set_relationships_in_owner(event_buffer, owner_id, &updated)?;
}
}
Ok(())
}
pub fn remove_multi(
&mut self,
event_buffer: &mut EventBuffer,
ids: &[EntityId],
) -> Result<(), RepositoryError> {
let entities = self.redb_table.get_multi(ids)?;
if entities.is_empty() || entities.iter().all(|e| e.is_none()) {
return Ok(());
}
let mut cells_ids: Vec<EntityId> = entities
.iter()
.flat_map(|entity| entity.as_ref().map(|entity| entity.cells.clone()))
.flatten()
.collect();
cells_ids.sort();
cells_ids.dedup();
repository_factory::write::create_table_cell_repository(self.transaction)
.remove_multi(event_buffer, &cells_ids)?;
let affected_owner_ids: Vec<EntityId> = {
let owner_repo =
repository_factory::write::create_document_repository(self.transaction);
owner_repo
.get_relationships_from_right_ids(&DocumentRelationshipField::Tables, ids)?
.into_iter()
.map(|(owner_id, _)| owner_id)
.collect()
};
let mut owner_rel_before: std::collections::HashMap<EntityId, Vec<EntityId>> =
std::collections::HashMap::new();
for owner_id in &affected_owner_ids {
owner_rel_before.insert(*owner_id, self.get_relationships_from_owner(owner_id)?);
}
self.redb_table.remove_multi(ids)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Removed)),
ids: ids.into(),
data: None,
});
{
let removed_set: std::collections::HashSet<EntityId> = ids.iter().copied().collect();
for owner_id in &affected_owner_ids {
if let Some(rel_ids) = owner_rel_before.get(owner_id) {
let updated: Vec<EntityId> = rel_ids
.iter()
.copied()
.filter(|rid| !removed_set.contains(rid))
.collect();
self.set_relationships_in_owner(event_buffer, owner_id, &updated)?;
}
}
}
Ok(())
}
pub fn get_relationship(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError> {
self.redb_table.get_relationship(id, field)
}
pub fn get_relationship_many(
&self,
ids: &[EntityId],
field: &TableRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError> {
self.redb_table.get_relationship_many(ids, field)
}
pub fn get_relationship_count(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<usize, RepositoryError> {
self.redb_table.get_relationship_count(id, field)
}
pub fn get_relationship_in_range(
&self,
id: &EntityId,
field: &TableRelationshipField,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>, RepositoryError> {
self.redb_table
.get_relationship_in_range(id, field, offset, limit)
}
pub fn get_relationships_from_right_ids(
&self,
field: &TableRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError> {
self.redb_table
.get_relationships_from_right_ids(field, right_ids)
}
pub fn set_relationship_multi(
&mut self,
event_buffer: &mut EventBuffer,
field: &TableRelationshipField,
relationships: Vec<(EntityId, Vec<EntityId>)>,
) -> Result<(), RepositoryError> {
let all_right_ids: Vec<EntityId> = relationships
.iter()
.flat_map(|(_, ids)| ids.iter().copied())
.collect();
if !all_right_ids.is_empty() {
match field {
TableRelationshipField::Cells => {
let child_repo =
repository_factory::write::create_table_cell_repository(self.transaction);
let found = child_repo.get_multi(&all_right_ids)?;
let missing: Vec<_> = all_right_ids
.iter()
.zip(found.iter())
.filter(|(_, entity)| entity.is_none())
.map(|(id, _)| *id)
.collect();
if !missing.is_empty() {
return Err(RepositoryError::MissingRelationshipTarget {
operation: "set_relationship_multi",
ids: missing,
});
}
}
}
}
self.redb_table
.set_relationship_multi(field, relationships.clone())?;
for (left_id, right_ids) in relationships {
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: vec![left_id],
data: Some(format!(
"{}:{}",
field,
right_ids
.iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(",")
)),
});
}
Ok(())
}
pub fn set_relationship(
&mut self,
event_buffer: &mut EventBuffer,
id: &EntityId,
field: &TableRelationshipField,
right_ids: &[EntityId],
) -> Result<(), RepositoryError> {
if !right_ids.is_empty() {
match field {
TableRelationshipField::Cells => {
let child_repo =
repository_factory::write::create_table_cell_repository(self.transaction);
let found = child_repo.get_multi(right_ids)?;
let missing: Vec<_> = right_ids
.iter()
.zip(found.iter())
.filter(|(_, entity)| entity.is_none())
.map(|(id, _)| *id)
.collect();
if !missing.is_empty() {
return Err(RepositoryError::MissingRelationshipTarget {
operation: "set_relationship",
ids: missing,
});
}
}
}
}
self.redb_table.set_relationship(id, field, right_ids)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: vec![*id],
data: Some(format!(
"{}:{}",
field,
right_ids
.iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(",")
)),
});
Ok(())
}
pub fn move_relationship_ids(
&mut self,
event_buffer: &mut EventBuffer,
id: &EntityId,
field: &TableRelationshipField,
ids_to_move: &[EntityId],
new_index: i32,
) -> Result<Vec<EntityId>, RepositoryError> {
let reordered = self
.redb_table
.move_relationship_ids(id, field, ids_to_move, new_index)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Table(EntityEvent::Updated)),
ids: vec![*id],
data: Some(format!(
"{}:{}",
field,
reordered
.iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(",")
)),
});
Ok(reordered)
}
pub fn get_relationships_from_owner(
&self,
owner_id: &EntityId,
) -> Result<Vec<EntityId>, RepositoryError> {
let repo = repository_factory::write::create_document_repository(self.transaction);
repo.get_relationship(owner_id, &DocumentRelationshipField::Tables)
}
pub fn set_relationships_in_owner(
&mut self,
event_buffer: &mut EventBuffer,
owner_id: &EntityId,
ids: &[EntityId],
) -> Result<(), RepositoryError> {
let mut repo = repository_factory::write::create_document_repository(self.transaction);
repo.set_relationship(
event_buffer,
owner_id,
&DocumentRelationshipField::Tables,
ids,
)
}
pub fn snapshot(&self, _ids: &[EntityId]) -> Result<EntityTreeSnapshot, RepositoryError> {
let store_snap = self.transaction.snapshot_store();
Ok(EntityTreeSnapshot {
store_snapshot: Some(store_snap),
})
}
pub fn restore(
&mut self,
event_buffer: &mut EventBuffer,
snap: &EntityTreeSnapshot,
) -> Result<(), RepositoryError> {
let store_snap = snap
.store_snapshot
.as_ref()
.ok_or_else(|| RepositoryError::Serialization("missing store snapshot".into()))?;
self.transaction.restore_store(store_snap);
let store = self.transaction.get_store();
let mut emit = |entity: DirectAccessEntity, ids: Vec<EntityId>| {
if !ids.is_empty() {
event_buffer.push(Event {
origin: Origin::DirectAccess(entity),
ids,
data: None,
});
}
};
let table_ids: Vec<_> = store.tables.read().unwrap().keys().copied().collect();
emit(
DirectAccessEntity::Table(EntityEvent::Created),
table_ids.clone(),
);
emit(DirectAccessEntity::Table(EntityEvent::Updated), table_ids);
let tc_ids: Vec<_> = store.table_cells.read().unwrap().keys().copied().collect();
emit(
DirectAccessEntity::TableCell(EntityEvent::Created),
tc_ids.clone(),
);
emit(DirectAccessEntity::TableCell(EntityEvent::Updated), tc_ids);
Ok(())
}
}
pub struct TableRepositoryRO<'a> {
redb_table: Box<dyn TableTableRO + 'a>,
}
impl<'a> TableRepositoryRO<'a> {
pub fn new(redb_table: Box<dyn TableTableRO + 'a>) -> Self {
TableRepositoryRO { redb_table }
}
pub fn get(&self, id: &EntityId) -> Result<Option<Table>, RepositoryError> {
self.redb_table.get(id)
}
pub fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Table>>, RepositoryError> {
self.redb_table.get_multi(ids)
}
pub fn get_all(&self) -> Result<Vec<Table>, RepositoryError> {
self.redb_table.get_all()
}
pub fn get_relationship(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError> {
self.redb_table.get_relationship(id, field)
}
pub fn get_relationship_many(
&self,
ids: &[EntityId],
field: &TableRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError> {
self.redb_table.get_relationship_many(ids, field)
}
pub fn get_relationship_count(
&self,
id: &EntityId,
field: &TableRelationshipField,
) -> Result<usize, RepositoryError> {
self.redb_table.get_relationship_count(id, field)
}
pub fn get_relationship_in_range(
&self,
id: &EntityId,
field: &TableRelationshipField,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>, RepositoryError> {
self.redb_table
.get_relationship_in_range(id, field, offset, limit)
}
pub fn get_relationships_from_right_ids(
&self,
field: &TableRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError> {
self.redb_table
.get_relationships_from_right_ids(field, right_ids)
}
}