use std::collections::BTreeMap;
use super::test_support::exec_autocommit;
use crate::graph::Graph;
use crate::overlay::ExecGraph;
use crate::row::Row;
use relate_core::{NodeId, Value};
fn file_node(g: &Graph, content: &str, name: &str, path: &str) -> NodeId {
g.create_node(
vec!["File".into()],
BTreeMap::from([
("content".into(), Value::String(content.into())),
("name".into(), Value::String(name.into())),
("path".into(), Value::String(path.into())),
]),
)
.unwrap()
}
fn file_text_index(g: &Graph, prop: &str) {
exec_autocommit(
g,
&format!("CREATE TEXT INDEX file_{prop} FOR (f:File) ON (f.{prop})"),
)
.unwrap();
}
fn file_candidates(g: &Graph, conjunct: &str) -> Vec<NodeId> {
let parsed = relate_parser::parser::parse_expression_str(conjunct).unwrap();
let expr = relate_front::lower_expr(&parsed).unwrap();
let conjuncts = crate::exec::pushdown::flatten_and_owned(&expr);
let mut ids = crate::pattern_walk::node_candidates(
&ExecGraph::read(g),
&file_pattern(),
&Row::new(),
&BTreeMap::new(),
&conjuncts,
)
.unwrap();
ids.sort_unstable();
ids
}
fn file_pattern() -> relate_ir::NodePattern {
use relate_ir::{LabelExpr, NodePattern};
NodePattern {
variable: Some("f".into()),
labels: vec![("File".into(), Default::default())],
label_expr: Some(LabelExpr::Label("File".into())),
properties: None,
prop_keys: Vec::new(),
where_clause: None,
force_scan: false,
}
}
fn walrecord_files() -> (Graph, NodeId, NodeId, NodeId, NodeId, NodeId) {
let g = Graph::default();
g.begin_write().unwrap();
let content = file_node(&g, "has WalRecord", "a", "a.txt");
let name = file_node(&g, "plain", "WalRecord notes", "b.txt");
let path = file_node(&g, "plain", "b", "dir/WalRecord");
let both = file_node(&g, "WalRecord", "WalRecord", "both.txt");
let none = file_node(&g, "plain", "c", "c.txt");
let _person = g
.create_node(
vec!["Person".into()],
BTreeMap::from([("name".into(), Value::String("WalRecord".into()))]),
)
.unwrap();
g.commit_write();
(g, content, name, path, both, none)
}
fn suffix_files() -> (Graph, NodeId, NodeId, NodeId) {
let g = Graph::default();
g.begin_write().unwrap();
let both = file_node(&g, "has WalRecord", "lib.rs", "src/lib.rs");
let path = file_node(&g, "plain", "notes.txt", "dir/WalRecord");
let _content_only = file_node(&g, "has WalRecord", "notes.txt", "notes.txt");
let suffix_inside = file_node(&g, "has WalRecord", "lib.rs.bak", "bak");
let _rs_only = file_node(&g, "plain", "other.rs", "other.rs");
let _none = file_node(&g, "plain", "c", "c.txt");
g.commit_write();
(g, both, path, suffix_inside)
}
#[test]
fn or_union_candidates_skip_the_non_matching_file() {
let (g, content, name, path, both, none) = walrecord_files();
file_text_index(&g, "content");
file_text_index(&g, "name");
const UNION: &str =
"f.content CONTAINS 'WalRecord' OR f.name CONTAINS 'WalRecord' OR f.path CONTAINS 'WalRecord'";
let scanned = file_candidates(&g, UNION);
let mut every_file = vec![content, name, path, both, none];
every_file.sort_unstable();
assert_eq!(
scanned, every_file,
"a missing path index declines the union and scans :File"
);
file_text_index(&g, "path");
let sought = file_candidates(&g, UNION);
let mut matches = vec![content, name, path, both];
matches.sort_unstable();
assert_eq!(sought, matches);
}
#[test]
fn ends_with_and_intersects_the_text_index() {
let (g, both, path, suffix_inside) = suffix_files();
file_text_index(&g, "content");
file_text_index(&g, "name");
file_text_index(&g, "path");
let sought = file_candidates(
&g,
"(f.content CONTAINS 'WalRecord' AND f.name ENDS WITH '.rs') OR f.path CONTAINS 'WalRecord'",
);
let mut expected = vec![both, path, suffix_inside];
expected.sort_unstable();
assert_eq!(sought, expected);
}
#[test]
fn several_served_or_conjuncts_intersect_their_unions() {
let (g, content, _name, _path, both, _none) = walrecord_files();
file_text_index(&g, "content");
file_text_index(&g, "name");
file_text_index(&g, "path");
let sought = file_candidates(
&g,
"(f.content CONTAINS 'WalRecord' OR f.name CONTAINS 'WalRecord') \
AND (f.content CONTAINS 'Record' OR f.path CONTAINS 'Record')",
);
let mut expected = vec![content, both];
expected.sort_unstable();
assert_eq!(sought, expected);
}