-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish refactoring of database lookup into slurm-free backend; fix so…
…me corner cases in image selection
- Loading branch information
Showing
9 changed files
with
249 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# overview | ||
|
||
## current | ||
|
||
`mount` | ||
- `is_valid_mount_point` | ||
- `do_mount` | ||
|
||
`util/helper` | ||
- split | ||
- is_sha | ||
- is_file | ||
|
||
`sqlite`: | ||
The sqlite/sqlite.hpp header exposes an api around sqlite | ||
|
||
`parse_args`: | ||
- the `find_repo_image` function that returs the path of the image should be moved to the database | ||
|
||
## proposed | ||
|
||
|
||
`sqlite`: | ||
- refactor so that it exposes an API around the database - we shouldn't leak underlying database implementation to the calling scope | ||
- rename `db` | ||
|
||
## sqlite | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include <algorithm> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "database.hpp" | ||
#include "sqlite.hpp" | ||
|
||
#include "../util/filesystem.hpp" | ||
|
||
namespace db { | ||
|
||
struct uenv_result { | ||
std::string name; | ||
std::string version; | ||
std::string tag; | ||
std::string sha; | ||
|
||
uenv_result() = delete; | ||
|
||
uenv_result(std::string name, std::string version, std::string tag, | ||
std::string sha); | ||
uenv_result(SQLiteStatement &result) | ||
: uenv_result(result.getColumn(result.getColumnIndex("name")), | ||
result.getColumn(result.getColumnIndex("sha256")), | ||
result.getColumn(result.getColumnIndex("tag")), | ||
result.getColumn(result.getColumnIndex("version"))) {} | ||
friend bool operator<(const uenv_result &, const uenv_result &); | ||
}; | ||
|
||
util::expected<std::string, std::string> | ||
find_image(const uenv_desc &desc, const std::string &repo_path, | ||
std::optional<std::string> uenv_arch) { | ||
try { | ||
std::string dbpath = repo_path + "/index.db"; | ||
// check if dbpath exists. | ||
if (!util::is_file(dbpath)) { | ||
return util::unexpected("Can't open uenv repo. " + dbpath + | ||
" is not a file."); | ||
} | ||
SQLiteDB db(dbpath, sqlite_open::readonly); | ||
|
||
// get all results | ||
std::vector<uenv_result> results; | ||
if (desc.sha) { | ||
if (desc.sha.value().size() < 64) { | ||
SQLiteStatement query(db, "SELECT * FROM records WHERE id = :id"); | ||
query.bind(":id", desc.sha.value()); | ||
while (query.execute()) { | ||
results.emplace_back(query); | ||
} | ||
} else { | ||
SQLiteStatement query(db, "SELECT * FROM records WHERE sha256 = :sha"); | ||
query.bind(":sha", desc.sha.value()); | ||
while (query.execute()) { | ||
results.emplace_back(query); | ||
} | ||
} | ||
} else { | ||
std::string query_str = "SELECT * FROM records WHERE "; | ||
std::vector<std::string> filter; | ||
if (uenv_arch) { | ||
filter.push_back("uarch"); | ||
} | ||
if (desc.name) { | ||
filter.push_back("name"); | ||
} | ||
if (desc.version) { | ||
filter.push_back("version"); | ||
} | ||
if (desc.tag) { | ||
filter.push_back("tag"); | ||
} | ||
for (size_t i = 0; i < filter.size(); ++i) { | ||
if (i > 0) { | ||
query_str += " AND "; | ||
} | ||
query_str += filter[i] + " = " + ":" + filter[i]; | ||
} | ||
SQLiteStatement query(db, query_str); | ||
if (uenv_arch) { | ||
query.bind(":uarch", uenv_arch.value()); | ||
} | ||
if (desc.name) { | ||
query.bind(":name", desc.name.value()); | ||
} | ||
if (desc.version) { | ||
query.bind(":version", desc.version.value()); | ||
} | ||
if (desc.tag) { | ||
query.bind(":tag", desc.tag.value()); | ||
} | ||
while (query.execute()) { | ||
results.emplace_back(query); | ||
} | ||
} | ||
|
||
// sort the results by sha, and build a list of unique sha | ||
std::sort(results.begin(), results.end(), | ||
[](auto &lhs, auto &rhs) { return lhs.sha < rhs.sha; }); | ||
std::set<std::string> shas; | ||
for (const auto &r : results) { | ||
shas.insert(r.sha); | ||
} | ||
if (shas.size() > 1) { | ||
std::stringstream ss; | ||
ss << "More than one uenv matches.\n"; | ||
for (auto &d : results) { | ||
ss << d.name << "/" << d.version << ":" << d.tag << "\t" << d.sha | ||
<< "\n"; | ||
} | ||
return util::unexpected(ss.str()); | ||
} | ||
if (results.empty()) { | ||
return util::unexpected("No uenv matches the request. Run `uenv image " | ||
"ls` to list available images."); | ||
} | ||
return repo_path + "/images/" + results[0].sha + "/store.squashfs"; | ||
} catch (const SQLiteError &e) { | ||
return util::unexpected(std::string("internal database error: ") + | ||
e.what()); | ||
} | ||
} | ||
|
||
} // namespace db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <optional> | ||
#include <set> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "../util/expected.hpp" | ||
|
||
namespace db { | ||
|
||
struct uenv_desc { | ||
using entry_t = std::optional<std::string>; | ||
entry_t name; | ||
entry_t version; | ||
entry_t tag; | ||
entry_t sha; | ||
}; | ||
|
||
util::expected<std::string, std::string> | ||
find_image(const uenv_desc &desc, const std::string &repo_path, | ||
std::optional<std::string> uenv_arch); | ||
|
||
} // namespace db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.