cyfs_lib/root_state/
cache.rs

1use crate::*;
2use cyfs_base::*;
3
4use std::sync::Arc;
5
6pub struct ObjectMapNOCCacheAdapter {
7    noc: NamedObjectCacheRef,
8}
9
10impl ObjectMapNOCCacheAdapter {
11    pub fn new(noc: NamedObjectCacheRef) -> Self {
12        Self { noc }
13    }
14
15    pub fn new_noc_cache(noc: NamedObjectCacheRef) -> ObjectMapNOCCacheRef {
16        let ret = Self::new(noc);
17        Arc::new(Box::new(ret) as Box<dyn ObjectMapNOCCache>)
18    }
19}
20
21#[async_trait::async_trait]
22impl ObjectMapNOCCache for ObjectMapNOCCacheAdapter {
23    async fn exists(&self, dec_id: Option<ObjectId>, object_id: &ObjectId) -> BuckyResult<bool> {
24        let noc_req = NamedObjectCacheExistsObjectRequest {
25            object_id: object_id.clone(),
26            source: RequestSourceInfo::new_local_dec_or_system(dec_id),
27        };
28
29        let resp = self.noc.exists_object(&noc_req).await.map_err(|e| {
30            error!("exists object map from noc error! id={}, {}", object_id, e);
31            e
32        })?;
33
34        if resp.meta && resp.object {
35            Ok(true)
36        } else {
37            Ok(false)
38        }
39    }
40
41    async fn get_object_map_ex(
42        &self,
43        dec_id: Option<ObjectId>,
44        object_id: &ObjectId,
45    ) -> BuckyResult<Option<ObjectMapCacheItem>> {
46        let noc_req = NamedObjectCacheGetObjectRequest {
47            source: RequestSourceInfo::new_local_dec_or_system(dec_id),
48            object_id: object_id.clone(),
49            last_access_rpath: None,
50            flags: 0,
51        };
52
53        let resp = self.noc.get_object(&noc_req).await.map_err(|e| {
54            error!("load object map from noc error! id={}, {}", object_id, e);
55            e
56        })?;
57
58        match resp {
59            Some(resp) => {
60                match ObjectMap::raw_decode(&resp.object.object_raw) {
61                    Ok((object, _)) => {
62                        // 首次加载后,直接设置id缓存,减少一次id计算
63                        object.direct_set_object_id_on_init(object_id);
64
65                        let item = ObjectMapCacheItem {
66                            object,
67                            access: AccessString::new(resp.meta.access_string),
68                        };
69                        Ok(Some(item))
70                    }
71                    Err(e) => {
72                        error!("decode ObjectMap object error: id={}, {}", object_id, e);
73                        Err(e)
74                    }
75                }
76            }
77            None => Ok(None),
78        }
79    }
80
81    async fn put_object_map(
82        &self,
83        dec_id: Option<ObjectId>,
84        object_id: ObjectId,
85        object: ObjectMap,
86        access: Option<AccessString>,
87    ) -> BuckyResult<()> {
88        let object_raw = object.to_vec().unwrap();
89        let object = AnyNamedObject::Standard(StandardObject::ObjectMap(object));
90        let object = NONObjectInfo::new(object_id, object_raw, Some(Arc::new(object)));
91
92        let req = NamedObjectCachePutObjectRequest {
93            source: RequestSourceInfo::new_local_dec_or_system(dec_id),
94            object,
95            storage_category: NamedObjectStorageCategory::Storage,
96            context: None,
97            last_access_rpath: None,
98            access_string: access.map(|v| v.value()),
99        };
100
101        self.noc.put_object(&req).await.map_err(|e| {
102            error!(
103                "insert object map to noc error! id={}, dec={:?}, {}",
104                object_id, dec_id, e
105            );
106            e
107        })?;
108
109        Ok(())
110    }
111}