heliosdb-nano 3.23.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Ingestion adapters (FR 4 §4.2).
//!
//! Each adapter takes a **structured** user table — one that the
//! caller has already loaded (via INSERT, COPY, an external parser,
//! etc.) — and projects it into the universal `_hdb_graph_*` schema.
//! The Nano project does not ship raw mbox or JSON-tracker parsers;
//! users either call the adapter against pre-parsed rows or use the
//! optional helper constructors on each adapter's options struct.
//!
//! The four adapters are:
//!
//! * [`ingest_docs`]    — text column → DocSection + DocChunk nodes.
//!                        Splits on Markdown-style heading boundaries
//!                        (`# heading`, `## heading`) when `chunk_by =
//!                        Headings`; otherwise one DocChunk per row.
//!                        `PART_OF` edges connect chunks to their
//!                        section.
//! * [`ingest_email`]   — Email + Person nodes.  AUTHORED_BY /
//!                        REPLIES_TO / SENT_TO edges.
//! * [`ingest_issues`]  — Issue + Comment + Person nodes.
//!                        REPORTED_BY / REPLIES_TO / FIXED_BY edges.
//! * [`ingest_qa`]      — InvestorQuestion + Answer + Person nodes.
//!                        ASKS_ABOUT / ANSWERED_BY edges.
//!
//! Idempotent via `source_ref` keys (`doc:<id>`, `email:<mid>`, …).

use std::collections::{HashMap, HashSet};

use crate::{EmbeddedDatabase, Error, Result, Value};

use super::schema::ensure_tables;

// -- ingest_docs -------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct IngestDocsOptions {
    pub source_table: String,
    /// Column that holds the unique stable id per row (path, guid, …).
    pub id_col: String,
    /// Column that holds the full text body.
    pub text_col: String,
    /// Optional column for the title; falls back to the first line of
    /// the body.
    pub title_col: Option<String>,
    /// How to split the body.
    pub chunk_by: ChunkStrategy,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChunkStrategy {
    /// One DocChunk per row, no splitting.
    Row,
    /// Split on Markdown atx headings (`#`, `##`, …).  Each heading
    /// becomes a DocSection; the text beneath becomes a DocChunk
    /// with a `PART_OF` edge into the section.
    Headings,
}

#[derive(Debug, Clone, Default)]
pub struct IngestStats {
    pub nodes_added: u64,
    pub edges_added: u64,
    pub rows_seen: u64,
    pub rows_skipped: u64,
}

pub fn ingest_docs(
    db: &EmbeddedDatabase,
    opts: &IngestDocsOptions,
) -> Result<IngestStats> {
    ensure_tables(db)?;
    let mut stats = IngestStats::default();
    let cols = match &opts.title_col {
        Some(tc) => format!("{id}, {text}, {title}", id = opts.id_col, text = opts.text_col, title = tc),
        None => format!("{id}, {text}", id = opts.id_col, text = opts.text_col),
    };
    let rows = db.query(
        &format!("SELECT {cols} FROM {tbl}", tbl = opts.source_table),
        &[],
    )?;
    // Dedup against what's already projected.
    let existing = source_refs_with_prefix(db, "doc:")?;
    for row in rows {
        stats.rows_seen += 1;
        let id = as_string(row.values.first()).unwrap_or_default();
        if id.is_empty() {
            stats.rows_skipped += 1;
            continue;
        }
        let body = as_string(row.values.get(1)).unwrap_or_default();
        let title = opts
            .title_col
            .as_deref()
            .and_then(|_| as_string(row.values.get(2)))
            .or_else(|| body.lines().next().map(|l| l.trim_start_matches('#').trim().to_string()));

        match opts.chunk_by {
            ChunkStrategy::Row => {
                let src_ref = format!("doc:{id}");
                if existing.contains(&src_ref) {
                    continue;
                }
                let node_id = insert_node(
                    db,
                    "DocChunk",
                    &src_ref,
                    title.as_deref(),
                    Some(&body),
                )?;
                let _ = node_id;
                stats.nodes_added += 1;
            }
            ChunkStrategy::Headings => {
                let chunks = split_markdown_headings(&body);
                let mut section_parent: Option<i64> = None;
                for (i, (level, heading, content)) in chunks.iter().enumerate() {
                    if *level > 0 {
                        // DocSection
                        let section_ref = format!("doc:{id}:section:{i}");
                        if !existing.contains(&section_ref) {
                            let sid = insert_node(
                                db,
                                "DocSection",
                                &section_ref,
                                Some(heading),
                                None,
                            )?;
                            section_parent = Some(sid);
                            stats.nodes_added += 1;
                        }
                    }
                    if !content.trim().is_empty() {
                        let chunk_ref = format!("doc:{id}:chunk:{i}");
                        if existing.contains(&chunk_ref) {
                            continue;
                        }
                        let chunk_title = if !heading.is_empty() {
                            Some(heading.as_str())
                        } else {
                            title.as_deref()
                        };
                        let cid = insert_node(
                            db,
                            "DocChunk",
                            &chunk_ref,
                            chunk_title,
                            Some(content),
                        )?;
                        stats.nodes_added += 1;
                        if let Some(pid) = section_parent {
                            insert_edge(db, cid, pid, "PART_OF", 1.0)?;
                            stats.edges_added += 1;
                        }
                    }
                }
            }
        }
    }
    Ok(stats)
}

fn split_markdown_headings(body: &str) -> Vec<(u32, String, String)> {
    // Returns tuples of (heading_level, heading_text, content_text).
    // level=0 means "leading content with no preceding heading".
    let mut chunks: Vec<(u32, String, String)> = Vec::new();
    let mut cur_level: u32 = 0;
    let mut cur_heading = String::new();
    let mut cur_body = String::new();
    for line in body.lines() {
        let trimmed = line.trim_start();
        let hashes = trimmed.chars().take_while(|c| *c == '#').count();
        if hashes > 0 && hashes <= 6 && trimmed[hashes..].starts_with(' ') {
            // Start of a new heading section — flush the current chunk.
            if !cur_heading.is_empty() || !cur_body.is_empty() {
                chunks.push((cur_level, cur_heading.clone(), cur_body.clone()));
            }
            cur_level = hashes as u32;
            cur_heading = trimmed[hashes..].trim().to_string();
            cur_body.clear();
        } else {
            cur_body.push_str(line);
            cur_body.push('\n');
        }
    }
    if !cur_heading.is_empty() || !cur_body.is_empty() {
        chunks.push((cur_level, cur_heading, cur_body));
    }
    chunks
}

// -- ingest_email ------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct IngestEmailOptions {
    pub source_table: String,
    pub message_id_col: String,
    pub from_col: String,
    pub to_col: Option<String>,
    pub subject_col: Option<String>,
    pub body_col: String,
    pub in_reply_to_col: Option<String>,
}

pub fn ingest_email(
    db: &EmbeddedDatabase,
    opts: &IngestEmailOptions,
) -> Result<IngestStats> {
    ensure_tables(db)?;
    let mut stats = IngestStats::default();

    let mut sel = format!(
        "SELECT {mid}, {from}, {body}",
        mid = opts.message_id_col,
        from = opts.from_col,
        body = opts.body_col,
    );
    if let Some(t) = &opts.to_col { sel.push_str(&format!(", {t}")); } else { sel.push_str(", NULL"); }
    if let Some(s) = &opts.subject_col { sel.push_str(&format!(", {s}")); } else { sel.push_str(", NULL"); }
    if let Some(r) = &opts.in_reply_to_col { sel.push_str(&format!(", {r}")); } else { sel.push_str(", NULL"); }
    sel.push_str(&format!(" FROM {}", opts.source_table));
    let rows = db.query(&sel, &[])?;

    let mut existing_emails = source_refs_with_prefix(db, "email:")?;
    let mut known_persons: HashMap<String, i64> =
        source_refs_map_with_prefix(db, "person:")?;

    // Second pass so we can resolve in_reply_to to its message id.
    let mut mid_to_node: HashMap<String, i64> = HashMap::new();

    for row in &rows {
        stats.rows_seen += 1;
        let mid = as_string(row.values.first()).unwrap_or_default();
        if mid.is_empty() {
            stats.rows_skipped += 1;
            continue;
        }
        let from = as_string(row.values.get(1)).unwrap_or_default();
        let body = as_string(row.values.get(2)).unwrap_or_default();
        let to = as_string(row.values.get(3)).unwrap_or_default();
        let subject = as_string(row.values.get(4)).unwrap_or_default();
        let _ = as_string(row.values.get(5));

        let src_ref = format!("email:{mid}");
        if existing_emails.contains(&src_ref) {
            // Still need to know the node id for in_reply_to resolution.
            if let Some(gid) = lookup_graph_node(db, &src_ref)? {
                mid_to_node.insert(mid.clone(), gid);
            }
            continue;
        }
        let eid = insert_node(
            db,
            "Email",
            &src_ref,
            if subject.is_empty() { None } else { Some(&subject) },
            Some(&body),
        )?;
        mid_to_node.insert(mid.clone(), eid);
        existing_emails.insert(src_ref);
        stats.nodes_added += 1;

        if !from.is_empty() {
            let pid = upsert_person(db, &mut known_persons, &from, &mut stats)?;
            insert_edge(db, eid, pid, "AUTHORED_BY", 1.0)?;
            stats.edges_added += 1;
        }
        if !to.is_empty() {
            for addr in to.split(',').map(str::trim).filter(|s| !s.is_empty()) {
                let pid = upsert_person(db, &mut known_persons, addr, &mut stats)?;
                insert_edge(db, eid, pid, "SENT_TO", 1.0)?;
                stats.edges_added += 1;
            }
        }
    }

    // Second pass: REPLIES_TO.
    for row in &rows {
        let mid = as_string(row.values.first()).unwrap_or_default();
        let reply_to = as_string(row.values.get(5)).unwrap_or_default();
        if reply_to.is_empty() {
            continue;
        }
        let (Some(this), Some(parent)) = (
            mid_to_node.get(&mid).copied(),
            mid_to_node.get(&reply_to).copied(),
        ) else {
            continue;
        };
        insert_edge(db, this, parent, "REPLIES_TO", 1.0)?;
        stats.edges_added += 1;
    }

    Ok(stats)
}

// -- ingest_issues -----------------------------------------------------------

#[derive(Debug, Clone)]
pub struct IngestIssuesOptions {
    pub source_table: String,
    pub id_col: String,
    pub title_col: String,
    pub body_col: String,
    pub reporter_col: Option<String>,
    /// Optional column holding JSON-encoded comments, shape
    /// `[{"author":"..","body":".."}, ...]`. Each comment becomes a
    /// Comment node with a REPLIES_TO edge to the issue.
    pub comments_json_col: Option<String>,
    /// Optional column holding JSON-encoded fix refs (commit shas or
    /// symbol qualified names) to connect via FIXED_BY edges.
    pub fixed_by_json_col: Option<String>,
}

pub fn ingest_issues(
    db: &EmbeddedDatabase,
    opts: &IngestIssuesOptions,
) -> Result<IngestStats> {
    ensure_tables(db)?;
    let mut stats = IngestStats::default();
    let mut sel = format!(
        "SELECT {id}, {title}, {body}",
        id = opts.id_col,
        title = opts.title_col,
        body = opts.body_col,
    );
    if let Some(c) = &opts.reporter_col { sel.push_str(&format!(", {c}")); } else { sel.push_str(", NULL"); }
    if let Some(c) = &opts.comments_json_col { sel.push_str(&format!(", {c}")); } else { sel.push_str(", NULL"); }
    if let Some(c) = &opts.fixed_by_json_col { sel.push_str(&format!(", {c}")); } else { sel.push_str(", NULL"); }
    sel.push_str(&format!(" FROM {}", opts.source_table));
    let rows = db.query(&sel, &[])?;

    let mut existing = source_refs_with_prefix(db, "issue:")?;
    let mut known_persons: HashMap<String, i64> =
        source_refs_map_with_prefix(db, "person:")?;

    for row in rows {
        stats.rows_seen += 1;
        let id = as_string(row.values.first()).unwrap_or_default();
        if id.is_empty() {
            stats.rows_skipped += 1;
            continue;
        }
        let title = as_string(row.values.get(1)).unwrap_or_default();
        let body = as_string(row.values.get(2)).unwrap_or_default();
        let reporter = as_string(row.values.get(3)).unwrap_or_default();
        let comments = as_string(row.values.get(4)).unwrap_or_default();
        let fixed_by = as_string(row.values.get(5)).unwrap_or_default();

        let src_ref = format!("issue:{id}");
        if existing.contains(&src_ref) {
            continue;
        }
        let iid = insert_node(db, "Issue", &src_ref, Some(&title), Some(&body))?;
        existing.insert(src_ref);
        stats.nodes_added += 1;

        if !reporter.is_empty() {
            let pid = upsert_person(db, &mut known_persons, &reporter, &mut stats)?;
            insert_edge(db, iid, pid, "REPORTED_BY", 1.0)?;
            stats.edges_added += 1;
        }

        if !comments.is_empty() {
            if let Ok(arr) = serde_json::from_str::<serde_json::Value>(&comments) {
                if let Some(list) = arr.as_array() {
                    for (ci, c) in list.iter().enumerate() {
                        let author = c.get("author").and_then(|v| v.as_str()).unwrap_or("");
                        let cbody = c.get("body").and_then(|v| v.as_str()).unwrap_or("");
                        if cbody.is_empty() {
                            continue;
                        }
                        let c_ref = format!("issue:{id}:comment:{ci}");
                        let cid = insert_node(
                            db,
                            "Comment",
                            &c_ref,
                            None,
                            Some(cbody),
                        )?;
                        stats.nodes_added += 1;
                        insert_edge(db, cid, iid, "REPLIES_TO", 1.0)?;
                        stats.edges_added += 1;
                        if !author.is_empty() {
                            let pid = upsert_person(
                                db,
                                &mut known_persons,
                                author,
                                &mut stats,
                            )?;
                            insert_edge(db, cid, pid, "AUTHORED_BY", 1.0)?;
                            stats.edges_added += 1;
                        }
                    }
                }
            }
        }

        if !fixed_by.is_empty() {
            if let Ok(arr) = serde_json::from_str::<serde_json::Value>(&fixed_by) {
                if let Some(list) = arr.as_array() {
                    for item in list {
                        let Some(tgt) = item.as_str() else { continue };
                        let sid = upsert_external_ref(db, tgt, "ExternalRef")?;
                        insert_edge(db, iid, sid, "FIXED_BY", 1.0)?;
                        stats.edges_added += 1;
                    }
                }
            }
        }
    }
    Ok(stats)
}

// -- ingest_qa ---------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct IngestQaOptions {
    pub source_table: String,
    pub id_col: String,
    pub question_col: String,
    pub answer_col: Option<String>,
    pub asker_col: Option<String>,
    pub answerer_col: Option<String>,
}

pub fn ingest_qa(
    db: &EmbeddedDatabase,
    opts: &IngestQaOptions,
) -> Result<IngestStats> {
    ensure_tables(db)?;
    let mut stats = IngestStats::default();
    let mut sel = format!(
        "SELECT {id}, {q}",
        id = opts.id_col,
        q = opts.question_col
    );
    if let Some(c) = &opts.answer_col { sel.push_str(&format!(", {c}")); } else { sel.push_str(", NULL"); }
    if let Some(c) = &opts.asker_col { sel.push_str(&format!(", {c}")); } else { sel.push_str(", NULL"); }
    if let Some(c) = &opts.answerer_col { sel.push_str(&format!(", {c}")); } else { sel.push_str(", NULL"); }
    sel.push_str(&format!(" FROM {}", opts.source_table));
    let rows = db.query(&sel, &[])?;

    let mut existing = source_refs_with_prefix(db, "qa:")?;
    let mut known_persons: HashMap<String, i64> =
        source_refs_map_with_prefix(db, "person:")?;

    for row in rows {
        stats.rows_seen += 1;
        let id = as_string(row.values.first()).unwrap_or_default();
        if id.is_empty() {
            stats.rows_skipped += 1;
            continue;
        }
        let question = as_string(row.values.get(1)).unwrap_or_default();
        let answer = as_string(row.values.get(2)).unwrap_or_default();
        let asker = as_string(row.values.get(3)).unwrap_or_default();
        let answerer = as_string(row.values.get(4)).unwrap_or_default();

        let q_ref = format!("qa:{id}:q");
        if existing.contains(&q_ref) {
            continue;
        }
        let qid = insert_node(
            db,
            "InvestorQuestion",
            &q_ref,
            Some(&format!("Q-{id}")),
            Some(&question),
        )?;
        existing.insert(q_ref);
        stats.nodes_added += 1;

        if !asker.is_empty() {
            let pid = upsert_person(db, &mut known_persons, &asker, &mut stats)?;
            insert_edge(db, qid, pid, "AUTHORED_BY", 1.0)?;
            stats.edges_added += 1;
        }

        if !answer.is_empty() {
            let a_ref = format!("qa:{id}:a");
            let aid = insert_node(
                db,
                "Answer",
                &a_ref,
                Some(&format!("A-{id}")),
                Some(&answer),
            )?;
            stats.nodes_added += 1;
            insert_edge(db, aid, qid, "ANSWERED_BY", 1.0)?;
            stats.edges_added += 1;
            if !answerer.is_empty() {
                let pid =
                    upsert_person(db, &mut known_persons, &answerer, &mut stats)?;
                insert_edge(db, aid, pid, "AUTHORED_BY", 1.0)?;
                stats.edges_added += 1;
            }
        }
    }

    Ok(stats)
}

// -- helpers -----------------------------------------------------------------

fn as_string(v: Option<&Value>) -> Option<String> {
    match v {
        Some(Value::String(s)) => Some(s.clone()),
        _ => None,
    }
}

fn insert_node(
    db: &EmbeddedDatabase,
    kind: &str,
    source_ref: &str,
    title: Option<&str>,
    text: Option<&str>,
) -> Result<i64> {
    let (_, rows) = db.execute_params_returning(
        "INSERT INTO _hdb_graph_nodes (node_kind, source_ref, title, text) \
         VALUES ($1, $2, $3, $4) RETURNING node_id",
        &[
            Value::String(kind.into()),
            Value::String(source_ref.into()),
            title.map(|s| Value::String(s.into())).unwrap_or(Value::Null),
            text.map(|s| Value::String(s.into())).unwrap_or(Value::Null),
        ],
    )?;
    rows.first()
        .and_then(|r| r.values.first())
        .and_then(|v| match v {
            Value::Int4(n) => Some(*n as i64),
            Value::Int8(n) => Some(*n),
            _ => None,
        })
        .ok_or_else(|| Error::query_execution("insert_node: no RETURNING node_id"))
}

fn insert_edge(
    db: &EmbeddedDatabase,
    from: i64,
    to: i64,
    kind: &str,
    weight: f64,
) -> Result<()> {
    db.execute_params_returning(
        "INSERT INTO _hdb_graph_edges (from_node, to_node, edge_kind, weight) \
         VALUES ($1, $2, $3, $4)",
        &[
            Value::Int8(from),
            Value::Int8(to),
            Value::String(kind.into()),
            Value::Float8(weight),
        ],
    )?;
    Ok(())
}

fn source_refs_with_prefix(db: &EmbeddedDatabase, prefix: &str) -> Result<HashSet<String>> {
    let rows = db.query_params(
        "SELECT source_ref FROM _hdb_graph_nodes WHERE source_ref LIKE $1",
        &[Value::String(format!("{prefix}%"))],
    )?;
    let mut out = HashSet::with_capacity(rows.len());
    for row in rows {
        if let Some(Value::String(s)) = row.values.first() {
            out.insert(s.clone());
        }
    }
    Ok(out)
}

fn source_refs_map_with_prefix(
    db: &EmbeddedDatabase,
    prefix: &str,
) -> Result<HashMap<String, i64>> {
    let rows = db.query_params(
        "SELECT source_ref, node_id FROM _hdb_graph_nodes WHERE source_ref LIKE $1",
        &[Value::String(format!("{prefix}%"))],
    )?;
    let mut out = HashMap::with_capacity(rows.len());
    for row in rows {
        let s = match row.values.first() {
            Some(Value::String(s)) => s.clone(),
            _ => continue,
        };
        let id = match row.values.get(1) {
            Some(Value::Int4(n)) => *n as i64,
            Some(Value::Int8(n)) => *n,
            _ => continue,
        };
        out.insert(s, id);
    }
    Ok(out)
}

fn lookup_graph_node(db: &EmbeddedDatabase, source_ref: &str) -> Result<Option<i64>> {
    let rows = db.query_params(
        "SELECT node_id FROM _hdb_graph_nodes WHERE source_ref = $1",
        &[Value::String(source_ref.into())],
    )?;
    Ok(rows.first().and_then(|r| r.values.first()).and_then(|v| match v {
        Value::Int4(n) => Some(*n as i64),
        Value::Int8(n) => Some(*n),
        _ => None,
    }))
}

fn upsert_person(
    db: &EmbeddedDatabase,
    cache: &mut HashMap<String, i64>,
    ident: &str,
    stats: &mut IngestStats,
) -> Result<i64> {
    let src_ref = format!("person:{ident}");
    if let Some(id) = cache.get(&src_ref) {
        return Ok(*id);
    }
    let pid = insert_node(db, "Person", &src_ref, Some(ident), None)?;
    cache.insert(src_ref, pid);
    stats.nodes_added += 1;
    Ok(pid)
}

fn upsert_external_ref(db: &EmbeddedDatabase, target: &str, kind: &str) -> Result<i64> {
    let src_ref = format!("ext:{target}");
    if let Some(id) = lookup_graph_node(db, &src_ref)? {
        return Ok(id);
    }
    insert_node(db, kind, &src_ref, Some(target), None)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn heading_splitter_simple() {
        let chunks = split_markdown_headings("# A\n\nbody\n\n## B\n\nmore\n");
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0].0, 1);
        assert_eq!(chunks[0].1, "A");
        assert_eq!(chunks[1].0, 2);
        assert_eq!(chunks[1].1, "B");
    }
}