cyfs_lib/traversal/object/
object_map.rs

1use super::def::*;
2use cyfs_base::*;
3
4pub struct ObjectMapTraverser {
5    cache: ObjectMapOpEnvCacheRef,
6    current: NormalObject,
7    cb: ObjectTraverserCallBackRef,
8}
9
10impl ObjectMapTraverser {
11    pub fn new(
12        cache: ObjectMapOpEnvCacheRef,
13        current: NormalObject,
14        cb: ObjectTraverserCallBackRef,
15    ) -> Self {
16        Self { cache, current, cb }
17    }
18
19    pub async fn tranverse(self) -> BuckyResult<()> {
20        let target = self.current.object.object_id.clone();
21        let mut visitor = ObjectMapFullVisitor::new(Box::new(self));
22        visitor.visit(&target).await
23    }
24}
25
26#[async_trait::async_trait]
27impl ObjectMapVisitor for ObjectMapTraverser {
28    async fn visit_hub_item(&mut self, item: &ObjectId) -> BuckyResult<()> {
29        trace!("visit hub item: {}", item);
30
31        let obj = SubObject {
32            object_id: item.to_owned(),
33        };
34        let item = TraverseObjectItem::Sub(obj);
35        self.cb.on_object(item).await
36    }
37
38    async fn visit_map_item(&mut self, key: &str, item: &ObjectId) -> BuckyResult<()> {
39        trace!(
40            "visit map item: {}={}, {:?}",
41            key,
42            item,
43            item.obj_type_code()
44        );
45
46        let obj = self
47            .current
48            .derive_normal(item.to_owned(), Some(key), false);
49        let item = TraverseObjectItem::Normal(obj);
50        self.cb.on_object(item).await
51    }
52
53    async fn visit_set_item(&mut self, item: &ObjectId) -> BuckyResult<()> {
54        trace!("visit set item: {}, {:?}", item, item.obj_type_code());
55
56        let obj = self.current.derive_normal(item.to_owned(), None, false);
57        let item = TraverseObjectItem::Normal(obj);
58        self.cb.on_object(item).await
59    }
60
61    async fn visit_diff_map_item(
62        &mut self,
63        key: &str,
64        item: &ObjectMapDiffMapItem,
65    ) -> BuckyResult<()> {
66        trace!("visit diff map item: {}={}", key, item);
67
68        // FIXME prev = altered - diff, should keep in touch here?;
69
70        if let Some(id) = &item.diff {
71            let obj = self.current.derive_normal(id.to_owned(), Some(key), false);
72            let item = TraverseObjectItem::Normal(obj);
73            self.cb.on_object(item).await?;
74        }
75
76        if let Some(altered) = &item.altered {
77            let obj = self
78                .current
79                .derive_normal(altered.to_owned(), Some(key), false);
80            let item = TraverseObjectItem::Normal(obj);
81            self.cb.on_object(item).await?;
82        }
83
84        Ok(())
85    }
86
87    async fn visit_diff_set_item(&mut self, item: &ObjectMapDiffSetItem) -> BuckyResult<()> {
88        trace!("visit diff set item: {}", item);
89
90        if let Some(altered) = &item.altered {
91            let obj = self.current.derive_normal(altered.to_owned(), None, false);
92            let item = TraverseObjectItem::Normal(obj);
93            self.cb.on_object(item).await?;
94        }
95
96        Ok(())
97    }
98}
99
100#[async_trait::async_trait]
101impl ObjectMapVisitLoader for ObjectMapTraverser {
102    fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
103        self
104    }
105
106    async fn get_object_map(&mut self, id: &ObjectId) -> BuckyResult<Option<ObjectMapRef>> {
107        self.cache.get_object_map(id).await
108    }
109}
110
111impl ObjectMapVisitorProvider for ObjectMapTraverser {}