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
//! Link extraction and local index.
//!
//! Local writes are made crash-durable by [`repair::index_or_queue`]: call it
//! with every record written to the PDS, commit the DB before advancing the
//! publish cursor, and drain queued repairs with
//! [`repair::drain_link_repairs`]. See [`repair`] for the full ordering
//! contract.
use serde_json::Value;
pub use crate::db::model::Link;
pub use repair::{LinkRepairCounters, drain_link_repairs, index_or_queue, repair_after_commit};
pub mod repair;
use crate::constants::{
ATPROTO_COLLECTION, CHANNEL_COLLECTION, MANIFEST_COLLECTION, PROJECT_COLLECTION,
PURL_COLLECTION, SOFTWARE_COLLECTION, STATUS_COLLECTION, VERSION_COLLECTION,
};
use crate::db::DbHandle;
/// Constellation source-path syntax; `[]` expands over array elements.
pub const LINK_SPECS: &[(&str, &[&str])] = &[
(
ATPROTO_COLLECTION,
&[
"nvd.uri",
"software[].project.uri",
"software[].channels[].uri",
"software[].versions[].uri",
"software[].manifests[].uri",
],
),
(SOFTWARE_COLLECTION, &["project.uri"]),
(PURL_COLLECTION, &["projects[].uri"]),
(PROJECT_COLLECTION, &["parent", "renamedTo"]),
(MANIFEST_COLLECTION, &["project"]),
(VERSION_COLLECTION, &["project", "manifest.uri"]),
(CHANNEL_COLLECTION, &["project", "version.uri"]),
(STATUS_COLLECTION, &["subject"]),
("com.nahdig.release.projectMeta", &["project"]),
];
fn target(value: &Value) -> Vec<(String, Option<String>)> {
if let Some(uri) = value.as_str() {
return if uri.starts_with("at://") {
vec![(uri.to_owned(), None)]
} else {
Vec::new()
};
}
let Some(object) = value.as_object() else {
return Vec::new();
};
let Some(uri) = object.get("uri").and_then(Value::as_str) else {
return Vec::new();
};
if !uri.starts_with("at://") {
return Vec::new();
}
let cid = object.get("cid").and_then(Value::as_str).map(str::to_owned);
vec![(uri.to_owned(), cid)]
}
fn walk(value: &Value, segments: &[&str]) -> Vec<(String, Option<String>)> {
let Some((head, rest)) = segments.split_first() else {
return target(value);
};
let Some(object) = value.as_object() else {
return Vec::new();
};
match head.strip_suffix("[]") {
Some(field) => {
let Some(items) = object.get(field).and_then(Value::as_array) else {
return Vec::new();
};
items.iter().flat_map(|item| walk(item, rest)).collect()
}
None => match object.get(*head) {
Some(item) => walk(item, rest),
None => Vec::new(),
},
}
}
/// Return links for the collection's configured paths, in spec order.
pub fn extract_links(collection: &str, record: &Value) -> Vec<Link> {
let paths = LINK_SPECS
.iter()
.find_map(|(name, paths)| (*name == collection).then_some(*paths))
.unwrap_or(&[]);
let mut links = Vec::new();
let mut seen = std::collections::HashSet::new();
for path in paths {
let segments: Vec<&str> = path.split('.').collect();
// A trailing `.uri` names the link target; walk to its containing
// strongRef so the CID is captured too.
let segments = match segments.split_last() {
Some((last, head)) if *last == "uri" => head,
_ => &segments[..],
};
for (subject_uri, subject_cid) in walk(record, segments) {
if !seen.insert((path.to_string(), subject_uri.clone())) {
continue;
}
links.push(Link {
path: (*path).to_owned(),
subject_uri,
subject_cid,
});
}
}
links
}
/// Replace the local link rows for a successfully written record.
pub async fn index_record(
db: &DbHandle,
repo_did: &str,
uri: &str,
cid: Option<&str>,
collection: &str,
record: &Value,
) -> anyhow::Result<()> {
let links = extract_links(collection, record);
let rkey = uri.rsplit('/').next().unwrap_or(uri);
db.replace_links(repo_did, uri, collection, rkey, cid, &links)
.await
}