1use std::fmt::Display;
4
5use crate::{
6 database::transactions::Transaction,
7 direct_access::repository_factory,
8 entities::Entity,
9 event::{DirectAccessEntity, EntityEvent, Event, EventBuffer, Origin},
10 snapshot::EntityTreeSnapshot,
11 types::EntityId,
12};
13
14use crate::direct_access::workspace::WorkspaceRelationshipField;
15use crate::error::RepositoryError;
16use serde::{Deserialize, Serialize};
17
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub enum EntityRelationshipField {
20 Fields,
21 InheritsFrom,
22 Relationships,
23}
24
25impl Display for EntityRelationshipField {
26 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
27 write!(f, "{:?}", self)
28 }
29}
30
31pub trait EntityTable {
32 fn create(&mut self, entity: &Entity) -> Result<Entity, RepositoryError>;
33 fn create_multi(&mut self, entities: &[Entity]) -> Result<Vec<Entity>, RepositoryError>;
34 fn get(&self, id: &EntityId) -> Result<Option<Entity>, RepositoryError>;
35 fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Entity>>, RepositoryError>;
36 fn get_all(&self) -> Result<Vec<Entity>, RepositoryError>;
37 fn update(&mut self, entity: &Entity) -> Result<Entity, RepositoryError>;
38 fn update_multi(&mut self, entities: &[Entity]) -> Result<Vec<Entity>, RepositoryError>;
39 fn update_with_relationships(&mut self, entity: &Entity) -> Result<Entity, RepositoryError>;
40 fn update_with_relationships_multi(
41 &mut self,
42 entities: &[Entity],
43 ) -> Result<Vec<Entity>, RepositoryError>;
44 fn remove(&mut self, id: &EntityId) -> Result<(), RepositoryError>;
45 fn remove_multi(&mut self, ids: &[EntityId]) -> Result<(), RepositoryError>;
46 fn get_relationship(
47 &self,
48 id: &EntityId,
49 field: &EntityRelationshipField,
50 ) -> Result<Vec<EntityId>, RepositoryError>;
51 fn get_relationship_many(
52 &self,
53 ids: &[EntityId],
54 field: &EntityRelationshipField,
55 ) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError>;
56 fn get_relationship_count(
57 &self,
58 id: &EntityId,
59 field: &EntityRelationshipField,
60 ) -> Result<usize, RepositoryError>;
61 fn get_relationship_in_range(
62 &self,
63 id: &EntityId,
64 field: &EntityRelationshipField,
65 offset: usize,
66 limit: usize,
67 ) -> Result<Vec<EntityId>, RepositoryError>;
68 fn get_relationships_from_right_ids(
69 &self,
70 field: &EntityRelationshipField,
71 right_ids: &[EntityId],
72 ) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError>;
73 fn set_relationship_multi(
74 &mut self,
75 field: &EntityRelationshipField,
76 relationships: Vec<(EntityId, Vec<EntityId>)>,
77 ) -> Result<(), RepositoryError>;
78 fn set_relationship(
79 &mut self,
80 id: &EntityId,
81 field: &EntityRelationshipField,
82 right_ids: &[EntityId],
83 ) -> Result<(), RepositoryError>;
84 fn move_relationship_ids(
85 &mut self,
86 id: &EntityId,
87 field: &EntityRelationshipField,
88 ids_to_move: &[EntityId],
89 new_index: i32,
90 ) -> Result<Vec<EntityId>, RepositoryError>;
91}
92
93pub trait EntityTableRO {
94 fn get(&self, id: &EntityId) -> Result<Option<Entity>, RepositoryError>;
95 fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Entity>>, RepositoryError>;
96 fn get_all(&self) -> Result<Vec<Entity>, RepositoryError>;
97 fn get_relationship(
98 &self,
99 id: &EntityId,
100 field: &EntityRelationshipField,
101 ) -> Result<Vec<EntityId>, RepositoryError>;
102 fn get_relationship_many(
103 &self,
104 ids: &[EntityId],
105 field: &EntityRelationshipField,
106 ) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError>;
107 fn get_relationship_count(
108 &self,
109 id: &EntityId,
110 field: &EntityRelationshipField,
111 ) -> Result<usize, RepositoryError>;
112 fn get_relationship_in_range(
113 &self,
114 id: &EntityId,
115 field: &EntityRelationshipField,
116 offset: usize,
117 limit: usize,
118 ) -> Result<Vec<EntityId>, RepositoryError>;
119 fn get_relationships_from_right_ids(
120 &self,
121 field: &EntityRelationshipField,
122 right_ids: &[EntityId],
123 ) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError>;
124}
125
126pub struct EntityRepository<'a> {
127 table: Box<dyn EntityTable + 'a>,
128 transaction: &'a Transaction,
129}
130
131impl<'a> EntityRepository<'a> {
132 pub fn new(table: Box<dyn EntityTable + 'a>, transaction: &'a Transaction) -> Self {
133 EntityRepository { table, transaction }
134 }
135
136 pub fn create_orphan(
137 &mut self,
138 event_buffer: &mut EventBuffer,
139 entity: &Entity,
140 ) -> Result<Entity, RepositoryError> {
141 let new = self.table.create(entity)?;
142 event_buffer.push(Event {
143 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Created)),
144 ids: vec![new.id],
145 data: None,
146 });
147 Ok(new)
148 }
149
150 pub fn create_orphan_multi(
151 &mut self,
152 event_buffer: &mut EventBuffer,
153 entities: &[Entity],
154 ) -> Result<Vec<Entity>, RepositoryError> {
155 let new_entities = self.table.create_multi(entities)?;
156 event_buffer.push(Event {
157 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Created)),
158 ids: new_entities.iter().map(|e| e.id).collect(),
159 data: None,
160 });
161 Ok(new_entities)
162 }
163 pub fn create(
164 &mut self,
165 event_buffer: &mut EventBuffer,
166 entity: &Entity,
167 owner_id: EntityId,
168 index: i32,
169 ) -> Result<Entity, RepositoryError> {
170 let new = self.table.create(entity)?;
171 let created_id = new.id;
172
173 let mut relationship_ids = self.get_relationships_from_owner(&owner_id)?;
174 if index >= 0 && (index as usize) < relationship_ids.len() {
176 relationship_ids.insert(index as usize, created_id);
177 } else {
178 relationship_ids.push(created_id);
179 }
180
181 self.set_relationships_in_owner(event_buffer, &owner_id, &relationship_ids)?;
182 event_buffer.push(Event {
183 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Created)),
184 ids: vec![created_id],
185 data: None,
186 });
187 Ok(new)
188 }
189
190 pub fn create_multi(
191 &mut self,
192 event_buffer: &mut EventBuffer,
193 entities: &[Entity],
194 owner_id: EntityId,
195 index: i32,
196 ) -> Result<Vec<Entity>, RepositoryError> {
197 let new_entities = self.table.create_multi(entities)?;
198 let created_ids: Vec<EntityId> = new_entities.iter().map(|e| e.id).collect();
199
200 let mut relationship_ids = self.get_relationships_from_owner(&owner_id)?;
201 if index >= 0 && (index as usize) < relationship_ids.len() {
202 for (i, id) in created_ids.iter().enumerate() {
203 relationship_ids.insert(index as usize + i, *id);
204 }
205 } else {
206 relationship_ids.extend(created_ids.iter());
207 }
208
209 self.set_relationships_in_owner(event_buffer, &owner_id, &relationship_ids)?;
210 event_buffer.push(Event {
211 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Created)),
212 ids: created_ids,
213 data: None,
214 });
215 Ok(new_entities)
216 }
217
218 pub fn get(&self, id: &EntityId) -> Result<Option<Entity>, RepositoryError> {
219 self.table.get(id)
220 }
221 pub fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Entity>>, RepositoryError> {
222 self.table.get_multi(ids)
223 }
224 pub fn get_all(&self) -> Result<Vec<Entity>, RepositoryError> {
225 self.table.get_all()
226 }
227
228 pub fn update(
229 &mut self,
230 event_buffer: &mut EventBuffer,
231 entity: &Entity,
232 ) -> Result<Entity, RepositoryError> {
233 let updated = self.table.update(entity)?;
234 event_buffer.push(Event {
235 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
236 ids: vec![updated.id],
237 data: None,
238 });
239 Ok(updated)
240 }
241
242 pub fn update_multi(
243 &mut self,
244 event_buffer: &mut EventBuffer,
245 entities: &[Entity],
246 ) -> Result<Vec<Entity>, RepositoryError> {
247 let updated = self.table.update_multi(entities)?;
248 event_buffer.push(Event {
249 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
250 ids: updated.iter().map(|e| e.id).collect(),
251 data: None,
252 });
253 Ok(updated)
254 }
255
256 pub fn update_with_relationships(
257 &mut self,
258 event_buffer: &mut EventBuffer,
259 entity: &Entity,
260 ) -> Result<Entity, RepositoryError> {
261 let updated = self.table.update_with_relationships(entity)?;
262 event_buffer.push(Event {
263 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
264 ids: vec![updated.id],
265 data: None,
266 });
267 Ok(updated)
268 }
269
270 pub fn update_with_relationships_multi(
271 &mut self,
272 event_buffer: &mut EventBuffer,
273 entities: &[Entity],
274 ) -> Result<Vec<Entity>, RepositoryError> {
275 let updated = self.table.update_with_relationships_multi(entities)?;
276 event_buffer.push(Event {
277 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
278 ids: updated.iter().map(|e| e.id).collect(),
279 data: None,
280 });
281 Ok(updated)
282 }
283
284 pub fn remove(
285 &mut self,
286 event_buffer: &mut EventBuffer,
287 id: &EntityId,
288 ) -> Result<(), RepositoryError> {
289 let entity = match self.table.get(id)? {
290 Some(e) => e,
291 None => return Ok(()),
292 };
293 let fields = entity.fields.clone();
296 let relationships = entity.relationships.clone();
297
298 repository_factory::write::create_field_repository(self.transaction)?
301 .remove_multi(event_buffer, &fields)?;
302 repository_factory::write::create_relationship_repository(self.transaction)?
303 .remove_multi(event_buffer, &relationships)?;
304 let affected_owner_ids: Vec<EntityId> = {
306 let owner_repo =
307 repository_factory::write::create_workspace_repository(self.transaction)?;
308 owner_repo
309 .get_relationships_from_right_ids(&WorkspaceRelationshipField::Entities, &[*id])?
310 .into_iter()
311 .map(|(owner_id, _)| owner_id)
312 .collect()
313 };
314 let mut owner_rel_before: std::collections::HashMap<EntityId, Vec<EntityId>> =
316 std::collections::HashMap::new();
317 for owner_id in &affected_owner_ids {
318 owner_rel_before.insert(*owner_id, self.get_relationships_from_owner(owner_id)?);
319 }
320
321 self.table.remove(id)?;
323 event_buffer.push(Event {
324 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Removed)),
325 ids: vec![*id],
326 data: None,
327 });
328 for owner_id in &affected_owner_ids {
330 if let Some(rel_ids) = owner_rel_before.get(owner_id) {
331 let updated: Vec<EntityId> =
332 rel_ids.iter().copied().filter(|rid| *rid != *id).collect();
333 self.set_relationships_in_owner(event_buffer, owner_id, &updated)?;
334 }
335 }
336
337 Ok(())
338 }
339
340 pub fn remove_multi(
341 &mut self,
342 event_buffer: &mut EventBuffer,
343 ids: &[EntityId],
344 ) -> Result<(), RepositoryError> {
345 let entities = self.table.get_multi(ids)?;
346 if entities.is_empty() || entities.iter().all(|e| e.is_none()) {
347 return Ok(());
348 }
349
350 let mut fields_ids: Vec<EntityId> = entities
353 .iter()
354 .flat_map(|entity| entity.as_ref().map(|entity| entity.fields.clone()))
355 .flatten()
356 .collect();
357 fields_ids.sort();
359 fields_ids.dedup();
360 let mut relationships_ids: Vec<EntityId> = entities
361 .iter()
362 .flat_map(|entity| entity.as_ref().map(|entity| entity.relationships.clone()))
363 .flatten()
364 .collect();
365 relationships_ids.sort();
367 relationships_ids.dedup();
368
369 repository_factory::write::create_field_repository(self.transaction)?
372 .remove_multi(event_buffer, &fields_ids)?;
373 repository_factory::write::create_relationship_repository(self.transaction)?
374 .remove_multi(event_buffer, &relationships_ids)?;
375 let affected_owner_ids: Vec<EntityId> = {
377 let owner_repo =
378 repository_factory::write::create_workspace_repository(self.transaction)?;
379 owner_repo
380 .get_relationships_from_right_ids(&WorkspaceRelationshipField::Entities, ids)?
381 .into_iter()
382 .map(|(owner_id, _)| owner_id)
383 .collect()
384 };
385 let mut owner_rel_before: std::collections::HashMap<EntityId, Vec<EntityId>> =
387 std::collections::HashMap::new();
388 for owner_id in &affected_owner_ids {
389 owner_rel_before.insert(*owner_id, self.get_relationships_from_owner(owner_id)?);
390 }
391
392 self.table.remove_multi(ids)?;
393 event_buffer.push(Event {
394 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Removed)),
395 ids: ids.into(),
396 data: None,
397 });
398 {
400 let removed_set: std::collections::HashSet<EntityId> = ids.iter().copied().collect();
401 for owner_id in &affected_owner_ids {
402 if let Some(rel_ids) = owner_rel_before.get(owner_id) {
403 let updated: Vec<EntityId> = rel_ids
404 .iter()
405 .copied()
406 .filter(|rid| !removed_set.contains(rid))
407 .collect();
408 self.set_relationships_in_owner(event_buffer, owner_id, &updated)?;
409 }
410 }
411 }
412
413 Ok(())
414 }
415 pub fn get_relationship(
416 &self,
417 id: &EntityId,
418 field: &EntityRelationshipField,
419 ) -> Result<Vec<EntityId>, RepositoryError> {
420 self.table.get_relationship(id, field)
421 }
422 pub fn get_relationship_many(
423 &self,
424 ids: &[EntityId],
425 field: &EntityRelationshipField,
426 ) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError> {
427 self.table.get_relationship_many(ids, field)
428 }
429 pub fn get_relationship_count(
430 &self,
431 id: &EntityId,
432 field: &EntityRelationshipField,
433 ) -> Result<usize, RepositoryError> {
434 self.table.get_relationship_count(id, field)
435 }
436 pub fn get_relationship_in_range(
437 &self,
438 id: &EntityId,
439 field: &EntityRelationshipField,
440 offset: usize,
441 limit: usize,
442 ) -> Result<Vec<EntityId>, RepositoryError> {
443 self.table
444 .get_relationship_in_range(id, field, offset, limit)
445 }
446 pub fn get_relationships_from_right_ids(
447 &self,
448 field: &EntityRelationshipField,
449 right_ids: &[EntityId],
450 ) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError> {
451 self.table
452 .get_relationships_from_right_ids(field, right_ids)
453 }
454
455 pub fn set_relationship_multi(
456 &mut self,
457 event_buffer: &mut EventBuffer,
458 field: &EntityRelationshipField,
459 relationships: Vec<(EntityId, Vec<EntityId>)>,
460 ) -> Result<(), RepositoryError> {
461 let all_right_ids: Vec<EntityId> = relationships
463 .iter()
464 .flat_map(|(_, ids)| ids.iter().copied())
465 .collect();
466 if !all_right_ids.is_empty() {
467 match field {
468 EntityRelationshipField::Fields => {
469 let child_repo =
470 repository_factory::write::create_field_repository(self.transaction)?;
471 let found = child_repo.get_multi(&all_right_ids)?;
472 let missing: Vec<_> = all_right_ids
473 .iter()
474 .zip(found.iter())
475 .filter(|(_, entity)| entity.is_none())
476 .map(|(id, _)| *id)
477 .collect();
478 if !missing.is_empty() {
479 return Err(RepositoryError::MissingRelationshipTarget {
480 operation: "set_relationship_multi",
481 ids: missing,
482 });
483 }
484 }
485 EntityRelationshipField::InheritsFrom => {
486 let child_repo =
487 repository_factory::write::create_entity_repository(self.transaction)?;
488 let found = child_repo.get_multi(&all_right_ids)?;
489 let missing: Vec<_> = all_right_ids
490 .iter()
491 .zip(found.iter())
492 .filter(|(_, entity)| entity.is_none())
493 .map(|(id, _)| *id)
494 .collect();
495 if !missing.is_empty() {
496 return Err(RepositoryError::MissingRelationshipTarget {
497 operation: "set_relationship_multi",
498 ids: missing,
499 });
500 }
501 }
502 EntityRelationshipField::Relationships => {
503 let child_repo = repository_factory::write::create_relationship_repository(
504 self.transaction,
505 )?;
506 let found = child_repo.get_multi(&all_right_ids)?;
507 let missing: Vec<_> = all_right_ids
508 .iter()
509 .zip(found.iter())
510 .filter(|(_, entity)| entity.is_none())
511 .map(|(id, _)| *id)
512 .collect();
513 if !missing.is_empty() {
514 return Err(RepositoryError::MissingRelationshipTarget {
515 operation: "set_relationship_multi",
516 ids: missing,
517 });
518 }
519 }
520 }
521 }
522 self.table
523 .set_relationship_multi(field, relationships.clone())?;
524 for (left_id, right_ids) in relationships {
525 event_buffer.push(Event {
526 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
527 ids: vec![left_id],
528 data: Some(format!(
529 "{}:{}",
530 field,
531 right_ids
532 .iter()
533 .map(|id| id.to_string())
534 .collect::<Vec<_>>()
535 .join(",")
536 )),
537 });
538 }
539 Ok(())
540 }
541
542 pub fn set_relationship(
543 &mut self,
544 event_buffer: &mut EventBuffer,
545 id: &EntityId,
546 field: &EntityRelationshipField,
547 right_ids: &[EntityId],
548 ) -> Result<(), RepositoryError> {
549 if !right_ids.is_empty() {
551 match field {
552 EntityRelationshipField::Fields => {
553 let child_repo =
554 repository_factory::write::create_field_repository(self.transaction)?;
555 let found = child_repo.get_multi(right_ids)?;
556 let missing: Vec<_> = right_ids
557 .iter()
558 .zip(found.iter())
559 .filter(|(_, entity)| entity.is_none())
560 .map(|(id, _)| *id)
561 .collect();
562 if !missing.is_empty() {
563 return Err(RepositoryError::MissingRelationshipTarget {
564 operation: "set_relationship",
565 ids: missing,
566 });
567 }
568 }
569 EntityRelationshipField::InheritsFrom => {
570 let child_repo =
571 repository_factory::write::create_entity_repository(self.transaction)?;
572 let found = child_repo.get_multi(right_ids)?;
573 let missing: Vec<_> = right_ids
574 .iter()
575 .zip(found.iter())
576 .filter(|(_, entity)| entity.is_none())
577 .map(|(id, _)| *id)
578 .collect();
579 if !missing.is_empty() {
580 return Err(RepositoryError::MissingRelationshipTarget {
581 operation: "set_relationship",
582 ids: missing,
583 });
584 }
585 }
586 EntityRelationshipField::Relationships => {
587 let child_repo = repository_factory::write::create_relationship_repository(
588 self.transaction,
589 )?;
590 let found = child_repo.get_multi(right_ids)?;
591 let missing: Vec<_> = right_ids
592 .iter()
593 .zip(found.iter())
594 .filter(|(_, entity)| entity.is_none())
595 .map(|(id, _)| *id)
596 .collect();
597 if !missing.is_empty() {
598 return Err(RepositoryError::MissingRelationshipTarget {
599 operation: "set_relationship",
600 ids: missing,
601 });
602 }
603 }
604 }
605 }
606 self.table.set_relationship(id, field, right_ids)?;
607 event_buffer.push(Event {
608 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
609 ids: vec![*id],
610 data: Some(format!(
611 "{}:{}",
612 field,
613 right_ids
614 .iter()
615 .map(|id| id.to_string())
616 .collect::<Vec<_>>()
617 .join(",")
618 )),
619 });
620 Ok(())
621 }
622
623 pub fn move_relationship_ids(
624 &mut self,
625 event_buffer: &mut EventBuffer,
626 id: &EntityId,
627 field: &EntityRelationshipField,
628 ids_to_move: &[EntityId],
629 new_index: i32,
630 ) -> Result<Vec<EntityId>, RepositoryError> {
631 let reordered = self
632 .table
633 .move_relationship_ids(id, field, ids_to_move, new_index)?;
634 event_buffer.push(Event {
635 origin: Origin::DirectAccess(DirectAccessEntity::Entity(EntityEvent::Updated)),
636 ids: vec![*id],
637 data: Some(format!(
638 "{}:{}",
639 field,
640 reordered
641 .iter()
642 .map(|id| id.to_string())
643 .collect::<Vec<_>>()
644 .join(",")
645 )),
646 });
647 Ok(reordered)
648 }
649 pub fn get_relationships_from_owner(
650 &self,
651 owner_id: &EntityId,
652 ) -> Result<Vec<EntityId>, RepositoryError> {
653 let repo = repository_factory::write::create_workspace_repository(self.transaction)?;
654 repo.get_relationship(owner_id, &WorkspaceRelationshipField::Entities)
655 }
656
657 pub fn set_relationships_in_owner(
658 &mut self,
659 event_buffer: &mut EventBuffer,
660 owner_id: &EntityId,
661 ids: &[EntityId],
662 ) -> Result<(), RepositoryError> {
663 let mut repo = repository_factory::write::create_workspace_repository(self.transaction)?;
664 repo.set_relationship(
665 event_buffer,
666 owner_id,
667 &WorkspaceRelationshipField::Entities,
668 ids,
669 )
670 }
671
672 pub fn snapshot(&self, _ids: &[EntityId]) -> Result<EntityTreeSnapshot, RepositoryError> {
673 let store_snap = self.transaction.snapshot_store();
674 Ok(EntityTreeSnapshot {
675 store_snapshot: Some(store_snap),
676 })
677 }
678
679 pub fn restore(
680 &mut self,
681 event_buffer: &mut EventBuffer,
682 snap: &EntityTreeSnapshot,
683 ) -> Result<(), RepositoryError> {
684 let store_snap = snap
685 .store_snapshot
686 .as_ref()
687 .ok_or_else(|| RepositoryError::Serialization("missing store snapshot".into()))?;
688 self.transaction.restore_store(store_snap);
689
690 let store = self.transaction.get_store();
691
692 let mut emit = |entity: DirectAccessEntity, ids: Vec<EntityId>| {
693 if !ids.is_empty() {
694 event_buffer.push(Event {
695 origin: Origin::DirectAccess(entity),
696 ids,
697 data: None,
698 });
699 }
700 };
701
702 let entity_ids: Vec<_> = store.entitys.read().unwrap().keys().copied().collect();
704 emit(
705 DirectAccessEntity::Entity(EntityEvent::Created),
706 entity_ids.clone(),
707 );
708 emit(DirectAccessEntity::Entity(EntityEvent::Updated), entity_ids);
709
710 {
713 let child_ids: Vec<_> = store.fields.read().unwrap().keys().copied().collect();
714 emit(
715 DirectAccessEntity::Field(EntityEvent::Created),
716 child_ids.clone(),
717 );
718 emit(DirectAccessEntity::Field(EntityEvent::Updated), child_ids);
719 }
720 {
721 let child_ids: Vec<_> = store
722 .relationships
723 .read()
724 .unwrap()
725 .keys()
726 .copied()
727 .collect();
728 emit(
729 DirectAccessEntity::Relationship(EntityEvent::Created),
730 child_ids.clone(),
731 );
732 emit(
733 DirectAccessEntity::Relationship(EntityEvent::Updated),
734 child_ids,
735 );
736 }
737
738 Ok(())
739 }
740}
741
742pub struct EntityRepositoryRO<'a> {
743 table: Box<dyn EntityTableRO + 'a>,
744}
745impl<'a> EntityRepositoryRO<'a> {
746 pub fn new(table: Box<dyn EntityTableRO + 'a>) -> Self {
747 EntityRepositoryRO { table }
748 }
749 pub fn get(&self, id: &EntityId) -> Result<Option<Entity>, RepositoryError> {
750 self.table.get(id)
751 }
752 pub fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Entity>>, RepositoryError> {
753 self.table.get_multi(ids)
754 }
755 pub fn get_all(&self) -> Result<Vec<Entity>, RepositoryError> {
756 self.table.get_all()
757 }
758 pub fn get_relationship(
759 &self,
760 id: &EntityId,
761 field: &EntityRelationshipField,
762 ) -> Result<Vec<EntityId>, RepositoryError> {
763 self.table.get_relationship(id, field)
764 }
765 pub fn get_relationship_many(
766 &self,
767 ids: &[EntityId],
768 field: &EntityRelationshipField,
769 ) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>, RepositoryError> {
770 self.table.get_relationship_many(ids, field)
771 }
772 pub fn get_relationship_count(
773 &self,
774 id: &EntityId,
775 field: &EntityRelationshipField,
776 ) -> Result<usize, RepositoryError> {
777 self.table.get_relationship_count(id, field)
778 }
779 pub fn get_relationship_in_range(
780 &self,
781 id: &EntityId,
782 field: &EntityRelationshipField,
783 offset: usize,
784 limit: usize,
785 ) -> Result<Vec<EntityId>, RepositoryError> {
786 self.table
787 .get_relationship_in_range(id, field, offset, limit)
788 }
789 pub fn get_relationships_from_right_ids(
790 &self,
791 field: &EntityRelationshipField,
792 right_ids: &[EntityId],
793 ) -> Result<Vec<(EntityId, Vec<EntityId>)>, RepositoryError> {
794 self.table
795 .get_relationships_from_right_ids(field, right_ids)
796 }
797}