diff --git a/include/libdnf5/rpm/package_query.hpp b/include/libdnf5/rpm/package_query.hpp index a0ac087db..a60d21d65 100644 --- a/include/libdnf5/rpm/package_query.hpp +++ b/include/libdnf5/rpm/package_query.hpp @@ -618,6 +618,13 @@ class PackageQuery : public PackageSet { // @replaces libdnf/sack/query.hpp:method:addFilter(int keyname, int cmp_type, int match) - cmp_type = HY_PKG_LATEST_PER_ARCH void filter_latest_evr(int limit = 1); + /// Group packages by `name`. Then within each group, keep packages that correspond with up to `limit` of (all but) latest `evr`s in the group. + /// + /// @param limit If `limit` > 0, keep `limit` number `evr`s in each group. + /// If `limit` < 0, keep all **but** `limit` last `evr`s in each group. + /// @since 5.2 + void filter_latest_evr_any_arch(int limit = 1); + /// Group packages by `name` and `arch`. Then within each group, keep packages that correspond with up to `limit` of (all but) earliest `evr`s in the group. /// /// @param limit If `limit` > 0, keep `limit` number `evr`s in each group. @@ -625,6 +632,13 @@ class PackageQuery : public PackageSet { /// @since 5.0 void filter_earliest_evr(int limit = 1); + /// Group packages by `name`. Then within each group, keep packages that correspond with up to `limit` of (all but) earliest `evr`s in the group. + /// + /// @param limit If `limit` > 0, keep `limit` number `evr`s in each group. + /// If `limit` < 0, keep all **but** `limit` last `evr`s in each group. + /// @since 5.2 + void filter_earliest_evr_any_arch(int limit = 1); + /// Group packages by `name` and `arch`. Then within each group, keep packages that belong to a repo with the highest priority (the lowest number). /// The filter works only on available packages, installed packages are not affected. /// diff --git a/libdnf5/rpm/package_query.cpp b/libdnf5/rpm/package_query.cpp index e9cd7ee70..897f14e4f 100644 --- a/libdnf5/rpm/package_query.cpp +++ b/libdnf5/rpm/package_query.cpp @@ -2435,6 +2435,19 @@ static int latest_cmp(const Id * ap, const Id * bp, libdnf5::solv::RpmPool * poo return *ap - *bp; } +static int latest_ignore_arch_cmp(const Id * ap, const Id * bp, libdnf5::solv::RpmPool * pool) { + Solvable * sa = pool->id2solvable(*ap); + Solvable * sb = pool->id2solvable(*bp); + int r; + r = sa->name - sb->name; + if (r) + return r; + r = pool->evrcmp(sb->evr, sa->evr, EVRCMP_COMPARE); + if (r) + return r; + return *ap - *bp; +} + static int earliest_cmp(const Id * ap, const Id * bp, libdnf5::solv::RpmPool * pool) { Solvable * sa = pool->id2solvable(*ap); Solvable * sb = pool->id2solvable(*bp); @@ -2453,11 +2466,27 @@ static int earliest_cmp(const Id * ap, const Id * bp, libdnf5::solv::RpmPool * p return *ap - *bp; } +static int earliest_ignore_arch_cmp(const Id * ap, const Id * bp, libdnf5::solv::RpmPool * pool) { + Solvable * sa = pool->id2solvable(*ap); + Solvable * sb = pool->id2solvable(*bp); + int r; + r = sa->name - sb->name; + if (r) + return r; + r = pool->evrcmp(sb->evr, sa->evr, EVRCMP_COMPARE); + if (r > 0) + return -1; + if (r < 0) + return 1; + return *ap - *bp; +} + static void filter_first_sorted_by( libdnf5::solv::RpmPool & pool, int limit, int (*cmp)(const Id * a, const Id * b, libdnf5::solv::RpmPool * pool), - libdnf5::solv::SolvMap & data) { + libdnf5::solv::SolvMap & data, + bool group_by_arch = true) { libdnf5::solv::IdQueue samename; for (Id candidate_id : data) { samename.push_back(candidate_id); @@ -2471,7 +2500,7 @@ static void filter_first_sorted_by( int i; for (i = 0; i < samename.size(); ++i) { Solvable * considered = pool.id2solvable(samename[i]); - if (!highest || highest->name != considered->name || highest->arch != considered->arch) { + if (!highest || highest->name != considered->name || (group_by_arch && (highest->arch != considered->arch))) { /* start of a new block */ if (start_block == -1) { highest = considered; @@ -2492,10 +2521,18 @@ void PackageQuery::filter_latest_evr(int limit) { filter_first_sorted_by(get_rpm_pool(p_impl->base), limit, latest_cmp, *p_impl); } +void PackageQuery::filter_latest_evr_any_arch(int limit) { + filter_first_sorted_by(get_rpm_pool(p_impl->base), limit, latest_ignore_arch_cmp, *p_impl, false); +} + void PackageQuery::filter_earliest_evr(int limit) { filter_first_sorted_by(get_rpm_pool(p_impl->base), limit, earliest_cmp, *p_impl); } +void PackageQuery::filter_earliest_evr_any_arch(int limit) { + filter_first_sorted_by(get_rpm_pool(p_impl->base), limit, earliest_ignore_arch_cmp, *p_impl, false); +} + static inline bool priority_solvable_cmp_key(const Solvable * first, const Solvable * second) { if (first->name != second->name) { return first->name < second->name; diff --git a/test/data/repos-solv/solv-multiarch.repo b/test/data/repos-solv/solv-multiarch.repo new file mode 100644 index 000000000..b966d66a7 --- /dev/null +++ b/test/data/repos-solv/solv-multiarch.repo @@ -0,0 +1,13 @@ +=Ver: 3.0 + +=Pkg: foo 1.2 1 x86_64 +=Prv: foo = 1.2 1 + +=Pkg: foo 1.2 2 noarch +=Prv: foo = 1.2 2 + +=Pkg: bar 4.5 1 noarch +=Prv: bar = 4.5 1 + +=Pkg: bar 4.5 2 x86_64 +=Prv: bar = 4.5 2 diff --git a/test/libdnf5/rpm/test_package_query.cpp b/test/libdnf5/rpm/test_package_query.cpp index edbb2bdbe..f0ccac63c 100644 --- a/test/libdnf5/rpm/test_package_query.cpp +++ b/test/libdnf5/rpm/test_package_query.cpp @@ -115,6 +115,31 @@ void RpmPackageQueryTest::test_filter_latest_evr() { } } +void RpmPackageQueryTest::test_filter_latest_evr_ignore_arch() { + add_repo_solv("solv-multiarch"); + + { + // Result of filter_latest_evr should include a package from each arch + PackageQuery query(base); + query.filter_latest_evr(1); + std::vector expected = { + get_pkg("foo-0:1.2-1.x86_64"), + get_pkg("foo-0:1.2-2.noarch"), + get_pkg("bar-0:4.5-1.noarch"), + get_pkg("bar-0:4.5-2.x86_64"), + }; + CPPUNIT_ASSERT_EQUAL(expected, to_vector(query)); + } + { + // Result of filter_latest_evr_ignore_arch should include only the latest + // packages, regardless of arch + PackageQuery query(base); + query.filter_latest_evr_any_arch(1); + std::vector expected = {get_pkg("foo-0:1.2-2.noarch"), get_pkg("bar-0:4.5-2.x86_64")}; + CPPUNIT_ASSERT_EQUAL(expected, to_vector(query)); + } +} + void RpmPackageQueryTest::test_filter_earliest_evr() { add_repo_solv("solv-repo1"); add_repo_solv("solv-24pkgs"); @@ -173,6 +198,31 @@ void RpmPackageQueryTest::test_filter_earliest_evr() { } } +void RpmPackageQueryTest::test_filter_earliest_evr_ignore_arch() { + add_repo_solv("solv-multiarch"); + + { + // Result of filter_earliest_evr should include a package from each arch + PackageQuery query(base); + query.filter_earliest_evr(1); + std::vector expected = { + get_pkg("foo-0:1.2-1.x86_64"), + get_pkg("foo-0:1.2-2.noarch"), + get_pkg("bar-0:4.5-1.noarch"), + get_pkg("bar-0:4.5-2.x86_64"), + }; + CPPUNIT_ASSERT_EQUAL(expected, to_vector(query)); + } + { + // Result of filter_earliest_evr_ignore_arch should include only the earliest + // packages, regardless of arch + PackageQuery query(base); + query.filter_earliest_evr_any_arch(1); + std::vector expected = {get_pkg("foo-0:1.2-1.x86_64"), get_pkg("bar-0:4.5-1.noarch")}; + CPPUNIT_ASSERT_EQUAL(expected, to_vector(query)); + } +} + void RpmPackageQueryTest::test_filter_name() { add_repo_solv("solv-repo1"); diff --git a/test/libdnf5/rpm/test_package_query.hpp b/test/libdnf5/rpm/test_package_query.hpp index 35e4ad1c7..5c4c50f60 100644 --- a/test/libdnf5/rpm/test_package_query.hpp +++ b/test/libdnf5/rpm/test_package_query.hpp @@ -33,7 +33,9 @@ class RpmPackageQueryTest : public BaseTestCase { #ifndef WITH_PERFORMANCE_TESTS CPPUNIT_TEST(test_size); CPPUNIT_TEST(test_filter_latest_evr); + CPPUNIT_TEST(test_filter_latest_evr_ignore_arch); CPPUNIT_TEST(test_filter_earliest_evr); + CPPUNIT_TEST(test_filter_earliest_evr_ignore_arch); CPPUNIT_TEST(test_filter_name); CPPUNIT_TEST(test_filter_name_packgset); CPPUNIT_TEST(test_filter_nevra_packgset); @@ -66,7 +68,9 @@ class RpmPackageQueryTest : public BaseTestCase { void test_size(); void test_filter_latest_evr(); + void test_filter_latest_evr_ignore_arch(); void test_filter_earliest_evr(); + void test_filter_earliest_evr_ignore_arch(); void test_filter_name(); void test_filter_name_packgset(); void test_filter_nevra_packgset();