use aria_engine_backends::ipo::{IpoEdge, IpoNode, NodeRecord};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet, HashMap};
#[must_use]
pub fn norm(s: &str) -> String {
s.to_ascii_lowercase().replace('-', "_")
}
pub(crate) struct NodeTok {
pub kindlike: Vec<String>,
pub tags: Vec<String>,
}
pub(crate) struct GraphIndex<'a> {
pub nodes: &'a [IpoNode],
pub edges: &'a [IpoEdge],
pub records: &'a BTreeMap<u64, NodeRecord>,
pub tok: Vec<NodeTok>,
by_kind: HashMap<String, Vec<usize>>,
by_kindlike: HashMap<String, Vec<usize>>,
by_tag: HashMap<String, Vec<usize>>,
by_rel: HashMap<String, Vec<usize>>,
first_prop: HashMap<String, Value>,
id_to_idx: HashMap<u64, usize>,
}
fn push_unique(map: &mut HashMap<String, Vec<usize>>, key: String, idx: usize) {
let v = map.entry(key).or_default();
if v.last() != Some(&idx) {
v.push(idx);
}
}
impl<'a> GraphIndex<'a> {
pub fn build(
nodes: &'a [IpoNode],
edges: &'a [IpoEdge],
records: &'a BTreeMap<u64, NodeRecord>,
) -> Self {
let mut tok = Vec::with_capacity(nodes.len());
let mut by_kind: HashMap<String, Vec<usize>> = HashMap::new();
let mut by_kindlike: HashMap<String, Vec<usize>> = HashMap::new();
let mut by_tag: HashMap<String, Vec<usize>> = HashMap::new();
let mut id_to_idx = HashMap::with_capacity(nodes.len());
for (i, n) in nodes.iter().enumerate() {
id_to_idx.entry(n.id).or_insert(i);
let kind = norm(n.node_type.as_str());
let mut kindlike = vec![kind.clone()];
let mut tags: Vec<String> = Vec::new();
if let Some(rec) = records.get(&n.id) {
if let Some(l) = rec.label.as_deref() {
kindlike.push(norm(l));
}
if let Some(Value::String(k)) =
rec.properties.get("kind").or_else(|| rec.properties.get("type"))
{
kindlike.push(norm(k));
}
match rec.properties.get("tags") {
Some(Value::Array(arr)) => {
tags.extend(arr.iter().filter_map(Value::as_str).map(norm));
}
Some(Value::String(s)) => tags.push(norm(s)),
_ => {
if let Some(s) = rec.properties.get("tag").and_then(Value::as_str) {
tags.push(norm(s));
}
}
}
tags.extend(crate::typecast::cast_tags(rec).iter().map(|t| norm(t)));
}
kindlike.sort_unstable();
kindlike.dedup();
tags.sort_unstable();
tags.dedup();
push_unique(&mut by_kind, kind.clone(), i);
for k in &kindlike {
push_unique(&mut by_kindlike, k.clone(), i);
}
for t in &tags {
push_unique(&mut by_tag, t.clone(), i);
}
tok.push(NodeTok { kindlike, tags });
}
let mut by_rel: HashMap<String, Vec<usize>> = HashMap::new();
for (i, e) in edges.iter().enumerate() {
push_unique(&mut by_rel, norm(e.edge_type.as_str()), i);
}
let mut first_prop: HashMap<String, Value> = HashMap::new();
for rec in records.values() {
for (k, v) in &rec.properties {
first_prop.entry(k.clone()).or_insert_with(|| v.clone());
}
}
Self {
nodes,
edges,
records,
tok,
by_kind,
by_kindlike,
by_tag,
by_rel,
first_prop,
id_to_idx,
}
}
fn gather(map: &HashMap<String, Vec<usize>>, tokens: &[String], out: &mut BTreeSet<usize>) {
for t in tokens {
if let Some(v) = map.get(t) {
out.extend(v.iter().copied());
}
}
}
pub fn nodes_by_kind(&self, tokens: &[String], out: &mut BTreeSet<usize>) {
Self::gather(&self.by_kind, tokens, out);
}
pub fn nodes_by_kindlike(&self, tokens: &[String], out: &mut BTreeSet<usize>) {
Self::gather(&self.by_kindlike, tokens, out);
}
pub fn nodes_by_tag(&self, tokens: &[String], out: &mut BTreeSet<usize>) {
Self::gather(&self.by_tag, tokens, out);
}
pub fn edges_by_rel(&self, tokens: &[String]) -> BTreeSet<usize> {
let mut out = BTreeSet::new();
if tokens.is_empty() {
out.extend(0..self.edges.len());
} else {
Self::gather(&self.by_rel, tokens, &mut out);
}
out
}
pub fn first_prop(&self, key: &str) -> Option<&Value> {
self.first_prop.get(key)
}
pub fn idx_of(&self, id: u64) -> Option<usize> {
self.id_to_idx.get(&id).copied()
}
pub fn kindlike_hits(&self, idx: usize, allowed: &[String]) -> bool {
self.tok[idx].kindlike.iter().any(|k| allowed.contains(k))
}
pub fn tags_of(&self, idx: usize) -> &[String] {
&self.tok[idx].tags
}
pub fn has_tags(&self) -> bool {
!self.by_tag.is_empty()
}
}