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
447
448
449
450
// 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 anyhow::{ensure, Context, Result};
use mmap_rs::Mmap;

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

/// Trait implemented by both [`NoTimestamps`] and all implementors of [`Timestamps`],
/// to allow loading timestamp properties only if needed.
pub trait MaybeTimestamps {}
impl<T: OptTimestamps> MaybeTimestamps for T {}

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

#[diagnostic::on_unimplemented(
    label = "does not have Timestamp properties loaded",
    note = "Use `let graph = graph.load_properties(|props| props.load_timestamps()).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 timestamp properties (either in-memory or memory-mapped)
pub trait OptTimestamps: MaybeTimestamps + PropertiesBackend {
    /// Returns `None` if out of bound, `Some(i64::MIN)` if the node has no author timestamp
    fn author_timestamp(&self, node: NodeId) -> PropertiesResult<'_, Option<i64>, Self>;
    /// Returns `None` if out of bound, `Some(i16::MIN)` if the node has no author timestamp offset
    fn author_timestamp_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<i16>, Self>;
    /// Returns `None` if out of bound, `Some(i64::MIN)` if the node has no committer timestamp
    fn committer_timestamp(&self, node: NodeId) -> PropertiesResult<'_, Option<i64>, Self>;
    /// Returns `None` if out of bound, `Some(i16::MIN)` if the node has no committer timestamp offset
    fn committer_timestamp_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<i16>, Self>;
}

#[diagnostic::on_unimplemented(
    label = "does not have Timestamp properties loaded",
    note = "Use `let graph = graph.load_properties(|props| props.load_timestamps()).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 timestamp properties (either in-memory or memory-mapped)
pub trait Timestamps: OptTimestamps<DataFilesAvailability = GuaranteedDataFiles> {}
impl<T: OptTimestamps<DataFilesAvailability = GuaranteedDataFiles>> Timestamps for T {}

pub struct OptMappedTimestamps {
    author_timestamp: Result<NumberMmap<BigEndian, i64, Mmap>, UnavailableProperty>,
    author_timestamp_offset: Result<NumberMmap<BigEndian, i16, Mmap>, UnavailableProperty>,
    committer_timestamp: Result<NumberMmap<BigEndian, i64, Mmap>, UnavailableProperty>,
    committer_timestamp_offset: Result<NumberMmap<BigEndian, i16, Mmap>, UnavailableProperty>,
}
impl PropertiesBackend for OptMappedTimestamps {
    type DataFilesAvailability = OptionalDataFiles;
}
impl OptTimestamps for OptMappedTimestamps {
    #[inline(always)]
    fn author_timestamp(&self, node: NodeId) -> PropertiesResult<'_, Option<i64>, Self> {
        self.author_timestamp
            .as_ref()
            .map(|author_timestamps| author_timestamps.get(node))
    }
    #[inline(always)]
    fn author_timestamp_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<i16>, Self> {
        self.author_timestamp_offset
            .as_ref()
            .map(|author_timestamp_offsets| author_timestamp_offsets.get(node))
    }
    #[inline(always)]
    fn committer_timestamp(&self, node: NodeId) -> PropertiesResult<'_, Option<i64>, Self> {
        self.committer_timestamp
            .as_ref()
            .map(|committer_timestamps| committer_timestamps.get(node))
    }
    #[inline(always)]
    fn committer_timestamp_offset(&self, node: NodeId) -> PropertiesResult<'_, Option<i16>, Self> {
        self.committer_timestamp_offset
            .as_ref()
            .map(|committer_timestamp_offsets| committer_timestamp_offsets.get(node))
    }
}

pub struct MappedTimestamps {
    author_timestamp: NumberMmap<BigEndian, i64, Mmap>,
    author_timestamp_offset: NumberMmap<BigEndian, i16, Mmap>,
    committer_timestamp: NumberMmap<BigEndian, i64, Mmap>,
    committer_timestamp_offset: NumberMmap<BigEndian, i16, Mmap>,
}
impl PropertiesBackend for MappedTimestamps {
    type DataFilesAvailability = GuaranteedDataFiles;
}

impl OptTimestamps for MappedTimestamps {
    #[inline(always)]
    fn author_timestamp(&self, node: NodeId) -> Option<i64> {
        (&self.author_timestamp).get(node)
    }
    #[inline(always)]
    fn author_timestamp_offset(&self, node: NodeId) -> Option<i16> {
        (&self.author_timestamp_offset).get(node)
    }
    #[inline(always)]
    fn committer_timestamp(&self, node: NodeId) -> Option<i64> {
        (&self.committer_timestamp).get(node)
    }
    #[inline(always)]
    fn committer_timestamp_offset(&self, node: NodeId) -> Option<i16> {
        (&self.committer_timestamp_offset).get(node)
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VecTimestamps {
    author_timestamp: Vec<i64>,
    author_timestamp_offset: Vec<i16>,
    committer_timestamp: Vec<i64>,
    committer_timestamp_offset: Vec<i16>,
}

impl VecTimestamps {
    /// Builds [`VecTimestamps`] from 4-tuples of `(author_timestamp, author_timestamp_offset,
    /// committer_timestamp, committer_timestamp_offset)`
    #[allow(clippy::type_complexity)]
    pub fn new(
        timestamps: Vec<(Option<i64>, Option<i16>, Option<i64>, Option<i16>)>,
    ) -> Result<Self> {
        let mut author_timestamp = Vec::with_capacity(timestamps.len());
        let mut author_timestamp_offset = Vec::with_capacity(timestamps.len());
        let mut committer_timestamp = Vec::with_capacity(timestamps.len());
        let mut committer_timestamp_offset = Vec::with_capacity(timestamps.len());
        for (a_ts, a_ts_o, c_ts, c_ts_o) in timestamps {
            ensure!(
                a_ts != Some(i64::MIN),
                "author timestamp may not be {}",
                i64::MIN
            );
            ensure!(
                a_ts_o != Some(i16::MIN),
                "author timestamp offset may not be {}",
                i16::MIN
            );
            ensure!(
                c_ts != Some(i64::MIN),
                "committer timestamp may not be {}",
                i64::MIN
            );
            ensure!(
                c_ts_o != Some(i16::MIN),
                "committer timestamp offset may not be {}",
                i16::MIN
            );
            author_timestamp.push(a_ts.unwrap_or(i64::MIN));
            author_timestamp_offset.push(a_ts_o.unwrap_or(i16::MIN));
            committer_timestamp.push(c_ts.unwrap_or(i64::MIN));
            committer_timestamp_offset.push(c_ts_o.unwrap_or(i16::MIN));
        }
        Ok(VecTimestamps {
            author_timestamp,
            author_timestamp_offset,
            committer_timestamp,
            committer_timestamp_offset,
        })
    }
}

impl PropertiesBackend for VecTimestamps {
    type DataFilesAvailability = GuaranteedDataFiles;
}
impl OptTimestamps for VecTimestamps {
    #[inline(always)]
    fn author_timestamp(&self, node: NodeId) -> Option<i64> {
        self.author_timestamp.get(node)
    }
    #[inline(always)]
    fn author_timestamp_offset(&self, node: NodeId) -> Option<i16> {
        self.author_timestamp_offset.get(node)
    }
    #[inline(always)]
    fn committer_timestamp(&self, node: NodeId) -> Option<i64> {
        self.committer_timestamp.get(node)
    }
    #[inline(always)]
    fn committer_timestamp_offset(&self, node: NodeId) -> Option<i16> {
        self.committer_timestamp_offset.get(node)
    }
}

impl<
        MAPS: MaybeMaps,
        PERSONS: MaybePersons,
        CONTENTS: MaybeContents,
        STRINGS: MaybeStrings,
        LABELNAMES: MaybeLabelNames,
    > SwhGraphProperties<MAPS, NoTimestamps, PERSONS, CONTENTS, STRINGS, LABELNAMES>
{
    /// Consumes a [`SwhGraphProperties`] and returns a new one with these methods
    /// available:
    ///
    /// * [`SwhGraphProperties::author_timestamp`]
    /// * [`SwhGraphProperties::author_timestamp_offset`]
    /// * [`SwhGraphProperties::committer_timestamp`]
    /// * [`SwhGraphProperties::committer_timestamp_offset`]
    pub fn load_timestamps(
        self,
    ) -> Result<SwhGraphProperties<MAPS, MappedTimestamps, PERSONS, CONTENTS, STRINGS, LABELNAMES>>
    {
        let OptMappedTimestamps {
            author_timestamp,
            author_timestamp_offset,
            committer_timestamp,
            committer_timestamp_offset,
        } = self.get_timestamps()?;
        let timestamps = MappedTimestamps {
            author_timestamp: author_timestamp?,
            author_timestamp_offset: author_timestamp_offset?,
            committer_timestamp: committer_timestamp?,
            committer_timestamp_offset: committer_timestamp_offset?,
        };
        self.with_timestamps(timestamps)
    }

    /// Equivalent to [`Self::load_timestamps`] that does not require all files to be present
    pub fn opt_load_timestamps(
        self,
    ) -> Result<SwhGraphProperties<MAPS, OptMappedTimestamps, PERSONS, CONTENTS, STRINGS, LABELNAMES>>
    {
        let timestamps = self.get_timestamps()?;
        self.with_timestamps(timestamps)
    }

    fn get_timestamps(&self) -> Result<OptMappedTimestamps> {
        Ok(OptMappedTimestamps {
            author_timestamp: load_if_exists(&self.path, AUTHOR_TIMESTAMP, |path| {
                NumberMmap::new(path, self.num_nodes).context("Could not load author_timestamp")
            })?,
            author_timestamp_offset: load_if_exists(&self.path, AUTHOR_TIMESTAMP_OFFSET, |path| {
                NumberMmap::new(path, self.num_nodes)
                    .context("Could not load author_timestamp_offset")
            })?,
            committer_timestamp: load_if_exists(&self.path, COMMITTER_TIMESTAMP, |path| {
                NumberMmap::new(path, self.num_nodes).context("Could not load committer_timestamp")
            })?,
            committer_timestamp_offset: load_if_exists(
                &self.path,
                COMMITTER_TIMESTAMP_OFFSET,
                |path| {
                    NumberMmap::new(path, self.num_nodes)
                        .context("Could not load committer_timestamp_offset")
                },
            )?,
        })
    }

    /// Alternative to [`load_timestamps`](Self::load_timestamps) that allows using arbitrary
    /// timestamps implementations
    pub fn with_timestamps<TIMESTAMPS: MaybeTimestamps>(
        self,
        timestamps: TIMESTAMPS,
    ) -> Result<SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, STRINGS, LABELNAMES>> {
        Ok(SwhGraphProperties {
            maps: self.maps,
            timestamps,
            persons: self.persons,
            contents: self.contents,
            strings: self.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 timestamps of `revision` and `release` nodes
///
/// Only available after calling [`load_timestamps`](SwhGraphProperties::load_timestamps)
/// or [`load_all_properties`](crate::graph::SwhBidirectionalGraph::load_all_properties)
impl<
        MAPS: MaybeMaps,
        TIMESTAMPS: OptTimestamps,
        PERSONS: MaybePersons,
        CONTENTS: MaybeContents,
        STRINGS: MaybeStrings,
        LABELNAMES: MaybeLabelNames,
    > SwhGraphProperties<MAPS, TIMESTAMPS, PERSONS, CONTENTS, STRINGS, LABELNAMES>
{
    /// Returns the number of seconds since Epoch that a release or revision was
    /// authored at
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn author_timestamp(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Option<i64>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(self.try_author_timestamp(node_id), |author_timestamp| {
            author_timestamp.unwrap_or_else(|e| panic!("Cannot get author timestamp: {e}"))
        })
    }

    /// Returns the number of seconds since Epoch that a release or revision was
    /// authored at
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no author timestamp
    #[inline]
    pub fn try_author_timestamp(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<i64>, OutOfBoundError>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.timestamps.author_timestamp(node_id),
            |author_timestamp| match author_timestamp {
                None => Err(OutOfBoundError {
                    index: node_id,
                    len: self.num_nodes,
                }),
                Some(i64::MIN) => Ok(None),
                Some(ts) => Ok(Some(ts)),
            },
        )
    }

    /// Returns the UTC offset in minutes of a release or revision's authorship date
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn author_timestamp_offset(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Option<i16>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.try_author_timestamp_offset(node_id),
            |author_timestamp_offset| {
                author_timestamp_offset
                    .unwrap_or_else(|e| panic!("Cannot get author timestamp offset: {e}"))
            },
        )
    }

    /// Returns the UTC offset in minutes of a release or revision's authorship date
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no author timestamp
    #[inline]
    pub fn try_author_timestamp_offset(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<i16>, OutOfBoundError>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.timestamps.author_timestamp_offset(node_id),
            |author_timestamp_offset| match author_timestamp_offset {
                None => Err(OutOfBoundError {
                    index: node_id,
                    len: self.num_nodes,
                }),
                Some(i16::MIN) => Ok(None),
                Some(offset) => Ok(Some(offset)),
            },
        )
    }

    /// Returns the number of seconds since Epoch that a revision was committed at
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn committer_timestamp(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Option<i64>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.try_committer_timestamp(node_id),
            |committer_timestamp| {
                committer_timestamp
                    .unwrap_or_else(|e| panic!("Cannot get committer timestamp: {e}"))
            },
        )
    }

    /// Returns the number of seconds since Epoch that a revision was committed at
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no committer timestamp
    #[inline]
    pub fn try_committer_timestamp(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<i64>, OutOfBoundError>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.timestamps.committer_timestamp(node_id),
            |committer_timestamp| match committer_timestamp {
                None => Err(OutOfBoundError {
                    index: node_id,
                    len: self.num_nodes,
                }),
                Some(i64::MIN) => Ok(None),
                Some(ts) => Ok(Some(ts)),
            },
        )
    }

    /// Returns the UTC offset in minutes of a revision's committer date
    ///
    /// # Panics
    ///
    /// If the node id does not exist
    #[inline]
    pub fn committer_timestamp_offset(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Option<i16>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.try_committer_timestamp_offset(node_id),
            |committer_timestamp_offset| {
                committer_timestamp_offset
                    .unwrap_or_else(|e| panic!("Cannot get committer timestamp: {e}"))
            },
        )
    }

    /// Returns the UTC offset in minutes of a revision's committer date
    ///
    /// Returns `Err` if the node id is unknown, and `Ok(None)` if the node has
    /// no committer timestamp
    #[inline]
    pub fn try_committer_timestamp_offset(
        &self,
        node_id: NodeId,
    ) -> PropertiesResult<'_, Result<Option<i16>, OutOfBoundError>, TIMESTAMPS> {
        TIMESTAMPS::map_if_available(
            self.timestamps.committer_timestamp_offset(node_id),
            |committer_timestamp_offset| match committer_timestamp_offset {
                None => Err(OutOfBoundError {
                    index: node_id,
                    len: self.num_nodes,
                }),
                Some(i16::MIN) => Ok(None),
                Some(offset) => Ok(Some(offset)),
            },
        )
    }
}