cyfs-lib 0.8.3

Rust cyfs-lib package
Documentation
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::*;
use cyfs_base::*;

use serde::{Deserialize, Serialize};
use std::sync::Arc;

// Whether the delete operation returns the original value, the default does not return
pub const CYFS_NOC_FLAG_DELETE_WITH_QUERY: u32 = 0x01 << 1;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NamedObjectStorageCategory {
    Storage = 0,
    Cache = 1,
}

impl NamedObjectStorageCategory {
    pub fn as_u8(&self) -> u8 {
        *self as u8
    }
}

impl Default for NamedObjectStorageCategory {
    fn default() -> Self {
        Self::Cache
    }
}

impl TryFrom<u8> for NamedObjectStorageCategory {
    type Error = BuckyError;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        let v = match value {
            0 => Self::Storage,
            1 => Self::Cache,
            _ => {
                let msg = format!("invalid NamedObjectStorageCategory value: {}", value);
                error!("{}", msg);
                return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
            }
        };

        Ok(v)
    }
}

pub struct NamedObjectCachePutObjectRequest {
    pub source: RequestSourceInfo,
    pub object: NONObjectInfo,
    pub storage_category: NamedObjectStorageCategory,
    pub context: Option<String>,
    pub last_access_rpath: Option<String>,
    pub access_string: Option<u32>,
}

#[derive(Clone, Copy, Debug)]
pub enum NamedObjectCachePutObjectResult {
    Accept,
    AlreadyExists,
    Updated,
    Merged,
}

pub struct NamedObjectCachePutObjectResponse {
    pub result: NamedObjectCachePutObjectResult,
    pub update_time: Option<u64>,
    pub expires_time: Option<u64>,
}

// update object's related meta info (except meta info from object data)
#[derive(Debug)]
pub struct NamedObjectCacheUpdateObjectMetaRequest {
    pub source: RequestSourceInfo,
    pub object_id: ObjectId,

    pub storage_category: Option<NamedObjectStorageCategory>,
    pub context: Option<String>,
    pub last_access_rpath: Option<String>,
    pub access_string: Option<u32>,
}

impl NamedObjectCacheUpdateObjectMetaRequest {
    pub fn is_empty(&self) -> bool {
        self.storage_category.is_none()
            && self.context.is_none()
            && self.last_access_rpath.is_none()
            && self.access_string.is_none()
    }
}

pub const NAMED_OBJECT_CACHE_GET_OBJECT_FLAG_NO_UPDATE_LAST_ACCESS: u32 = 0x01;

// get_object
#[derive(Clone)]
pub struct NamedObjectCacheGetObjectRequest {
    pub source: RequestSourceInfo,

    pub object_id: ObjectId,

    pub last_access_rpath: Option<String>,

    pub flags: u32,
}

impl NamedObjectCacheGetObjectRequest {
    pub fn set_no_update_last_access(&mut self) {
        self.flags |= NAMED_OBJECT_CACHE_GET_OBJECT_FLAG_NO_UPDATE_LAST_ACCESS;
    }
    
    pub fn is_no_update_last_access(&self) -> bool {
        self.flags & NAMED_OBJECT_CACHE_GET_OBJECT_FLAG_NO_UPDATE_LAST_ACCESS == NAMED_OBJECT_CACHE_GET_OBJECT_FLAG_NO_UPDATE_LAST_ACCESS
    }
}

#[derive(Clone, Debug)]
pub struct NamedObjectMetaData {
    pub object_id: ObjectId,
    pub object_type: u16,

    pub owner_id: Option<ObjectId>,
    pub create_dec_id: ObjectId,

    // the item in noc's related times
    pub insert_time: u64,
    pub update_time: u64,

    // object's create_time, update_time and expired_time
    pub object_create_time: Option<u64>,
    pub object_update_time: Option<u64>,
    pub object_expired_time: Option<u64>,

    // object related fields
    pub author: Option<ObjectId>,
    pub dec_id: Option<ObjectId>,

    pub storage_category: NamedObjectStorageCategory,
    pub context: Option<String>,

    pub last_access_rpath: Option<String>,
    pub access_string: u32,
}

#[derive(Clone)]
pub struct NamedObjectCacheObjectRawData {
    // object maybe missing while meta info is still here
    pub object: Option<NONObjectInfo>,

    pub meta: NamedObjectMetaData,
}

#[derive(Clone)]
pub struct NamedObjectCacheObjectData {
    // object must be there
    pub object: NONObjectInfo,

    pub meta: NamedObjectMetaData,
}

// delete_object
#[derive(Clone)]
pub struct NamedObjectCacheDeleteObjectRequest {
    pub source: RequestSourceInfo,

    pub object_id: ObjectId,
    pub flags: u32,
}

#[derive(Clone)]
pub struct NamedObjectCacheDeleteObjectResponse {
    pub deleted_count: u32,

    // object maybe missing while meta info is still here
    pub object: Option<NONObjectInfo>,

    pub meta: Option<NamedObjectMetaData>,
}

// exists_object
pub struct NamedObjectCacheExistsObjectRequest {
    pub source: RequestSourceInfo,

    pub object_id: ObjectId,
}

pub struct NamedObjectCacheExistsObjectResponse {
    pub meta: bool,
    pub object: bool,
}

// check_access
pub struct NamedObjectCacheCheckObjectAccessRequest {
    pub source: RequestSourceInfo,

    pub object_id: ObjectId,
    pub required_access: AccessPermissions,
}

// stat
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamedObjectCacheStat {
    pub count: u64,
    pub storage_size: u64,
}

#[derive(Debug, Clone)]
pub struct NamedObjectCacheSelectObjectFilter {
    pub obj_type: Option<u16>,
}

impl Default for NamedObjectCacheSelectObjectFilter {
    fn default() -> Self {
        Self {
            obj_type: None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct NamedObjectCacheSelectObjectOption {
    // The number of readings per page
    pub page_size: usize,

    // The page number currently read, starting from 0
    pub page_index: usize,
}

impl Default for NamedObjectCacheSelectObjectOption {
    fn default() -> Self {
        Self {
            page_size: 1024,
            page_index: 0,
        }
    }
}

#[derive(Clone)]
pub struct NamedObjectCacheSelectObjectRequest {
    // filters
    pub filter: NamedObjectCacheSelectObjectFilter,

    // configs
    pub opt: NamedObjectCacheSelectObjectOption,
}

#[derive(Debug)]
pub struct NamedObjectCacheSelectObjectData {
    pub object_id: ObjectId,
}

#[derive(Debug)]
pub struct NamedObjectCacheSelectObjectResponse {
    pub list: Vec<NamedObjectCacheSelectObjectData>,
}

#[async_trait::async_trait]
pub trait NamedObjectCache: Sync + Send {
    async fn put_object(
        &self,
        req: &NamedObjectCachePutObjectRequest,
    ) -> BuckyResult<NamedObjectCachePutObjectResponse>;

    async fn get_object(
        &self,
        req: &NamedObjectCacheGetObjectRequest,
    ) -> BuckyResult<Option<NamedObjectCacheObjectData>> {
        match self.get_object_raw(req).await? {
            Some(ret) => match ret.object {
                Some(object) => Ok(Some(NamedObjectCacheObjectData {
                    object,
                    meta: ret.meta,
                })),
                None => {
                    warn!(
                        "get object meta from noc but object missing! {}",
                        req.object_id
                    );
                    Ok(None)
                }
            },
            None => Ok(None),
        }
    }

    async fn get_object_raw(
        &self,
        req: &NamedObjectCacheGetObjectRequest,
    ) -> BuckyResult<Option<NamedObjectCacheObjectRawData>>;

    async fn delete_object(
        &self,
        req: &NamedObjectCacheDeleteObjectRequest,
    ) -> BuckyResult<NamedObjectCacheDeleteObjectResponse>;

    async fn exists_object(
        &self,
        req: &NamedObjectCacheExistsObjectRequest,
    ) -> BuckyResult<NamedObjectCacheExistsObjectResponse>;

    async fn update_object_meta(
        &self,
        req: &NamedObjectCacheUpdateObjectMetaRequest,
    ) -> BuckyResult<()>;

    async fn check_object_access(
        &self,
        req: &NamedObjectCacheCheckObjectAccessRequest,
    ) -> BuckyResult<Option<()>>;

    async fn stat(&self) -> BuckyResult<NamedObjectCacheStat>;

    // for internal use only
    async fn select_object(
        &self,
        req: &NamedObjectCacheSelectObjectRequest,
    ) -> BuckyResult<NamedObjectCacheSelectObjectResponse>;

    fn bind_object_meta_access_provider(
        &self,
        object_meta_access_provider: NamedObjectCacheObjectMetaAccessProviderRef,
    );
}

pub type NamedObjectCacheRef = Arc<Box<dyn NamedObjectCache>>;

impl ObjectSelectorDataProvider for NamedObjectMetaData {
    fn object_id(&self) -> &ObjectId {
        &self.object_id
    }
    fn obj_type(&self) -> u16 {
        self.object_type
    }

    fn object_dec_id(&self) -> &Option<ObjectId> {
        &self.dec_id
    }
    fn object_author(&self) -> &Option<ObjectId> {
        &self.author
    }
    fn object_owner(&self) -> &Option<ObjectId> {
        &self.owner_id
    }

    fn object_create_time(&self) -> Option<u64> {
        self.object_create_time
    }
    fn object_update_time(&self) -> Option<u64> {
        self.object_update_time
    }
    fn object_expired_time(&self) -> Option<u64> {
        self.object_expired_time
    }

    fn update_time(&self) -> &u64 {
        &self.update_time
    }
    fn insert_time(&self) -> &u64 {
        &self.insert_time
    }
}

#[async_trait::async_trait]
pub trait NamedObjectCacheObjectMetaAccessProvider: Sync + Send {
    async fn check_access(
        &self,
        target_dec_id: &ObjectId,
        object_data: &dyn ObjectSelectorDataProvider,
        source: &RequestSourceInfo,
        permissions: AccessPermissions,
    ) -> BuckyResult<Option<()>>;
}

pub type NamedObjectCacheObjectMetaAccessProviderRef = Arc<Box<dyn NamedObjectCacheObjectMetaAccessProvider>>;

#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum NamedObjectRelationType {
    InnerPath = 0,
}

impl Into<u8> for NamedObjectRelationType {
    fn into(self) -> u8 {
        match self {
            Self::InnerPath => 0,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NamedObjectRelationCacheKey {
    pub object_id: ObjectId,
    pub relation_type: NamedObjectRelationType,
    pub relation: String,
}

#[derive(Clone, Debug)]
pub struct NamedObjectRelationCachePutRequest {
    pub cache_key: NamedObjectRelationCacheKey,
    pub target_object_id: Option<ObjectId>,
}

#[derive(Clone, Debug)]
pub struct NamedObjectRelationCacheGetRequest {
    pub cache_key: NamedObjectRelationCacheKey,
    pub flags: u32,
}

#[derive(Clone)]
pub struct NamedObjectRelationCacheData {
    pub target_object_id: Option<ObjectId>,
}

#[async_trait::async_trait]
pub trait NamedObjectRelationCache: Send + Sync {
    async fn put(&self, req: &NamedObjectRelationCachePutRequest) -> BuckyResult<()>;
    async fn get(&self, req: &NamedObjectRelationCacheGetRequest) -> BuckyResult<Option<NamedObjectRelationCacheData>>;
}

pub type NamedObjectRelationCacheRef = Arc<Box<dyn NamedObjectRelationCache>>;