Skip to content

Commit ef13860

Browse files
committed
Signed-off-by: J Robert Ray <[email protected]>
1 parent e42fe45 commit ef13860

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

crates/spk-solve/crates/package-iterator/src/package_iterator.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,17 @@ impl SortedBuildIterator {
491491
Ok(sbi)
492492
}
493493

494+
pub async fn new_from_builds(
495+
builds: VecDeque<BuildWithRepos>,
496+
builds_with_impossible_requests: HashMap<BuildIdent, Compatibility>,
497+
) -> Result<Self> {
498+
let mut sbi = SortedBuildIterator { builds };
499+
500+
sbi.sort_by_build_option_values(builds_with_impossible_requests)
501+
.await;
502+
Ok(sbi)
503+
}
504+
494505
/// Helper for making BuildKey structures used in the sorting in
495506
/// sort_by_build_option_values() below
496507
fn make_option_values_build_key(

crates/spk-solve/src/cdcl_solver/spk_provider.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::borrow::Cow;
66
use std::cell::RefCell;
7-
use std::collections::{BTreeSet, HashMap, HashSet};
7+
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
88
use std::str::FromStr;
99
use std::sync::Arc;
1010

@@ -39,6 +39,7 @@ use spk_schema::prelude::{HasVersion, Named};
3939
use spk_schema::version::Version;
4040
use spk_schema::version_range::{parse_version_range, DoubleEqualsVersion, Ranged, VersionFilter};
4141
use spk_schema::{BuildIdent, Deprecate, Opt, OptionMap, Package, Recipe, Request, VersionIdent};
42+
use spk_solve_package_iterator::SortedBuildIterator;
4243
use spk_storage::RepositoryHandle;
4344
use tracing::{debug_span, Instrument};
4445

@@ -661,6 +662,29 @@ impl SpkProvider {
661662
}
662663
}
663664

665+
/// Order two builds based on which should be preferred to include in a
666+
/// solve as a candidate.
667+
///
668+
/// Generally this means a build with newer dependencies is ordered first.
669+
async fn sort_builds(
670+
&self,
671+
a: &LocatedBuildIdentWithComponent,
672+
b: &LocatedBuildIdentWithComponent,
673+
) -> std::cmp::Ordering {
674+
// This function should _not_ return `std::cmp::Ordering::Equal` unless
675+
// `a` and `b` are the same build (in practice `a` and `b` will never be
676+
// the same build).
677+
678+
// Embedded stubs are always ordered last.
679+
match (a.ident.is_embedded(), b.ident.is_embedded()) {
680+
(true, false) => return std::cmp::Ordering::Greater,
681+
(false, true) => return std::cmp::Ordering::Less,
682+
_ => {}
683+
};
684+
685+
todo!()
686+
}
687+
664688
pub fn var_requirements(&mut self, requests: &[Request]) -> Vec<VersionSetId> {
665689
self.global_var_requests.reserve(requests.len());
666690
requests
@@ -971,7 +995,26 @@ impl DependencyProvider for SpkProvider {
971995
}
972996

973997
async fn sort_candidates(&self, _solver: &SolverCache<Self>, solvables: &mut [SolvableId]) {
974-
// This implementation just picks the highest version.
998+
let located_builds = solvables
999+
.iter()
1000+
.enumerate()
1001+
.filter_map(|(index, solvable)| {
1002+
let solvable = self.pool.resolve_solvable(*solvable);
1003+
match &solvable.record {
1004+
SpkSolvable::LocatedBuildIdentWithComponent(
1005+
located_build_ident_with_component,
1006+
) => Some((located_build_ident_with_component, index)),
1007+
_ => None,
1008+
}
1009+
})
1010+
.collect::<Vec<_>>();
1011+
1012+
let mut builds = VecDeque::new();
1013+
1014+
if let Ok(mut sbi) = SortedBuildIterator::new_from_builds(builds, HashMap::new()).await {
1015+
todo!("sort solvables based on this order");
1016+
}
1017+
9751018
// TODO: The ordering should take component names into account, so
9761019
// the run component or the build component is tried first in the
9771020
// appropriate situations.
@@ -1009,17 +1052,7 @@ impl DependencyProvider for SpkProvider {
10091052
(_, Build::Source) => return std::cmp::Ordering::Less,
10101053
_ => {}
10111054
};
1012-
// Sort embedded packges second last
1013-
match (a.ident.build(), b.ident.build()) {
1014-
(Build::Embedded(_), Build::Embedded(_)) => {
1015-
// TODO: Could perhaps sort on the parent
1016-
// package to prefer a newer parent.
1017-
std::cmp::Ordering::Equal
1018-
}
1019-
(Build::Embedded(_), _) => std::cmp::Ordering::Greater,
1020-
(_, Build::Embedded(_)) => std::cmp::Ordering::Less,
1021-
_ => std::cmp::Ordering::Equal,
1022-
}
1055+
self.sort_builds(a, b).await
10231056
}
10241057
ord => ord,
10251058
}

0 commit comments

Comments
 (0)