swh-graph 11.4.1

Compressed in-memory representation of the Software Heritage archive graph
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
// Copyright (C) 2023-2024  The Software Heritage developers
// See the AUTHORS file at the top-level directory of this distribution
// License: GNU General Public License version 3, or any later version
// See top-level LICENSE file for more information

use mmap_rs::Mmap;

use super::suffixes::*;
use super::*;
use crate::graph::NodeId;

/// Trait implemented by both [`NoStrings`] and all implementors of [`Strings`],
/// to allow loading string properties only if needed.
pub trait MaybeStrings {}
impl<S: OptStrings> MaybeStrings for S {}

/// Placeholder for when string properties are not loaded
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NoStrings;
impl MaybeStrings for NoStrings {}

#[diagnostic::on_unimplemented(
    label = "does not have String properties loaded",
    note = "Use `let graph = graph.load_properties(|props| props.load_string()).unwrap()` to load them",
    note = "Or replace `graph.init_properties()` with `graph.load_all_properties::<DynMphf>().unwrap()` to load all properties"
)]
/// Trait implemented by all implementors of [`MaybeStrings`] but [`NoStrings`]
pub trait OptStrings: MaybeStrings + PropertiesBackend {
    /// Returns an array with all messages, separated by `b'\n'`
    fn message(&self) -> PropertiesResult<'_, &[u8], Self>;
    /// Returns the position of the first character of `node`'s message in [`Self::message`],
    /// or `None` if it is out of bound, or `Some(u64::MAX)` if the node has no message
    fn message_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<u64>, Self>;
    /// Returns an array with all messages, separated by `b'\n'`
    fn tag_name(&self) -> PropertiesResult<'_, &[u8], Self>;
    /// Returns the position of the first character of `node`'s tag_name in [`Self::tag_name`],
    /// or `None` if it is out of bound, or `Some(u64::MAX)` if the node has no tag_name
    fn tag_name_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<u64>, Self>;
}

#[diagnostic::on_unimplemented(
    label = "does not have String properties loaded",
    note = "Use `let graph = graph.load_properties(|props| props.load_string()).unwrap()` to load them",
    note = "Or replace `graph.init_properties()` with `graph.load_all_properties::<DynMphf>().unwrap()` to load all properties"
)]
/// Trait for backend storage of string properties (either in-memory or memory-mapped)
pub trait Strings: OptStrings<DataFilesAvailability = GuaranteedDataFiles> {}
impl<S: OptStrings<DataFilesAvailability = GuaranteedDataFiles>> Strings for S {}

/// Variant of [`MappedStrings`] that checks at runtime that files are present every time
/// it is accessed
pub struct OptMappedStrings {
    message: Result<Mmap, UnavailableProperty>,
    message_offset: Result<NumberMmap<BigEndian, u64, Mmap>, UnavailableProperty>,
    tag_name: Result<Mmap, UnavailableProperty>,
    tag_name_offset: Result<NumberMmap<BigEndian, u64, Mmap>, UnavailableProperty>,
}
impl PropertiesBackend for OptMappedStrings {
    type DataFilesAvailability = OptionalDataFiles;
}
impl OptStrings for OptMappedStrings {
    #[inline(always)]
    fn message(&self) -> PropertiesResult<'_, &[u8], Self> {
        self.message.as_deref()
    }
    #[inline(always)]
    fn message_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<u64>, Self> {
        self.message_offset
            .as_ref()
            .map(|message_offsets| message_offsets.get(node))
    }
    #[inline(always)]
    fn tag_name(&self) -> PropertiesResult<'_, &[u8], Self> {
        self.tag_name.as_deref()
    }
    #[inline(always)]
    fn tag_name_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<u64>, Self> {
        self.tag_name_offset
            .as_ref()
            .map(|tag_name_offsets| tag_name_offsets.get(node))
    }
}

/// [`Strings`] implementation backed by files guaranteed to be present once the graph is loaded
pub struct MappedStrings {
    message: Mmap,
    message_offset: NumberMmap<BigEndian, u64, Mmap>,
    tag_name: Mmap,
    tag_name_offset: NumberMmap<BigEndian, u64, Mmap>,
}
impl PropertiesBackend for MappedStrings {
    type DataFilesAvailability = GuaranteedDataFiles;
}
impl OptStrings for MappedStrings {
    #[inline(always)]
    fn message(&self) -> &[u8] {
        &self.message
    }
    #[inline(always)]
    fn message_offset(&self, node: NodeId) -> Option<u64> {
        (&self.message_offset).get(node)
    }
    #[inline(always)]
    fn tag_name(&self) -> &[u8] {
        &self.tag_name
    }
    #[inline(always)]
    fn tag_name_offset(&self, node: NodeId) -> Option<u64> {
        (&self.tag_name_offset).get(node)
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VecStrings {
    message: Vec<u8>,
    message_offset: Vec<u64>,
    tag_name: Vec<u8>,
    tag_name_offset: Vec<u64>,
}

impl VecStrings {
    /// Returns [`VecStrings`] from pairs of `(message, tag_name)`
    pub fn new<Msg: AsRef<[u8]>, TagName: AsRef<[u8]>>(
        data: Vec<(Option<Msg>, Option<TagName>)>,
    ) -> Result<Self> {
        let base64 = base64_simd::STANDARD;

        let mut message = Vec::new();
        let mut message_offset = Vec::new();
        let mut tag_name = Vec::new();
        let mut tag_name_offset = Vec::new();

        for (msg, tag) in data.into_iter() {
            match msg {
                Some(msg) => {
                    let msg = base64.encode_to_string(msg);
                    message_offset.push(
                        message
                            .len()
                            .try_into()
                            .context("total message size overflowed usize")?,
                    );
                    message.extend(msg.as_bytes());
                    message.push(b'\n');
                }
                None => message_offset.push(u64::MAX),
            }
            match tag {
                Some(tag) => {
                    let tag = base64.encode_to_string(tag);
                    tag_name_offset.push(
                        tag_name
                            .len()
                            .try_into()
                            .context("total tag_name size overflowed usize")?,
                    );
                    tag_name.extend(tag.as_bytes());
                    tag_name.push(b'\n');
                }
                None => tag_name_offset.push(u64::MAX),
            }
        }

        Ok(VecStrings {
            message,
            message_offset,
            tag_name,
            tag_name_offset,
        })
    }
}

impl PropertiesBackend for VecStrings {
    type DataFilesAvailability = GuaranteedDataFiles;
}
impl OptStrings for VecStrings {
    #[inline(always)]
    fn message(&self) -> &[u8] {
        self.message.as_slice()
    }
    #[inline(always)]
    fn message_offset(&self, node: NodeId) -> Option<u64> {
        self.message_offset.get(node)
    }
    #[inline(always)]
    fn tag_name(&self) -> &[u8] {
        self.tag_name.as_slice()
    }
    #[inline(always)]
    fn tag_name_offset(&self, node: NodeId) -> Option<u64> {
        self.tag_name_offset.get(node)
    }
}

impl<
        MAPS: MaybeMaps,
        TIMESTAMPS: MaybeTimestamps,
        PERSONS: MaybePersons,
        CONTENTS: MaybeContents,
        LABELNAMES: MaybeLabelNames,
    > SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, NoStrings, LABELNAMES>
{
    /// Consumes a [`SwhGraphProperties`] and returns a new one with these methods
    /// available:
    ///
    /// * [`SwhGraphProperties::message_base64`]
    /// * [`SwhGraphProperties::message`]
    /// * [`SwhGraphProperties::tag_name_base64`]
    /// * [`SwhGraphProperties::tag_name`]
    pub fn load_strings(
        self,
    ) -> Result<SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, MappedStrings, LABELNAMES>>
    {
        let OptMappedStrings {
            message,
            message_offset,
            tag_name,
            tag_name_offset,
        } = self.get_strings()?;
        let strings = MappedStrings {
            message: message?,
            message_offset: message_offset?,
            tag_name: tag_name?,
            tag_name_offset: tag_name_offset?,
        };
        self.with_strings(strings)
    }
    /// Equivalent to [`Self::load_strings`] that does not require all files to be present
    pub fn opt_load_strings(
        self,
    ) -> Result<SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, OptMappedStrings, LABELNAMES>>
    {
        let strings = self.get_strings()?;
        self.with_strings(strings)
    }

    fn get_strings(&self) -> Result<OptMappedStrings> {
        Ok(OptMappedStrings {
            message: load_if_exists(&self.path, MESSAGE, |path| mmap(path))
                .context("Could not load message")?,
            message_offset: load_if_exists(&self.path, MESSAGE_OFFSET, |path| {
                NumberMmap::new(path, self.num_nodes)
            })
            .context("Could not load message_offset")?,
            tag_name: load_if_exists(&self.path, TAG_NAME, |path| mmap(path))
                .context("Could not load tag_name")?,
            tag_name_offset: load_if_exists(&self.path, TAG_NAME_OFFSET, |path| {
                NumberMmap::new(path, self.num_nodes)
            })
            .context("Could not load tag_name_offset")?,
        })
    }

    /// Alternative to [`load_strings`](Self::load_strings) that allows using arbitrary
    /// strings implementations
    pub fn with_strings<STRINGS: MaybeStrings>(
        self,
        strings: STRINGS,
    ) -> Result<SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, STRINGS, LABELNAMES>> {
        Ok(SwhGraphProperties {
            maps: self.maps,
            timestamps: self.timestamps,
            persons: self.persons,
            contents: self.contents,
            strings,
            label_names: self.label_names,
            path: self.path,
            num_nodes: self.num_nodes,
            label_names_are_in_base64_order: self.label_names_are_in_base64_order,
        })
    }
}

/// Functions to access message of `revision`/`release` nodes, and names of `release` nodes
///
/// Only available after calling [`load_strings`](SwhGraphProperties::load_strings)
/// or [`load_all_properties`](crate::graph::SwhBidirectionalGraph::load_all_properties)
impl<
        MAPS: MaybeMaps,
        TIMESTAMPS: MaybeTimestamps,
        PERSONS: MaybePersons,
        CONTENTS: MaybeContents,
        STRINGS: OptStrings,
        LABELNAMES: MaybeLabelNames,
    > SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, STRINGS, LABELNAMES>
{
    #[inline(always)]
    fn message_or_tag_name_base64<'a>(
        &self,
        what: &'static str,
        data: &'a [u8],
        offset: Option<u64>,
        node_id: NodeId,
    ) -> Result<Option<&'a [u8]>, OutOfBoundError> {
        match offset {
            None => Err(OutOfBoundError {
                // Unknown node
                index: node_id,
                len: self.num_nodes,
            }),
            Some(u64::MAX) => Ok(None), // No message
            Some(offset) => {
                let offset = offset as usize;
                let slice: &[u8] = data.get(offset..).unwrap_or_else(|| {
                    panic!("Missing {what} for node {node_id} at offset {offset}")
                });
                Ok(slice
                    .iter()
                    .position(|&c| c == b'\n')
                    .map(|end| &slice[..end]))
            }
        }
    }

    /// Returns the base64-encoded message of a revision or release,
    /// or the base64-encoded URL of an origin
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn message_base64(&self, node_id: NodeId) -> PropertiesResult<'_, Option<&[u8]>, STRINGS> {
        STRINGS::map_if_available(
            self.try_message_base64(node_id),
            |message: Result<_, OutOfBoundError>| {
                message.unwrap_or_else(|e| panic!("Cannot get node message: {e}"))
            },
        )
    }

    /// Returns the base64-encoded message of a revision or release
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no message.
    #[inline]
    pub fn try_message_base64(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<&[u8]>, OutOfBoundError>, STRINGS> {
        STRINGS::map_if_available(
            STRINGS::zip_if_available(self.strings.message(), self.strings.message_offset(node_id)),
            |(messages, message_offset)| {
                self.message_or_tag_name_base64("message", messages, message_offset, node_id)
            },
        )
    }
    /// Returns the message of a revision or release,
    /// or the URL of an origin
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn message(&self, node_id: NodeId) -> PropertiesResult<'_, Option<Vec<u8>>, STRINGS> {
        STRINGS::map_if_available(self.try_message(node_id), |message| {
            message.unwrap_or_else(|e| panic!("Cannot get node message: {e}"))
        })
    }

    /// Returns the message of a revision or release,
    /// or the URL of an origin
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no message.
    #[inline]
    pub fn try_message(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<Vec<u8>>, OutOfBoundError>, STRINGS> {
        let base64 = base64_simd::STANDARD;
        STRINGS::map_if_available(self.try_message_base64(node_id), |message_opt_res| {
            message_opt_res.map(|message_opt| {
                message_opt.map(|message| {
                    base64
                        .decode_to_vec(message)
                        .unwrap_or_else(|e| panic!("Could not decode node message: {e}"))
                })
            })
        })
    }

    /// Returns the tag name of a release, base64-encoded
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn tag_name_base64(&self, node_id: NodeId) -> PropertiesResult<'_, Option<&[u8]>, STRINGS> {
        STRINGS::map_if_available(self.try_tag_name_base64(node_id), |tag_name| {
            tag_name.unwrap_or_else(|e| panic!("Cannot get node tag: {e}"))
        })
    }

    /// Returns the tag name of a release, base64-encoded
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no tag name.
    #[inline]
    pub fn try_tag_name_base64(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<&[u8]>, OutOfBoundError>, STRINGS> {
        STRINGS::map_if_available(
            STRINGS::zip_if_available(
                self.strings.tag_name(),
                self.strings.tag_name_offset(node_id),
            ),
            |(tag_names, tag_name_offset)| {
                self.message_or_tag_name_base64("tag_name", tag_names, tag_name_offset, node_id)
            },
        )
    }

    /// Returns the tag name of a release
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn tag_name(&self, node_id: NodeId) -> PropertiesResult<'_, Option<Vec<u8>>, STRINGS> {
        STRINGS::map_if_available(self.try_tag_name(node_id), |tag_name| {
            tag_name.unwrap_or_else(|e| panic!("Cannot get node tag name: {e}"))
        })
    }

    /// Returns the tag name of a release
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no tag name.
    #[inline]
    pub fn try_tag_name(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<Vec<u8>>, OutOfBoundError>, STRINGS> {
        let base64 = base64_simd::STANDARD;
        STRINGS::map_if_available(self.try_tag_name_base64(node_id), |tag_name_opt_res| {
            tag_name_opt_res.map(|tag_name_opt| {
                tag_name_opt.map(|tag_name| {
                    base64.decode_to_vec(tag_name).unwrap_or_else(|_| {
                        panic!("Could not decode tag_name of node {node_id}: {tag_name:?}")
                    })
                })
            })
        })
    }
}