use std::fmt::Display;
use crate::{
database::transactions::Transaction,
direct_access::repository_factory,
entities::Document,
event::{DirectAccessEntity, EntityEvent, Event, EventBuffer, Origin},
snapshot::EntityTreeSnapshot,
types::EntityId,
};
use crate::direct_access::root::RootRelationshipField;
use crate::error::RepositoryError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentRelationshipField {
Frames,
Lists,
Resources,
Tables,
}
impl Display for DocumentRelationshipField {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
pub trait DocumentTable {
fn create(&mut self, entity: &Document) -> Result<Document, RepositoryError>;
fn create_multi(&mut self, entities: &[Document]) -> Result<Vec<Document>, RepositoryError>;
fn get(&self, id: &EntityId) -> Result<Option<Document>, RepositoryError>;
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Document>>, RepositoryError>;
fn get_all(&self) -> Result<Vec<Document>, RepositoryError>;
fn update(&mut self, entity: &Document) -> Result<Document, RepositoryError>;
fn update_multi(&mut self, entities: &[Document]) -> Result<Vec<Document>, RepositoryError>;
fn update_with_relationships(&mut self, entity: &Document)
-> Result<Document, RepositoryError>;
fn update_with_relationships_multi(
&mut self,
entities: &[Document],
) -> Result<Vec<Document>, 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: &DocumentRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationship_many(
&self,
ids: &[EntityId],
field: &DocumentRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError>;
fn get_relationship_count(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
) -> Result<usize, RepositoryError>;
fn get_relationship_in_range(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationships_from_right_ids(
&self,
field: &DocumentRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError>;
fn set_relationship_multi(
&mut self,
field: &DocumentRelationshipField,
relationships: Vec<(EntityId, Vec<EntityId>)>,
) -> Result<(), RepositoryError>;
fn set_relationship(
&mut self,
id: &EntityId,
field: &DocumentRelationshipField,
right_ids: &[EntityId],
) -> Result<(), RepositoryError>;
fn move_relationship_ids(
&mut self,
id: &EntityId,
field: &DocumentRelationshipField,
ids_to_move: &[EntityId],
new_index: i32,
) -> Result<Vec<EntityId>, RepositoryError>;
}
pub trait DocumentTableRO {
fn get(&self, id: &EntityId) -> Result<Option<Document>, RepositoryError>;
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Document>>, RepositoryError>;
fn get_all(&self) -> Result<Vec<Document>, RepositoryError>;
fn get_relationship(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationship_many(
&self,
ids: &[EntityId],
field: &DocumentRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError>;
fn get_relationship_count(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
) -> Result<usize, RepositoryError>;
fn get_relationship_in_range(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>, RepositoryError>;
fn get_relationships_from_right_ids(
&self,
field: &DocumentRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError>;
}
pub struct DocumentRepository<'a> {
redb_table: Box<dyn DocumentTable + 'a>,
transaction: &'a Transaction,
}
impl<'a> DocumentRepository<'a> {
pub fn new(redb_table: Box<dyn DocumentTable + 'a>, transaction: &'a Transaction) -> Self {
DocumentRepository {
redb_table,
transaction,
}
}
pub fn create_orphan(
&mut self,
event_buffer: &mut EventBuffer,
entity: &Document,
) -> Result<Document, RepositoryError> {
let new = self.redb_table.create(entity)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(EntityEvent::Created)),
ids: vec![new.id],
data: None,
});
Ok(new)
}
pub fn create_orphan_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Document],
) -> Result<Vec<Document>, RepositoryError> {
let new_entities = self.redb_table.create_multi(entities)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(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: &Document,
owner_id: EntityId,
_index: i32,
) -> Result<Document, 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 relationship_ids.is_empty() {
relationship_ids = vec![created_id];
} else {
self.remove_multi(event_buffer, &relationship_ids)?;
relationship_ids = vec![created_id];
}
self.set_relationships_in_owner(event_buffer, &owner_id, &relationship_ids)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(EntityEvent::Created)),
ids: vec![created_id],
data: None,
});
Ok(new)
}
pub fn create_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Document],
owner_id: EntityId,
_index: i32,
) -> Result<Vec<Document>, 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 relationship_ids.is_empty() {
relationship_ids = created_ids.clone();
} else {
self.remove_multi(event_buffer, &relationship_ids)?;
relationship_ids = created_ids.clone();
}
self.set_relationships_in_owner(event_buffer, &owner_id, &relationship_ids)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(EntityEvent::Created)),
ids: created_ids,
data: None,
});
Ok(new_entities)
}
pub fn get(&self, id: &EntityId) -> Result<Option<Document>, RepositoryError> {
self.redb_table.get(id)
}
pub fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Document>>, RepositoryError> {
self.redb_table.get_multi(ids)
}
pub fn get_all(&self) -> Result<Vec<Document>, RepositoryError> {
self.redb_table.get_all()
}
pub fn update(
&mut self,
event_buffer: &mut EventBuffer,
entity: &Document,
) -> Result<Document, RepositoryError> {
let updated = self.redb_table.update(entity)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(EntityEvent::Updated)),
ids: vec![updated.id],
data: None,
});
Ok(updated)
}
pub fn update_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Document],
) -> Result<Vec<Document>, RepositoryError> {
let updated = self.redb_table.update_multi(entities)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(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: &Document,
) -> Result<Document, RepositoryError> {
let updated = self.redb_table.update_with_relationships(entity)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(EntityEvent::Updated)),
ids: vec![updated.id],
data: None,
});
Ok(updated)
}
pub fn update_with_relationships_multi(
&mut self,
event_buffer: &mut EventBuffer,
entities: &[Document],
) -> Result<Vec<Document>, RepositoryError> {
let updated = self.redb_table.update_with_relationships_multi(entities)?;
event_buffer.push(Event {
origin: Origin::DirectAccess(DirectAccessEntity::Document(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 frames = entity.frames.clone();
let resources = entity.resources.clone();
let lists = entity.lists.clone();
let tables = entity.tables.clone();
repository_factory::write::create_frame_repository(self.transaction)
.remove_multi(event_buffer, &frames)?;
repository_factory::write::create_resource_repository(self.transaction)
.remove_multi(event_buffer, &resources)?;
repository_factory::write::create_list_repository(self.transaction)
.remove_multi(event_buffer, &lists)?;
repository_factory::write::create_table_repository(self.transaction)
.remove_multi(event_buffer, &tables)?;
let affected_owner_ids: Vec<EntityId> = {
let owner_repo = repository_factory::write::create_root_repository(self.transaction);
owner_repo
.get_relationships_from_right_ids(&RootRelationshipField::Document, &[*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::Document(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 frames_ids: Vec<EntityId> = entities
.iter()
.flat_map(|entity| entity.as_ref().map(|entity| entity.frames.clone()))
.flatten()
.collect();
frames_ids.sort();
frames_ids.dedup();
let mut resources_ids: Vec<EntityId> = entities
.iter()
.flat_map(|entity| entity.as_ref().map(|entity| entity.resources.clone()))
.flatten()
.collect();
resources_ids.sort();
resources_ids.dedup();
let mut lists_ids: Vec<EntityId> = entities
.iter()
.flat_map(|entity| entity.as_ref().map(|entity| entity.lists.clone()))
.flatten()
.collect();
lists_ids.sort();
lists_ids.dedup();
let mut tables_ids: Vec<EntityId> = entities
.iter()
.flat_map(|entity| entity.as_ref().map(|entity| entity.tables.clone()))
.flatten()
.collect();
tables_ids.sort();
tables_ids.dedup();
repository_factory::write::create_frame_repository(self.transaction)
.remove_multi(event_buffer, &frames_ids)?;
repository_factory::write::create_resource_repository(self.transaction)
.remove_multi(event_buffer, &resources_ids)?;
repository_factory::write::create_list_repository(self.transaction)
.remove_multi(event_buffer, &lists_ids)?;
repository_factory::write::create_table_repository(self.transaction)
.remove_multi(event_buffer, &tables_ids)?;
let affected_owner_ids: Vec<EntityId> = {
let owner_repo = repository_factory::write::create_root_repository(self.transaction);
owner_repo
.get_relationships_from_right_ids(&RootRelationshipField::Document, 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::Document(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: &DocumentRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError> {
self.redb_table.get_relationship(id, field)
}
pub fn get_relationship_many(
&self,
ids: &[EntityId],
field: &DocumentRelationshipField,
) -> 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: &DocumentRelationshipField,
) -> Result<usize, RepositoryError> {
self.redb_table.get_relationship_count(id, field)
}
pub fn get_relationship_in_range(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
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: &DocumentRelationshipField,
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: &DocumentRelationshipField,
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 {
DocumentRelationshipField::Frames => {
let child_repo =
repository_factory::write::create_frame_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,
});
}
}
DocumentRelationshipField::Lists => {
let child_repo =
repository_factory::write::create_list_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,
});
}
}
DocumentRelationshipField::Resources => {
let child_repo =
repository_factory::write::create_resource_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,
});
}
}
DocumentRelationshipField::Tables => {
let child_repo =
repository_factory::write::create_table_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::Document(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: &DocumentRelationshipField,
right_ids: &[EntityId],
) -> Result<(), RepositoryError> {
if !right_ids.is_empty() {
match field {
DocumentRelationshipField::Frames => {
let child_repo =
repository_factory::write::create_frame_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,
});
}
}
DocumentRelationshipField::Lists => {
let child_repo =
repository_factory::write::create_list_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,
});
}
}
DocumentRelationshipField::Resources => {
let child_repo =
repository_factory::write::create_resource_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,
});
}
}
DocumentRelationshipField::Tables => {
let child_repo =
repository_factory::write::create_table_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::Document(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: &DocumentRelationshipField,
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::Document(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_root_repository(self.transaction);
repo.get_relationship(owner_id, &RootRelationshipField::Document)
}
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_root_repository(self.transaction);
repo.set_relationship(
event_buffer,
owner_id,
&RootRelationshipField::Document,
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 doc_ids: Vec<_> = store.documents.read().unwrap().keys().copied().collect();
emit(
DirectAccessEntity::Document(EntityEvent::Created),
doc_ids.clone(),
);
emit(DirectAccessEntity::Document(EntityEvent::Updated), doc_ids);
let frame_ids: Vec<_> = store.frames.read().unwrap().keys().copied().collect();
emit(
DirectAccessEntity::Frame(EntityEvent::Created),
frame_ids.clone(),
);
emit(DirectAccessEntity::Frame(EntityEvent::Updated), frame_ids);
let block_ids: Vec<_> = store.blocks.read().unwrap().keys().copied().collect();
emit(
DirectAccessEntity::Block(EntityEvent::Created),
block_ids.clone(),
);
emit(DirectAccessEntity::Block(EntityEvent::Updated), block_ids);
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);
let elem_ids: Vec<_> = store
.inline_elements
.read()
.unwrap()
.keys()
.copied()
.collect();
emit(
DirectAccessEntity::InlineElement(EntityEvent::Created),
elem_ids,
);
let list_ids: Vec<_> = store.lists.read().unwrap().keys().copied().collect();
emit(DirectAccessEntity::List(EntityEvent::Created), list_ids);
let res_ids: Vec<_> = store.resources.read().unwrap().keys().copied().collect();
emit(DirectAccessEntity::Resource(EntityEvent::Created), res_ids);
Ok(())
}
}
pub struct DocumentRepositoryRO<'a> {
redb_table: Box<dyn DocumentTableRO + 'a>,
}
impl<'a> DocumentRepositoryRO<'a> {
pub fn new(redb_table: Box<dyn DocumentTableRO + 'a>) -> Self {
DocumentRepositoryRO { redb_table }
}
pub fn get(&self, id: &EntityId) -> Result<Option<Document>, RepositoryError> {
self.redb_table.get(id)
}
pub fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Document>>, RepositoryError> {
self.redb_table.get_multi(ids)
}
pub fn get_all(&self) -> Result<Vec<Document>, RepositoryError> {
self.redb_table.get_all()
}
pub fn get_relationship(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
) -> Result<Vec<EntityId>, RepositoryError> {
self.redb_table.get_relationship(id, field)
}
pub fn get_relationship_many(
&self,
ids: &[EntityId],
field: &DocumentRelationshipField,
) -> 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: &DocumentRelationshipField,
) -> Result<usize, RepositoryError> {
self.redb_table.get_relationship_count(id, field)
}
pub fn get_relationship_in_range(
&self,
id: &EntityId,
field: &DocumentRelationshipField,
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: &DocumentRelationshipField,
right_ids: &[EntityId],
) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError> {
self.redb_table
.get_relationships_from_right_ids(field, right_ids)
}
}