1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use super::def::GlobalStateAccessMode;
use super::output_request::*;
use crate::GlobalStateCategory;
use cyfs_base::*;

use std::sync::Arc;

#[async_trait::async_trait]
pub trait GlobalStateOutputProcessor: Sync + Send + 'static {
    fn get_category(&self) -> GlobalStateCategory;

    async fn get_current_root(
        &self,
        req: RootStateGetCurrentRootOutputRequest,
    ) -> BuckyResult<RootStateGetCurrentRootOutputResponse>;

    async fn create_op_env(
        &self,
        req: RootStateCreateOpEnvOutputRequest,
    ) -> BuckyResult<OpEnvOutputProcessorRef>;
}

pub type GlobalStateOutputProcessorRef = Arc<Box<dyn GlobalStateOutputProcessor>>;

#[async_trait::async_trait]
pub trait OpEnvOutputProcessor: Sync + Send + 'static {
    // 获取当前op_env的托管sid
    fn get_sid(&self) -> u64;

    fn get_category(&self) -> GlobalStateCategory;

    // single_op_env methods
    async fn load(&self, req: OpEnvLoadOutputRequest) -> BuckyResult<()>;

    async fn load_by_path(&self, req: OpEnvLoadByPathOutputRequest) -> BuckyResult<()>;

    async fn create_new(&self, req: OpEnvCreateNewOutputRequest) -> BuckyResult<()>;

    // get_current_root
    async fn get_current_root(
        &self,
        req: OpEnvGetCurrentRootOutputRequest,
    ) -> BuckyResult<OpEnvGetCurrentRootOutputResponse>;

    // lock and transcation
    async fn lock(&self, req: OpEnvLockOutputRequest) -> BuckyResult<()>;
    async fn commit(&self, req: OpEnvCommitOutputRequest)
        -> BuckyResult<OpEnvCommitOutputResponse>;
    async fn abort(&self, req: OpEnvAbortOutputRequest) -> BuckyResult<()>;

    // metadata
    async fn metadata(
        &self,
        req: OpEnvMetadataOutputRequest,
    ) -> BuckyResult<OpEnvMetadataOutputResponse>;

    // map methods
    async fn get_by_key(
        &self,
        req: OpEnvGetByKeyOutputRequest,
    ) -> BuckyResult<OpEnvGetByKeyOutputResponse>;
    async fn insert_with_key(&self, req: OpEnvInsertWithKeyOutputRequest) -> BuckyResult<()>;
    async fn set_with_key(
        &self,
        req: OpEnvSetWithKeyOutputRequest,
    ) -> BuckyResult<OpEnvSetWithKeyOutputResponse>;

    async fn remove_with_key(
        &self,
        req: OpEnvRemoveWithKeyOutputRequest,
    ) -> BuckyResult<OpEnvRemoveWithKeyOutputResponse>;

    // set methods
    async fn contains(
        &self,
        req: OpEnvContainsOutputRequest,
    ) -> BuckyResult<OpEnvContainsOutputResponse>;
    async fn insert(&self, req: OpEnvInsertOutputRequest)
        -> BuckyResult<OpEnvInsertOutputResponse>;
    async fn remove(&self, req: OpEnvRemoveOutputRequest)
        -> BuckyResult<OpEnvRemoveOutputResponse>;

    // iterator methods
    async fn next(&self, req: OpEnvNextOutputRequest) -> BuckyResult<OpEnvNextOutputResponse>;
    async fn reset(&self, req: OpEnvResetOutputRequest) -> BuckyResult<()>;

    async fn list(&self, req: OpEnvListOutputRequest) -> BuckyResult<OpEnvListOutputResponse>;
}

pub type OpEnvOutputProcessorRef = Arc<Box<dyn OpEnvOutputProcessor>>;

#[async_trait::async_trait]
pub trait GlobalStateAccessorOutputProcessor: Sync + Send + 'static {
    async fn get_object_by_path(
        &self,
        req: RootStateAccessorGetObjectByPathOutputRequest,
    ) -> BuckyResult<RootStateAccessorGetObjectByPathOutputResponse>;

    async fn list(
        &self,
        req: RootStateAccessorListOutputRequest,
    ) -> BuckyResult<RootStateAccessorListOutputResponse>;
}

pub type GlobalStateAccessorOutputProcessorRef = Arc<Box<dyn GlobalStateAccessorOutputProcessor>>;

#[async_trait::async_trait]
pub trait GlobalStateRawProcessor: Send + Sync {
    fn isolate_id(&self) -> &ObjectId;

    fn category(&self) -> GlobalStateCategory;

    fn access_mode(&self) -> GlobalStateAccessMode;

    // return (global_root, revision)
    fn get_current_root(&self) -> (ObjectId, u64);

    fn get_root_revision(&self, root: &ObjectId) -> Option<u64>;

    fn root_cache(&self) -> &ObjectMapRootCacheRef;

    fn is_dec_exists(&self, dec_id: &ObjectId) -> bool;

    // return (global_root, revision, dec_root)
    async fn get_dec_root(
        &self,
        dec_id: &ObjectId,
    ) -> BuckyResult<Option<(ObjectId, u64, ObjectId)>>;

    async fn get_dec_root_manager(
        &self,
        dec_id: &ObjectId,
        auto_create: bool,
    ) -> BuckyResult<ObjectMapRootManagerRef>;
}

pub type GlobalStateRawProcessorRef = Arc<Box<dyn GlobalStateRawProcessor>>;

#[async_trait::async_trait]
pub trait GlobalStateManagerRawProcessor: Send + Sync {
    // get relate methods
    async fn get_root_state(&self, isolate_id: &ObjectId) -> Option<GlobalStateRawProcessorRef> {
        self.get_global_state(GlobalStateCategory::RootState, isolate_id)
            .await
    }

    async fn get_local_cache(&self, isolate_id: &ObjectId) -> Option<GlobalStateRawProcessorRef> {
        self.get_global_state(GlobalStateCategory::LocalCache, isolate_id)
            .await
    }

    async fn get_global_state(
        &self,
        category: GlobalStateCategory,
        isolate_id: &ObjectId,
    ) -> Option<GlobalStateRawProcessorRef>;

    // loa relate methods
    async fn load_root_state(
        &self,
        isolate_id: &ObjectId,
        owner: Option<ObjectId>,
        auto_create: bool,
    ) -> BuckyResult<Option<GlobalStateRawProcessorRef>> {
        self.load_global_state(GlobalStateCategory::RootState, isolate_id, owner, auto_create).await
    }

    async fn load_local_cache(
        &self,
        isolate_id: &ObjectId,
        owner: Option<ObjectId>,
        auto_create: bool,
    ) -> BuckyResult<Option<GlobalStateRawProcessorRef>> {
        self.load_global_state(GlobalStateCategory::LocalCache, isolate_id, owner, auto_create).await
    }

    async fn load_global_state(
        &self,
        category: GlobalStateCategory,
        isolate_id: &ObjectId,
        owner: Option<ObjectId>,
        auto_create: bool,
    ) -> BuckyResult<Option<GlobalStateRawProcessorRef>>;
}

pub type GlobalStateManagerRawProcessorRef = Arc<Box<dyn GlobalStateManagerRawProcessor>>;

/*
async fn usage(state_manager: GlobalStateManagerRawProcessorRef) -> BuckyResult<()> {
    let isolate_id = ObjectId::default();
    let owner = Some(PeopleId::default().object_id().clone());
    let group_state = state_manager.load_root_state(&isolate_id, owner, true).await?.unwrap();

    // get dec's root, create if not exists
    let dec_id = cyfs_core::DecAppId::default();
    let dec_group_state = group_state.get_dec_root_manager(dec_id.object_id(), true).await?;

    let op_env = dec_group_state.create_op_env(None).unwrap();
    // do something with op_env
    op_env.commit().await.unwrap();

    Ok(())
}
*/