DistroqueryAPI: optimize and refactor getProvidersForRequirement adding getProvidersForRequirementFromDb

This commit is contained in:
Silvan Calarco 2024-07-07 20:13:23 +02:00
parent 44b651292f
commit 6797d4a9ae
4 changed files with 53 additions and 21 deletions

View File

@ -100,13 +100,13 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
"%' OR description LIKE '%" + query + "%'";
sql = "SELECT u.*,COUNT(*) OVER() AS total_count FROM ("
"SELECT name,arch,version,release,summary,buildtime,0 AS sort FROM sources " + query_append;
"SELECT name,arch,version,release,summary,buildtime,'src' AS reparch, 0 AS sort FROM sources " + query_append;
int archorder = 1;
for (auto arch: ct->arch) {
if (arch == NULL) break;
sql += " UNION SELECT name,arch,version,release,summary,buildtime," + to_string(archorder) +
" AS sort FROM '" + repository + "_" + arch + "'.packages " + query_append;
sql += " UNION SELECT name,arch,version,release,summary,buildtime,'" + string(arch) + "' AS reparch," +
to_string(archorder) + " AS sort FROM '" + repository + "_" + arch + "'.packages " + query_append;
archorder++;
}
@ -128,7 +128,7 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
}
json package;
package["name"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "name")));
package["arch"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "arch")));
package["arch"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "reparch")));
package["version"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "version")));
package["release"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "release")));
package["summary"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "summary")));
@ -145,6 +145,7 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
j["error"] = "error preparing query '" + sql + "': " + sqlite3_errmsg(db);
return j;
}
sqlite3_close(db);
return j;
}
@ -193,6 +194,7 @@ json DistroqueryAPI::getPackageSourceDetailsById(configTag* ct, long id) {
j["error"] = "no results from query '" + sql + "'";
return j;
}
sqlite3_close(db);
return j;
}
@ -227,27 +229,18 @@ json DistroqueryAPI::getBuiltPackagesFromSourceID(configTag* ct, long id) {
j["error"] = "error preparing query '" + sql + "': " + sqlite3_errmsg(db);
return j;
}
sqlite3_close(db);
}
j["archs"] = archs;
return j;
}
json DistroqueryAPI::getProvidersForRequirement(string repository, string requirement, string flags, string version, string onlyarch) {
json DistroqueryAPI::getProvidersForRequirementFromDb(sqlite3 *db , configTag* ct, string requirement,
string flags, string version, string onlyarch) {
json j;
sqlite3_stmt *stmt;
struct configTag* ct = findRepositoryByTag(repository.c_str());
if (ct == NULL) {
j["error"] = "repository with tag '" + repository + "' does not exist";
return j;
}
auto db = openRepositoryDatabase(ct);
if (!db) {
j["error"] = "error opening sources database for repository " + string(ct->tag);
return j;
}
string sql = "SELECT u.* FROM (";
int intflags = rpmSenseStringToFlags(flags);
@ -256,7 +249,6 @@ json DistroqueryAPI::getProvidersForRequirement(string repository, string requir
for (auto arch: ct->arch) {
if (arch == NULL) break;
if (onlyarch != "" && onlyarch != arch) continue;
attachCtDatabases(ct, db, arch);
int i = 0;
while (ct->repository[i]) {
if (a + i > 0) sql += " UNION ";
@ -288,7 +280,7 @@ json DistroqueryAPI::getProvidersForRequirement(string repository, string requir
string provideVersion = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "provideversion")));
provider["version"] = provideVersion;
if (flags != "") {
if (flags != "" && provideVersion != "") {
// Check that provide version matches with required flags and version
if (checkVersionWithFlags(version.c_str(), intflags, provideVersion.c_str()) != 1)
continue;
@ -308,6 +300,33 @@ json DistroqueryAPI::getProvidersForRequirement(string repository, string requir
j["providers"]["archs"] = archs;
else
j["providers"] = archs[onlyarch];
return j;
}
json DistroqueryAPI::getProvidersForRequirement(string repository, string requirement, string flags, string version, string onlyarch) {
json j;
struct configTag* ct = findRepositoryByTag(repository.c_str());
if (ct == NULL) {
j["error"] = "repository with tag '" + repository + "' does not exist";
return j;
}
auto db = openRepositoryDatabase(ct);
if (!db) {
j["error"] = "error opening sources database for repository " + string(ct->tag);
return j;
}
if (onlyarch != "") {
attachCtDatabases(ct, db, onlyarch);
} else {
attachCtDatabasesAllArchs(ct, db);
}
j = getProvidersForRequirementFromDb(db, ct, requirement, flags, version, onlyarch);
sqlite3_close(db);
return j;
}
@ -384,6 +403,7 @@ json DistroqueryAPI::getPackageSourceDetails(string repository, string package)
j["error"] = "error preparing query '" + sql + "'";
return j;
}
sqlite3_close(db);
return j;
}
@ -498,8 +518,8 @@ json DistroqueryAPI::getPackageDetails(string repository, string package, string
return j;
}
sqlite3_close(db);
return j;
}
void DistroqueryAPI::getApiResponse(string path_info) {
@ -578,7 +598,7 @@ void DistroqueryAPI::getApiResponse(string path_info) {
}
cout << details.dump();
} else if (path_split[0] == "providers") {
// API service: package
// API service: providers
if (path_split.size() != 3 && path_split.size() != 5)
sendErrorResponse("expected 3 or 5 arguments for " + path_split[0]);
json providers;

View File

@ -43,6 +43,8 @@ class DistroqueryAPI {
void sendErrorResponse(string message);
json getRepositoryPackages(string repository, int per_page, int page, string query);
json getPackageSourceDetailsById(configTag* ct, long id);
json getProvidersForRequirementFromDb(sqlite3 *db , configTag* ct, string requirement,
string flags, string version, string onlyarch);
json getProvidersForRequirement(string repository, string requirement, string flags,
string version, string onlyarch="");
json getPackageSourceDetails(string repository, string package);

View File

@ -100,6 +100,13 @@ void attachCtDatabases(struct configTag* ct, sqlite3 *db, string arch) {
}
}
void attachCtDatabasesAllArchs(struct configTag* ct, sqlite3 *db) {
for (auto arch: ct->arch) {
if (arch == NULL) break;
attachCtDatabases(ct, db, arch);
}
}
void attachRepositoryDatabases(struct configTag* ct, sqlite3 *db, string arch, int source) {
string dbname;
string sql;

View File

@ -31,7 +31,10 @@ string rpmSenseFlagsToString(int flags);
int rpmSenseStringToFlags(string flags);
vector<string> split(string str, string token);
sqlite3* openRepositoryDatabase(struct configTag* ct, string arch = "", string append = "");
// Attach all upstream dbs for given ct repository and given arch (default = source)
void attachCtDatabases(struct configTag* ct, sqlite3 *db, string arch = "");
// Attach all upstream dbs for given ct repository and all archs
void attachCtDatabasesAllArchs(struct configTag* ct, sqlite3 *db);
void attachRepositoryDatabases(struct configTag* ct, sqlite3 *db, string arch, int source);
int sqlite3_find_column_id(sqlite3_stmt *stmt, const char* table, const char* name);
string urlDecode(string str);