mushroomdb 0.6.6

Embedded graph database with Cypher queries, rule triggers, and Arrow export
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
//! A synthetic code graph, shaped exactly like what `ingest-git` writes.
//!
//! Shared by every `repograph` suite. Nothing here reads a real repository:
//! the paths, authors, shas and hashes are invented, and every number is a
//! function of the file's index, so two builds of this store are identical.
//!
//! # Shape
//!
//! | Label | n | Notes |
//! |---|---|---|
//! | `File` | 30 | 12 in `src/core`, 10 in `src/web`, 8 in `tests` |
//! | `Symbol` | 12 | `calls_to` crosses directories, so `CALLS` does too |
//! | `Author` | 4 | named; `TOP_AUTHOR` in-degree 12 / 10 / 7 / 1 |
//! | `Commit` | 40 | `ts` spans 5 quarters, 8 commits each |
//! | `Concept` | 2 | one whose `source_hashes` no longer match its file |
//! | `Note` | 1 | `about` the busiest file |
//! | `GitSync` | 1 | `sha` of the newest commit |
//!
//! Each directory's files import that directory's first file, so `IMPORTS`
//! alone partitions the graph into three components. Every commit touches the
//! first three files of one directory plus one rotating other, so `CO_CHANGED`
//! reinforces the same three groups.
//!
//! The last file of `tests` doubles as the fixture's **document**: alongside
//! everything a source file carries it has the three props `ingest-git` writes
//! for Markdown — `headings`, `body` and a `mentions` list — so the `MENTIONS`
//! edge and the evidence read off it have something to be read from. It
//! mentions a file in its own directory, so the three components stay three.

// Every integration test is its own crate and includes this file as a private
// module, so whatever one suite does not call is dead code in that suite's
// build. The fixture is shared, so that is expected rather than a warning.
#![allow(dead_code)]

use core_api::repograph::rules::{about_rule, concept_sources_rule, ABOUT_LABELS};
use core_api::{default_max_edges, Direction, GraphDb, Predicate, RuleDef, Value};
use std::path::{Path, PathBuf};

/// Seconds in a day, and in the quarter the commit clock advances by.
pub const DAY_SECS: i64 = 86_400;
const QUARTER: i64 = 91 * DAY_SECS;
/// `ts` of the oldest commit. An arbitrary fixed epoch — nothing reads a clock.
pub const T0: i64 = 1_600_000_000;
/// The `GitSync` marker's `synced_at`: twelve minutes after the newest commit,
/// so a test that passes `now_ts: Some(SYNCED_AT + 720)` reads `12m ago`.
pub const SYNCED_AT: i64 = T0 + 4 * QUARTER + 7 * DAY_SECS + 60;
/// Commits per quarter, over 5 quarters.
const PER_QUARTER: usize = 8;
/// Total commits.
pub const COMMITS: usize = 40;

/// The three directories and how many files each holds.
pub const DIRS: [(&str, usize, &str); 3] = [
    ("src/core", 12, "c"),
    ("src/web", 10, "w"),
    ("tests", 8, "t"),
];

/// A scratch directory under the system temp dir, unique per process and call.
#[must_use]
pub fn tmp(name: &str) -> PathBuf {
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("clock")
        .as_nanos();
    let d = std::env::temp_dir().join(format!(
        "graphdb-repograph-{}-{}-{}",
        name,
        std::process::id(),
        nanos
    ));
    let _ = std::fs::remove_dir_all(&d);
    d
}

/// Open an empty store at `dir`.
#[must_use]
pub fn open(dir: &Path) -> GraphDb<core_storage::fs::RealFs> {
    GraphDb::open(dir).expect("open")
}

/// The key of file `i` in directory `d`.
#[must_use]
pub fn file_key(d: usize, i: usize) -> String {
    let (dir, _, stem) = DIRS[d];
    format!("{dir}/{stem}{i:02}.rs")
}

/// Every file key, in the order the store is built.
#[must_use]
pub fn all_files() -> Vec<String> {
    let mut out = Vec::new();
    for (d, &(_, n, _)) in DIRS.iter().enumerate() {
        for i in 0..n {
            out.push(file_key(d, i));
        }
    }
    out
}

/// The sha of commit `i`. Forty hex characters, distinct in the first seven so
/// an abbreviated sha still identifies it.
#[must_use]
pub fn sha(i: usize) -> String {
    format!("{:07x}{:033x}", 0x00a1_b2c3usize + i * 7919, i)
}

/// The `ts` of commit `i`: eight commits per quarter, one day apart.
#[must_use]
pub fn commit_ts(i: usize) -> i64 {
    T0 + (i / PER_QUARTER) as i64 * QUARTER + (i % PER_QUARTER) as i64 * DAY_SECS
}

/// The newest commit's `ts` — what [`repo_map`](core_api::repograph::repo_map)
/// takes as "now" when no `now_ts` is given.
#[must_use]
pub fn newest_ts() -> i64 {
    commit_ts(COMMITS - 1)
}

/// The files commit `i` touches: the first three of its directory, plus one
/// that rotates through the rest.
#[must_use]
pub fn touched(i: usize) -> Vec<String> {
    let d = i % DIRS.len();
    let n = DIRS[d].1;
    let mut files: Vec<String> = (0..3).map(|k| file_key(d, k)).collect();
    files.push(file_key(d, 3 + (i / DIRS.len()) % (n - 3)));
    files
}

/// The invented content hash of a file.
#[must_use]
pub fn hash_of(key: &str) -> String {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for b in key.bytes() {
        h ^= u64::from(b);
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    }
    format!("{h:016x}")
}

/// The fixture's one document: the last file of `tests`.
#[must_use]
pub fn doc_key() -> String {
    file_key(2, DIRS[2].1 - 1)
}

/// The file [`doc_key`]'s body mentions, under the heading [`DOC_HEADING`].
#[must_use]
pub fn doc_mentions() -> String {
    file_key(2, 1)
}

/// The headings of [`doc_key`], in document order.
pub const DOC_HEADINGS: [&str; 3] = ["Test notes", "Fixtures", "Running"];
/// The heading the mention sits under — the second, not the first, so a test
/// can tell "nearest heading above the mention" from "the document's first".
pub const DOC_HEADING: &str = DOC_HEADINGS[1];

/// The body of [`doc_key`]: Markdown, with the mention under its second
/// heading and a heading after it that must not be picked.
#[must_use]
pub fn doc_body() -> String {
    format!(
        "# {}\n\nWhat the suite covers.\n\n## {}\n\nThe sample is built by `{}`.\n\n## {}\n\ncargo test\n",
        DOC_HEADINGS[0],
        DOC_HEADINGS[1],
        doc_mentions(),
        DOC_HEADINGS[2],
    )
}

/// The author of commit `i`. The four authors take the commits in turn.
#[must_use]
pub fn commit_author(i: usize) -> &'static str {
    AUTHORS[i % AUTHORS.len()].0
}

/// The author who owns file `i` of directory `d`. The last file of `tests`
/// belongs to the fourth author, so every author owns at least one file.
#[must_use]
pub fn owner_of(d: usize, i: usize) -> &'static str {
    match (d, i) {
        (2, 7) => "d@example.test",
        (0, _) => "a@example.test",
        (1, _) => "b@example.test",
        _ => "c@example.test",
    }
}

/// The four authors, `(key, name)`.
pub const AUTHORS: [(&str, &str); 4] = [
    ("a@example.test", "Ada Example"),
    ("b@example.test", "Bea Example"),
    ("c@example.test", "Cy Example"),
    ("d@example.test", "Dee Example"),
];

/// The twelve symbols: `(file dir, file index, name, callee index)`. A callee
/// index points into this same table; `None` means the symbol calls nothing.
const SYMBOLS: [(usize, usize, &str, Option<usize>); 12] = [
    (0, 0, "core::init", None),
    (0, 1, "core::run", Some(0)),
    (0, 2, "core::stop", Some(0)),
    (0, 3, "core::load", Some(1)),
    (0, 4, "core::save", Some(1)),
    (0, 5, "core::flush", Some(4)),
    (1, 0, "web::serve", Some(0)),
    (1, 1, "web::route", Some(6)),
    (1, 2, "web::render", Some(1)),
    (1, 3, "web::auth", Some(2)),
    (2, 0, "tests::smoke", Some(6)),
    (2, 1, "tests::api", Some(7)),
];

/// The key of symbol `n` of [`SYMBOLS`].
fn symbol_key(n: usize) -> String {
    let (d, i, name, _) = SYMBOLS[n];
    format!("{}#{name}", file_key(d, i))
}

fn s(v: &str) -> Value {
    Value::Str(v.to_string())
}

fn list(items: &[String]) -> Value {
    Value::List(items.iter().map(|i| s(i)).collect())
}

/// Build the store described in the module docs at `dir`.
///
/// Nodes go in first and rules last, mirroring `ingest-git`: a rule backfills
/// once, on creation, so every derived edge exists by the time this returns.
#[must_use]
pub fn synthetic_repo_store(dir: &Path) -> GraphDb<core_storage::fs::RealFs> {
    let mut db = open(dir);

    for (key, name) in AUTHORS {
        db.insert_node("Author", key, vec![("name".into(), s(name))])
            .expect("author");
    }

    // Which commits touched each file, and how they split between authors —
    // the same two props `ingest-git` writes, so `owners` reads a real
    // distribution rather than an invented one.
    let mut commits_of: std::collections::BTreeMap<String, Vec<String>> = Default::default();
    let mut authors_of: std::collections::BTreeMap<
        String,
        std::collections::BTreeMap<&str, usize>,
    > = Default::default();
    for i in 0..COMMITS {
        for f in touched(i) {
            commits_of.entry(f.clone()).or_default().push(sha(i));
            *authors_of
                .entry(f)
                .or_default()
                .entry(commit_author(i))
                .or_default() += 1;
        }
    }

    for (d, &(dir_name, n, _)) in DIRS.iter().enumerate() {
        for i in 0..n {
            let key = file_key(d, i);
            let mut props = vec![
                ("id".into(), s(&key)),
                ("path".into(), s(&key)),
                ("dir".into(), s(dir_name)),
                ("ext".into(), s("rs")),
                ("lang".into(), s("rust")),
                ("hash".into(), s(&hash_of(&key))),
                ("lines".into(), Value::Int(40 + i as i64)),
                ("top_author_id".into(), s(owner_of(d, i))),
            ];
            // Every file but the first of its directory imports that first
            // file, and quotes the line it did so on.
            if i > 0 {
                let target = file_key(d, 0);
                props.push(("imports".into(), list(std::slice::from_ref(&target))));
                props.push((
                    "import_lines".into(),
                    list(&[format!("{target}\t{}", 3 + i)]),
                ));
            }
            // The one document: what `ingest-git` writes for a Markdown file,
            // on top of what every file here carries.
            if key == doc_key() {
                props.push(("mentions".into(), list(&[doc_mentions()])));
                props.push(("headings".into(), list(&DOC_HEADINGS.map(str::to_string))));
                props.push(("body".into(), s(&doc_body())));
            }
            let cs = commits_of.get(&key).cloned().unwrap_or_default();
            if !cs.is_empty() {
                props.push(("n_commits".into(), Value::Int(cs.len() as i64)));
                props.push(("commits".into(), list(&cs)));
                // `email\tcount` in email order, exactly as `ingest-git`
                // stores the per-file distribution.
                let counts: Vec<String> = authors_of
                    .get(&key)
                    .into_iter()
                    .flatten()
                    .map(|(email, n)| format!("{email}\t{n}"))
                    .collect();
                props.push(("author_counts".into(), list(&counts)));
            }
            db.insert_node("File", &key, props).expect("file");
        }
    }

    for (n, &(d, i, name, callee)) in SYMBOLS.iter().enumerate() {
        let key = symbol_key(n);
        let file = file_key(d, i);
        let mut props = vec![
            ("id".into(), s(&key)),
            ("name".into(), s(name)),
            ("kind".into(), s("function")),
            ("path".into(), s(&file)),
            ("file_id".into(), s(&file)),
            ("line_start".into(), Value::Int(10 + n as i64)),
            ("line_end".into(), Value::Int(20 + n as i64)),
            ("signature".into(), s(&format!("fn {name}()"))),
            ("doc".into(), s(&format!("what {name} does"))),
        ];
        if let Some(c) = callee {
            let target = symbol_key(c);
            props.push(("calls_to".into(), list(std::slice::from_ref(&target))));
            props.push((
                "call_lines".into(),
                list(&[format!("{target}\t{}", 12 + n)]),
            ));
        }
        db.insert_node("Symbol", &key, props).expect("symbol");
    }

    for i in 0..COMMITS {
        let key = sha(i);
        db.insert_node(
            "Commit",
            &key,
            vec![
                ("id".into(), s(&key)),
                ("message".into(), s(&format!("change {i:02}"))),
                ("ts".into(), Value::Int(commit_ts(i))),
                ("author_id".into(), s(AUTHORS[i % AUTHORS.len()].0)),
            ],
        )
        .expect("commit");
    }
    for i in 0..COMMITS {
        for f in touched(i) {
            db.insert_edge("TOUCHED", &sha(i), &f).expect("touched");
        }
    }

    // One concept still agrees with the file it was learned from; the other
    // records a hash that file no longer has.
    let fresh = file_key(0, 0);
    db.insert_node(
        "Concept",
        "concept:startup",
        vec![
            ("id".into(), s("concept:startup")),
            ("name".into(), s("startup path")),
            ("summary".into(), s("how the core boots")),
            ("source_files".into(), list(std::slice::from_ref(&fresh))),
            ("source_hashes".into(), list(&[hash_of(&fresh)])),
            ("extracted_by".into(), s("agent")),
            ("extracted_at".into(), Value::Int(newest_ts())),
        ],
    )
    .expect("concept");
    let stale = file_key(1, 0);
    db.insert_node(
        "Concept",
        "concept:routing",
        vec![
            ("id".into(), s("concept:routing")),
            ("name".into(), s("routing")),
            ("summary".into(), s("how requests are routed")),
            ("source_files".into(), list(&[stale])),
            (
                "source_hashes".into(),
                list(&["0000000000000000".to_string()]),
            ),
            ("extracted_by".into(), s("agent")),
            ("extracted_at".into(), Value::Int(T0)),
        ],
    )
    .expect("concept");

    db.insert_node(
        "Note",
        "note:0001",
        vec![
            ("id".into(), s("note:0001")),
            (
                "text".into(),
                s("the core entry point is worth reading first"),
            ),
            ("kind".into(), s("note")),
            ("ts".into(), Value::Int(newest_ts())),
            ("source".into(), s("agent")),
            ("about".into(), list(&[file_key(0, 0)])),
        ],
    )
    .expect("note");

    db.insert_node(
        "GitSync",
        "__mushroomdb_git_sync__",
        vec![
            ("id".into(), s("__mushroomdb_git_sync__")),
            ("sha".into(), s(&sha(COMMITS - 1))),
            ("synced_at".into(), Value::Int(SYNCED_AT)),
            ("repo".into(), s("/synthetic/repo")),
            ("recurse".into(), Value::Bool(false)),
            ("prs".into(), Value::Bool(false)),
            ("structure".into(), Value::Bool(true)),
            ("docs".into(), Value::Bool(true)),
        ],
    )
    .expect("gitsync");

    for def in rules() {
        db.create_rule(def).expect("rule");
    }
    db
}

/// The rules `ingest-git` and `structure` declare, recreated here because
/// `core-api` cannot depend on the CLI crate that owns them. Kept in the same
/// creation order, with the same predicates, edge types and fan-outs.
#[must_use]
pub fn rules() -> Vec<RuleDef> {
    let mut out = vec![
        key_rule(
            "auto_fk_symbol_file_id",
            "Symbol",
            "File",
            "file_id",
            "DEFINES",
        ),
        key_rule("imports", "File", "File", "imports", "IMPORTS"),
        key_rule("calls", "Symbol", "Symbol", "calls_to", "CALLS"),
        key_rule("mentions", "File", "File", "mentions", "MENTIONS"),
        concept_sources_rule(),
        key_rule(
            "auto_fk_commit_author_id",
            "Commit",
            "Author",
            "author_id",
            "AUTHOR",
        ),
        key_rule(
            "auto_fk_file_top_author_id",
            "File",
            "Author",
            "top_author_id",
            "TOP_AUTHOR",
        ),
    ];
    // `synthetic_repo_store` inserts every node before any rule, so by the
    // time these backfill, every `ABOUT_LABELS` target already exists — the
    // same condition `structure::ensure_rules_and_fulltext`'s own gate checks
    // before it will declare one of these, just satisfied unconditionally
    // here rather than tested for.
    for label in ABOUT_LABELS {
        out.push(about_rule(label));
    }
    let co = Predicate::Overlap {
        field: "commits".into(),
        min: 0.25,
    };
    out.push(RuleDef {
        name: "co_changed".into(),
        src_label: "File".into(),
        dst_label: "File".into(),
        predicate: co.clone(),
        edge_type: "CO_CHANGED".into(),
        weight_prop: Some("score".into()),
        max_edges: Some(10),
        approximate: false,
        via_label: None,
        via_edge: None,
        via_dir: None,
        namespace: None,
    });
    out.push(RuleDef {
        name: "knows".into(),
        src_label: "Author".into(),
        dst_label: "File".into(),
        predicate: co,
        edge_type: "KNOWS".into(),
        weight_prop: Some("score".into()),
        max_edges: Some(20),
        approximate: false,
        via_label: Some("File".into()),
        via_edge: Some("TOP_AUTHOR".into()),
        via_dir: Some(Direction::In),
        namespace: None,
    });
    out
}

fn key_rule(name: &str, src: &str, dst: &str, field: &str, edge: &str) -> RuleDef {
    let predicate = Predicate::KeyMatch {
        field: field.into(),
    };
    let max_edges = Some(default_max_edges(&predicate));
    RuleDef {
        name: name.into(),
        src_label: src.into(),
        dst_label: dst.into(),
        predicate,
        edge_type: edge.into(),
        weight_prop: None,
        max_edges,
        approximate: false,
        via_label: None,
        via_edge: None,
        via_dir: None,
        namespace: None,
    }
}