enrichment/
lib.rs

1#![deny(warnings)]
2
3pub mod find_enrichment_table_records;
4pub mod get_enrichment_table_record;
5pub mod tables;
6
7#[cfg(test)]
8mod test_util;
9mod vrl_util;
10
11use dyn_clone::DynClone;
12pub use tables::{TableRegistry, TableSearch};
13use vrl::compiler::Function;
14use vrl::value::{ObjectMap, Value};
15
16#[derive(Copy, Clone, Debug, PartialEq, Eq)]
17pub struct IndexHandle(pub usize);
18
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub enum Condition<'a> {
21    /// Condition exactly matches the field value.
22    Equals { field: &'a str, value: Value },
23    /// The date in the field is between from and to (inclusive).
24    BetweenDates {
25        field: &'a str,
26        from: chrono::DateTime<chrono::Utc>,
27        to: chrono::DateTime<chrono::Utc>,
28    },
29    /// The date in the field is greater than or equal to `from`.
30    FromDate {
31        field: &'a str,
32        from: chrono::DateTime<chrono::Utc>,
33    },
34    /// The date in the field is less than or equal to `to`.
35    ToDate {
36        field: &'a str,
37        to: chrono::DateTime<chrono::Utc>,
38    },
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum Case {
43    Sensitive,
44    Insensitive,
45}
46
47/// Enrichment tables represent additional data sources that can be used to enrich the event data
48/// passing through Vector.
49pub trait Table: DynClone {
50    /// Search the enrichment table data with the given condition.
51    /// All conditions must match (AND).
52    ///
53    /// # Errors
54    /// Errors if no rows, or more than 1 row is found.
55    fn find_table_row<'a>(
56        &self,
57        case: Case,
58        condition: &'a [Condition<'a>],
59        select: Option<&[String]>,
60        wildcard: Option<&Value>,
61        index: Option<IndexHandle>,
62    ) -> Result<ObjectMap, String>;
63
64    /// Search the enrichment table data with the given condition.
65    /// All conditions must match (AND).
66    /// Can return multiple matched records
67    fn find_table_rows<'a>(
68        &self,
69        case: Case,
70        condition: &'a [Condition<'a>],
71        select: Option<&[String]>,
72        wildcard: Option<&Value>,
73        index: Option<IndexHandle>,
74    ) -> Result<Vec<ObjectMap>, String>;
75
76    /// Hints to the enrichment table what data is going to be searched to allow it to index the
77    /// data in advance.
78    ///
79    /// # Errors
80    /// Errors if the fields are not in the table.
81    fn add_index(&mut self, case: Case, fields: &[&str]) -> Result<IndexHandle, String>;
82
83    /// Returns a list of the field names that are in each index
84    fn index_fields(&self) -> Vec<(Case, Vec<String>)>;
85
86    /// Returns true if the underlying data has changed and the table needs reloading.
87    fn needs_reload(&self) -> bool;
88}
89
90dyn_clone::clone_trait_object!(Table);
91
92pub fn vrl_functions() -> Vec<Box<dyn Function>> {
93    vec![
94        Box::new(get_enrichment_table_record::GetEnrichmentTableRecord) as _,
95        Box::new(find_enrichment_table_records::FindEnrichmentTableRecords) as _,
96    ]
97}