Skip to main content

uv_resolver/resolver/
index.rs

1use std::hash::BuildHasherDefault;
2use std::sync::Arc;
3
4use rustc_hash::FxHasher;
5use uv_distribution_types::{IndexUrl, VariantsJsonContent};
6use uv_normalize::PackageName;
7use uv_once_map::{RegisteredEntry, RegisteredOnceMap};
8use uv_resolver_types::DistributionMetadataIndex;
9
10use crate::prioritized_distribution::RegistryVariantsJson;
11use crate::resolver::provider::{MetadataUnavailable, VersionsResponse};
12
13/// In-memory index of package metadata.
14#[derive(Default, Clone)]
15pub struct InMemoryIndex(Arc<SharedInMemoryIndex>);
16
17#[derive(Default)]
18struct SharedInMemoryIndex {
19    /// A map from package name to the metadata for that package and the index where the metadata
20    /// came from.
21    implicit: FxOnceMap<PackageName, Arc<VersionsResponse>>,
22
23    explicit: FxOnceMap<(PackageName, IndexUrl), Arc<VersionsResponse>>,
24
25    /// A map from a concrete distribution to its metadata.
26    distributions: DistributionMetadataIndex,
27
28    /// A map from an index variant metadata file to its parsed contents.
29    variants_json: FxOnceMap<RegistryVariantsJson, Arc<ParsedVariantsJsonResponse>>,
30}
31
32pub(crate) type ParsedVariantsJsonResponse = Result<VariantsJsonContent, MetadataUnavailable>;
33pub(crate) type FxOnceMap<K, V> = RegisteredOnceMap<K, V, BuildHasherDefault<FxHasher>>;
34pub(crate) type FxRegisteredEntry<'a, K, V> =
35    RegisteredEntry<'a, K, V, BuildHasherDefault<FxHasher>>;
36
37impl InMemoryIndex {
38    /// Returns a reference to the package metadata map.
39    pub(crate) fn implicit(&self) -> &FxOnceMap<PackageName, Arc<VersionsResponse>> {
40        &self.0.implicit
41    }
42
43    /// Returns a reference to the package metadata map.
44    pub(crate) fn explicit(&self) -> &FxOnceMap<(PackageName, IndexUrl), Arc<VersionsResponse>> {
45        &self.0.explicit
46    }
47
48    /// Returns a reference to the distribution metadata map.
49    pub fn distributions(&self) -> &DistributionMetadataIndex {
50        &self.0.distributions
51    }
52
53    pub(crate) fn variants_json(
54        &self,
55    ) -> &FxOnceMap<RegistryVariantsJson, Arc<ParsedVariantsJsonResponse>> {
56        &self.0.variants_json
57    }
58
59    /// Return exclusive access to distribution metadata when no cloned index can use it.
60    pub fn distributions_mut(&mut self) -> Option<&mut DistributionMetadataIndex> {
61        Arc::get_mut(&mut self.0).map(|index| &mut index.distributions)
62    }
63}