cyfs_lib/traversal/
local_loader.rs

1use super::traverser::*;
2use crate::*;
3use cyfs_base::*;
4use cyfs_bdt::ChunkReaderRef;
5use cyfs_util::AsyncReadWithSeek;
6
7use std::sync::Arc;
8
9pub struct ObjectTraverserLocalLoader {
10    noc: NamedObjectCacheRef,
11    chunk_store: ChunkReaderRef,
12}
13
14impl ObjectTraverserLocalLoader {
15    pub fn new(noc: NamedObjectCacheRef, chunk_store: ChunkReaderRef) -> Self {
16        Self { noc, chunk_store }
17    }
18
19    pub fn into_reader(self) -> ObjectTraverserLoaderRef {
20        Arc::new(Box::new(self))
21    }
22}
23
24#[async_trait::async_trait]
25impl ObjectTraverserLoader for ObjectTraverserLocalLoader {
26    async fn get_object(
27        &self,
28        object_id: &ObjectId,
29    ) -> BuckyResult<Option<ObjectTraverserLoaderObjectData>> {
30        let mut req = NamedObjectCacheGetObjectRequest {
31            source: RequestSourceInfo::new_local_system(),
32            object_id: object_id.to_owned(),
33            last_access_rpath: None,
34            flags: 0,
35        };
36        req.set_no_update_last_access();
37        
38        match self.noc.get_object(&req).await {
39            Ok(Some(data)) => {
40                let ret = ObjectTraverserLoaderObjectData {
41                    object: data.object,
42                    meta: Some(data.meta),
43                };
44
45                Ok(Some(ret))
46            }
47            Ok(None) => {
48                warn!("traverser get object from noc but not found! {}", object_id);
49                Ok(None)
50            }
51            Err(e) => {
52                error!("traverser get object from noc error! {}, {}", object_id, e);
53                Err(e)
54            }
55        }
56    }
57
58    async fn get_chunk(
59        &self,
60        chunk_id: &ChunkId,
61    ) -> BuckyResult<Option<Box<dyn AsyncReadWithSeek + Unpin + Send + Sync>>> {
62        match self.chunk_store.get(chunk_id).await {
63            Ok(chunk) => Ok(Some(chunk)),
64            Err(e) if e.code() == BuckyErrorCode::NotFound => {
65                warn!(
66                    "traverser get chunk from chunk store but not found! {}",
67                    chunk_id
68                );
69                Ok(None)
70            }
71            Err(e) => {
72                error!(
73                    "traverser get chunk from chunk store error! {}, {}",
74                    chunk_id, e
75                );
76                Err(e)
77            }
78        }
79    }
80}