quamina 0.4.0

Fast pattern-matching library for filtering JSON events
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use super::*;

/// Shorthand for constructing an `EventField` for tests.
fn field(path: &str, value: &str) -> EventField {
    EventField {
        path: path.to_string(),
        value: value.to_string(),
        array_trail: vec![],
        is_number: false,
    }
}

// ========================================================================
// AutomatonValueMatcher Tests (arena-based)
// ========================================================================

#[test]
fn test_automaton_value_matcher_string() {
    let mut matcher: AutomatonValueMatcher<String> = AutomatonValueMatcher::new();
    matcher.add_string_match(b"hello", "p1".to_string());
    matcher.add_string_match(b"world", "p2".to_string());

    let matches = matcher.match_value(b"hello");
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p1".to_string()));

    let matches = matcher.match_value(b"world");
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p2".to_string()));

    let matches = matcher.match_value(b"foo");
    assert!(matches.is_empty());
}

#[test]
fn test_automaton_value_matcher_prefix() {
    let mut matcher: AutomatonValueMatcher<String> = AutomatonValueMatcher::new();
    matcher.add_prefix_match(b"prod-", "p1".to_string());
    matcher.add_prefix_match(b"test-", "p2".to_string());

    let matches = matcher.match_value(b"prod-123");
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p1".to_string()));

    let matches = matcher.match_value(b"test-abc");
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p2".to_string()));

    let matches = matcher.match_value(b"dev-xyz");
    assert!(matches.is_empty());
}

#[test]
fn test_automaton_value_matcher_shellstyle_single() {
    // Test with a single shellstyle pattern (no merging)
    let mut matcher: AutomatonValueMatcher<String> = AutomatonValueMatcher::new();
    matcher.add_shellstyle_match(b"*.txt", "p1".to_string());

    let matches = matcher.match_value(b"file.txt");
    assert!(
        matches.contains(&"p1".to_string()),
        "file.txt should match *.txt"
    );

    let matches = matcher.match_value(b".txt");
    assert!(
        matches.contains(&"p1".to_string()),
        ".txt should match *.txt"
    );

    let matches = matcher.match_value(b"foo");
    assert!(matches.is_empty(), "foo should not match *.txt");
}

#[test]
fn test_automaton_value_matcher_shellstyle_multiple() {
    // Test with multiple shellstyle patterns (with merging)
    let mut matcher: AutomatonValueMatcher<String> = AutomatonValueMatcher::new();
    matcher.add_shellstyle_match(b"*.txt", "p1".to_string());
    matcher.add_shellstyle_match(b"test*", "p2".to_string());

    let matches = matcher.match_value(b"random");
    assert!(matches.is_empty(), "random should not match any pattern");
}

#[test]
fn test_automaton_value_matcher_mixed() {
    // Test mixing different pattern types
    let mut matcher: AutomatonValueMatcher<String> = AutomatonValueMatcher::new();
    matcher.add_string_match(b"exact", "exact_match".to_string());
    matcher.add_prefix_match(b"pre-", "prefix_match".to_string());

    let matches = matcher.match_value(b"exact");
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"exact_match".to_string()));

    let matches = matcher.match_value(b"pre-fix");
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"prefix_match".to_string()));
}

// ========================================================================
// ArenaSmallTable Tests (ported from chain SmallTable tests)
// ========================================================================

#[test]
fn test_arena_small_table_step() {
    use arena::ArenaSmallTable;

    let table = ArenaSmallTable::new();

    // Test that all valid bytes return NONE for empty table
    for b in 0..BYTE_CEILING as u8 {
        let (s, eps) = table.step(b);
        assert!(
            s.is_none(),
            "byte {b} should have no transition in empty table"
        );
        assert!(
            eps.is_empty(),
            "byte {b} should have no epsilons in empty table"
        );
    }
}

#[test]
fn test_arena_small_table_with_mappings() {
    use arena::{ArenaSmallTable, StateArena, StateId};
    use std::sync::Arc;

    let mut arena = StateArena::new();
    let next_field = Arc::new(FieldMatcher::new());
    let next_state = arena.alloc_with_table(ArenaSmallTable::new());
    arena[next_state].field_transitions.push(next_field);

    let table = ArenaSmallTable::with_mappings(StateId::NONE, b"ab", &[next_state, next_state]);

    let (step_a, _) = table.step(b'a');
    assert!(!step_a.is_none(), "byte 'a' should have a transition");

    let (step_b, _) = table.step(b'b');
    assert!(!step_b.is_none(), "byte 'b' should have a transition");

    let (step_c, _) = table.step(b'c');
    assert!(step_c.is_none(), "byte 'c' should have no transition");
}

// ========================================================================
// CoreMatcher Tests
// ========================================================================

#[test]
fn test_core_matcher_single_field_exact() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Add pattern: {"status": ["active"]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("active".to_string())],
            )],
        )
        .unwrap();

    // Create event fields (sorted by path)
    let fields = vec![field("status", "active")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p1".to_string()));
}

#[test]
fn test_core_matcher_single_field_no_match() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("active".to_string())],
            )],
        )
        .unwrap();

    let fields = vec![field("status", "inactive")];

    let matches = matcher.matches_for_fields(&fields);
    assert!(matches.is_empty());
}

#[test]
fn test_core_matcher_exists_true() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Pattern: {"name": [{"exists": true}]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[("name".to_string(), vec![Matcher::Exists(true)])],
        )
        .unwrap();

    // Event with name field present
    let fields = vec![field("name", "anything")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(
        matches.len(),
        1,
        "exists:true should match when field exists"
    );
}

#[test]
fn test_core_matcher_exists_false() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Pattern: {"name": [{"exists": false}]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[("name".to_string(), vec![Matcher::Exists(false)])],
        )
        .unwrap();

    // Event without name field
    let fields = vec![field("other", "value")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(
        matches.len(),
        1,
        "exists:false should match when field is absent"
    );
}

#[test]
fn test_core_matcher_multi_field_and() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Pattern: {"status": ["active"], "type": ["user"]}
    // Both fields must match (AND semantics)
    matcher
        .add_pattern(
            "p1".to_string(),
            &[
                (
                    "status".to_string(),
                    vec![Matcher::Exact("active".to_string())],
                ),
                ("type".to_string(), vec![Matcher::Exact("user".to_string())]),
            ],
        )
        .unwrap();

    // Event with both fields matching
    let fields = vec![field("status", "active"), field("type", "user")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(
        matches.len(),
        1,
        "multi-field AND should match when all fields match"
    );
}

#[test]
fn test_core_matcher_multi_field_partial_no_match() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Pattern: {"status": ["active"], "type": ["user"]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[
                (
                    "status".to_string(),
                    vec![Matcher::Exact("active".to_string())],
                ),
                ("type".to_string(), vec![Matcher::Exact("user".to_string())]),
            ],
        )
        .unwrap();

    // Event with only status matching
    let fields = vec![field("status", "active"), field("type", "admin")];

    let matches = matcher.matches_for_fields(&fields);
    assert!(
        matches.is_empty(),
        "multi-field AND should not match with partial field match"
    );
}

#[test]
fn test_core_matcher_or_within_field() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Pattern: {"status": ["active", "pending"]} - OR within field
    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![
                    Matcher::Exact("active".to_string()),
                    Matcher::Exact("pending".to_string()),
                ],
            )],
        )
        .unwrap();

    // Should match "active"
    let fields1 = vec![field("status", "active")];
    let matches1 = matcher.matches_for_fields(&fields1);
    assert_eq!(matches1.len(), 1, "OR within field should match 'active'");

    // Should match "pending"
    let fields2 = vec![field("status", "pending")];
    let matches2 = matcher.matches_for_fields(&fields2);
    assert_eq!(matches2.len(), 1, "OR within field should match 'pending'");

    // Should not match "completed"
    let fields3 = vec![field("status", "completed")];
    let matches3 = matcher.matches_for_fields(&fields3);
    assert!(
        matches3.is_empty(),
        "OR within field should not match 'completed'"
    );
}

#[test]
fn test_core_matcher_multiple_patterns() {
    use crate::json::Matcher;

    let matcher: CoreMatcher<String> = CoreMatcher::new();

    // Pattern 1: {"status": ["active"]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("active".to_string())],
            )],
        )
        .unwrap();

    // Pattern 2: {"status": ["pending"]}
    matcher
        .add_pattern(
            "p2".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("pending".to_string())],
            )],
        )
        .unwrap();

    // Should match p1 only
    let fields = vec![field("status", "active")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p1".to_string()));
}

// ========================================================================
// ThreadSafeCoreMatcher Tests
// ========================================================================

#[test]
fn test_thread_safe_core_matcher_send_sync() {
    // Compile-time check that ThreadSafeCoreMatcher is Send + Sync
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<ThreadSafeCoreMatcher<String>>();
}

#[test]
fn test_thread_safe_core_matcher_single_field() {
    use crate::json::Matcher;

    let matcher: ThreadSafeCoreMatcher<String> = ThreadSafeCoreMatcher::new();

    // Add pattern: {"status": ["active"]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("active".to_string())],
            )],
        )
        .unwrap();

    // Create event fields
    let fields = vec![field("status", "active")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p1".to_string()));
}

#[test]
fn test_thread_safe_core_matcher_no_match() {
    use crate::json::Matcher;

    let matcher: ThreadSafeCoreMatcher<String> = ThreadSafeCoreMatcher::new();

    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("active".to_string())],
            )],
        )
        .unwrap();

    let fields = vec![field("status", "inactive")];

    let matches = matcher.matches_for_fields(&fields);
    assert!(matches.is_empty());
}

#[test]
fn test_thread_safe_core_matcher_exists_true() {
    use crate::json::Matcher;

    let matcher: ThreadSafeCoreMatcher<String> = ThreadSafeCoreMatcher::new();

    // Pattern: {"name": [{"exists": true}]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[("name".to_string(), vec![Matcher::Exists(true)])],
        )
        .unwrap();

    // Event with name field present
    let fields = vec![field("name", "anything")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(
        matches.len(),
        1,
        "exists:true should match when field exists"
    );
}

#[test]
fn test_thread_safe_core_matcher_exists_false() {
    use crate::json::Matcher;

    let matcher: ThreadSafeCoreMatcher<String> = ThreadSafeCoreMatcher::new();

    // Pattern: {"name": [{"exists": false}]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[("name".to_string(), vec![Matcher::Exists(false)])],
        )
        .unwrap();

    // Event without name field
    let fields = vec![field("other", "value")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(
        matches.len(),
        1,
        "exists:false should match when field is absent"
    );
}

#[test]
fn test_thread_safe_core_matcher_multiple_patterns() {
    use crate::json::Matcher;

    let matcher: ThreadSafeCoreMatcher<String> = ThreadSafeCoreMatcher::new();

    // Pattern 1: {"status": ["active"]}
    matcher
        .add_pattern(
            "p1".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("active".to_string())],
            )],
        )
        .unwrap();

    // Pattern 2: {"status": ["pending"]}
    matcher
        .add_pattern(
            "p2".to_string(),
            &[(
                "status".to_string(),
                vec![Matcher::Exact("pending".to_string())],
            )],
        )
        .unwrap();

    // Should match p1 only
    let fields = vec![field("status", "active")];

    let matches = matcher.matches_for_fields(&fields);
    assert_eq!(matches.len(), 1);
    assert!(matches.contains(&"p1".to_string()));
}