Basic pres filtering added

This commit is contained in:
William Brown 2018-11-07 16:54:02 +10:00
parent 51797bc125
commit a684b1095a
3 changed files with 50 additions and 8 deletions

View file

@ -146,7 +146,7 @@ impl Backend {
} }
// Take filter, and AuditEvent ref? // Take filter, and AuditEvent ref?
pub fn search(&self, filter: Filter) -> Vec<Entry> { pub fn search(&self, filt: Filter) -> Vec<Entry> {
// Do things // Do things
// Alloc a vec for the entries. // Alloc a vec for the entries.
// FIXME: Make this actually a good size for the result set ... // FIXME: Make this actually a good size for the result set ...
@ -177,10 +177,10 @@ impl Backend {
// Now, de-serialise the raw_entries back to entries // Now, de-serialise the raw_entries back to entries
let entries: Vec<Entry> = raw_entries let entries: Vec<Entry> = raw_entries
.iter() .iter()
.map(|val| { .filter_map(|val| {
// TODO: Should we do better than unwrap? // TODO: Should we do better than unwrap?
let e = serde_json::from_str(val.as_str()).unwrap(); let e: Entry = serde_json::from_str(val.as_str()).unwrap();
if filter.entry_match_no_index(e) { if filt.entry_match_no_index(&e) {
Some(e) Some(e)
} else { } else {
None None
@ -220,6 +220,7 @@ mod tests {
extern crate tokio; extern crate tokio;
use super::super::entry::Entry; use super::super::entry::Entry;
use super::super::filter::Filter;
use super::super::log::{self, EventLog, LogEvent}; use super::super::log::{self, EventLog, LogEvent};
use super::{Backend, BackendError}; use super::{Backend, BackendError};
@ -261,7 +262,9 @@ mod tests {
assert!(single_result.is_ok()); assert!(single_result.is_ok());
let entries = be.search(); // Construct a filter
let filt = Filter::Pres(String::from("userid"));
let entries = be.search(filt);
println!("{:?}", entries); println!("{:?}", entries);
// There should only be one entry so is this enough? // There should only be one entry so is this enough?

View file

@ -56,6 +56,10 @@ impl Entry {
// We need access to the current system schema here now ... // We need access to the current system schema here now ...
true true
} }
pub fn pres(&self, attr: &str) -> bool {
self.attrs.contains_key(attr)
}
} }
// pub trait Entry { // pub trait Entry {
@ -167,4 +171,18 @@ mod tests {
println!("d: {}", d.as_str()); println!("d: {}", d.as_str());
} }
#[test]
fn test_entry_pres() {
let mut e: Entry = Entry::new();
e.add_ava(String::from("userid"), String::from("william"))
.unwrap();
assert!(e.validate());
assert!(e.pres("userid"));
assert!(!e.pres("name"));
}
} }

View file

@ -12,7 +12,7 @@ pub enum Filter {
// This is attr - value // This is attr - value
Eq(String, String), Eq(String, String),
Sub(String, String), Sub(String, String),
Pres(String, String), Pres(String),
Or(Vec<Filter>), Or(Vec<Filter>),
And(Vec<Filter>), And(Vec<Filter>),
Not(Vec<Filter>), Not(Vec<Filter>),
@ -39,9 +39,30 @@ impl Filter {
// What other parse types do we need? // What other parse types do we need?
// Assert if this filter matches the entry (no index) // Assert if this filter matches the entry (no index)
pub fn entry_match_no_index(e: Entry) -> bool { pub fn entry_match_no_index(&self, e: &Entry) -> bool {
// Go through the filter components and check them in the entry. // Go through the filter components and check them in the entry.
false // This is recursive!!!!
match self {
Filter::Eq(_, _) => {
false
}
Filter::Sub(_, _) => {
false
}
Filter::Pres(attr) => {
// Given attr, is is present in the entry?
e.pres(attr.as_str())
}
Filter::Or(_) => {
false
}
Filter::And(_) => {
false
}
Filter::Not(_) => {
false
}
}
} }
} }