exocore-store 0.1.10

Store / indexation layer of Exocore (Distributed applications framework)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use exocore_protos::{
    generated::exocore_store::{
        entity_query, ordering, trait_field_predicate, trait_query, EntityQuery, MatchPredicate,
        Ordering, Paging, ReferencePredicate, TraitFieldPredicate, TraitFieldReferencePredicate,
        TraitPredicate, TraitQuery,
    },
    message::NamedMessage,
    reflect::FieldId,
    store::{AllPredicate, IdsPredicate, OperationsPredicate, Projection, Reference},
};

use crate::{
    entity::{EntityId, TraitId},
    mutation::OperationId,
};

pub type WatchToken = u64;
pub type ResultHash = u64;

#[derive(Clone)]
pub struct QueryBuilder {
    query: EntityQuery,
}

impl QueryBuilder {
    pub fn matches<T: Into<String>>(query: T) -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Match(MatchPredicate {
                    query: query.into(),
                    ..Default::default()
                })),
                ..Default::default()
            },
        }
    }

    pub fn references<T: Into<ReferencePredicateWrapper>>(reference: T) -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Reference(reference.into().0)),
                ..Default::default()
            },
        }
    }

    pub fn with_operations(operation_ids: Vec<OperationId>) -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Operations(OperationsPredicate {
                    operation_ids,
                })),
                ..Default::default()
            },
        }
    }

    pub fn with_trait_name<T: Into<String>>(trait_name: T) -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Trait(TraitPredicate {
                    trait_name: trait_name.into(),
                    ..Default::default()
                })),
                ..Default::default()
            },
        }
    }

    pub fn with_trait<T: NamedMessage>() -> QueryBuilder {
        Self::with_trait_name(T::full_name())
    }

    pub fn with_trait_name_query<T: Into<String>>(
        trait_name: T,
        query: TraitQuery,
    ) -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Trait(TraitPredicate {
                    trait_name: trait_name.into(),
                    query: Some(query),
                })),
                ..Default::default()
            },
        }
    }

    pub fn with_trait_query<T: NamedMessage>(query: TraitQuery) -> QueryBuilder {
        Self::with_trait_name_query(T::full_name(), query)
    }

    pub fn with_id<E: Into<String>>(id: E) -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Ids(IdsPredicate {
                    ids: vec![id.into()],
                })),
                ..Default::default()
            },
        }
    }

    pub fn with_ids<I>(ids: I) -> QueryBuilder
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Ids(IdsPredicate {
                    ids: ids.into_iter().map(|i| i.into()).collect(),
                })),
                ..Default::default()
            },
        }
    }

    pub fn all() -> QueryBuilder {
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::All(AllPredicate {})),
                ..Default::default()
            },
        }
    }

    #[cfg(any(test, feature = "tests-utils"))]
    pub fn test(success: bool) -> QueryBuilder {
        use exocore_protos::generated::exocore_store::TestPredicate;
        QueryBuilder {
            query: EntityQuery {
                predicate: Some(entity_query::Predicate::Test(TestPredicate { success })),
                ..Default::default()
            },
        }
    }

    pub fn with_paging(mut self, paging: Paging) -> Self {
        self.query.paging = Some(paging);
        self
    }

    pub fn count(mut self, count: u32) -> Self {
        match self.query.paging.as_mut() {
            Some(paging) => paging.count = count,
            None => {
                self.query.paging = Some(Paging {
                    count,
                    ..Default::default()
                })
            }
        }

        self
    }

    pub fn project<P: Into<ProjectionWrapper>>(mut self, projection: P) -> Self {
        self.query.projections.push(projection.into().0);
        self
    }

    pub fn projects<I>(mut self, projections: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<ProjectionWrapper>,
    {
        self.query.projections = projections.into_iter().map(|p| p.into().0).collect();
        self
    }

    pub fn skip_if_results_equals(mut self, result_hash: ResultHash) -> Self {
        self.query.result_hash = result_hash;
        self
    }

    pub fn paging_or_default(&self) -> Paging {
        self.query.paging.clone().unwrap_or_else(default_paging)
    }

    pub fn with_watch_token(mut self, token: WatchToken) -> Self {
        self.query.watch_token = token;
        self
    }

    pub fn order_by_field<F: Into<String>>(mut self, field: F, ascending: bool) -> Self {
        if self.query.ordering.is_none() {
            self.query.ordering = Some(Ordering::default());
        }

        if let Some(ordering) = self.query.ordering.as_mut() {
            ordering.value = Some(ordering::Value::Field(field.into()));
            ordering.ascending = ascending;
        }

        self
    }

    pub fn order_by_operations(mut self, ascending: bool) -> Self {
        if self.query.ordering.is_none() {
            self.query.ordering = Some(Ordering::default());
        }

        if let Some(ordering) = self.query.ordering.as_mut() {
            ordering.value = Some(ordering::Value::OperationId(true));
            ordering.ascending = ascending;
        }

        self
    }

    pub fn order_by_score(mut self, ascending: bool, recency_boost: bool) -> Self {
        if self.query.ordering.is_none() {
            self.query.ordering = Some(Ordering::default());
        }

        if let Some(ordering) = self.query.ordering.as_mut() {
            ordering.value = Some(ordering::Value::Score(true));
            ordering.ascending = ascending;
            ordering.no_recency_boost = !recency_boost;
        }

        self
    }

    pub fn order_ascending(mut self, ascending: bool) -> Self {
        if self.query.ordering.is_none() {
            self.query.ordering = Some(Ordering::default());
        }

        if let Some(ordering) = self.query.ordering.as_mut() {
            ordering.ascending = ascending;
        }

        self
    }

    pub fn include_deleted(mut self) -> Self {
        self.query.include_deleted = true;
        self
    }

    pub fn build(self) -> EntityQuery {
        self.query
    }
}

pub struct TraitQueryBuilder {
    query: TraitQuery,
}

impl TraitQueryBuilder {
    pub fn matches<S: Into<String>>(query: S) -> TraitQueryBuilder {
        TraitQueryBuilder {
            query: TraitQuery {
                predicate: Some(trait_query::Predicate::Match(MatchPredicate {
                    query: query.into(),
                    ..Default::default()
                })),
            },
        }
    }

    pub fn field_equals<F: Into<String>, V: Into<FieldPredicateValueWrapper>>(
        field: F,
        value: V,
    ) -> TraitQueryBuilder {
        TraitQueryBuilder {
            query: TraitQuery {
                predicate: Some(trait_query::Predicate::Field(TraitFieldPredicate {
                    field: field.into(),
                    value: Some(value.into().0),
                    operator: trait_field_predicate::Operator::Equal.into(),
                })),
            },
        }
    }

    pub fn field_references<F: Into<String>, V: Into<ReferencePredicateWrapper>>(
        field: F,
        reference: V,
    ) -> TraitQueryBuilder {
        TraitQueryBuilder {
            query: TraitQuery {
                predicate: Some(trait_query::Predicate::Reference(
                    TraitFieldReferencePredicate {
                        field: field.into(),
                        reference: Some(reference.into().0),
                    },
                )),
            },
        }
    }

    pub fn build(self) -> TraitQuery {
        self.query
    }
}

pub struct ProjectionBuilder {
    projection: Projection,
}

impl ProjectionBuilder {
    pub fn for_package_prefix<S: Into<String>>(package: S) -> ProjectionBuilder {
        ProjectionBuilder {
            projection: Projection {
                package: vec![package.into()],
                ..Default::default()
            },
        }
    }

    pub fn for_trait_name<S: Into<String>>(trait_name: S) -> ProjectionBuilder {
        ProjectionBuilder {
            projection: Projection {
                package: vec![format!("{}$", trait_name.into())],
                ..Default::default()
            },
        }
    }

    pub fn for_trait<T: NamedMessage>() -> ProjectionBuilder {
        Self::for_trait_name(T::full_name())
    }

    pub fn for_all() -> ProjectionBuilder {
        ProjectionBuilder {
            projection: Default::default(),
        }
    }

    pub fn skip(mut self) -> ProjectionBuilder {
        self.projection.skip = true;
        self
    }

    pub fn return_all(self) -> ProjectionBuilder {
        self
    }

    pub fn return_fields(mut self, field_ids: Vec<FieldId>) -> ProjectionBuilder {
        self.projection.field_ids = field_ids;
        self
    }

    pub fn return_field_groups(mut self, field_groups: Vec<FieldId>) -> ProjectionBuilder {
        self.projection.field_group_ids = field_groups;
        self
    }

    pub fn build(self) -> Projection {
        self.projection
    }
}

pub struct FieldPredicateValueWrapper(trait_field_predicate::Value);

impl From<EntityId> for FieldPredicateValueWrapper {
    fn from(id: EntityId) -> Self {
        FieldPredicateValueWrapper(trait_field_predicate::Value::String(id))
    }
}

impl From<&str> for FieldPredicateValueWrapper {
    fn from(s: &str) -> Self {
        FieldPredicateValueWrapper(trait_field_predicate::Value::String(s.to_string()))
    }
}

pub struct ReferencePredicateWrapper(ReferencePredicate);

impl From<EntityId> for ReferencePredicateWrapper {
    fn from(id: EntityId) -> Self {
        ReferencePredicateWrapper(ReferencePredicate {
            entity_id: id,
            trait_id: String::new(),
        })
    }
}

impl From<(EntityId, TraitId)> for ReferencePredicateWrapper {
    fn from(tup: (EntityId, TraitId)) -> Self {
        ReferencePredicateWrapper(ReferencePredicate {
            entity_id: tup.0,
            trait_id: tup.1,
        })
    }
}

impl From<&str> for ReferencePredicateWrapper {
    fn from(s: &str) -> Self {
        ReferencePredicateWrapper(ReferencePredicate {
            entity_id: s.to_string(),
            trait_id: String::new(),
        })
    }
}

impl From<(&str, &str)> for ReferencePredicateWrapper {
    fn from(tup: (&str, &str)) -> Self {
        ReferencePredicateWrapper(ReferencePredicate {
            entity_id: tup.0.to_string(),
            trait_id: tup.1.to_string(),
        })
    }
}

impl From<Reference> for ReferencePredicateWrapper {
    fn from(r: Reference) -> Self {
        ReferencePredicateWrapper(ReferencePredicate {
            entity_id: r.entity_id,
            trait_id: r.trait_id,
        })
    }
}

pub struct ProjectionWrapper(Projection);

impl From<Projection> for ProjectionWrapper {
    fn from(p: Projection) -> Self {
        ProjectionWrapper(p)
    }
}

impl From<ProjectionBuilder> for ProjectionWrapper {
    fn from(b: ProjectionBuilder) -> Self {
        ProjectionWrapper(b.build())
    }
}

pub fn default_paging() -> Paging {
    Paging {
        count: 10,
        ..Default::default()
    }
}

pub fn validate_paging(paging: &mut Paging) {
    if paging.count == 0 {
        paging.count = 10;
    }
}