swh_graph/views/contiguous_subgraph/
strings.rs

1// Copyright (C) 2025  The Software Heritage developers
2// See the AUTHORS file at the top-level directory of this distribution
3// License: GNU General Public License version 3, or any later version
4// See top-level LICENSE file for more information
5
6//! Implementation of [`properties::Strings`] for [`ContiguousSubgraph`]
7
8use super::*;
9use properties::{NoStrings, OptStrings, PropertiesBackend, PropertiesResult, SwhGraphProperties};
10
11impl<
12        G: SwhGraphWithProperties<Strings: OptStrings>,
13        N: ContractionBackend,
14        MAPS: properties::MaybeMaps,
15        TIMESTAMPS: properties::MaybeTimestamps,
16        PERSONS: properties::MaybePersons,
17        CONTENTS: properties::MaybeContents,
18        LABELNAMES: properties::MaybeLabelNames,
19    > ContiguousSubgraph<G, N, MAPS, TIMESTAMPS, PERSONS, CONTENTS, NoStrings, LABELNAMES>
20{
21    /// Makes [`OptStrings`] available on this [`ContiguousSubgraph`].
22    ///
23    /// Requires the underlying graph to implement [`OptStrings`].
24    pub fn with_strings(
25        self,
26    ) -> ContiguousSubgraph<
27        G,
28        N,
29        MAPS,
30        TIMESTAMPS,
31        PERSONS,
32        CONTENTS,
33        ContiguousSubgraphStrings<G, N>,
34        LABELNAMES,
35    > {
36        let ContiguousSubgraph {
37            properties:
38                SwhGraphProperties {
39                    path,
40                    num_nodes,
41                    maps,
42                    timestamps,
43                    persons,
44                    contents,
45                    strings: NoStrings,
46                    label_names,
47                    label_names_are_in_base64_order,
48                },
49            inner,
50        } = self;
51
52        ContiguousSubgraph {
53            properties: SwhGraphProperties {
54                path,
55                num_nodes,
56                maps,
57                timestamps,
58                persons,
59                contents,
60                strings: ContiguousSubgraphStrings {
61                    graph: Arc::clone(&inner),
62                },
63                label_names,
64                label_names_are_in_base64_order,
65            },
66            inner,
67        }
68    }
69}
70
71/// View for [`MaybeStrings`] that renumbers nodes, as part of [`ContiguousSubgraph`]
72pub struct ContiguousSubgraphStrings<
73    G: SwhGraphWithProperties<Strings: OptStrings>,
74    N: ContractionBackend,
75> {
76    graph: Arc<ContiguousSubgraphInner<G, N>>,
77}
78
79impl<G: SwhGraphWithProperties<Strings: OptStrings>, N: ContractionBackend> PropertiesBackend
80    for ContiguousSubgraphStrings<G, N>
81{
82    type DataFilesAvailability =
83        <<G as SwhGraphWithProperties>::Strings as PropertiesBackend>::DataFilesAvailability;
84}
85impl<G: SwhGraphWithProperties<Strings: OptStrings>, N: ContractionBackend> OptStrings
86    for ContiguousSubgraphStrings<G, N>
87{
88    #[inline(always)]
89    fn message(&self) -> PropertiesResult<'_, &[u8], Self> {
90        // returned as-is, we only need to change the offsets
91        self.graph.underlying_graph.properties().strings.message()
92    }
93    #[inline(always)]
94    fn message_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<u64>, Self> {
95        self.graph
96            .underlying_graph
97            .properties()
98            .strings
99            .message_offset(self.graph.contraction.underlying_node_id(node))
100    }
101    #[inline(always)]
102    fn tag_name(&self) -> PropertiesResult<'_, &[u8], Self> {
103        // returned as-is, we only need to change the offsets
104        self.graph.underlying_graph.properties().strings.tag_name()
105    }
106    #[inline(always)]
107    fn tag_name_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<u64>, Self> {
108        self.graph
109            .underlying_graph
110            .properties()
111            .strings
112            .tag_name_offset(self.graph.contraction.underlying_node_id(node))
113    }
114}