soaprs-memory 0.2.0

Reference in-memory repository adapter for soaprs
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Reference in-memory implementation of soaprs ports.

mod cqrs;
mod events;

pub use cqrs::{
    MemoryEventOutbox, MemoryEventStore, MemoryInboxStore, MemoryOutboxStore,
    MemoryProjectionCheckpointStore, MemorySagaActionStore, MemorySagaOutbox, MemorySagaStore,
    MemorySnapshotStore, MemoryStoredEventStore, MemoryTransactionalProjection,
};
pub use events::{MemoryEventBus, SubscriptionId};

use std::{cmp::Ordering, sync::RwLock};

use soaprs_core::{BoxFuture, Entity, SoapError, SoapResult};
use soaprs_repository::{
    Condition, FieldName, FindParams, LogicalOperator, Operator, ReadRepository, ScalarValue, Sort,
    SortDirection, WriteRepository,
};

/// Provides logical field values to the in-memory query evaluator.
pub trait Queryable {
    /// Reports whether the entity exposes this logical query field.
    fn supports_field(field: &FieldName) -> bool;

    /// Returns the value of a supported logical field.
    ///
    /// Nullable fields return [`ScalarValue::Null`]. An unknown field must
    /// return [`SoapError::validation`], even though the repository validates
    /// field support before evaluating entities.
    fn field_value(&self, field: &FieldName) -> SoapResult<ScalarValue>;
}

/// Thread-safe in-memory repository used as a reference adapter.
#[derive(Debug)]
pub struct MemoryRepository<E> {
    entities: RwLock<Vec<E>>,
}

impl<E> MemoryRepository<E> {
    /// Creates an empty repository.
    pub const fn new() -> Self {
        Self {
            entities: RwLock::new(Vec::new()),
        }
    }

    fn read_entities(&self) -> SoapResult<std::sync::RwLockReadGuard<'_, Vec<E>>> {
        self.entities
            .read()
            .map_err(|_| SoapError::infrastructure("in-memory repository read lock poisoned"))
    }

    fn write_entities(&self) -> SoapResult<std::sync::RwLockWriteGuard<'_, Vec<E>>> {
        self.entities
            .write()
            .map_err(|_| SoapError::infrastructure("in-memory repository write lock poisoned"))
    }
}

impl<E> Default for MemoryRepository<E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<E> ReadRepository<E> for MemoryRepository<E>
where
    E: Entity + Queryable + Clone + 'static,
{
    fn find(&self, params: FindParams) -> BoxFuture<'_, SoapResult<Vec<E>>> {
        Box::pin(async move {
            params.validate()?;
            validate_find_fields::<E>(&params)?;
            let entities = self.read_entities()?;
            let mut matched = entities
                .iter()
                .filter_map(
                    |entity| match matches_condition(entity, params.condition.as_ref()) {
                        Ok(true) => Some(Ok(entity.clone())),
                        Ok(false) => None,
                        Err(error) => Some(Err(error)),
                    },
                )
                .collect::<SoapResult<Vec<_>>>()?;

            for sort in params.sort.iter().rev() {
                sort_entities(&mut matched, sort)?;
            }

            let available = matched.len().saturating_sub(params.offset);
            let take = params.limit.unwrap_or(available);
            Ok(matched.into_iter().skip(params.offset).take(take).collect())
        })
    }

    fn get<'a>(&'a self, id: &'a E::Id) -> BoxFuture<'a, SoapResult<Option<E>>> {
        Box::pin(async move {
            let entities = self.read_entities()?;
            Ok(entities.iter().find(|entity| entity.id() == id).cloned())
        })
    }

    fn count(&self, params: FindParams) -> BoxFuture<'_, SoapResult<u64>> {
        Box::pin(async move {
            params.validate()?;
            if let Some(condition) = &params.condition {
                validate_condition_fields::<E>(condition)?;
            }
            let entities = self.read_entities()?;
            let mut count = 0_u64;
            for entity in entities.iter() {
                if matches_condition(entity, params.condition.as_ref())? {
                    count = count.saturating_add(1);
                }
            }
            Ok(count)
        })
    }
}

impl<E> WriteRepository<E> for MemoryRepository<E>
where
    E: Entity + Queryable + Clone + 'static,
{
    fn insert(&self, entity: E) -> BoxFuture<'_, SoapResult<()>> {
        Box::pin(async move {
            let mut entities = self.write_entities()?;
            if entities.iter().any(|existing| existing.id() == entity.id()) {
                return Err(SoapError::conflict("duplicate entity identifier"));
            }
            entities.push(entity);
            Ok(())
        })
    }

    fn replace(&self, entity: E) -> BoxFuture<'_, SoapResult<()>> {
        Box::pin(async move {
            let mut entities = self.write_entities()?;
            let Some(index) = entities
                .iter()
                .position(|existing| existing.id() == entity.id())
            else {
                return Err(SoapError::not_found("entity identifier"));
            };
            entities[index] = entity;
            Ok(())
        })
    }

    fn remove<'a>(&'a self, id: &'a E::Id) -> BoxFuture<'a, SoapResult<bool>> {
        Box::pin(async move {
            let mut entities = self.write_entities()?;
            let Some(index) = entities.iter().position(|entity| entity.id() == id) else {
                return Ok(false);
            };
            entities.remove(index);
            Ok(true)
        })
    }
}

fn matches_condition<E>(entity: &E, condition: Option<&Condition>) -> SoapResult<bool>
where
    E: Queryable,
{
    match condition {
        None => Ok(true),
        Some(Condition::Group {
            operator,
            conditions,
        }) => match operator {
            LogicalOperator::And => {
                for condition in conditions {
                    if !matches_condition(entity, Some(condition))? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            LogicalOperator::Or => {
                for condition in conditions {
                    if matches_condition(entity, Some(condition))? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
        },
        Some(Condition::Predicate {
            field,
            operator,
            value,
        }) => evaluate_predicate(entity.field_value(field)?, *operator, value.as_ref()),
    }
}

fn evaluate_predicate(
    actual: ScalarValue,
    operator: Operator,
    expected: Option<&ScalarValue>,
) -> SoapResult<bool> {
    validate_entity_value(&actual)?;
    match operator {
        Operator::IsNull => Ok(matches!(actual, ScalarValue::Null)),
        Operator::IsNotNull => Ok(!matches!(actual, ScalarValue::Null)),
        _ if matches!(actual, ScalarValue::Null) => Ok(false),
        Operator::Eq | Operator::Ne => {
            let expected = expected
                .ok_or_else(|| SoapError::validation("equality requires a non-null value"))?;
            let equivalent = actual
                .compare(expected)
                .ok_or_else(|| SoapError::validation("equality requires compatible value types"))?
                .is_eq();
            Ok(if operator == Operator::Eq {
                equivalent
            } else {
                !equivalent
            })
        }
        Operator::Gt | Operator::Gte | Operator::Lt | Operator::Lte => {
            let ordering = comparable_ordering(Some(&actual), expected)?;
            Ok(match operator {
                Operator::Gt => ordering == Ordering::Greater,
                Operator::Gte => ordering != Ordering::Less,
                Operator::Lt => ordering == Ordering::Less,
                Operator::Lte => ordering != Ordering::Greater,
                _ => false,
            })
        }
        Operator::In | Operator::NotIn => {
            let Some(ScalarValue::List(values)) = expected else {
                return Err(SoapError::validation("set operators require a list value"));
            };
            if let Some(first) = values.first() {
                if actual.compare(first).is_none() {
                    return Err(SoapError::validation(
                        "set membership requires compatible value types",
                    ));
                }
            }
            let contains = values.iter().any(|item| actual.equivalent(item));
            Ok(if operator == Operator::In {
                contains
            } else {
                !contains
            })
        }
        Operator::Like => {
            let (ScalarValue::String(actual), Some(ScalarValue::String(pattern))) =
                (&actual, expected)
            else {
                return Err(SoapError::validation("LIKE requires string values"));
            };
            Ok(like_matches(actual, pattern))
        }
    }
}

fn validate_find_fields<E>(params: &FindParams) -> SoapResult<()>
where
    E: Queryable,
{
    if let Some(condition) = &params.condition {
        validate_condition_fields::<E>(condition)?;
    }
    for sort in &params.sort {
        validate_field::<E>(&sort.field)?;
    }
    Ok(())
}

fn validate_condition_fields<E>(condition: &Condition) -> SoapResult<()>
where
    E: Queryable,
{
    match condition {
        Condition::Predicate { field, .. } => validate_field::<E>(field),
        Condition::Group { conditions, .. } => {
            for condition in conditions {
                validate_condition_fields::<E>(condition)?;
            }
            Ok(())
        }
    }
}

fn validate_field<E>(field: &FieldName) -> SoapResult<()>
where
    E: Queryable,
{
    if E::supports_field(field) {
        Ok(())
    } else {
        Err(SoapError::validation(format!(
            "unknown query field `{field}`"
        )))
    }
}

fn sort_entities<E>(entities: &mut [E], sort: &Sort) -> SoapResult<()>
where
    E: Queryable,
{
    for entity in entities.iter() {
        validate_entity_value(&entity.field_value(&sort.field)?)?;
    }

    let mut failure = None;
    entities.sort_by(|left, right| {
        if failure.is_some() {
            return Ordering::Equal;
        }

        match compare_sort_values(
            left.field_value(&sort.field),
            right.field_value(&sort.field),
        ) {
            Ok(ordering) => match sort.direction {
                SortDirection::Ascending => ordering,
                SortDirection::Descending => ordering.reverse(),
            },
            Err(error) => {
                failure = Some(error);
                Ordering::Equal
            }
        }
    });

    match failure {
        Some(error) => Err(error),
        None => Ok(()),
    }
}

fn compare_sort_values(
    left: SoapResult<ScalarValue>,
    right: SoapResult<ScalarValue>,
) -> SoapResult<Ordering> {
    let left = left?;
    let right = right?;
    validate_entity_value(&left)?;
    validate_entity_value(&right)?;

    match (&left, &right) {
        (ScalarValue::Null, ScalarValue::Null) => Ok(Ordering::Equal),
        (ScalarValue::Null, _) => Ok(Ordering::Less),
        (_, ScalarValue::Null) => Ok(Ordering::Greater),
        _ => left
            .compare(&right)
            .ok_or_else(|| SoapError::validation("sorting requires compatible field value types")),
    }
}

fn validate_entity_value(value: &ScalarValue) -> SoapResult<()> {
    value.validate()?;
    if matches!(value, ScalarValue::List(_)) {
        Err(SoapError::validation(
            "entity query fields must contain scalar values",
        ))
    } else {
        Ok(())
    }
}

fn comparable_ordering(
    actual: Option<&ScalarValue>,
    expected: Option<&ScalarValue>,
) -> SoapResult<Ordering> {
    let (Some(actual), Some(expected)) = (actual, expected) else {
        return Err(SoapError::validation(
            "comparison requires two non-null values",
        ));
    };
    actual
        .compare(expected)
        .ok_or_else(|| SoapError::validation("comparison requires compatible value types"))
}

fn like_matches(value: &str, pattern: &str) -> bool {
    let value: Vec<_> = value.chars().collect();
    let pattern: Vec<_> = pattern.chars().collect();
    let mut table = vec![vec![false; pattern.len() + 1]; value.len() + 1];
    table[0][0] = true;
    for pattern_index in 1..=pattern.len() {
        if pattern[pattern_index - 1] == '%' {
            table[0][pattern_index] = table[0][pattern_index - 1];
        }
    }
    for value_index in 1..=value.len() {
        for pattern_index in 1..=pattern.len() {
            table[value_index][pattern_index] = match pattern[pattern_index - 1] {
                '%' => {
                    table[value_index][pattern_index - 1] || table[value_index - 1][pattern_index]
                }
                '_' => table[value_index - 1][pattern_index - 1],
                character => {
                    character == value[value_index - 1] && table[value_index - 1][pattern_index - 1]
                }
            };
        }
    }
    table[value.len()][pattern.len()]
}

#[cfg(test)]
mod tests {
    use std::cmp::Ordering;

    use soaprs_core::{SoapError, SoapErrorKind};
    use soaprs_repository::{Operator, ScalarValue};

    use super::{comparable_ordering, evaluate_predicate, like_matches};

    #[test]
    fn evaluates_all_comparison_operators() {
        let actual = ScalarValue::I64(10);

        assert_eq!(
            evaluate_predicate(actual.clone(), Operator::Eq, Some(&ScalarValue::U64(10))).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(actual.clone(), Operator::Ne, Some(&ScalarValue::I64(11))).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(actual.clone(), Operator::Gt, Some(&ScalarValue::I64(9))).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(actual.clone(), Operator::Gte, Some(&ScalarValue::I64(10))).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(actual.clone(), Operator::Lt, Some(&ScalarValue::I64(11))).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(actual, Operator::Lte, Some(&ScalarValue::F64(10.0))).ok(),
            Some(true)
        );
    }

    #[test]
    fn evaluates_set_and_null_operators() {
        let values = ScalarValue::List(vec![ScalarValue::I64(1), ScalarValue::U64(2)]);

        assert_eq!(
            evaluate_predicate(ScalarValue::I64(2), Operator::In, Some(&values)).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(ScalarValue::I64(3), Operator::NotIn, Some(&values)).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(ScalarValue::Null, Operator::IsNull, None).ok(),
            Some(true)
        );
        assert_eq!(
            evaluate_predicate(ScalarValue::Null, Operator::IsNotNull, None).ok(),
            Some(false)
        );
        assert_eq!(
            evaluate_predicate(ScalarValue::Null, Operator::Ne, Some(&ScalarValue::I64(1))).ok(),
            Some(false)
        );
    }

    #[test]
    fn like_uses_characters_instead_of_utf8_bytes() {
        assert!(like_matches("Łódź", "_ód%"));
        assert!(!like_matches("Łódź", "__ód%"));
        assert!(like_matches(r"a\b", r"a\b"));
    }

    #[test]
    fn incompatible_comparisons_return_validation_errors() {
        let result = comparable_ordering(
            Some(&ScalarValue::String("10".into())),
            Some(&ScalarValue::I64(10)),
        );

        assert_eq!(
            result.as_ref().map_err(SoapError::kind),
            Err(SoapErrorKind::Validation)
        );
        assert_ne!(result.ok(), Some(Ordering::Equal));
    }
}