mitrid_core 0.9.4

Core library of the Mitrid framework
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
//! # Store
//!
//! `store` is the module providing the traits implemented by stores and by types that
//! can be stored in and retrieved from a store.

use base::Result;
use base::Checkable;
use base::Datable;
use base::Serializable;
use base::{Eval, EvalMut};
use io::Permission;
use io::Session;

/// Trait representing the operations implemented by a store.
pub trait Store<S>
    where   S: Datable + Serializable,
            Self: 'static + Sized + Send + Sync + Checkable
{
    /// Retrieves a new `Session` from the store.
    fn session(&mut self, permission: &Permission) -> Result<Session<S>>;
    
    /// Counts the store items starting from the `from` key until, not included, the `to` key.
    fn count(&mut self, session: &Session<S>, from: Option<Vec<u8>>, to: Option<Vec<u8>>) -> Result<u64>;

    /// Counts the store items starting with the given prefix.
    fn count_prefix(&mut self,
                    session: &Session<S>,
                    prefix: &[u8])
        -> Result<u64>;
    
    /// Lists the store items starting from the `from` key until, not included, the `to` key.
    fn list(&mut self,
            session: &Session<S>,
            from: Option<Vec<u8>>,
            to: Option<Vec<u8>>,
            count: Option<u64>,
            skip: u64)
        -> Result<Vec<Vec<u8>>>;

    /// Lists the store items starting with the given prefix.
    fn list_prefix(&mut self,
                   session: &Session<S>,
                   prefix: &[u8],
                   count: Option<u64>,
                   skip: u64)
        -> Result<Vec<Vec<u8>>>;
    
    /// Lookups an item from its key.
    fn lookup(&mut self, session: &Session<S>, key: &[u8]) -> Result<bool>;
    
    /// Retrieves an item from its key. The item should already exist in the store before the operation.
    fn get(&mut self, session: &Session<S>, key: &[u8]) -> Result<Vec<u8>>;
    
    /// Creates an item in the store. The item should not exist in the store before the operation.
    fn create(&mut self, session: &Session<S>, key: &[u8], value: &[u8]) -> Result<()>;
    
    /// Updates an item in the store. The item should already exist in the store before the operation.
    fn update(&mut self, session: &Session<S>, key: &[u8], value: &[u8]) -> Result<()>;
    
    /// Creates an item in the store if absent, update it if present.
    fn upsert(&mut self, session: &Session<S>, key: &[u8], value: &[u8]) -> Result<()>;
    
    /// Deletes an item from the store. The item should already exist in the store before the operation.
    fn delete(&mut self, session: &Session<S>, key: &[u8]) -> Result<()>;
    
    /// Eval operation in the store.
    fn eval<E, P, R>(&mut self, session: &Session<S>, params: &P, evaluator: &E) -> Result<R>
        where   E: Eval<Self, P, R>,
                P: Datable,
                R: Datable
    {
        session.check()?;
        params.check()?;

        if session.is_expired()? {
            return Err(String::from("expired session"));
        }

        if session.permission > Permission::Read {
            return Err(String::from("invalid permission")).into();
        }

        evaluator.eval(self, params)
    }
    
    /// Evals mutably in the store.
    fn eval_mut<E, P, R>(&mut self, session: &Session<S>, params: &P, evaluator: &mut E) -> Result<R>
        where   E: EvalMut<Self, P, R>,
                P: Datable,
                R: Datable
    {
        session.check()?;
        params.check()?;

        if session.is_expired()? {
            return Err(String::from("expired session"));
        }

        if session.permission < Permission::Write {
            return Err(String::from("invalid permission")).into();
        }

        evaluator.eval_mut(self, params)
    }
}

/// Trait implemented by types that can be stored and retrieved from a store.
pub trait Storable<St, S, K, V>
    where   St: Store<S>,
            S: Datable + Serializable,
            K: Ord + Datable + Serializable,
            V: Datable + Serializable,
            Self: Datable + Serializable
{
    /// Returns the store prefix of the implementor.
    fn store_prefix() -> Vec<u8>;

    /// Returns the store key of the implementor.
    fn store_key(&self) -> Result<K>;

    /// Retrieves the store value of the implementor.
    fn store_value(&self) -> Result<V>;

    /// Retrieves an instance of the implementor from a store value.
    fn from_store_value(value: &[u8]) -> Result<Self> {
        Self::from_bytes(value)
    }
    
    /// Counts the store items starting from the `from` key until, not included, the `to` key.
    fn store_count(store: &mut St, from: Option<K>, to: Option<K>) -> Result<u64> {
        let permission = Permission::Read;

        let session = store.session(&permission)?;

        from.check()?;
        to.check()?;

        let prefix = Self::store_prefix();

        if from.is_none() && to.is_none() {
            return store.count_prefix(&session, &prefix);
        }

        if let Some(ref from) = from {
            if let Some(ref to) = to {
                if from >= to {
                    return Err(String::from("invalid range"));
                } 
            }
        }

        let store_from = if let Some(k) = from {
            let mut from_key = Vec::new();
            from_key.extend_from_slice(&prefix);
            from_key.extend(&k.to_bytes()?);

            Some(from_key)
        } else {
            None
        };

        let store_to = if let Some(k) = to {
            let mut to_key = Vec::new();
            to_key.extend_from_slice(&prefix);
            to_key.extend(&k.to_bytes()?);

            Some(to_key)
        } else {
            None
        };

        store.count(&session, store_from, store_to)
    }
    
    /// Lists the store items starting from the `from` key until, not included, the `to` key.
    fn store_list(store: &mut St,
                  from: Option<K>,
                  to: Option<K>,
                  count: Option<u64>,
                  skip: u64)
        -> Result<Vec<Self>>
    {
        let permission = Permission::Read;

        let session = store.session(&permission)?;

        from.check()?;
        to.check()?;

        if let Some(count) = count {
            if count == 0 {
                return Err(String::from("invalid count"));
            }

            if skip > count {
                return Err(String::from("invalid skip"));
            }
        }

        let mut list = Vec::new();

        let prefix = Self::store_prefix();

        if from.is_none() && to.is_none() {
            for value in store.list_prefix(&session, &prefix, count, skip)?.iter() {
                list.push(Self::from_store_value(&value)?);
            }

            return Ok(list)
        }

        if let Some(from) = from.clone() {
            if let Some(to) = to.clone() {
                if from >= to {
                    return Err(String::from("invalid range"));
                } 
            }
        }

        let store_from = if let Some(k) = from {
            let mut from_key = Vec::new();
            from_key.extend_from_slice(&prefix);
            from_key.extend(&k.to_bytes()?);

            Some(from_key)
        } else {
            None
        };

        let store_to = if let Some(k) = to {
            let mut to_key = Vec::new();
            to_key.extend_from_slice(&prefix);
            to_key.extend(&k.to_bytes()?);

            Some(to_key)
        } else {
            None
        };

        for value in store.list(&session, store_from, store_to, count, skip)?.iter() {
            list.push(Self::from_store_value(&value)?);
        }

        Ok(list)
    }
    
    /// Lookups an item from its key.
    fn store_lookup(store: &mut St, key: &K) -> Result<bool> {
        let permission = Permission::Read;

        let session = store.session(&permission)?;

        key.check()?;

        let mut store_key = Vec::new();

        let prefix = Self::store_prefix();
        
        store_key.extend_from_slice(&prefix);
        store_key.extend_from_slice(&key.to_bytes()?);

        store.lookup(&session, &store_key)
    }
    
    /// Retrieves an item from its key. The item should already exist in the store before the operation.
    fn store_get(store: &mut St, key: &K) -> Result<Self> {
        let permission = Permission::Read;

        let session = store.session(&permission)?;

        key.check()?;
        
        let mut store_key = Vec::new();

        let prefix = Self::store_prefix();
        
        store_key.extend_from_slice(&prefix);
        store_key.extend_from_slice(&key.to_bytes()?);

        let value = store.get(&session, &store_key)?;
        Self::from_store_value(&value)
    }
    
    /// Creates an item in the store. The item should not exist in the store before the operation.
    fn store_create(&self, store: &mut St) -> Result<()> {
        let permission = Permission::Write;

        let session = store.session(&permission)?;

        let key = self.store_key()?;

        let value = self.store_value()?;
        
        let mut store_key = Vec::new();

        let prefix = Self::store_prefix();
        
        store_key.extend_from_slice(&prefix);
        store_key.extend_from_slice(&key.to_bytes()?);

        let store_value = value.to_bytes()?;

        store.create(&session, &store_key, &store_value)
    }
    
    /// Updates the item in the store. The item should already exist in the store before the operation.
    fn store_update(&self, store: &mut St) -> Result<()> {
        let permission = Permission::Write;

        let session = store.session(&permission)?;

        let key = self.store_key()?;

        let value = self.store_value()?;
        
        let mut store_key = Vec::new();

        let prefix = Self::store_prefix();
        
        store_key.extend_from_slice(&prefix);
        store_key.extend_from_slice(&key.to_bytes()?);

        let store_value = value.to_bytes()?;

        store.update(&session, &store_key, &store_value)
    }
    
    /// Creates the item in the store if absent, update it if present.
    fn store_upsert(&self, store: &mut St) -> Result<()> {
        let permission = Permission::Write;

        let session = store.session(&permission)?;

        let key = self.store_key()?;

        let value = self.store_value()?;
        
        let mut store_key = Vec::new();

        let prefix = Self::store_prefix();
        
        store_key.extend_from_slice(&prefix);
        store_key.extend_from_slice(&key.to_bytes()?);

        let store_value = value.to_bytes()?;
        
        store.upsert(&session, &store_key, &store_value)
    }
    
    /// Deletes the item from the store. The item should already exist in the store before the operation.
    fn store_delete(&self, store: &mut St) -> Result<()> {
        let permission = Permission::Write;

        let session = store.session(&permission)?;

        let key = self.store_key()?;
        
        let mut store_key = Vec::new();

        let prefix = Self::store_prefix();
        
        store_key.extend_from_slice(&prefix);
        store_key.extend_from_slice(&key.to_bytes()?);
        
        store.delete(&session, &store_key)
    }

    /// Eval operation in the store.
    fn store_eval<E, P, R>(store: &mut St, session: &Session<S>, params: &P, evaluator: &E)
        -> Result<R>
        where   E: Eval<St, P, R>,
                P: Datable,
                R: Datable
    {
        params.check()?;
        session.check()?;

        if session.is_expired()? {
            return Err(String::from("expired session"));
        }

        if session.permission > Permission::Read {
            return Err(String::from("invalid permission")).into();
        }

        store.eval(session, params, evaluator)
    }

    /// Evals mutably in the store.
    fn store_eval_mut<E, P, R>(store: &mut St, session: &Session<S>, params: &P, evaluator: &mut E)
        -> Result<R>
        where   E: EvalMut<St, P, R>,
                P: Datable,
                R: Datable
    {
        params.check()?;
        session.check()?;

        if session.is_expired()? {
            return Err(String::from("expired session"));
        }

        if session.permission < Permission::Write {
            return Err(String::from("invalid permission")).into();
        }

        store.eval_mut(session, params, evaluator)
    }
}