MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / parse_pagerank_rows

Function parse_pagerank_rows

nodedb-client/src/graph_dsl.rs:52–81  ·  view source on GitHub ↗

Parse a `(columns, rows)` PageRank result into `(node_id, rank)` pairs. The result schema is `node_id` (text) + `rank` (float). `rank` may arrive as a float (native protocol) or as a text-encoded number (pgwire), so both are accepted. An empty result with no columns (empty graph) yields an empty vec; any other column-shape mismatch is a structured error — no silent fallback to wrong data.

(
    columns: &[String],
    rows: &[Vec<Value>],
)

Source from the content-addressed store, hash-verified

50/// vec; any other column-shape mismatch is a structured error — no silent
51/// fallback to wrong data.
52pub(crate) fn parse_pagerank_rows(
53 columns: &[String],
54 rows: &[Vec<Value>],
55) -> NodeDbResult<Vec<(String, f64)>> {
56 let node_idx = columns.iter().position(|c| c == "node_id");
57 let rank_idx = columns.iter().position(|c| c == "rank");
58 let (Some(ni), Some(ri)) = (node_idx, rank_idx) else {
59 if rows.is_empty() {
60 return Ok(Vec::new());
61 }
62 return Err(NodeDbError::storage(format!(
63 "unexpected pagerank columns: {columns:?} (expected [node_id, rank])"
64 )));
65 };
66
67 let mut out = Vec::with_capacity(rows.len());
68 for row in rows {
69 let node = row
70 .get(ni)
71 .and_then(|v| v.as_str())
72 .ok_or_else(|| NodeDbError::storage("pagerank row missing node_id"))?
73 .to_string();
74 let rank = row
75 .get(ri)
76 .and_then(value_as_f64)
77 .ok_or_else(|| NodeDbError::storage("pagerank row missing or non-numeric rank"))?;
78 out.push((node, rank));
79 }
80 Ok(out)
81}
82
83/// Coerce a result cell to `f64`, accepting either a native numeric value or a
84/// text-encoded number (the pgwire transport renders floats as text).

Callers 3

graph_pagerank_implMethod · 0.85
graph_pagerank_implMethod · 0.85

Calls 7

to_stringMethod · 0.80
iterMethod · 0.45
is_emptyMethod · 0.45
lenMethod · 0.45
getMethod · 0.45
as_strMethod · 0.45
pushMethod · 0.45

Tested by 1