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

View File

@ -43,6 +43,8 @@ class DistroqueryAPI {
void sendErrorResponse(string message); void sendErrorResponse(string message);
json getRepositoryPackages(string repository, int per_page, int page, string query); json getRepositoryPackages(string repository, int per_page, int page, string query);
json getPackageSourceDetailsById(configTag* ct, long id); 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, json getProvidersForRequirement(string repository, string requirement, string flags,
string version, string onlyarch=""); string version, string onlyarch="");
json getPackageSourceDetails(string repository, string package); 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) { void attachRepositoryDatabases(struct configTag* ct, sqlite3 *db, string arch, int source) {
string dbname; string dbname;
string sql; string sql;

View File

@ -31,7 +31,10 @@ string rpmSenseFlagsToString(int flags);
int rpmSenseStringToFlags(string flags); int rpmSenseStringToFlags(string flags);
vector<string> split(string str, string token); vector<string> split(string str, string token);
sqlite3* openRepositoryDatabase(struct configTag* ct, string arch = "", string append = ""); 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 = ""); 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); 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); int sqlite3_find_column_id(sqlite3_stmt *stmt, const char* table, const char* name);
string urlDecode(string str); string urlDecode(string str);