Skip to content

Commit

Permalink
Record runs in status db.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Oct 9, 2024
1 parent f5dba2e commit 94e9f03
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ set(COMMON_SOURCES
SharedFile.cc
Stats.cc
StatusDB.cc
StatusDBRun.cc
superfluous.cc
TomlSchema.cc
Tree.cc
Expand Down
52 changes: 23 additions & 29 deletions src/StatusDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ std::unordered_map<int, std::string> StatusDB::queries = {
"delete from run where run_id not in (select run_id from run order by date desc limit :count)"},
{DELETE_RUN_DATE, "delete from run where date < :date"},
{FIND_DAT, "select dat_id from dat where name = :name and version = :version"},
{INSERT_DAT, "inesrt into dat (name, version) values (:name, :version)"},
{INSERT_DAT, "insert into dat (name, version) values (:name, :version)"},
{INSERT_GAME,
"insert into game (run_id, dat_id, name, checksum, status) values (:run_id, :dat_id, :name, :checksum, :status"},
"insert into game (run_id, dat_id, name, checksum, status) values (:run_id, :dat_id, :name, :checksum, :status)"},
{INSERT_RUN, "insert into run (date) values (:date)"},
{LIST_RUNS, "select run_id, date from run order by date descending"},
{QUERY_GAME, "select dat_id, name, checksum, status from game where run_id = :run_id"},
Expand Down Expand Up @@ -201,6 +201,19 @@ std::unordered_map<GameStatus, std::vector<std::string>> StatusDB::get_run_statu
return names;
}

int64_t StatusDB::find_dat(const DatEntry& dat) {
auto stmt = get_statement(FIND_DAT);
stmt->set_string("name", dat.name);
stmt->set_string("version", dat.version);

while (stmt->step()) {
return stmt->get_int("dat_id");
}

return -1;
}


std::unordered_map<GameStatus, uint64_t> StatusDB::get_run_status_counts(int64_t run_id) {
auto stmt = get_statement(QUERY_RUN_STATUS_COUNTS);

Expand All @@ -222,58 +235,39 @@ std::unordered_map<GameStatus, uint64_t> StatusDB::get_run_status_counts(int64_t
}

int64_t StatusDB::insert_dat(const DatEntry& dat) {
auto stmt = get_statement(FIND_DAT);
auto stmt = get_statement(INSERT_DAT);

stmt->set_string("name", dat.name);
stmt->set_string("version", dat.version);

if (stmt->step()) {
return stmt->get_int64("dat_id");
}

stmt = get_statement(INSERT_DAT);

stmt->set_string("name", dat.name);
stmt->set_string("version", dat.version);

if (stmt->step()) {
return stmt->get_rowid();
}
else {
throw Exception("can't insert dat"); // TODO: details
}
stmt->execute();
return stmt->get_rowid();
}


void StatusDB::insert_game(int64_t run_id, const Game& game, GameStatus status) {
void StatusDB::insert_game(int64_t run_id, const Game& game, int64_t dat_id, GameStatus status) {
auto stmt = get_statement(INSERT_GAME);

std::vector<uint8_t> checksum;

compute_combined_checksum(game, checksum);

stmt->set_int64("run_id", run_id);
stmt->set_uint64("dat_id", game.dat_no);
stmt->set_uint64("dat_id", dat_id);
stmt->set_string("name", game.name);
stmt->set_blob("checksum", checksum);
stmt->set_int("status", static_cast<int>(status));

if (!stmt->step()) {
throw Exception("can't insert game");
}
stmt->execute();
}

int64_t StatusDB::insert_run(time_t date) {
auto stmt = get_statement(INSERT_RUN);

stmt->set_int64("date", static_cast<int64_t>(date));

if (stmt->step()) {
return stmt->get_rowid();
}
else {
throw Exception("can't insert run");
}
stmt->execute();
return stmt->get_rowid();
}

void StatusDB::compute_combined_checksum(const Game& game, std::vector<uint8_t>& checksum) {
Expand Down
13 changes: 11 additions & 2 deletions src/StatusDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,23 @@ class StatusDB: public DB {
[[nodiscard]] std::vector<std::string> get_games_by_status(int64_t run_id, GameStatus status);
[[nodiscard]] std::unordered_map<GameStatus, std::vector<std::string>> get_run_status_names(int64_t run_id);
[[nodiscard]] std::unordered_map<GameStatus, uint64_t> get_run_status_counts(int64_t run_id);
[[nodiscard]] int64_t insert_dat(const DatEntry& dat);
void insert_game(int64_t run_id, const Game& game, GameStatus status);
[[nodiscard]] int64_t find_dat(const DatEntry& dat);
void insert_game(int64_t run_id, const Game& game, int64_t dat_id, GameStatus status);
[[nodiscard]] int64_t insert_run(time_t date);
[[nodiscard]] int64_t insert_dat(const DatEntry& dat);


protected:
[[nodiscard]] std::string get_query(int name, bool parameterized) const override;

private:
class DatInfo {
public:
explicit DatInfo(const DatEntry& dat): name(dat.name), version(dat.version) {}
std::string name;
std::string version;
};

static std::unordered_map<int, std::string> queries;

DBStatement *get_statement(Statement name) { return get_statement_internal(name); }
Expand Down
59 changes: 59 additions & 0 deletions src/StatusDBRun.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
StatDBRun.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "StatusDBRun.h"

StatusDBRun::StatusDBRun(std::shared_ptr<StatusDB> db_, RomDB *romdb): db(std::move(db_)), romdb(romdb) {
run_id = db->insert_run(time(nullptr));
dats = romdb->read_dat();
}

void StatusDBRun::insert_game_status(const Game& game, const GameStatus& game_status) {
if (!db) {
return;
}
db->insert_game(run_id, game, get_dat_id(game.dat_no), game_status);
}

int64_t StatusDBRun::get_dat_id(size_t dat_no) {
auto it = dat_ids.find(dat_no);
if (it != dat_ids.end()) {
return it->second;
}

auto id = db->find_dat(dats[dat_no]);
if (id < 0) {
id = db->insert_dat(dats[dat_no]);
}

dat_ids[dat_no] = id;
return id;
}
55 changes: 55 additions & 0 deletions src/StatusDBRun.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef STATDBRUN_H
#define STATDBRUN_H

/*
StatDBRun.h --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "RomDB.h"
#include "StatusDB.h"

class StatusDBRun {
public:
StatusDBRun() = default;
StatusDBRun(std::shared_ptr<StatusDB> db, RomDB *romdb);

void insert_game_status(const Game& game, const GameStatus &game_status);

private:
int64_t get_dat_id(size_t dat_no);

std::shared_ptr<StatusDB> db;
RomDB *romdb{};
int64_t run_id{-1};
std::vector<DatEntry> dats;
std::unordered_map<size_t, int64_t> dat_ids;
};

#endif //STATDBRUN_H
7 changes: 5 additions & 2 deletions src/Tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@

#include "check.h"
#include "check_util.h"
#include "CkmameCache.h"
#include "diagnostics.h"
#include "fix.h"
#include "Fixdat.h"
#include "globals.h"
#include "RomDB.h"
#include "Progress.h"
#include "RomDB.h"
#include "StatusDB.h"
#include "warn.h"
#include "CkmameCache.h"


Tree check_tree;
Expand Down Expand Up @@ -196,6 +197,8 @@ void Tree::process(GameArchives *archives) {
ckmame_cache->complete_games.insert(game->name);
}

status_run.insert_game_status(*game.get(), res.game);

/* TODO: includes too much when rechecking */
if (configuration.create_fixdat) {
Fixdat::write_entry(game.get(), &res);
Expand Down
2 changes: 2 additions & 0 deletions src/ckmame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ bool CkMame::execute(const std::vector<std::string> &arguments) {

Progress::enable();
if (checking_all_games && configuration.fix_romset && configuration.status_db != "none") {
ensure_dir(std::filesystem::path(configuration.status_db), true);
status_db = std::make_shared<StatusDB>(configuration.status_db, DBH_WRITE|DBH_CREATE);
status_run = StatusDBRun(status_db, db.get());
}

check_tree.traverse();
Expand Down
2 changes: 2 additions & 0 deletions src/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
Configuration configuration;

Output output;

StatusDBRun status_run;
3 changes: 3 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@

#include "Configuration.h"
#include "Output.h"
#include "StatusDBRun.h"

extern Configuration configuration;

extern Output output;

extern StatusDBRun status_run;

#endif // HAD_GLOBALS_H

0 comments on commit 94e9f03

Please sign in to comment.