dcavalca / rpms / libdnf

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