Skip to content

Commit

Permalink
Clean Query
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Jan 31, 2024
1 parent a182b1d commit 15ce499
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 213 deletions.
13 changes: 11 additions & 2 deletions libmamba/include/mamba/api/repoquery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@

namespace mamba
{
bool repoquery(
enum class QueryResultFormat
{
Json = 0,
Tree = 1,
Table = 2,
Pretty = 3,
RecursiveTable = 4,
};

[[nodiscard]] auto repoquery(
Configuration& config,
QueryType type,
QueryResultFormat format,
bool use_local,
const std::vector<std::string>& query
);
) -> bool;
}
100 changes: 37 additions & 63 deletions libmamba/include/mamba/core/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef MAMBA_CORE_QUERY_HPP
#define MAMBA_CORE_QUERY_HPP

#include <iosfwd>
#include <map>
#include <string>
#include <string_view>
Expand All @@ -17,39 +18,8 @@
#include "mamba/specs/package_info.hpp"
#include "mamba/util/graph.hpp"

typedef struct s_Solvable Solvable;

namespace mamba
{
using GraphicsParams = Context::GraphicsParams;

void print_dep_graph(
std::ostream& out,
Solvable* s,
const std::string& solv_str,
int level,
int max_level,
bool last,
const std::string& prefix
);

class query_result;

class Query
{
public:

Query(MPool& pool);

query_result find(const std::vector<std::string>& queries) const;
query_result whoneeds(const std::string& query, bool tree) const;
query_result depends(const std::string& query, bool tree) const;

private:

std::reference_wrapper<MPool> m_pool;
};

enum class QueryType
{
Search,
Expand All @@ -58,47 +28,40 @@ namespace mamba
};

constexpr auto enum_name(QueryType t) -> std::string_view;
auto QueryType_from_name(std::string_view name) -> QueryType;

enum class QueryResultFormat
{
Json = 0,
Tree = 1,
Table = 2,
Pretty = 3,
RecursiveTable = 4,
};
auto query_type_parse(std::string_view name) -> QueryType;

class query_result
class QueryResult
{
public:

using GraphicsParams = Context::GraphicsParams;
using dependency_graph = util::DiGraph<specs::PackageInfo>;

query_result(QueryType type, const std::string& query, dependency_graph&& dep_graph);
QueryResult(QueryType type, std::string query, dependency_graph dep_graph);
QueryResult(const QueryResult&) = default;
QueryResult(QueryResult&&) = default;

~query_result() = default;
~QueryResult() = default;

query_result(const query_result&) = default;
query_result& operator=(const query_result&) = default;
query_result(query_result&&) = default;
query_result& operator=(query_result&&) = default;
auto operator=(const QueryResult&) -> QueryResult& = default;
auto operator=(QueryResult&&) -> QueryResult& = default;

QueryType query_type() const;
const std::string& query() const;
[[nodiscard]] auto query_type() const -> QueryType;
[[nodiscard]] auto query() const -> const std::string&;

query_result& sort(std::string_view field);
query_result& groupby(std::string_view field);
query_result& reset();
auto sort(std::string_view field) -> QueryResult&;
auto groupby(std::string_view field) -> QueryResult&;
auto reset() -> QueryResult&;

std::ostream& table(std::ostream&) const;
std::ostream& table(std::ostream&, const std::vector<std::string_view>& fmt) const;
std::ostream& tree(std::ostream&, const GraphicsParams& graphics) const;
nlohmann::json json() const;
auto table(std::ostream&) const -> std::ostream&;
auto table(std::ostream&, const std::vector<std::string_view>& fmt) const -> std::ostream&;
auto tree(std::ostream&, const GraphicsParams& graphics) const -> std::ostream&;
[[nodiscard]] auto json() const -> nlohmann::json;

std::ostream& pretty(std::ostream&, const Context::OutputParams& outputParams) const;
auto pretty(std::ostream&, const Context::OutputParams& outputParams) const -> std::ostream&;

bool empty() const;
[[nodiscard]] auto empty() const -> bool;

private:

Expand All @@ -107,7 +70,6 @@ namespace mamba
using ordered_package_list = std::map<std::string, package_id_list>;

void reset_pkg_view_list();
std::string get_package_repr(const specs::PackageInfo& pkg) const;

QueryType m_type;
std::string m_query;
Expand All @@ -116,6 +78,20 @@ namespace mamba
ordered_package_list m_ordered_pkg_id_list = {};
};

class Query
{
public:

[[nodiscard]] static auto find(MPool& pool, const std::vector<std::string>& queries)
-> QueryResult;

[[nodiscard]] static auto whoneeds(MPool& pool, const std::string& query, bool tree)
-> QueryResult;

[[nodiscard]] static auto depends(MPool& pool, const std::string& query, bool tree)
-> QueryResult;
};

/********************
* Implementation *
********************/
Expand All @@ -133,7 +109,5 @@ namespace mamba
}
throw std::invalid_argument("Invalid enum value");
}

} // namespace mamba

#endif // MAMBA_QUERY_HPP
}
#endif
16 changes: 9 additions & 7 deletions libmamba/src/api/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ namespace mamba
{
auto& ctx = config.context();
auto pool = repoquery_init(ctx, config, format, use_local);
Query q(pool);


if (type == QueryType::Search)
{
auto res = q.find(queries);
auto res = Query::find(pool, queries);
switch (format)
{
case QueryResultFormat::Json:
Expand All @@ -111,9 +109,11 @@ namespace mamba
{
throw std::invalid_argument("Only one query supported for 'depends'.");
}
auto res = q.depends(
auto res = Query::depends(
pool,
queries.front(),
format == QueryResultFormat::Tree || format == QueryResultFormat::RecursiveTable
/* tree= */ format == QueryResultFormat::Tree
|| format == QueryResultFormat::RecursiveTable
);
switch (format)
{
Expand All @@ -136,9 +136,11 @@ namespace mamba
{
throw std::invalid_argument("Only one query supported for 'whoneeds'.");
}
auto res = q.whoneeds(
auto res = Query::whoneeds(
pool,
queries.front(),
format == QueryResultFormat::Tree || format == QueryResultFormat::RecursiveTable
/* tree= */ format == QueryResultFormat::Tree
|| format == QueryResultFormat::RecursiveTable
);
switch (format)
{
Expand Down
Loading

0 comments on commit 15ce499

Please sign in to comment.