Skip to content

Commit

Permalink
pathfinder: add database reset options, add version to database
Browse files Browse the repository at this point in the history
  • Loading branch information
XPhyro committed Aug 18, 2024
1 parent 4e11a89 commit f668d1c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/cpp/project/pathfinder/src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
#include <xph/exec_info.hpp>

#include "cmd.hpp"
#include "db.hpp"

paf::cli::cli(int argc, char** argv)
{
bool help = false;
bool show_help = false;
bool reset_dir_db = false;
bool reset_file_db = false;
auto cli = lyra::cli() |
lyra::help(help).description("Pathfinding interface for interactive shells.");
lyra::help(show_help).description("Pathfinding interface for interactive shells.") |
lyra::opt(reset_dir_db)["--reset-directory-database"]("reset directory database") |
lyra::opt(reset_file_db)["--reset-file-database"]("reset file database");

paf::alias _{ cli };
paf::mark _{ cli };
Expand All @@ -32,10 +37,16 @@ paf::cli::cli(int argc, char** argv)
std::exit(EXIT_FAILURE);
}

if (help) {
if (show_help) {
std::cout << cli << '\n';
std::exit(EXIT_SUCCESS);
}

if (reset_dir_db)
paf::db::reset_db(paf::db_type::directory);

if (reset_file_db)
paf::db::reset_db(paf::db_type::file);
}

[[noreturn]] void paf::cli::run(void)
Expand Down
39 changes: 35 additions & 4 deletions src/cpp/project/pathfinder/src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#include <hedley.h>

#include <xph/db.h>
#include <xph/die.hpp>

namespace fs = std::filesystem;

using db_version_t = std::uint64_t;

namespace paf {
static std::optional<std::vector<db_item>> db_dir_vec = std::nullopt,
db_file_vec = std::nullopt;
Expand All @@ -36,22 +39,44 @@ namespace paf {
}
}

static void load_db_v0(std::vector<db_item>& db_vec, std::ifstream& ifs)
{
for (std::string k, v; std::getline(ifs, k, '\0') && std::getline(ifs, v, '\0');)
db_vec.emplace_back(k, v);
}

static void load_db(std::vector<db_item>& db_vec, db_type type)
{
const auto path = get_db_path(type);
std::ifstream ifs{ path };

for (std::string k, v; std::getline(ifs, k, '\0') && std::getline(ifs, v, '\0');)
db_vec.emplace_back(k, v);
if (ifs.peek() == std::ifstream::traits_type::eof())
return;

db_version_t version;
ifs.read(reinterpret_cast<char*>(&version), sizeof(version));

if (version == 0)
load_db_v0(db_vec, ifs);
else
xph::die("cannot load database with version [", version, "]");
}

static void save_db(const std::vector<db_item>& db_vec, db_type type)
{
static const constexpr db_version_t db_version = 0uz;

const auto path = get_db_path(type);
const auto tmp_path = path + ".tmp";

for (std::ofstream ofs{ tmp_path }; const auto& item : db_vec)
ofs << item.keycode << '\0' << item.path << '\0';
{
std::ofstream ofs{ tmp_path };

ofs.write(reinterpret_cast<const char*>(&db_version), sizeof(db_version));

for (const auto& item : db_vec)
ofs << item.keycode << '\0' << item.path << '\0';
}

fs::rename(tmp_path, path);
}
Expand Down Expand Up @@ -80,6 +105,12 @@ namespace paf {
}
}

void db::reset_db(db_type type)
{
auto db_path = get_db_path(type);
fs::remove(db_path);
}

db::db(std::vector<db_item>& db_vec, db_type type) : type(type), m_db_vec(db_vec) {}

db::~db()
Expand Down
1 change: 1 addition & 0 deletions src/cpp/project/pathfinder/src/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace paf {
~db();

static db get_db(db_type type);
static void reset_db(db_type type);

void dump(std::string_view sep, std::string_view end);
std::optional<std::string> try_get_mark(const std::string& keycode);
Expand Down

0 comments on commit f668d1c

Please sign in to comment.