1use std::collections::HashMap;
21use std::path::Path;
22use std::sync::Arc;
23
24use anyhow::{Result, anyhow};
25use znippy_common::GUNNAR_GRAPH_MODULE;
26use znippy_common::arrow::array::{
27 Array, Int64Array, Int64Builder, ListBuilder, StringArray, StringBuilder, UInt32Array,
28 UInt32Builder,
29};
30use znippy_common::arrow::datatypes::{DataType, Field, Schema};
31use znippy_common::arrow::ipc::reader::StreamReader;
32use znippy_common::arrow::record_batch::RecordBatch;
33use znippy_common::read_reserved_section_bytes;
34
35#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct CommitNode {
38 pub oid: String,
39 pub parents: Vec<String>,
40 pub tree: Option<String>,
41 pub committer_time: Option<i64>,
43 pub generation: u32,
45}
46
47pub fn graph_schema() -> Arc<Schema> {
48 Arc::new(Schema::new(vec![
49 Field::new("oid", DataType::Utf8, false),
50 Field::new(
51 "parents",
52 DataType::List(Arc::new(Field::new("item", DataType::Utf8, true))),
53 false,
54 ),
55 Field::new("tree", DataType::Utf8, true),
56 Field::new("committer_time", DataType::Int64, true),
57 Field::new("generation", DataType::UInt32, false),
58 ]))
59}
60
61pub fn assign_generations(mut nodes: Vec<CommitNode>) -> Vec<CommitNode> {
68 let index: HashMap<&str, usize> = nodes
69 .iter()
70 .enumerate()
71 .map(|(i, n)| (n.oid.as_str(), i))
72 .collect();
73
74 let n = nodes.len();
76 let mut parent_ids: Vec<Vec<usize>> = Vec::with_capacity(n);
77 let mut children: Vec<Vec<usize>> = vec![Vec::new(); n];
78 let mut indeg: Vec<usize> = vec![0; n];
79 for (i, node) in nodes.iter().enumerate() {
80 let mut ps = Vec::new();
81 for p in &node.parents {
82 if let Some(&pi) = index.get(p.as_str()) {
83 if pi != i {
84 ps.push(pi);
85 }
86 }
87 }
88 ps.sort_unstable();
89 ps.dedup();
90 indeg[i] = ps.len();
91 for &pi in &ps {
92 children[pi].push(i);
93 }
94 parent_ids.push(ps);
95 }
96
97 let mut generation = vec![1u32; n];
98 let mut queue: Vec<usize> = (0..n).filter(|&i| indeg[i] == 0).collect();
99 let mut head = 0usize;
100 let mut settled = 0usize;
101 while head < queue.len() {
102 let i = queue[head];
103 head += 1;
104 settled += 1;
105 let g = generation[i];
106 for &c in &children[i] {
107 generation[c] = generation[c].max(g.saturating_add(1));
108 indeg[c] -= 1;
109 if indeg[c] == 0 {
110 queue.push(c);
111 }
112 }
113 }
114 debug_assert!(settled == n || n == 0);
118
119 for (i, node) in nodes.iter_mut().enumerate() {
120 node.generation = generation[i];
121 }
122 nodes.sort_by(|a, b| a.generation.cmp(&b.generation).then_with(|| a.oid.cmp(&b.oid)));
123 nodes
124}
125
126pub fn build_graph_batch(nodes: &[CommitNode]) -> Result<RecordBatch> {
128 let n = nodes.len();
129 let mut oid_b = StringBuilder::with_capacity(n, n * 64);
130 let mut parents_b = ListBuilder::new(StringBuilder::new());
131 let mut tree_b = StringBuilder::with_capacity(n, n * 64);
132 let mut time_b = Int64Builder::with_capacity(n);
133 let mut gen_b = UInt32Builder::with_capacity(n);
134
135 for node in nodes {
136 oid_b.append_value(&node.oid);
137 for p in &node.parents {
138 parents_b.values().append_value(p);
139 }
140 parents_b.append(true);
141 match &node.tree {
142 Some(t) => tree_b.append_value(t),
143 None => tree_b.append_null(),
144 }
145 match node.committer_time {
146 Some(t) => time_b.append_value(t),
147 None => time_b.append_null(),
148 }
149 gen_b.append_value(node.generation);
150 }
151
152 RecordBatch::try_new(
153 graph_schema(),
154 vec![
155 Arc::new(oid_b.finish()),
156 Arc::new(parents_b.finish()),
157 Arc::new(tree_b.finish()),
158 Arc::new(time_b.finish()),
159 Arc::new(gen_b.finish()),
160 ],
161 )
162 .map_err(|e| anyhow!("commit-graph batch: {e}"))
163}
164
165pub fn decode_graph(bytes: &[u8]) -> Result<Vec<CommitNode>> {
167 let reader = StreamReader::try_new(std::io::Cursor::new(bytes), None)
168 .map_err(|e| anyhow!("commit-graph reader: {e}"))?;
169 let mut out = Vec::new();
170 for batch in reader {
171 let batch = batch.map_err(|e| anyhow!("commit-graph batch read: {e}"))?;
172 let oids = col::<StringArray>(&batch, "oid")?;
173 let parents = batch
174 .column_by_name("parents")
175 .ok_or_else(|| anyhow!("commit-graph: no `parents` column"))?
176 .as_any()
177 .downcast_ref::<znippy_common::arrow::array::ListArray>()
178 .ok_or_else(|| anyhow!("commit-graph: `parents` is not a list"))?;
179 let trees = col::<StringArray>(&batch, "tree")?;
180 let times = col::<Int64Array>(&batch, "committer_time")?;
181 let gens = col::<UInt32Array>(&batch, "generation")?;
182
183 for i in 0..batch.num_rows() {
184 let plist = parents.value(i);
185 let plist = plist
186 .as_any()
187 .downcast_ref::<StringArray>()
188 .ok_or_else(|| anyhow!("commit-graph: parent items are not strings"))?;
189 out.push(CommitNode {
190 oid: oids.value(i).to_string(),
191 parents: (0..plist.len()).map(|j| plist.value(j).to_string()).collect(),
192 tree: (!trees.is_null(i)).then(|| trees.value(i).to_string()),
193 committer_time: (!times.is_null(i)).then(|| times.value(i)),
194 generation: gens.value(i),
195 });
196 }
197 }
198 Ok(out)
199}
200
201pub fn read_graph(archive: &Path) -> Result<Option<Vec<CommitNode>>> {
203 match read_reserved_section_bytes(archive, GUNNAR_GRAPH_MODULE)? {
204 Some(b) => Ok(Some(decode_graph(&b)?)),
205 None => Ok(None),
206 }
207}
208
209fn col<'a, T: Array + 'static>(batch: &'a RecordBatch, name: &str) -> Result<&'a T> {
210 batch
211 .column_by_name(name)
212 .ok_or_else(|| anyhow!("commit-graph: no `{name}` column"))?
213 .as_any()
214 .downcast_ref::<T>()
215 .ok_or_else(|| anyhow!("commit-graph: `{name}` has an unexpected type"))
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221
222 fn node(oid: &str, parents: &[&str]) -> CommitNode {
223 CommitNode {
224 oid: oid.to_string(),
225 parents: parents.iter().map(|s| s.to_string()).collect(),
226 tree: Some(format!("t{}", &oid[1..])),
227 committer_time: Some(1_700_000_000),
228 generation: 0,
229 }
230 }
231
232 fn hexid(c: char) -> String {
233 std::iter::repeat_n(c, 40).collect()
234 }
235
236 #[test]
237 fn generations_are_one_plus_the_max_parent() {
238 let a = hexid('a');
239 let b = hexid('b');
240 let c = hexid('c');
241 let d = hexid('d');
242 let nodes = assign_generations(vec![
244 node(&d, &[&b, &c]),
245 node(&b, &[&a]),
246 node(&c, &[&a]),
247 node(&a, &[]),
248 ]);
249 let g: HashMap<&str, u32> = nodes.iter().map(|n| (n.oid.as_str(), n.generation)).collect();
250 assert_eq!(g[a.as_str()], 1, "root");
251 assert_eq!(g[b.as_str()], 2);
252 assert_eq!(g[c.as_str()], 2);
253 assert_eq!(g[d.as_str()], 3, "merge is 1 + max(2,2)");
254
255 let gens: Vec<u32> = nodes.iter().map(|n| n.generation).collect();
257 assert!(gens.windows(2).all(|w| w[0] <= w[1]), "not topologically ordered: {gens:?}");
258 }
259
260 #[test]
261 fn a_parent_outside_the_archive_does_not_inflate_the_generation() {
262 let a = hexid('a');
263 let missing = hexid('f');
264 let nodes = assign_generations(vec![node(&a, &[&missing])]);
265 assert_eq!(nodes[0].generation, 1, "a commit whose parent is not in the archive is a root here");
266 }
267
268 #[test]
269 fn deep_chain_does_not_blow_the_stack() {
270 let ids: Vec<String> = (0..50_000u32).map(|i| format!("{i:040x}")).collect();
272 let mut nodes = Vec::with_capacity(ids.len());
273 for (i, id) in ids.iter().enumerate() {
274 let parents: Vec<&str> = if i == 0 { vec![] } else { vec![ids[i - 1].as_str()] };
275 nodes.push(node(id, &parents));
276 }
277 let out = assign_generations(nodes);
278 let max = out.iter().map(|n| n.generation).max().unwrap();
279 assert_eq!(max, 50_000);
280 }
281
282 #[test]
283 fn batch_roundtrips_through_arrow_ipc() {
284 let a = hexid('a');
285 let b = hexid('b');
286 let nodes = assign_generations(vec![node(&b, &[&a]), node(&a, &[])]);
287 let batch = build_graph_batch(&nodes).unwrap();
288
289 let mut buf = Vec::new();
290 {
291 let mut w = znippy_common::arrow::ipc::writer::StreamWriter::try_new(
292 &mut buf,
293 &graph_schema(),
294 )
295 .unwrap();
296 w.write(&batch).unwrap();
297 w.finish().unwrap();
298 }
299 let back = decode_graph(&buf).unwrap();
300 assert_eq!(back, nodes);
301 assert_eq!(back[1].parents, vec![a.clone()]);
302 assert_eq!(back[1].generation, 2);
303 }
304}