nitrite 0.3.0

An embedded NoSQL document database for Rust with collections, repositories, indexing, and ACID transactions
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
use regex::Regex;
use std::{any::Any, collections::HashMap, fmt::Display, sync::OnceLock};

use crate::{
    collection::Document,
    errors::{ErrorKind, NitriteError, NitriteResult},
    index::{
        text::{Tokenizer, TokenizerProvider},
        IndexMap,
    },
    DefaultFilter, StringTokenizer, Value,
};

use super::{is_element_match_filter, is_text_filter, Filter, FilterProvider};

/// A filter that matches documents using regular expressions.
///
/// This filter evaluates field values against a compiled regex pattern. The regex pattern
/// is compiled during initialization and cached for efficient evaluation. Invalid regex patterns
/// are logged but do not cause panics, allowing graceful error handling during filter application.
///
/// # Responsibilities
///
/// * **Pattern Compilation**: Compiles and validates regex patterns at initialization
/// * **Regex Matching**: Evaluates field values against the compiled pattern
/// * **String Extraction**: Converts field values to strings for pattern matching
/// * **Error Handling**: Safely handles invalid patterns and non-string field values
pub(crate) struct RegexFilter {
    field_name: OnceLock<String>,
    field_value: OnceLock<String>,
    pattern: OnceLock<Regex>,
    collection_name: OnceLock<String>,
}

impl RegexFilter {
    /// Creates a new regex filter with the specified field name and pattern.
    ///
    /// The regex pattern is compiled immediately. If the pattern is invalid, it is logged
    /// and stored as uninitialized, causing apply() to return an error when invoked.
    ///
    /// # Arguments
    ///
    /// * `field_name` - The name of the field to match against
    /// * `field_value` - The regular expression pattern to compile and use for matching
    ///
    /// # Returns
    ///
    /// A new `RegexFilter` instance with the specified field and pattern
    #[inline]
    pub(crate) fn new(field_name: String, field_value: String) -> Self {
        let name = OnceLock::new();
        let _ = name.set(field_name);

        let value = OnceLock::new();
        let _ = value.set(field_value.clone());

        let pattern = OnceLock::new();
        // Try to compile the regex, but don't panic - store None if compilation fails
        match Regex::new(&field_value) {
            Ok(regex) => {
                let _ = pattern.set(regex);
            }
            Err(e) => {
                log::error!("Invalid regex pattern '{}': {}", field_value, e);
                // Don't initialize pattern - get() will return None for invalid regex
            }
        }

        RegexFilter {
            field_name: name,
            field_value: value,
            pattern,
            collection_name: OnceLock::new(),
        }
    }
}

impl Display for RegexFilter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (self.field_name.get(), self.field_value.get()) {
            (Some(name), Some(value)) => write!(f, "({} =~ {})", name, value),
            (Some(name), None) => write!(f, "({} =~ unknown)", name),
            (None, Some(value)) => write!(f, "(unknown =~ {})", value),
            (None, None) => write!(f, "(unknown =~ unknown)"),
        }
    }
}

impl FilterProvider for RegexFilter {
    #[inline]
    fn apply(&self, entry: &Document) -> NitriteResult<bool> {
        let field_name = self.field_name.get()
            .ok_or_else(|| NitriteError::new("Field name not initialized", ErrorKind::InvalidFieldName))?;
        let value = entry.get(field_name)?;
        let value = value.as_string();
        if value.is_none() {
            return Ok(false);
        }

        match self.pattern.get() {
            Some(p) => {
                let val = value.as_ref().ok_or_else(|| NitriteError::new(
                    "Field value is null or not a string",
                    ErrorKind::InvalidOperation,
                ))?;
                Ok(p.is_match(val))
            }
            None => {
                log::error!("Invalid regex pattern for filter {}", self);
                Err(NitriteError::new(
                    "Invalid regex pattern",
                    ErrorKind::InvalidOperation,
                ))
            },
        }
    }

    fn get_collection_name(&self) -> NitriteResult<String> {
        self.collection_name.get()
            .cloned()
            .ok_or_else(|| {
                log::error!("Collection name is not set for filter");
                NitriteError::new(
                    "Collection name is not set",
                    ErrorKind::InvalidOperation,
                )
            })
    }

    fn set_collection_name(&self, collection_name: String) -> NitriteResult<()> {
        self.collection_name.get_or_init(|| collection_name);
        Ok(())
    }

    fn has_field(&self) -> bool {
        true
    }

    fn get_field_name(&self) -> NitriteResult<String> {
        self.field_name.get()
            .cloned()
            .ok_or_else(|| NitriteError::new("Field name not initialized", ErrorKind::InvalidFieldName))
    }

    fn set_field_name(&self, field_name: String) -> NitriteResult<()> {
        self.field_name.get_or_init(|| field_name);
        Ok(())
    }

    fn get_field_value(&self) -> NitriteResult<Option<Value>> {
        Ok(self.field_value.get()
            .map(|v| Value::String(v.clone())))
    }

    fn set_field_value(&self, field_value: Value) -> NitriteResult<()> {
        if let Value::String(string_value) = field_value {
            self.field_value.get_or_init(|| string_value.clone());
            // Try to compile the regex - if it fails, pattern remains uninitialized
            match Regex::new(&string_value) {
                Ok(regex) => {
                    self.pattern.get_or_init(|| regex);
                }
                Err(e) => {
                    log::error!("Invalid regex pattern '{}': {}", string_value, e);
                    return Err(NitriteError::new(
                        &format!("Invalid regex pattern: {}", e),
                        ErrorKind::InvalidOperation,
                    ));
                }
            }
            Ok(())
        } else {
            log::error!("Field value is not a string for filter {}", self);
            Err(NitriteError::new(
                "Field value is not a string",
                ErrorKind::InvalidOperation,
            ))
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}


#[derive(Clone)]
pub(crate) struct TextFilter {
    field_name: OnceLock<String>,
    field_value: OnceLock<String>,
    collection_name: OnceLock<String>,
    case_sensitive: OnceLock<bool>,
    tokenizer: OnceLock<Tokenizer>,
}

impl TextFilter {
    /// Creates a new text filter for the specified field with case sensitivity option.
    ///
    /// # Arguments
    ///
    /// * `field_name` - The name of the field to search in
    /// * `field_value` - The text to search for
    /// * `case_sensitive` - If true, search is case-sensitive; if false, case-insensitive
    ///
    /// # Returns
    ///
    /// A new `TextFilter` instance configured for text search
    #[inline]
    pub(crate) fn new(field_name: String, field_value: String, case_sensitive: bool) -> Self {
        let name = OnceLock::new();
        let _ = name.set(field_name);

        let value = OnceLock::new();
        let _ = value.set(field_value.clone());

        let case = OnceLock::new();
        let _ = case.set(case_sensitive);

        TextFilter {
            field_name: name,
            field_value: value,
            collection_name: OnceLock::new(),
            case_sensitive: case,
            tokenizer: OnceLock::new(),
        }
    }

    /// Sets the tokenizer for this text filter.
    ///
    /// The tokenizer is used for word-based text search and index optimization.
    /// It should be set before using index-accelerated search features.
    ///
    /// # Arguments
    ///
    /// * `tokenizer` - The tokenizer to use for text processing
    pub(crate) fn set_tokenizer(&self, tokenizer: Tokenizer) {
        self.tokenizer.get_or_init(|| tokenizer);
    }

    fn search_exact_by_index(
        &self,
        index_map: &IndexMap,
        search_string: String,
    ) -> NitriteResult<Vec<Value>> {
        let mut score_map = HashMap::new();
        
        // Get tokenizer with proper error handling
        let tokenizer = self.tokenizer.get()
            .ok_or_else(|| {
                log::error!("Tokenizer not initialized for text filter");
                NitriteError::new("Tokenizer not initialized", ErrorKind::InvalidOperation)
            })?;
        
        let words = tokenizer.tokenize(&search_string);

        for word in words {
            let value = index_map.get(&Value::String(word.clone()))?;
            
            if let Some(val) = value {
                if let Some(nitrite_ids) = val.as_array() {
                    for nitrite_id in nitrite_ids {
                        let count = score_map.entry(nitrite_id.clone()).or_insert(0);
                        *count += 1;
                    }
                }
            }

            // Handle case-insensitive search with proper error handling
            let case_sensitive = self.case_sensitive.get()
                .ok_or_else(|| {
                    log::error!("Case sensitive flag not initialized for text filter");
                    NitriteError::new("Case sensitive flag not initialized", ErrorKind::InvalidOperation)
                })?;
            
            if !*case_sensitive {
                // case-insensitive search
                let search_string = format!("i_{}", word.to_lowercase());
                let value = index_map.get(&Value::String(search_string))?;
                if let Some(val) = value {
                    if let Some(nitrite_ids) = val.as_array() {
                        for nitrite_id in nitrite_ids {
                            let count = score_map.entry(nitrite_id.clone()).or_insert(0);
                            *count += 1;
                        }
                    }
                }
            }
        }

        self.sorted_ids_by_score(score_map)
    }

    fn sorted_ids_by_score(&self, score_map: HashMap<Value, i32>) -> NitriteResult<Vec<Value>> {
        let mut sorted_map: Vec<_> = score_map.into_iter().collect();
        sorted_map.sort_by_key(|entry| std::cmp::Reverse(entry.1));

        let mut sorted_ids = Vec::new();
        for (key, _) in sorted_map {
            sorted_ids.push(key);
        }

        Ok(sorted_ids)
    }

    fn search_by_wildcard(
        &self,
        index_map: &IndexMap,
        search_string: String,
    ) -> NitriteResult<Vec<Value>> {
        if search_string.eq("*") {
            let field_name = self.field_name.get().map(|s| s.as_str()).unwrap_or("unknown");
            log::error!("'*' alone is not a valid search string for wildcard filter on field '{}'", field_name);
            return Err(NitriteError::new(
                &format!("Invalid wildcard search pattern '*' on field '{}'. Use '*text' (ends with), 'text*' (starts with), or '*text*' (contains)", field_name),
                ErrorKind::FilterError,
            ));
        }

        let string_tokenizer = StringTokenizer::new(DefaultFilter, &search_string);
        let tokens = string_tokenizer.collect::<Vec<_>>();
        if tokens.len() > 1 {
            let field_name = self.field_name.get().map(|s| s.as_str()).unwrap_or("unknown");
            log::error!("Wildcard search with multiple words '{}' cannot be applied on field '{}' - use phrase search instead", search_string, field_name);
            return Err(NitriteError::new(
                &format!("Wildcard search on field '{}' failed: '{}' contains multiple words. Use phrase search or split into multiple filters", field_name, search_string),
                ErrorKind::FilterError,
            ));
        }

        if search_string.starts_with("*") && !search_string.ends_with("*") {
            self.search_end_with(index_map, search_string)
        } else if !search_string.starts_with("*") && search_string.ends_with("*") {
            self.search_start_with(index_map, search_string)
        } else if search_string.starts_with("*") && search_string.ends_with("*") {
            self.search_contains(index_map, search_string)
        } else {
            self.search_exact_by_index(index_map, search_string)
        }
    }

    fn search_start_with(
        &self,
        index_map: &IndexMap,
        search_string: String,
    ) -> NitriteResult<Vec<Value>> {
        let mut nitrite_ids = Vec::new();
        let search_term = search_string.trim_end_matches('*');

        let entries = index_map.entries()?;
        for result in entries {
            let (key, value) = result?;
            if let Value::String(key_str) = &key {
                if key_str.starts_with(search_term) {
                    if let Value::Array(array) = value {
                        nitrite_ids.extend(array);
                    }
                }
            }
        }

        Ok(nitrite_ids)
    }

    fn search_end_with(
        &self,
        index_map: &IndexMap,
        search_string: String,
    ) -> NitriteResult<Vec<Value>> {
        let mut nitrite_ids = Vec::new();
        let search_term = search_string.trim_start_matches('*');

        let entries = index_map.entries()?;
        for result in entries {
            let (key, value) = result?;
            if let Value::String(key_str) = &key {
                if key_str.ends_with(search_term) {
                    if let Value::Array(array) = value {
                        nitrite_ids.extend(array);
                    }
                }
            }
        }

        Ok(nitrite_ids)
    }

    fn search_contains(
        &self,
        index_map: &IndexMap,
        search_string: String,
    ) -> NitriteResult<Vec<Value>> {
        let mut nitrite_ids = Vec::new();
        let search_term = search_string.trim_start_matches('*').trim_end_matches('*');

        let entries = index_map.entries()?;
        for result in entries {
            let (key, value) = result?;
            if let Value::String(key_str) = &key {
                if key_str.contains(search_term) {
                    if let Value::Array(array) = value {
                        nitrite_ids.extend(array);
                    }
                }
            }
        }

        Ok(nitrite_ids)
    }
}

impl Display for TextFilter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let case_sensitive = self.case_sensitive.get().unwrap_or(&false);
        let field_name = self.field_name.get().map(|s| s.as_str()).unwrap_or("unknown");
        let field_value = self.field_value.get().map(|s| s.as_str()).unwrap_or("unknown");

        if *case_sensitive {
            write!(
                f,
                "({} text {} case_sensitive)",
                field_name, field_value
            )
        } else {
            write!(
                f,
                "({} text_case_insensitive {})",
                field_name, field_value
            )
        }
    }
}

impl FilterProvider for TextFilter {
    #[inline]
    fn apply(&self, entry: &Document) -> NitriteResult<bool> {
        let field_name = self.field_name.get()
            .ok_or_else(|| NitriteError::new("Field name not initialized", ErrorKind::InvalidFieldName))?;
        let value = entry.get(field_name)?;
        let value = value.as_string();
        if value.is_none() {
            return Ok(false);
        }

        let field_value = self.field_value.get()
            .ok_or_else(|| NitriteError::new("Field value not initialized", ErrorKind::InvalidOperation))?;
        let case_sensitive = *self.case_sensitive.get()
            .ok_or_else(|| NitriteError::new("Case sensitive flag not initialized", ErrorKind::InvalidOperation))?;

        if case_sensitive {
            Ok(value.as_ref().ok_or_else(|| NitriteError::new(
                "Field value is null or not a string",
                ErrorKind::InvalidOperation,
            ))?.contains(field_value))
        } else {
            Ok(value.as_ref().ok_or_else(|| NitriteError::new(
                "Field value is null or not a string",
                ErrorKind::InvalidOperation,
            ))?.to_lowercase().contains(&field_value.to_lowercase()))
        }
    }

    fn apply_on_index(&self, index_map: &IndexMap) -> NitriteResult<Vec<Value>> {
        let search_string = self.field_value.get()
            .ok_or_else(|| NitriteError::new("Field value not initialized", ErrorKind::InvalidOperation))?
            .clone();
        self.search_by_wildcard(index_map, search_string)
    }

    fn get_collection_name(&self) -> NitriteResult<String> {
        self.collection_name.get()
            .cloned()
            .ok_or_else(|| {
                log::error!("Collection name is not set for filter");
                NitriteError::new(
                    "Collection name is not set",
                    ErrorKind::InvalidOperation,
                )
            })
    }

    fn set_collection_name(&self, collection_name: String) -> NitriteResult<()> {
        self.collection_name.get_or_init(|| collection_name);
        Ok(())
    }

    fn has_field(&self) -> bool {
        true
    }

    fn get_field_name(&self) -> NitriteResult<String> {
        self.field_name.get()
            .cloned()
            .ok_or_else(|| NitriteError::new("Field name not initialized", ErrorKind::InvalidFieldName))
    }

    fn set_field_name(&self, field_name: String) -> NitriteResult<()> {
        self.field_name.get_or_init(|| field_name);
        Ok(())
    }

    fn get_field_value(&self) -> NitriteResult<Option<Value>> {
        Ok(self.field_value.get()
            .map(|v| Value::String(v.clone())))
    }

    fn set_field_value(&self, field_value: Value) -> NitriteResult<()> {
        if let Value::String(string_value) = field_value {
            self.field_value.get_or_init(|| string_value);
            Ok(())
        } else {
            log::error!("Field value is not a string for filter {}", self);
            Err(NitriteError::new(
                "Field value is not a string",
                ErrorKind::InvalidOperation,
            ))
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// A filter that matches elements within arrays.
///
/// This filter evaluates a condition against each element in an array field and matches
/// documents where at least one element satisfies the condition. Elements can be documents
/// or scalar values (matched using a special `$` field name). The filter validates that
/// the wrapped filter is not itself an ElementMatchFilter or TextFilter to prevent invalid nesting.
///
/// # Responsibilities
///
/// * **Array Element Matching**: Evaluates filters against array elements
/// * **Document Handling**: Applies filters to document elements
/// * **Scalar Matching**: Matches scalar values using synthetic documents with `$` field
/// * **Filter Validation**: Prevents invalid filter nesting
/// * **Short-Circuit Evaluation**: Returns true on first matching element
pub(crate) struct ElementMatchFilter {
    field_name: OnceLock<String>,
    filter: Filter,
    collection_name: OnceLock<String>,
}

impl ElementMatchFilter {
    /// Creates a new element match filter for the specified array field and condition.
    ///
    /// The filter evaluates the provided condition against each element in the array field.
    /// For document elements, the condition is applied directly. For scalar elements,
    /// they are wrapped in a synthetic document with field name `$`.
    ///
    /// # Arguments
    ///
    /// * `field_name` - The name of the array field to filter
    /// * `filter` - The filter condition to evaluate against array elements
    ///
    /// # Returns
    ///
    /// A new `ElementMatchFilter` that matches arrays containing at least one matching element
    #[inline]
    pub(crate) fn new(field_name: String, filter: Filter) -> Self {
        let name = OnceLock::new();
        let _ = name.set(field_name);

        ElementMatchFilter {
            field_name: name,
            filter,
            collection_name: OnceLock::new(),
        }
    }

    fn has_element_match_filter(&self) -> bool {
        if is_element_match_filter(&self.filter) {
            return true;
        }
        false
    }

    fn has_text_filter(&self) -> bool {
        if is_text_filter(&self.filter) {
            return true;
        }
        false
    }

    fn matches(&self, value: Vec<Value>) -> NitriteResult<bool> {
        for v in value {
            if self.match_element(&v)? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn match_element(&self, value: &Value) -> NitriteResult<bool> {
        match value {
            Value::Document(doc) => self.filter.apply(doc),
            _ => {
                let mut doc = Document::new();
                doc.put("$", value.clone())?;
                self.filter.apply(&doc)
            }
        }
    }
}

impl Display for ElementMatchFilter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "(elemMatch {})", self.filter)
    }
}

impl FilterProvider for ElementMatchFilter {
    #[inline]
    fn apply(&self, entry: &Document) -> NitriteResult<bool> {
        if self.has_element_match_filter() {
            log::error!("ElementMatchFilter {} cannot have another ElementMatchFilter {}", self, self.filter);
            return Err(NitriteError::new(
                "ElementMatchFilter cannot have another ElementMatchFilter",
                ErrorKind::FilterError,
            ));
        }

        if self.has_text_filter() {
            log::error!("ElementMatchFilter {} cannot have TextFilter {}", self, self.filter);
            return Err(NitriteError::new(
                "ElementMatchFilter cannot have TextFilter",
                ErrorKind::FilterError,
            ));
        }

        let field_name = self.field_name.get()
            .ok_or_else(|| NitriteError::new("Field name not initialized", ErrorKind::InvalidFieldName))?;
        let value = entry.get(field_name)?;
        if value.is_null() {
            return Ok(false);
        }

        if let Value::Array(array) = value {
            return self.matches(array);
        }

        log::error!(
            "ElementMatchFilter can only be applied on array field, found {}",
            value
        );
        Err(NitriteError::new(
            "ElementMatchFilter can only be applied on array field",
            ErrorKind::FilterError,
        ))
    }

    fn get_collection_name(&self) -> NitriteResult<String> {
        self.collection_name.get()
            .cloned()
            .ok_or_else(|| {
                log::error!("Collection name is not set for filter {}", self);
                NitriteError::new(
                    "Collection name is not set",
                    ErrorKind::InvalidOperation,
                )
            })
    }

    fn set_collection_name(&self, collection_name: String) -> NitriteResult<()> {
        self.collection_name.get_or_init(|| collection_name);
        Ok(())
    }

    fn has_field(&self) -> bool {
        true
    }

    fn get_field_name(&self) -> NitriteResult<String> {
        self.field_name.get()
            .cloned()
            .ok_or_else(|| NitriteError::new("Field name not initialized", ErrorKind::InvalidFieldName))
    }

    fn set_field_name(&self, field_name: String) -> NitriteResult<()> {
        self.field_name.get_or_init(|| field_name);
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::collection::Document;
    use crate::filter::basic_filters::EqualsFilter;

    #[test]
    fn test_regex_filter_apply() {
        let filter = RegexFilter::new("field".to_string(), "test.*".to_string());
        let mut doc = Document::new();
        doc.put("field", Value::String("test123".to_string()))
            .unwrap();
        assert!(filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_regex_filter_apply_negative() {
        let filter = RegexFilter::new("field".to_string(), "test.*".to_string());
        let mut doc = Document::new();
        doc.put("field", Value::String("no_match".to_string()))
            .unwrap();
        assert!(!filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_text_filter_apply() {
        let filter = TextFilter::new("field".to_string(), "test".to_string(), false);
        let mut doc = Document::new();
        doc.put("field", Value::String("this is a test".to_string()))
            .unwrap();
        assert!(filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_text_filter_apply_negative() {
        let filter = TextFilter::new("field".to_string(), "test".to_string(), false);
        let mut doc = Document::new();
        doc.put("field", Value::String("no match".to_string()))
            .unwrap();
        assert!(!filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_element_match_filter_apply() {
        let inner_filter = EqualsFilter::new("inner_field".to_string(), Value::I32(42));
        let filter =
            ElementMatchFilter::new("field".to_string(), Filter::new(inner_filter));
        let mut doc = Document::new();
        let mut inner_doc = Document::new();
        inner_doc.put("inner_field", Value::I32(42)).unwrap();
        doc.put("field", Value::Array(vec![Value::Document(inner_doc)]))
            .unwrap();
        assert!(filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_element_match_filter_apply_negative() {
        let inner_filter = EqualsFilter::new("inner_field".to_string(), Value::I32(42));
        let filter =
            ElementMatchFilter::new("field".to_string(), Filter::new(inner_filter));
        let mut doc = Document::new();
        let mut inner_doc = Document::new();
        inner_doc.put("inner_field", Value::I32(43)).unwrap();
        doc.put("field", Value::Array(vec![Value::Document(inner_doc)]))
            .unwrap();
        assert!(!filter.apply(&doc).unwrap());
    }

    // Invalid regex pattern handling tests
    #[test]
    fn test_regex_filter_handles_invalid_pattern_in_constructor() {
        // Invalid regex pattern - should not panic, but pattern will be uninitialized
        let filter = RegexFilter::new("field".to_string(), "(?P<invalid>".to_string());
        // Creation should not panic
        assert!(filter.field_name.get().is_some());
        assert!(filter.field_value.get().is_some());
        // Pattern should not be initialized due to invalid regex
        assert!(filter.pattern.get().is_none());
    }

    #[test]
    fn test_regex_filter_valid_pattern_initializes() {
        let filter = RegexFilter::new("field".to_string(), "test.*".to_string());
        // Valid pattern should be initialized
        assert!(filter.pattern.get().is_some());
        let mut doc = Document::new();
        doc.put("field", Value::String("test123".to_string())).unwrap();
        assert!(filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_regex_filter_set_invalid_field_value_returns_error() {
        let filter = RegexFilter::new("field".to_string(), "valid.*".to_string());
        let result = filter.set_field_value(Value::String("(?P<invalid>".to_string()));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid regex"));
    }

    #[test]
    fn test_regex_filter_display_with_initialized_values() {
        let filter = RegexFilter::new("email".to_string(), r"^[a-z]+@.*\.com$".to_string());
        let display_str = format!("{}", filter);
        assert!(display_str.contains("email"));
        assert!(display_str.contains("=~"));
    }

    #[test]
    fn test_regex_filter_apply_with_invalid_pattern_returns_error() {
        // Create filter with invalid pattern through constructor
        let filter = RegexFilter::new("field".to_string(), "(?P<invalid>".to_string());
        let mut doc = Document::new();
        doc.put("field", Value::String("test".to_string())).unwrap();
        let result = filter.apply(&doc);
        // Should return error due to invalid pattern
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid regex"));
    }

    #[test]
    fn test_regex_filter_get_field_name_error_handling() {
        let filter = RegexFilter::new("test_field".to_string(), "test.*".to_string());
        let field_name = filter.get_field_name().unwrap();
        assert_eq!(field_name, "test_field");
    }

    // TextFilter tokenizer initialization and handling tests
    #[test]
    fn test_text_filter_basic_apply() {
        let filter = TextFilter::new("content".to_string(), "hello".to_string(), false);
        
        let mut doc = Document::new();
        doc.put("content", Value::String("hello world example".to_string())).unwrap();
        // This tests basic text matching without needing a tokenizer
        assert!(filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_text_filter_case_insensitive_apply() {
        let filter = TextFilter::new("field".to_string(), "TEST".to_string(), false);
        let mut doc = Document::new();
        doc.put("field", Value::String("this is a test".to_string())).unwrap();
        // Case-insensitive match should work
        assert!(filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_text_filter_case_sensitive_apply() {
        let filter = TextFilter::new("field".to_string(), "Test".to_string(), true);
        let mut doc = Document::new();
        doc.put("field", Value::String("this is a test".to_string())).unwrap();
        // Case-sensitive match should fail
        assert!(!filter.apply(&doc).unwrap());
    }

    #[test]
    fn test_text_filter_display_case_sensitive() {
        let filter = TextFilter::new("body".to_string(), "search_term".to_string(), true);
        let display_str = format!("{}", filter);
        assert!(display_str.contains("case_sensitive"));
        assert!(display_str.contains("body"));
    }

    #[test]
    fn test_text_filter_display_case_insensitive() {
        let filter = TextFilter::new("body".to_string(), "search_term".to_string(), false);
        let display_str = format!("{}", filter);
        assert!(display_str.contains("case_insensitive"));
        assert!(display_str.contains("body"));
    }

    #[test]
    fn test_text_filter_set_and_get_collection_name() {
        let filter = TextFilter::new("field".to_string(), "value".to_string(), false);
        filter.set_collection_name("my_collection".to_string()).unwrap();
        let name = filter.get_collection_name().unwrap();
        assert_eq!(name, "my_collection");
    }

    #[test]
    fn test_text_filter_get_field_name() {
        let filter = TextFilter::new("search_field".to_string(), "value".to_string(), false);
        let field_name = filter.get_field_name().unwrap();
        assert_eq!(field_name, "search_field");
    }

    #[test]
    fn test_text_filter_set_invalid_field_value_type() {
        let filter = TextFilter::new("field".to_string(), "value".to_string(), false);
        let result = filter.set_field_value(Value::I32(42));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not a string"));
    }
    
    #[test]
    fn test_element_match_filter_safe_field_name_access() {
        // Verify ElementMatchFilter::get_field_name uses safe access
        let inner_filter = EqualsFilter::new("inner".to_string(), Value::I32(1));
        let filter = ElementMatchFilter::new("items".to_string(), Filter::new(inner_filter));
        
        let result = filter.get_field_name();
        assert!(result.is_ok());
        let field_name = result.unwrap();
        assert_eq!(field_name, "items");
    }

    #[test]
    fn test_element_match_filter_safe_collection_name_access() {
        // Verify ElementMatchFilter::get_collection_name uses safe access
        let inner_filter = EqualsFilter::new("inner".to_string(), Value::I32(1));
        let filter = ElementMatchFilter::new("field".to_string(), Filter::new(inner_filter));
        
        filter.set_collection_name("test_collection".to_string()).unwrap();
        let result = filter.get_collection_name();
        assert!(result.is_ok());
        let name = result.unwrap();
        assert_eq!(name, "test_collection");
    }

    #[test]
    fn test_element_match_filter_collection_name_not_set_error() {
        // Verify safe error handling when collection name not initialized
        let inner_filter = EqualsFilter::new("inner".to_string(), Value::I32(1));
        let filter = ElementMatchFilter::new("field".to_string(), Filter::new(inner_filter));
        
        let result = filter.get_collection_name();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("Collection name is not set"));
    }

    #[test]
    fn test_regex_filter_safe_string_extraction_in_apply() {
        // Verify RegexFilter::apply safely handles string extraction
        let filter = RegexFilter::new("email".to_string(), r"^[a-z]+@.*\.com$".to_string());
        
        let mut doc = Document::new();
        doc.put("email", Value::String("user@example.com".to_string())).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_regex_filter_safe_handle_non_string_field() {
        // Verify RegexFilter::apply returns false for non-string fields
        let filter = RegexFilter::new("count".to_string(), r"\d+".to_string());
        
        let mut doc = Document::new();
        doc.put("count", Value::I32(42)).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    fn test_text_filter_safe_string_unwrapping_case_sensitive() {
        // Verify TextFilter::apply safely unwraps string with case sensitivity
        let filter = TextFilter::new("content".to_string(), "SEARCH".to_string(), true);
        
        let mut doc = Document::new();
        doc.put("content", Value::String("this is SEARCH term".to_string())).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_text_filter_safe_string_unwrapping_case_insensitive() {
        // Verify TextFilter::apply safely unwraps string case-insensitive
        let filter = TextFilter::new("content".to_string(), "search".to_string(), false);
        
        let mut doc = Document::new();
        doc.put("content", Value::String("this is SEARCH term".to_string())).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_element_match_filter_apply_with_empty_array() {
        // Verify ElementMatchFilter::apply safely handles empty arrays
        let inner_filter = EqualsFilter::new("value".to_string(), Value::I32(42));
        let filter = ElementMatchFilter::new("items".to_string(), Filter::new(inner_filter));
        
        let mut doc = Document::new();
        doc.put("items", Value::Array(vec![])).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    fn test_regex_filter_complex_pattern_matches() {
        // Verify regex patterns work correctly with safe extraction
        let pattern = r"^[A-Z][a-z]*$";
        let filter = RegexFilter::new("name".to_string(), pattern.to_string());
        
        let mut doc = Document::new();
        doc.put("name", Value::String("Alice".to_string())).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_element_match_filter_with_multiple_elements() {
        // Verify ElementMatchFilter correctly matches first matching element
        let inner_filter = EqualsFilter::new("status".to_string(), Value::String("active".to_string()));
        let filter = ElementMatchFilter::new("users".to_string(), Filter::new(inner_filter));
        
        let mut doc = Document::new();
        let mut user1 = Document::new();
        user1.put("status", Value::String("inactive".to_string())).unwrap();
        let mut user2 = Document::new();
        user2.put("status", Value::String("active".to_string())).unwrap();
        
        doc.put("users", Value::Array(vec![
            Value::Document(user1),
            Value::Document(user2),
        ])).unwrap();
        
        let result = filter.apply(&doc);
        assert!(result.is_ok());
        assert!(result.unwrap());
    }
}