mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-27 07:46:25 +00:00
feat(graph): a hub stands for its implicit tag, no separate tag node beside it
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58c3ee9611
commit
ad62a7d258
4 files changed
+70
-26
No files matched your search
@@ -926,23 +926,35 @@ class MemoryBackend implements GraphBackend {
|
|||||||
t.add(index[m]!);
|
t.add(index[m]!);
|
||||||
k.add(EdgeKind.contains.index);
|
k.add(EdgeKind.contains.index);
|
||||||
}
|
}
|
||||||
// Tags are virtual nodes after the pages; a page points at each of its tags.
|
// Tags are virtual nodes after the pages; a page points at each of its tags. A tag
|
||||||
|
// named like a hub uses the hub's node, and its members (already contained) skip it.
|
||||||
|
final hubNodes = {
|
||||||
|
for (final id in ids)
|
||||||
|
if (_isHub(id)) _tagKey(pages[id]!.title): index[id]!,
|
||||||
|
};
|
||||||
final tagNames = <String, String>{};
|
final tagNames = <String, String>{};
|
||||||
for (final p in pages.values) {
|
for (final p in pages.values) {
|
||||||
for (final tg in allTags(p)) {
|
for (final tg in allTags(p)) {
|
||||||
|
if (hubNodes.containsKey(_tagKey(tg))) continue;
|
||||||
tagNames.putIfAbsent(_tagKey(tg), () => tg);
|
tagNames.putIfAbsent(_tagKey(tg), () => tg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final tagKeys = tagNames.keys.toList()..sort();
|
final tagKeys = tagNames.keys.toList()..sort();
|
||||||
final tagIndex = {
|
final tagIndex = {
|
||||||
|
...hubNodes,
|
||||||
for (var j = 0; j < tagKeys.length; j++) tagKeys[j]: ids.length + j,
|
for (var j = 0; j < tagKeys.length; j++) tagKeys[j]: ids.length + j,
|
||||||
};
|
};
|
||||||
|
final contained = {
|
||||||
|
for (final (h, m) in containsEdges) (index[h]!, index[m]!),
|
||||||
|
};
|
||||||
for (final p in pages.values) {
|
for (final p in pages.values) {
|
||||||
final i = index[p.id];
|
final i = index[p.id];
|
||||||
if (i == null) continue;
|
if (i == null) continue;
|
||||||
for (final tg in allTags(p)) {
|
for (final tg in allTags(p)) {
|
||||||
|
final to = tagIndex[_tagKey(tg)]!;
|
||||||
|
if (to == i || contained.contains((to, i))) continue;
|
||||||
s.add(i);
|
s.add(i);
|
||||||
t.add(tagIndex[_tagKey(tg)]!);
|
t.add(to);
|
||||||
k.add(EdgeKind.tagged.index);
|
k.add(EdgeKind.tagged.index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ void main() {
|
|||||||
expect(() => b.setPageTags(j.id, ['x']), throwsException);
|
expect(() => b.setPageTags(j.id, ['x']), throwsException);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('a hub is an implicit tag of its members, merged once', () async {
|
test('a hub is an implicit tag of its members, shown as the hub', () async {
|
||||||
final b = MemoryBackend();
|
final b = MemoryBackend();
|
||||||
final media = b.createPage('Media');
|
final media = b.createPage('Media');
|
||||||
b.makeHub(media.id);
|
b.makeHub(media.id);
|
||||||
@@ -100,17 +100,10 @@ void main() {
|
|||||||
expect(cat.single.count, 2);
|
expect(cat.single.count, 2);
|
||||||
expect(b.pages[a.id]!.tags, isEmpty);
|
expect(b.pages[a.id]!.tags, isEmpty);
|
||||||
|
|
||||||
|
// In the graph the hub stands for its tag: no tag node, members only contained.
|
||||||
final data = await b.graphData();
|
final data = await b.graphData();
|
||||||
final tags = data.nodes.where((n) => n.isTag).toList();
|
expect(data.nodes.where((n) => n.isTag), isEmpty);
|
||||||
expect(tags.length, 1);
|
expect(data.kinds.contains(EdgeKind.tagged.index), isFalse);
|
||||||
expect(tags.single.backlinks, 2);
|
|
||||||
final ti = data.nodes.indexOf(tags.single);
|
|
||||||
final from = [
|
|
||||||
for (var e = 0; e < data.kinds.length; e++)
|
|
||||||
if (data.kinds[e] == EdgeKind.tagged.index && data.targets[e] == ti)
|
|
||||||
data.nodes[data.sources[e]].title,
|
|
||||||
]..sort();
|
|
||||||
expect(from, ['Bleach', 'Naruto']);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -285,7 +285,8 @@ pub(crate) fn build(g: &Graph, seq: i64) -> Result<GraphData> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tags are virtual nodes (`tag:<key>`, appended after the pages): every page carrying a tag,
|
/// Tags are virtual nodes (`tag:<key>`, appended after the pages): every page carrying a tag,
|
||||||
/// listed or implied by a type, gets one `TAGGED` edge to it.
|
/// listed or implied by a type, gets one `TAGGED` edge to it. A tag named like a hub already
|
||||||
|
/// in the graph (a hub's implicit tag) uses the hub's node instead of a second one.
|
||||||
fn tagged(
|
fn tagged(
|
||||||
g: &Graph,
|
g: &Graph,
|
||||||
index: &HashMap<String, u32>,
|
index: &HashMap<String, u32>,
|
||||||
@@ -301,7 +302,12 @@ fn tagged(
|
|||||||
.conn()
|
.conn()
|
||||||
.prepare("SELECT tag_key, page_id FROM page_tags ORDER BY tag_key, page_id")?;
|
.prepare("SELECT tag_key, page_id FROM page_tags ORDER BY tag_key, page_id")?;
|
||||||
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
|
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
|
||||||
let mut tag_index: HashMap<String, u32> = HashMap::new();
|
let mut tag_index: HashMap<String, u32> = nodes
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, n)| n.is_hub)
|
||||||
|
.map(|(i, n)| (title_key(&n.title), i as u32))
|
||||||
|
.collect();
|
||||||
for row in rows {
|
for row in rows {
|
||||||
let (key, page) = row?;
|
let (key, page) = row?;
|
||||||
let Some(&s) = index.get(&page) else {
|
let Some(&s) = index.get(&page) else {
|
||||||
@@ -325,8 +331,11 @@ fn tagged(
|
|||||||
});
|
});
|
||||||
nodes.len() as u32 - 1
|
nodes.len() as u32 - 1
|
||||||
});
|
});
|
||||||
|
// A hub's members already hang from it by a CONTAINS edge.
|
||||||
|
if !b.edges.contains_key(&(t, s, edge_kind::CONTAINS)) {
|
||||||
b.add(s, t, edge_kind::TAGGED, 1.0);
|
b.add(s, t, edge_kind::TAGGED, 1.0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -364,6 +364,9 @@ fn legacy_candidates_and_purge() {
|
|||||||
let (keep, _) = page_with(&g, "Keep", &["x"]);
|
let (keep, _) = page_with(&g, "Keep", &["x"]);
|
||||||
page_with(&g, "contents", &["- x"]);
|
page_with(&g, "contents", &["- x"]);
|
||||||
page_with(&g, "now", &[]);
|
page_with(&g, "now", &[]);
|
||||||
|
page_with(&g, "Blank", &["", " "]);
|
||||||
|
let (typed, _) = page_with(&g, "Typed", &[]);
|
||||||
|
g.set_page_type(typed, Some("persona".into())).unwrap();
|
||||||
g.create_hub("later".into(), String::new()).unwrap();
|
g.create_hub("later".into(), String::new()).unwrap();
|
||||||
{
|
{
|
||||||
let shared = g.shared();
|
let shared = g.shared();
|
||||||
@@ -389,6 +392,9 @@ fn legacy_candidates_and_purge() {
|
|||||||
assert!(!by("Template").preselected);
|
assert!(!by("Template").preselected);
|
||||||
assert_eq!(by("Template").reason, "type:: system");
|
assert_eq!(by("Template").reason, "type:: system");
|
||||||
assert!(c.iter().all(|x| x.title != "Keep"));
|
assert!(c.iter().all(|x| x.title != "Keep"));
|
||||||
|
assert_eq!(by("Blank").reason, "Empty");
|
||||||
|
assert!(by("Blank").preselected, "only blank blocks");
|
||||||
|
assert!(c.iter().all(|x| x.title != "Typed"), "a type is content");
|
||||||
|
|
||||||
// A bad id rejects the call before anything is deleted.
|
// A bad id rejects the call before anything is deleted.
|
||||||
assert!(g.purge_pages(vec![todo.clone(), "nope".into()]).is_err());
|
assert!(g.purge_pages(vec![todo.clone(), "nope".into()]).is_err());
|
||||||
@@ -453,6 +459,32 @@ fn tags_are_virtual_graph_nodes_with_their_own_edge_kind() {
|
|||||||
assert_eq!(g.pages_with_tag("IDEA".into()).unwrap().len(), 2);
|
assert_eq!(g.pages_with_tag("IDEA".into()).unwrap().len(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_tag_named_like_a_hub_uses_the_hub_node() {
|
||||||
|
let (_d, g) = open();
|
||||||
|
let hub = g.create_hub("Proyectos".into(), String::new()).unwrap().id;
|
||||||
|
let (member, _) = page_with(&g, "Synapse", &["x"]);
|
||||||
|
let (loose, _) = page_with(&g, "Otro", &["x"]);
|
||||||
|
g.add_to_hub(member.clone(), hub.clone()).unwrap();
|
||||||
|
g.add_page_tag(member, "proyectos".into()).unwrap();
|
||||||
|
g.add_page_tag(loose, "Proyectos".into()).unwrap();
|
||||||
|
let data = g.graph_data().unwrap();
|
||||||
|
assert!(
|
||||||
|
data.nodes.iter().all(|n| !n.is_tag),
|
||||||
|
"no tag node beside the hub"
|
||||||
|
);
|
||||||
|
let title = |i: u32| data.nodes[i as usize].title.clone();
|
||||||
|
let tagged: Vec<(String, String)> = (0..data.kinds.len())
|
||||||
|
.filter(|&e| data.kinds[e] == edge_kind::TAGGED)
|
||||||
|
.map(|e| (title(data.sources[e]), title(data.targets[e])))
|
||||||
|
.collect();
|
||||||
|
assert_eq!(
|
||||||
|
tagged,
|
||||||
|
[("Otro".to_string(), "Proyectos".to_string())],
|
||||||
|
"members already hang from the hub; other pages tagged with it point at it"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn page_type_and_tag_renaming_round_trip_through_the_api() {
|
fn page_type_and_tag_renaming_round_trip_through_the_api() {
|
||||||
let (_d, g) = open();
|
let (_d, g) = open();
|
||||||
@@ -475,7 +507,7 @@ fn page_type_and_tag_renaming_round_trip_through_the_api() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hub_members_point_at_one_virtual_tag_node_named_after_the_hub() {
|
fn a_hub_tag_and_a_same_name_type_need_no_tag_node_beside_the_hub() {
|
||||||
let (_d, g) = open();
|
let (_d, g) = open();
|
||||||
let hub = g.create_hub("Anime".into(), String::new()).unwrap();
|
let hub = g.create_hub("Anime".into(), String::new()).unwrap();
|
||||||
let (a, _) = page_with(&g, "Naruto", &["x"]);
|
let (a, _) = page_with(&g, "Naruto", &["x"]);
|
||||||
@@ -484,16 +516,14 @@ fn hub_members_point_at_one_virtual_tag_node_named_after_the_hub() {
|
|||||||
g.add_to_hub(b.clone(), hub.id.clone()).unwrap();
|
g.add_to_hub(b.clone(), hub.id.clone()).unwrap();
|
||||||
g.set_page_type(b.clone(), Some("anime".into())).unwrap();
|
g.set_page_type(b.clone(), Some("anime".into())).unwrap();
|
||||||
let data = g.graph_data().unwrap();
|
let data = g.graph_data().unwrap();
|
||||||
let tags: Vec<_> = data.nodes.iter().filter(|n| n.is_tag).collect();
|
assert!(
|
||||||
assert_eq!(tags.len(), 1, "hub tag and same-name type merge");
|
data.nodes.iter().all(|n| !n.is_tag),
|
||||||
assert_eq!(tags[0].title, "Anime");
|
"the hub stands for its tag"
|
||||||
let target = data.nodes.iter().position(|n| n.is_tag).unwrap() as u32;
|
);
|
||||||
let mut from: Vec<String> = (0..data.kinds.len())
|
assert!(
|
||||||
.filter(|&e| data.kinds[e] == edge_kind::TAGGED && data.targets[e] == target)
|
!data.kinds.contains(&edge_kind::TAGGED),
|
||||||
.map(|e| data.nodes[data.sources[e] as usize].title.clone())
|
"members already hang from the hub"
|
||||||
.collect();
|
);
|
||||||
from.sort();
|
|
||||||
assert_eq!(from, ["Bleach", "Naruto"]);
|
|
||||||
let cat = g.tag_catalog().unwrap();
|
let cat = g.tag_catalog().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(cat[0].name.to_lowercase().as_str(), cat[0].count),
|
(cat[0].name.to_lowercase().as_str(), cat[0].count),
|
||||||
|
|||||||
Reference in new issue
Block a user