|
4 | 4 |
|
5 | 5 | use std::borrow::Cow; |
6 | 6 | use std::cell::RefCell; |
7 | | -use std::collections::{BTreeSet, HashMap, HashSet}; |
| 7 | +use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; |
8 | 8 | use std::str::FromStr; |
9 | 9 | use std::sync::Arc; |
10 | 10 |
|
@@ -39,6 +39,7 @@ use spk_schema::prelude::{HasVersion, Named}; |
39 | 39 | use spk_schema::version::Version; |
40 | 40 | use spk_schema::version_range::{parse_version_range, DoubleEqualsVersion, Ranged, VersionFilter}; |
41 | 41 | use spk_schema::{BuildIdent, Deprecate, Opt, OptionMap, Package, Recipe, Request, VersionIdent}; |
| 42 | +use spk_solve_package_iterator::SortedBuildIterator; |
42 | 43 | use spk_storage::RepositoryHandle; |
43 | 44 | use tracing::{debug_span, Instrument}; |
44 | 45 |
|
@@ -661,6 +662,29 @@ impl SpkProvider { |
661 | 662 | } |
662 | 663 | } |
663 | 664 |
|
| 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 | + |
664 | 688 | pub fn var_requirements(&mut self, requests: &[Request]) -> Vec<VersionSetId> { |
665 | 689 | self.global_var_requests.reserve(requests.len()); |
666 | 690 | requests |
@@ -971,7 +995,26 @@ impl DependencyProvider for SpkProvider { |
971 | 995 | } |
972 | 996 |
|
973 | 997 | 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 | + |
975 | 1018 | // TODO: The ordering should take component names into account, so |
976 | 1019 | // the run component or the build component is tried first in the |
977 | 1020 | // appropriate situations. |
@@ -1009,17 +1052,7 @@ impl DependencyProvider for SpkProvider { |
1009 | 1052 | (_, Build::Source) => return std::cmp::Ordering::Less, |
1010 | 1053 | _ => {} |
1011 | 1054 | }; |
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 |
1023 | 1056 | } |
1024 | 1057 | ord => ord, |
1025 | 1058 | } |
|
0 commit comments