cyfs_lib/root_state/
processor.rs

1use super::def::GlobalStateAccessMode;
2use super::output_request::*;
3use crate::GlobalStateCategory;
4use cyfs_base::*;
5
6use std::sync::Arc;
7
8#[async_trait::async_trait]
9pub trait GlobalStateOutputProcessor: Sync + Send + 'static {
10    fn get_category(&self) -> GlobalStateCategory;
11
12    async fn get_current_root(
13        &self,
14        req: RootStateGetCurrentRootOutputRequest,
15    ) -> BuckyResult<RootStateGetCurrentRootOutputResponse>;
16
17    async fn create_op_env(
18        &self,
19        req: RootStateCreateOpEnvOutputRequest,
20    ) -> BuckyResult<OpEnvOutputProcessorRef>;
21}
22
23pub type GlobalStateOutputProcessorRef = Arc<Box<dyn GlobalStateOutputProcessor>>;
24
25#[async_trait::async_trait]
26pub trait OpEnvOutputProcessor: Sync + Send + 'static {
27    // 获取当前op_env的托管sid
28    fn get_sid(&self) -> u64;
29
30    fn get_category(&self) -> GlobalStateCategory;
31
32    // single_op_env methods
33    async fn load(&self, req: OpEnvLoadOutputRequest) -> BuckyResult<()>;
34
35    async fn load_by_path(&self, req: OpEnvLoadByPathOutputRequest) -> BuckyResult<()>;
36
37    async fn create_new(&self, req: OpEnvCreateNewOutputRequest) -> BuckyResult<()>;
38
39    // get_current_root
40    async fn get_current_root(
41        &self,
42        req: OpEnvGetCurrentRootOutputRequest,
43    ) -> BuckyResult<OpEnvGetCurrentRootOutputResponse>;
44
45    // lock and transcation
46    async fn lock(&self, req: OpEnvLockOutputRequest) -> BuckyResult<()>;
47    async fn commit(&self, req: OpEnvCommitOutputRequest)
48        -> BuckyResult<OpEnvCommitOutputResponse>;
49    async fn abort(&self, req: OpEnvAbortOutputRequest) -> BuckyResult<()>;
50
51    // metadata
52    async fn metadata(
53        &self,
54        req: OpEnvMetadataOutputRequest,
55    ) -> BuckyResult<OpEnvMetadataOutputResponse>;
56
57    // map methods
58    async fn get_by_key(
59        &self,
60        req: OpEnvGetByKeyOutputRequest,
61    ) -> BuckyResult<OpEnvGetByKeyOutputResponse>;
62    async fn insert_with_key(&self, req: OpEnvInsertWithKeyOutputRequest) -> BuckyResult<()>;
63    async fn set_with_key(
64        &self,
65        req: OpEnvSetWithKeyOutputRequest,
66    ) -> BuckyResult<OpEnvSetWithKeyOutputResponse>;
67
68    async fn remove_with_key(
69        &self,
70        req: OpEnvRemoveWithKeyOutputRequest,
71    ) -> BuckyResult<OpEnvRemoveWithKeyOutputResponse>;
72
73    // set methods
74    async fn contains(
75        &self,
76        req: OpEnvContainsOutputRequest,
77    ) -> BuckyResult<OpEnvContainsOutputResponse>;
78    async fn insert(&self, req: OpEnvInsertOutputRequest)
79        -> BuckyResult<OpEnvInsertOutputResponse>;
80    async fn remove(&self, req: OpEnvRemoveOutputRequest)
81        -> BuckyResult<OpEnvRemoveOutputResponse>;
82
83    // iterator methods
84    async fn next(&self, req: OpEnvNextOutputRequest) -> BuckyResult<OpEnvNextOutputResponse>;
85    async fn reset(&self, req: OpEnvResetOutputRequest) -> BuckyResult<()>;
86
87    async fn list(&self, req: OpEnvListOutputRequest) -> BuckyResult<OpEnvListOutputResponse>;
88}
89
90pub type OpEnvOutputProcessorRef = Arc<Box<dyn OpEnvOutputProcessor>>;
91
92#[async_trait::async_trait]
93pub trait GlobalStateAccessorOutputProcessor: Sync + Send + 'static {
94    async fn get_object_by_path(
95        &self,
96        req: RootStateAccessorGetObjectByPathOutputRequest,
97    ) -> BuckyResult<RootStateAccessorGetObjectByPathOutputResponse>;
98
99    async fn list(
100        &self,
101        req: RootStateAccessorListOutputRequest,
102    ) -> BuckyResult<RootStateAccessorListOutputResponse>;
103}
104
105pub type GlobalStateAccessorOutputProcessorRef = Arc<Box<dyn GlobalStateAccessorOutputProcessor>>;
106
107#[derive(Clone, Debug)]
108pub struct GlobalStateDecRootInfo {
109    pub dec_id: ObjectId,
110    pub dec_root: ObjectId,
111}
112
113#[derive(Clone, Debug)]
114pub struct GlobalStateRootInfo {
115    pub global_root: ObjectId,
116    pub revision: u64,
117
118    pub dec_list: Vec<GlobalStateDecRootInfo>,
119}
120
121#[async_trait::async_trait]
122pub trait GlobalStateRawProcessor: Send + Sync {
123    fn isolate_id(&self) -> &ObjectId;
124
125    fn category(&self) -> GlobalStateCategory;
126
127    fn access_mode(&self) -> GlobalStateAccessMode;
128
129    // return (global_root, revision)
130    fn get_current_root(&self) -> (ObjectId, u64);
131
132    fn get_root_revision(&self, root: &ObjectId) -> Option<u64>;
133
134    fn root_cache(&self) -> &ObjectMapRootCacheRef;
135
136    fn is_dec_exists(&self, dec_id: &ObjectId) -> bool;
137
138    async fn get_dec_root_info_list(&self) -> BuckyResult<GlobalStateRootInfo>;
139
140    // return (global_root, revision, dec_root)
141    async fn get_dec_root(
142        &self,
143        dec_id: &ObjectId,
144    ) -> BuckyResult<Option<(ObjectId, u64, ObjectId)>>;
145
146    async fn get_dec_root_manager(
147        &self,
148        dec_id: &ObjectId,
149        auto_create: bool,
150    ) -> BuckyResult<ObjectMapRootManagerRef>;
151}
152
153pub type GlobalStateRawProcessorRef = Arc<Box<dyn GlobalStateRawProcessor>>;
154
155#[derive(Clone, Debug)]
156pub struct GlobalStateIsolateInfo {
157    pub isolate_id: ObjectId,
158    pub owner: Option<ObjectId>,
159    pub create_time: u64,
160}
161
162#[async_trait::async_trait]
163pub trait GlobalStateManagerRawProcessor: Send + Sync {
164    // get all isolates of specified category
165    async fn get_isolate_list(&self, category: GlobalStateCategory) -> Vec<GlobalStateIsolateInfo>;
166
167    // get relate methods
168    async fn get_root_state(&self, isolate_id: &ObjectId) -> Option<GlobalStateRawProcessorRef> {
169        self.get_global_state(GlobalStateCategory::RootState, isolate_id)
170            .await
171    }
172
173    async fn get_local_cache(&self, isolate_id: &ObjectId) -> Option<GlobalStateRawProcessorRef> {
174        self.get_global_state(GlobalStateCategory::LocalCache, isolate_id)
175            .await
176    }
177
178    async fn get_global_state(
179        &self,
180        category: GlobalStateCategory,
181        isolate_id: &ObjectId,
182    ) -> Option<GlobalStateRawProcessorRef>;
183
184    // loa relate methods
185    async fn load_root_state(
186        &self,
187        isolate_id: &ObjectId,
188        owner: Option<ObjectId>,
189        auto_create: bool,
190    ) -> BuckyResult<Option<GlobalStateRawProcessorRef>> {
191        self.load_global_state(
192            GlobalStateCategory::RootState,
193            isolate_id,
194            owner,
195            auto_create,
196        )
197        .await
198    }
199
200    async fn load_local_cache(
201        &self,
202        isolate_id: &ObjectId,
203        owner: Option<ObjectId>,
204        auto_create: bool,
205    ) -> BuckyResult<Option<GlobalStateRawProcessorRef>> {
206        self.load_global_state(
207            GlobalStateCategory::LocalCache,
208            isolate_id,
209            owner,
210            auto_create,
211        )
212        .await
213    }
214
215    async fn load_global_state(
216        &self,
217        category: GlobalStateCategory,
218        isolate_id: &ObjectId,
219        owner: Option<ObjectId>,
220        auto_create: bool,
221    ) -> BuckyResult<Option<GlobalStateRawProcessorRef>>;
222}
223
224pub type GlobalStateManagerRawProcessorRef = Arc<Box<dyn GlobalStateManagerRawProcessor>>;
225
226/*
227async fn usage(state_manager: GlobalStateManagerRawProcessorRef) -> BuckyResult<()> {
228    let isolate_id = ObjectId::default();
229    let owner = Some(PeopleId::default().object_id().clone());
230    let group_state = state_manager.load_root_state(&isolate_id, owner, true).await?.unwrap();
231
232    // get dec's root, create if not exists
233    let dec_id = cyfs_core::DecAppId::default();
234    let dec_group_state = group_state.get_dec_root_manager(dec_id.object_id(), true).await?;
235
236    let op_env = dec_group_state.create_op_env(None).unwrap();
237    // do something with op_env
238    op_env.commit().await.unwrap();
239
240    Ok(())
241}
242*/