Skip to main content

common/direct_access/file/
file_repository.rs

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