richardphibel / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone
52797d
From e83ff88f8e4604024e4d5f4fd9f2fa4e5d4f03f9 Mon Sep 17 00:00:00 2001
52797d
From: Jaroslav Mracek <jmracek@redhat.com>
52797d
Date: Tue, 23 Nov 2021 14:14:23 +0100
52797d
Subject: [PATCH] Add getLatestModules()
52797d
52797d
It returns latest module packages for each module name, stream, and
52797d
architecture.
52797d
52797d
It is an alternative implementation to code base with
52797d
the old modular solver.
52797d
52797d
Alternative to 73868368120ceea97ada628a1aa42236fb42b89d
52797d
---
52797d
 libdnf/module/ModulePackageContainer.cpp | 39 +++++++++++++++++++++++++++++++++++++++
52797d
 libdnf/module/ModulePackageContainer.hpp |  9 +++++++++
52797d
 2 files changed, 48 insertions(+)
52797d
52797d
diff --git a/libdnf/module/ModulePackageContainer.cpp b/libdnf/module/ModulePackageContainer.cpp
52797d
index efab497..f9f861c 100644
52797d
--- a/libdnf/module/ModulePackageContainer.cpp
52797d
+++ b/libdnf/module/ModulePackageContainer.cpp
52797d
@@ -1037,6 +1037,45 @@ ModulePackageContainer::getLatestModulesPerRepo(ModuleState moduleFilter,
52797d
     return output;
52797d
 }
52797d
 
52797d
+std::vector<ModulePackage *>
52797d
+ModulePackageContainer::getLatestModules(const std::vector<ModulePackage *> modulePackages, bool activeOnly)
52797d
+{
52797d
+    std::vector<ModulePackage *> latestModules;
52797d
+    if (activeOnly) {
52797d
+        // When no active module return
52797d
+        if (!pImpl->activatedModules) {
52797d
+            return latestModules;
52797d
+        }
52797d
+    }
52797d
+    std::map<std::string, std::vector<ModulePackage *>> latest;
52797d
+
52797d
+    for (auto modulePackage : modulePackages) {
52797d
+        if (activeOnly && !isModuleActive(modulePackage)) {
52797d
+            continue;
52797d
+        }
52797d
+        auto nameStreamArch = modulePackage->getNameStream();
52797d
+        nameStreamArch.append(":");
52797d
+        nameStreamArch.append(modulePackage->getArchCStr());
52797d
+        auto & entries = latest[nameStreamArch];
52797d
+        if (entries.empty()) {
52797d
+            entries.push_back(modulePackage);
52797d
+        } else {
52797d
+            auto version = (*entries.begin())->getVersionNum();
52797d
+            if (version < modulePackage->getVersionNum()) {
52797d
+                entries.clear();
52797d
+                entries.push_back(modulePackage);
52797d
+            } else if (version == modulePackage->getVersionNum()) {
52797d
+                entries.push_back(modulePackage);
52797d
+            }
52797d
+        }
52797d
+    }
52797d
+    for (auto & entries : latest) {
52797d
+        for (auto modulePackage : entries.second) {
52797d
+            latestModules.push_back(modulePackage);
52797d
+        }
52797d
+    }
52797d
+    return latestModules;
52797d
+}
52797d
 
52797d
 std::pair<std::vector<std::vector<std::string>>, ModulePackageContainer::ModuleErrorType>
52797d
 ModulePackageContainer::resolveActiveModulePackages(bool debugSolver)
52797d
diff --git a/libdnf/module/ModulePackageContainer.hpp b/libdnf/module/ModulePackageContainer.hpp
52797d
index 7e5071b..42d5a9e 100644
52797d
--- a/libdnf/module/ModulePackageContainer.hpp
52797d
+++ b/libdnf/module/ModulePackageContainer.hpp
52797d
@@ -132,6 +132,15 @@ public:
52797d
     std::vector<std::vector<std::vector<ModulePackage *>>> getLatestModulesPerRepo(
52797d
         ModuleState moduleFilter, std::vector<ModulePackage *> modulePackages);
52797d
 
52797d
+    /**
52797d
+    * @brief Return all latest ModulePackages for each module Name, stream and architecture. In case of
52797d
+    * multiple latest packages, all will be returned. When activeOnly is true, it returns only the latest active
52797d
+    * packages.
52797d
+    *
52797d
+    * @return std::vector<ModulePackage *>
52797d
+    */
52797d
+    std::vector<ModulePackage *> getLatestModules(const std::vector<ModulePackage *> modulePackages, bool activeOnly);
52797d
+
52797d
     std::vector<ModulePackage *> requiresModuleEnablement(const libdnf::PackageSet & packages);
52797d
 
52797d
     /**
52797d
--
52797d
libgit2 1.1.0
52797d