sbom-tools 0.1.19

Semantic SBOM diff and analysis tool
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
533
534
535
536
537
538
539
540
541
//! Index structures for efficient SBOM querying.
//!
//! This module provides `NormalizedSbomIndex`, a precomputed index for fast
//! lookups and sorting operations on SBOMs. Building the index once avoids
//! repeated O(n) scans and string allocations during TUI operations.
//!
//! # Example
//!
//! ```ignore
//! use sbom_tools::model::{NormalizedSbom, NormalizedSbomIndex};
//!
//! let sbom = parse_sbom(&path)?;
//! let index = NormalizedSbomIndex::build(&sbom);
//!
//! // Fast dependency lookup - O(1) instead of O(edges)
//! let deps = index.dependencies_of(&component_id);
//!
//! // Fast case-insensitive name search - O(1) instead of O(components)
//! let matches = index.find_by_name_lower("openssl");
//! ```

use super::{CanonicalId, Component, DependencyEdge, NormalizedSbom};
use std::collections::HashMap;

/// Precomputed index for efficient SBOM queries.
///
/// This index is built once per SBOM and provides O(1) lookups for:
/// - Dependencies of a component (edges by source)
/// - Dependents of a component (edges by target)
/// - Components by lowercased name
/// - Pre-computed sort keys to avoid repeated string allocations
#[derive(Debug, Clone)]
#[must_use]
pub struct NormalizedSbomIndex {
    /// Edge indices by source component ID (for fast dependency lookup)
    edges_by_source: HashMap<CanonicalId, Vec<usize>>,
    /// Edge indices by target component ID (for fast dependent lookup)
    edges_by_target: HashMap<CanonicalId, Vec<usize>>,
    /// Component IDs by lowercased name (for case-insensitive search)
    by_name_lower: HashMap<String, Vec<CanonicalId>>,
    /// Pre-computed sort keys for each component
    sort_keys: HashMap<CanonicalId, ComponentSortKey>,
    /// Bom-ref (format_id) to canonical ID mapping for crypto cross-reference resolution
    bom_ref_map: HashMap<String, CanonicalId>,
    /// Total component count
    component_count: usize,
    /// Total edge count
    edge_count: usize,
}

/// Pre-computed lowercase strings for sorting without repeated allocations.
#[derive(Debug, Clone, Default)]
pub struct ComponentSortKey {
    /// Lowercased component name
    pub name_lower: String,
    /// Lowercased version string
    pub version_lower: String,
    /// Lowercased ecosystem name
    pub ecosystem_lower: String,
    /// Lowercased canonical ID
    pub id_lower: String,
    /// Lowercased PURL (if available)
    pub purl_lower: String,
    /// Lowercased group/namespace
    pub group_lower: String,
}

impl ComponentSortKey {
    /// Build sort key from a component
    #[must_use]
    pub fn from_component(comp: &Component) -> Self {
        Self {
            name_lower: comp.name.to_lowercase(),
            version_lower: comp.version.as_deref().unwrap_or("").to_lowercase(),
            ecosystem_lower: comp
                .ecosystem
                .as_ref()
                .map(|e| e.to_string().to_lowercase())
                .unwrap_or_default(),
            id_lower: comp.canonical_id.value().to_lowercase(),
            purl_lower: comp
                .identifiers
                .purl
                .as_deref()
                .unwrap_or("")
                .to_lowercase(),
            group_lower: comp.group.as_deref().unwrap_or("").to_lowercase(),
        }
    }

    /// Check if any field contains the query (case-insensitive)
    #[must_use]
    pub fn contains(&self, query_lower: &str) -> bool {
        self.name_lower.contains(query_lower)
            || self.version_lower.contains(query_lower)
            || self.purl_lower.contains(query_lower)
            || self.id_lower.contains(query_lower)
    }

    /// Check if name contains the query
    #[must_use]
    pub fn name_contains(&self, query_lower: &str) -> bool {
        self.name_lower.contains(query_lower)
    }
}

impl NormalizedSbomIndex {
    /// Build an index from a normalized SBOM.
    ///
    /// This is an O(n + m) operation where n = components and m = edges.
    /// The resulting index provides O(1) lookups.
    pub fn build(sbom: &NormalizedSbom) -> Self {
        let mut edges_by_source: HashMap<CanonicalId, Vec<usize>> = HashMap::new();
        let mut edges_by_target: HashMap<CanonicalId, Vec<usize>> = HashMap::new();
        let mut by_name_lower: HashMap<String, Vec<CanonicalId>> = HashMap::new();
        let mut sort_keys: HashMap<CanonicalId, ComponentSortKey> = HashMap::new();
        let mut bom_ref_map: HashMap<String, CanonicalId> = HashMap::new();

        // Index edges
        for (idx, edge) in sbom.edges.iter().enumerate() {
            edges_by_source
                .entry(edge.from.clone())
                .or_default()
                .push(idx);
            edges_by_target
                .entry(edge.to.clone())
                .or_default()
                .push(idx);
        }

        // Index components
        for (id, comp) in &sbom.components {
            // Index by lowercased name
            let name_lower = comp.name.to_lowercase();
            by_name_lower
                .entry(name_lower)
                .or_default()
                .push(id.clone());

            // Build sort key
            sort_keys.insert(id.clone(), ComponentSortKey::from_component(comp));

            // Index bom-ref (format_id) for crypto cross-reference resolution
            let format_id = &comp.identifiers.format_id;
            if !format_id.is_empty() {
                bom_ref_map.insert(format_id.clone(), id.clone());
            }
        }

        Self {
            edges_by_source,
            edges_by_target,
            by_name_lower,
            sort_keys,
            bom_ref_map,
            component_count: sbom.components.len(),
            edge_count: sbom.edges.len(),
        }
    }

    /// Get edge indices for dependencies of a component (outgoing edges).
    ///
    /// Returns empty slice if component has no dependencies.
    /// O(1) lookup instead of O(edges).
    pub fn dependency_indices(&self, id: &CanonicalId) -> &[usize] {
        self.edges_by_source
            .get(id)
            .map_or(&[], std::vec::Vec::as_slice)
    }

    /// Get edge indices for dependents of a component (incoming edges).
    ///
    /// Returns empty slice if component has no dependents.
    /// O(1) lookup instead of O(edges).
    pub fn dependent_indices(&self, id: &CanonicalId) -> &[usize] {
        self.edges_by_target
            .get(id)
            .map_or(&[], std::vec::Vec::as_slice)
    }

    /// Get dependencies of a component as edges.
    ///
    /// O(k) where k = number of dependencies (much faster than O(edges)).
    #[must_use]
    pub fn dependencies_of<'a>(
        &self,
        id: &CanonicalId,
        edges: &'a [DependencyEdge],
    ) -> Vec<&'a DependencyEdge> {
        self.dependency_indices(id)
            .iter()
            .filter_map(|&idx| edges.get(idx))
            .collect()
    }

    /// Get dependents of a component as edges.
    ///
    /// O(k) where k = number of dependents (much faster than O(edges)).
    #[must_use]
    pub fn dependents_of<'a>(
        &self,
        id: &CanonicalId,
        edges: &'a [DependencyEdge],
    ) -> Vec<&'a DependencyEdge> {
        self.dependent_indices(id)
            .iter()
            .filter_map(|&idx| edges.get(idx))
            .collect()
    }

    /// Find component IDs by lowercased name.
    ///
    /// O(1) lookup instead of O(components).
    pub fn find_by_name_lower(&self, name_lower: &str) -> &[CanonicalId] {
        self.by_name_lower
            .get(name_lower)
            .map_or(&[], std::vec::Vec::as_slice)
    }

    /// Find component IDs whose name contains the query (case-insensitive).
    ///
    /// `O(unique_names)` - still iterates but only over unique lowercased names,
    /// not all components.
    #[must_use]
    pub fn search_by_name(&self, query_lower: &str) -> Vec<CanonicalId> {
        self.by_name_lower
            .iter()
            .filter(|(name, _)| name.contains(query_lower))
            .flat_map(|(_, ids)| ids.iter().cloned())
            .collect()
    }

    /// Get the pre-computed sort key for a component.
    ///
    /// O(1) lookup, avoids repeated `to_lowercase()` calls during sorting.
    #[must_use]
    pub fn sort_key(&self, id: &CanonicalId) -> Option<&ComponentSortKey> {
        self.sort_keys.get(id)
    }

    /// Get all sort keys for iteration.
    #[must_use]
    pub const fn sort_keys(&self) -> &HashMap<CanonicalId, ComponentSortKey> {
        &self.sort_keys
    }

    /// Resolve a bom-ref string (used in crypto cross-references like
    /// `signatureAlgorithmRef`, `subjectPublicKeyRef`, `algorithmRef`)
    /// to the canonical component ID.
    #[must_use]
    pub fn resolve_bom_ref(&self, bom_ref: &str) -> Option<&CanonicalId> {
        self.bom_ref_map.get(bom_ref)
    }

    /// Check if component has any dependencies.
    #[must_use]
    pub fn has_dependencies(&self, id: &CanonicalId) -> bool {
        self.edges_by_source.get(id).is_some_and(|v| !v.is_empty())
    }

    /// Check if component has any dependents.
    #[must_use]
    pub fn has_dependents(&self, id: &CanonicalId) -> bool {
        self.edges_by_target.get(id).is_some_and(|v| !v.is_empty())
    }

    /// Get count of dependencies for a component.
    pub fn dependency_count(&self, id: &CanonicalId) -> usize {
        self.edges_by_source.get(id).map_or(0, std::vec::Vec::len)
    }

    /// Get count of dependents for a component.
    pub fn dependent_count(&self, id: &CanonicalId) -> usize {
        self.edges_by_target.get(id).map_or(0, std::vec::Vec::len)
    }

    /// Get total component count.
    #[must_use]
    pub const fn component_count(&self) -> usize {
        self.component_count
    }

    /// Get total edge count.
    #[must_use]
    pub const fn edge_count(&self) -> usize {
        self.edge_count
    }

    /// Get count of root components (no incoming edges).
    #[must_use]
    pub fn root_count(&self) -> usize {
        self.component_count
            .saturating_sub(self.edges_by_target.len())
            + self
                .edges_by_target
                .values()
                .filter(|v| v.is_empty())
                .count()
    }

    /// Get count of leaf components (no outgoing edges).
    #[must_use]
    pub fn leaf_count(&self) -> usize {
        self.component_count
            .saturating_sub(self.edges_by_source.len())
            + self
                .edges_by_source
                .values()
                .filter(|v| v.is_empty())
                .count()
    }
}

/// Builder for creating indexes with optional features.
#[derive(Debug, Default)]
#[must_use]
pub struct SbomIndexBuilder {
    /// Whether to build name index
    index_names: bool,
    /// Whether to build sort keys
    build_sort_keys: bool,
}

impl SbomIndexBuilder {
    /// Create a new builder with all features enabled.
    pub const fn new() -> Self {
        Self {
            index_names: true,
            build_sort_keys: true,
        }
    }

    /// Create a minimal builder (edges only).
    pub const fn minimal() -> Self {
        Self {
            index_names: false,
            build_sort_keys: false,
        }
    }

    /// Enable name indexing.
    pub const fn with_name_index(mut self) -> Self {
        self.index_names = true;
        self
    }

    /// Enable sort key building.
    pub const fn with_sort_keys(mut self) -> Self {
        self.build_sort_keys = true;
        self
    }

    /// Build the index with configured options.
    pub fn build(&self, sbom: &NormalizedSbom) -> NormalizedSbomIndex {
        let mut edges_by_source: HashMap<CanonicalId, Vec<usize>> = HashMap::new();
        let mut edges_by_target: HashMap<CanonicalId, Vec<usize>> = HashMap::new();
        let mut by_name_lower: HashMap<String, Vec<CanonicalId>> = HashMap::new();
        let mut sort_keys: HashMap<CanonicalId, ComponentSortKey> = HashMap::new();
        let mut bom_ref_map: HashMap<String, CanonicalId> = HashMap::new();

        // Always index edges
        for (idx, edge) in sbom.edges.iter().enumerate() {
            edges_by_source
                .entry(edge.from.clone())
                .or_default()
                .push(idx);
            edges_by_target
                .entry(edge.to.clone())
                .or_default()
                .push(idx);
        }

        // Optionally index components
        if self.index_names || self.build_sort_keys {
            for (id, comp) in &sbom.components {
                if self.index_names {
                    let name_lower = comp.name.to_lowercase();
                    by_name_lower
                        .entry(name_lower)
                        .or_default()
                        .push(id.clone());
                }

                if self.build_sort_keys {
                    sort_keys.insert(id.clone(), ComponentSortKey::from_component(comp));
                }

                // Always index bom-refs for crypto cross-reference resolution
                let format_id = &comp.identifiers.format_id;
                if !format_id.is_empty() {
                    bom_ref_map.insert(format_id.clone(), id.clone());
                }
            }
        }

        NormalizedSbomIndex {
            edges_by_source,
            edges_by_target,
            by_name_lower,
            sort_keys,
            bom_ref_map,
            component_count: sbom.components.len(),
            edge_count: sbom.edges.len(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{DependencyType, DocumentMetadata};

    fn make_test_sbom() -> NormalizedSbom {
        let mut sbom = NormalizedSbom::new(DocumentMetadata::default());

        // Add components
        let comp_a = Component::new("ComponentA".to_string(), "comp-a".to_string());
        let comp_b = Component::new("ComponentB".to_string(), "comp-b".to_string());
        let comp_c = Component::new("componentb".to_string(), "comp-c".to_string()); // Same name lowercase

        let id_a = comp_a.canonical_id.clone();
        let id_b = comp_b.canonical_id.clone();
        let id_c = comp_c.canonical_id.clone();

        sbom.add_component(comp_a);
        sbom.add_component(comp_b);
        sbom.add_component(comp_c);

        // Add edges: A -> B, A -> C
        sbom.add_edge(DependencyEdge::new(
            id_a.clone(),
            id_b.clone(),
            DependencyType::DependsOn,
        ));
        sbom.add_edge(DependencyEdge::new(
            id_a.clone(),
            id_c.clone(),
            DependencyType::DependsOn,
        ));

        sbom
    }

    #[test]
    fn test_build_index() {
        let sbom = make_test_sbom();
        let index = NormalizedSbomIndex::build(&sbom);

        assert_eq!(index.component_count(), 3);
        assert_eq!(index.edge_count(), 2);
    }

    #[test]
    fn test_dependency_lookup() {
        let sbom = make_test_sbom();
        let index = NormalizedSbomIndex::build(&sbom);

        let comp_a_id = sbom.components.keys().next().unwrap();

        // A has 2 dependencies
        assert_eq!(index.dependency_count(comp_a_id), 2);

        // Get actual edges
        let deps = index.dependencies_of(comp_a_id, &sbom.edges);
        assert_eq!(deps.len(), 2);
    }

    #[test]
    fn test_dependent_lookup() {
        let sbom = make_test_sbom();
        let index = NormalizedSbomIndex::build(&sbom);

        // B and C should have 1 dependent each (A)
        let comp_b_id = sbom.components.keys().nth(1).unwrap();
        assert_eq!(index.dependent_count(comp_b_id), 1);
    }

    #[test]
    fn test_name_lookup() {
        let sbom = make_test_sbom();
        let index = NormalizedSbomIndex::build(&sbom);

        // "componentb" should match both ComponentB and componentb
        let matches = index.find_by_name_lower("componentb");
        assert_eq!(matches.len(), 2);
    }

    #[test]
    fn test_name_search() {
        let sbom = make_test_sbom();
        let index = NormalizedSbomIndex::build(&sbom);

        // Search for "component" should match all 3
        let matches = index.search_by_name("component");
        assert_eq!(matches.len(), 3);
    }

    #[test]
    fn test_sort_keys() {
        let sbom = make_test_sbom();
        let index = NormalizedSbomIndex::build(&sbom);

        let comp_a_id = sbom.components.keys().next().unwrap();
        let sort_key = index.sort_key(comp_a_id).unwrap();

        assert_eq!(sort_key.name_lower, "componenta");
    }

    #[test]
    fn test_sort_key_contains() {
        let mut comp = Component::new("MyPackage".to_string(), "pkg-1".to_string());
        comp.version = Some("1.2.3".to_string());
        let key = ComponentSortKey::from_component(&comp);

        assert!(key.contains("mypack"));
        assert!(key.contains("1.2.3"));
        assert!(!key.contains("notfound"));
    }

    #[test]
    fn test_builder_minimal() {
        let sbom = make_test_sbom();
        let index = SbomIndexBuilder::minimal().build(&sbom);

        // Edges should still be indexed
        assert_eq!(index.edge_count(), 2);

        // But name lookup returns empty (not indexed)
        let matches = index.find_by_name_lower("componenta");
        assert!(matches.is_empty());
    }

    #[test]
    fn test_empty_sbom() {
        let sbom = NormalizedSbom::default();
        let index = NormalizedSbomIndex::build(&sbom);

        assert_eq!(index.component_count(), 0);
        assert_eq!(index.edge_count(), 0);
    }
}