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;
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::{VariantsJsonResponse, 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 response.
29    variants_json: FxOnceMap<RegistryVariantsJson, Arc<VariantsJsonResponse>>,
30}
31
32pub(crate) type FxOnceMap<K, V> = RegisteredOnceMap<K, V, BuildHasherDefault<FxHasher>>;
33pub(crate) type FxRegisteredEntry<'a, K, V> =
34    RegisteredEntry<'a, K, V, BuildHasherDefault<FxHasher>>;
35
36impl InMemoryIndex {
37    /// Returns a reference to the package metadata map.
38    pub(crate) fn implicit(&self) -> &FxOnceMap<PackageName, Arc<VersionsResponse>> {
39        &self.0.implicit
40    }
41
42    /// Returns a reference to the package metadata map.
43    pub(crate) fn explicit(&self) -> &FxOnceMap<(PackageName, IndexUrl), Arc<VersionsResponse>> {
44        &self.0.explicit
45    }
46
47    /// Returns a reference to the distribution metadata map.
48    pub fn distributions(&self) -> &DistributionMetadataIndex {
49        &self.0.distributions
50    }
51
52    pub(crate) fn variants_json(
53        &self,
54    ) -> &FxOnceMap<RegistryVariantsJson, Arc<VariantsJsonResponse>> {
55        &self.0.variants_json
56    }
57
58    /// Return exclusive access to distribution metadata when no cloned index can use it.
59    pub fn distributions_mut(&mut self) -> Option<&mut DistributionMetadataIndex> {
60        Arc::get_mut(&mut self.0).map(|index| &mut index.distributions)
61    }
62}