swh-graph 12.1.2

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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright (C) 2024-2026  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

//! Abstraction over possible Minimal-perfect hash functions

use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use anyhow::{bail, Context, Result};
use ph::fmph::GOFunction;
use ph::seeds::TwoToPowerBitsStatic;

use crate::graph::NodeId;
use crate::java_compat::mph::gov::GOVMPH;
use crate::map::{MappedPermutation, Permutation};
use crate::utils::suffix_path;
use crate::SWHID;

/// Minimal-perfect hash function over [`SWHID`].
///
/// See [`DynMphf`] which wraps all implementer structs in an enum to dynamically choose
/// which MPH algorithm to use with less overhead than `dyn SwhidMphf`.
pub trait SwhidMphf {
    fn num_keys(&self) -> usize;

    /// Hashes a SWHID's binary representation
    #[inline(always)]
    fn hash_array(&self, swhid: &[u8; SWHID::BYTES_SIZE]) -> Option<NodeId> {
        self.hash_swhid(&(*swhid).try_into().ok()?)
    }

    /// Hashes a SWHID's textual representation
    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId>;

    /// Hashes a SWHID's textual representation
    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId>;

    /// Hashes a [`SWHID`]
    #[inline(always)]
    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
        self.hash_str(swhid.to_string())
    }
}

#[diagnostic::on_unimplemented(
    label = "requires LoadableSwhidMphf (not just SwhidMphf)",
    note = "swh-graph v7.0.0 split some methods of the SwhidMphf trait into a new LoadableSwhidMphf trait."
)]
/// Sub-trait of [`SwhidMphf`] for "concrete" MPHF implementations
pub trait LoadableSwhidMphf: SwhidMphf {
    type WithMappedPermutation: SwhidMphf;

    fn load(basepath: impl AsRef<Path>) -> Result<Self>
    where
        Self: Sized;

    /// Given the base path of the MPH, mmaps the associated .order file and returns it
    fn with_mapped_permutation(
        self,
        basepath: impl AsRef<Path>,
    ) -> Result<Self::WithMappedPermutation>;
}

/// Composition of a MPHF and a permutation
pub struct PermutedMphf<MPHF: SwhidMphf, P: Permutation> {
    mphf: MPHF,
    permutation: P,
}

impl<MPHF: SwhidMphf, P: Permutation> SwhidMphf for PermutedMphf<MPHF, P> {
    #[inline(always)]
    fn num_keys(&self) -> usize {
        self.mphf.num_keys()
    }

    #[inline(always)]
    fn hash_array(&self, swhid: &[u8; SWHID::BYTES_SIZE]) -> Option<NodeId> {
        self.mphf
            .hash_array(swhid)
            .and_then(|id| self.permutation.get(id))
    }

    #[inline(always)]
    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
        self.mphf
            .hash_str(swhid)
            .and_then(|id| self.permutation.get(id))
    }

    #[inline(always)]
    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
        self.mphf
            .hash_str_array(swhid)
            .and_then(|id| self.permutation.get(id))
    }

    #[inline(always)]
    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
        self.mphf
            .hash_swhid(swhid)
            .and_then(|id| self.permutation.get(id))
    }
}

/// Trivial implementation of [`SwhidMphf`] that stores the list of items in a vector
pub struct VecMphf {
    pub swhids: Vec<SWHID>,
}

impl SwhidMphf for VecMphf {
    #[inline(always)]
    fn num_keys(&self) -> usize {
        self.swhids.len()
    }

    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
        swhid
            .as_ref()
            .try_into()
            .ok()
            .and_then(|swhid: SWHID| self.hash_swhid(&swhid))
    }

    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
        String::from_utf8(swhid.to_vec())
            .ok()
            .and_then(|swhid| self.hash_str(swhid))
    }

    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
        self.swhids.iter().position(|item| item == swhid)
    }
}

impl LoadableSwhidMphf for GOVMPH {
    type WithMappedPermutation = PermutedMphf<Self, MappedPermutation>;

    fn load(basepath: impl AsRef<Path>) -> Result<Self>
    where
        Self: Sized,
    {
        let path = suffix_path(basepath, ".cmph");
        GOVMPH::load(&path).with_context(|| format!("Could not load {}", path.display()))
    }

    fn with_mapped_permutation(
        self,
        basepath: impl AsRef<Path>,
    ) -> Result<Self::WithMappedPermutation> {
        let suffix = ".order"; // not .cmph.order because .order predates "modular" MPH functions
        let permutation = MappedPermutation::load_unchecked(&suffix_path(&basepath, suffix))?;
        Ok(PermutedMphf {
            mphf: self,
            permutation,
        })
    }
}

impl SwhidMphf for GOVMPH {
    #[inline(always)]
    fn num_keys(&self) -> usize {
        self.size() as usize
    }

    #[inline(always)]
    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
        Some(self.get_byte_array(swhid.as_ref().as_bytes()) as usize)
    }

    #[inline(always)]
    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
        Some(self.get_byte_array(swhid) as usize)
    }
}

#[cfg(feature = "pthash")]
mod swhid_pthash {
    use super::*;

    use pthash::{DictionaryDictionary, Minimal, MurmurHash2_128, PartitionedPhf, Phf};

    pub struct SwhidPthash(pub PartitionedPhf<Minimal, MurmurHash2_128, DictionaryDictionary>);

    impl SwhidPthash {
        pub fn load(path: impl AsRef<Path>) -> Result<Self>
        where
            Self: Sized,
        {
            let path = path.as_ref();
            PartitionedPhf::load(path)
                .with_context(|| format!("Could not load {}", path.display()))
                .map(SwhidPthash)
        }
    }

    /// Newtype to make SWHID hashable by PTHash
    pub(crate) struct HashableSWHID<T: AsRef<[u8]>>(pub T);

    impl<T: AsRef<[u8]>> pthash::Hashable for HashableSWHID<T> {
        type Bytes<'a>
            = &'a T
        where
            T: 'a;
        fn as_bytes(&self) -> Self::Bytes<'_> {
            &self.0
        }
    }

    impl LoadableSwhidMphf for SwhidPthash {
        type WithMappedPermutation = PermutedMphf<Self, MappedPermutation>;

        fn load(basepath: impl AsRef<Path>) -> Result<Self>
        where
            Self: Sized,
        {
            let path = suffix_path(basepath, ".pthash");
            SwhidPthash::load(path)
        }

        fn with_mapped_permutation(
            self,
            basepath: impl AsRef<Path>,
        ) -> Result<Self::WithMappedPermutation> {
            let suffix = ".pthash.order";
            let permutation = MappedPermutation::load_unchecked(&suffix_path(&basepath, suffix))?;
            Ok(PermutedMphf {
                mphf: self,
                permutation,
            })
        }
    }

    impl SwhidMphf for SwhidPthash {
        #[inline(always)]
        fn num_keys(&self) -> usize {
            self.0.num_keys() as usize
        }

        #[inline(always)]
        fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
            Some(self.0.hash(HashableSWHID(swhid.as_ref().as_bytes())) as usize)
        }

        #[inline(always)]
        fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
            Some(self.0.hash(HashableSWHID(swhid)) as usize)
        }
    }
}

#[cfg(feature = "pthash")]
pub use swhid_pthash::*;

pub struct SwhidFmphgo(pub GOFunction<TwoToPowerBitsStatic<4>, TwoToPowerBitsStatic<1>>);

impl SwhidFmphgo {
    pub fn load(path: impl AsRef<Path>) -> Result<Self>
    where
        Self: Sized,
    {
        let path = path.as_ref();
        let file =
            File::open(path).with_context(|| format!("Could not open {}", path.display()))?;
        ph::fmph::GOFunction::read(&mut BufReader::new(file))
            .with_context(|| format!("Could not load {}", path.display()))
            .map(SwhidFmphgo)
    }
}

impl LoadableSwhidMphf for SwhidFmphgo {
    type WithMappedPermutation = PermutedMphf<Self, MappedPermutation>;

    fn load(basepath: impl AsRef<Path>) -> Result<Self>
    where
        Self: Sized,
    {
        let path = suffix_path(basepath, ".fmphgo");
        SwhidFmphgo::load(path)
    }

    fn with_mapped_permutation(
        self,
        basepath: impl AsRef<Path>,
    ) -> Result<Self::WithMappedPermutation> {
        let suffix = ".fmphgo.order";
        let permutation = MappedPermutation::load_unchecked(&suffix_path(&basepath, suffix))?;
        Ok(PermutedMphf {
            mphf: self,
            permutation,
        })
    }
}

impl SwhidMphf for SwhidFmphgo {
    #[inline(always)]
    fn num_keys(&self) -> usize {
        self.0.len()
    }

    #[inline(always)]
    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
        self.0
            .get(swhid.as_ref().as_bytes())
            .and_then(|hash| usize::try_from(hash).ok())
    }

    #[inline(always)]
    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
        self.0
            .get(swhid.as_ref())
            .and_then(|hash| usize::try_from(hash).ok())
    }
}

/// Enum of possible implementations of [`SwhidMphf`].
///
/// Loads either [`SwhidPthash`] or [`GOVMPH`]
/// depending on which file is available at the given path.
pub enum DynMphf {
    Fmphgo(SwhidFmphgo),
    #[cfg(feature = "pthash")]
    Pthash(SwhidPthash),
    GOV(GOVMPH),
}

impl std::fmt::Debug for DynMphf {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DynMphf::Fmphgo(_) => write!(f, "DynMphf::Fmphgo(_)"),
            #[cfg(feature = "pthash")]
            DynMphf::Pthash(_) => write!(f, "DynMphf::Pthash(_)"),
            DynMphf::GOV(_) => write!(f, "DynMphf::GOV(_)"),
        }
    }
}

impl From<GOVMPH> for DynMphf {
    #[inline(always)]
    fn from(value: GOVMPH) -> DynMphf {
        DynMphf::GOV(value)
    }
}

#[cfg(feature = "pthash")]
impl From<SwhidPthash> for DynMphf {
    #[inline(always)]
    fn from(value: SwhidPthash) -> DynMphf {
        DynMphf::Pthash(value)
    }
}

impl From<SwhidFmphgo> for DynMphf {
    #[inline(always)]
    fn from(value: SwhidFmphgo) -> DynMphf {
        DynMphf::Fmphgo(value)
    }
}

impl LoadableSwhidMphf for DynMphf {
    type WithMappedPermutation = PermutedDynMphf;

    fn load(basepath: impl AsRef<Path>) -> Result<Self> {
        let basepath = basepath.as_ref();

        let gov_path = suffix_path(basepath, ".cmph");
        if gov_path.exists() {
            return LoadableSwhidMphf::load(basepath)
                .map(Self::GOV)
                .with_context(|| format!("Could not load {}", gov_path.display()));
        }

        let fmphgo_path = suffix_path(basepath, ".fmphgo");
        if fmphgo_path.exists() {
            return LoadableSwhidMphf::load(basepath)
                .map(Self::Fmphgo)
                .with_context(|| format!("Could not load {}", fmphgo_path.display()));
        }

        let pthash_path = suffix_path(basepath, ".pthash");
        if pthash_path.exists() {
            #[cfg(not(feature = "pthash"))]
            bail!(
                "Cannot load MPHF {} because pthash support is disabled. Recompile swh-graph with --features pthash.",
                pthash_path.display()
            );
            #[cfg(feature = "pthash")]
            return LoadableSwhidMphf::load(basepath)
                .map(Self::Pthash)
                .with_context(|| format!("Could not load {}", pthash_path.display()));
        }

        bail!(
            "Cannot load MPH function, neither {}, {}, nor {} exists.",
            fmphgo_path.display(),
            pthash_path.display(),
            gov_path.display()
        );
    }

    fn with_mapped_permutation(
        self,
        basepath: impl AsRef<Path>,
    ) -> Result<Self::WithMappedPermutation> {
        match self {
            Self::Fmphgo(mphf) => mphf
                .with_mapped_permutation(basepath)
                .map(PermutedDynMphf::Fmphgo),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf
                .with_mapped_permutation(basepath)
                .map(PermutedDynMphf::Pthash),
            Self::GOV(mphf) => mphf
                .with_mapped_permutation(basepath)
                .map(PermutedDynMphf::GOV),
        }
    }
}

impl SwhidMphf for DynMphf {
    #[inline(always)]
    fn num_keys(&self) -> usize {
        match self {
            Self::Fmphgo(mphf) => mphf.num_keys(),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.num_keys(),
            Self::GOV(mphf) => mphf.num_keys(),
        }
    }

    #[inline(always)]
    fn hash_array(&self, swhid: &[u8; SWHID::BYTES_SIZE]) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_array(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_array(swhid),
            Self::GOV(mphf) => mphf.hash_array(swhid),
        }
    }

    #[inline(always)]
    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_str(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_str(swhid),
            Self::GOV(mphf) => mphf.hash_str(swhid),
        }
    }

    #[inline(always)]
    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_str_array(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_str_array(swhid),
            Self::GOV(mphf) => mphf.hash_str_array(swhid),
        }
    }

    #[inline(always)]
    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_swhid(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_swhid(swhid),
            Self::GOV(mphf) => mphf.hash_swhid(swhid),
        }
    }
}

/// [`DynMphf`] composed with a permutation
pub enum PermutedDynMphf {
    Fmphgo(<SwhidFmphgo as LoadableSwhidMphf>::WithMappedPermutation),
    #[cfg(feature = "pthash")]
    Pthash(<SwhidPthash as LoadableSwhidMphf>::WithMappedPermutation),
    GOV(<GOVMPH as LoadableSwhidMphf>::WithMappedPermutation),
}

impl SwhidMphf for PermutedDynMphf {
    #[inline(always)]
    fn num_keys(&self) -> usize {
        match self {
            Self::Fmphgo(mphf) => mphf.num_keys(),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.num_keys(),
            Self::GOV(mphf) => mphf.num_keys(),
        }
    }

    #[inline(always)]
    fn hash_array(&self, swhid: &[u8; SWHID::BYTES_SIZE]) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_array(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_array(swhid),
            Self::GOV(mphf) => mphf.hash_array(swhid),
        }
    }

    #[inline(always)]
    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_str(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_str(swhid),
            Self::GOV(mphf) => mphf.hash_str(swhid),
        }
    }

    #[inline(always)]
    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_str_array(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_str_array(swhid),
            Self::GOV(mphf) => mphf.hash_str_array(swhid),
        }
    }

    #[inline(always)]
    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
        match self {
            Self::Fmphgo(mphf) => mphf.hash_swhid(swhid),
            #[cfg(feature = "pthash")]
            Self::Pthash(mphf) => mphf.hash_swhid(swhid),
            Self::GOV(mphf) => mphf.hash_swhid(swhid),
        }
    }
}