Skip to content

Commit

Permalink
Refactor find_in_archives() for further complete only optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Mar 20, 2024
1 parent 346d927 commit 28eef32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/find.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ static find_result_t check_match_old(filetype_t filetype, size_t detector_id, co
static find_result_t check_match_romset(filetype_t filetype, size_t detector_id, const std::string &game_name, const FileData *wanted_file, const FileData *candidate, Match *match);
static find_result_t find_in_db(RomDB *rdb, filetype_t filetype, size_t detector_id, const FileData *wanted_file, Archive *archive, const std::string &skip_game, const std::string &skip_file, Match *match, find_result_t (*)(filetype_t filetype, size_t detector_id, const std::string &game_name, const FileData *wanted_file, const FileData *candidate, Match *match));

static find_result_t find_in_archives_xxx(filetype_t filetype, size_t detector_id, const FileData *r, Match *m, bool needed_only);

static bool compute_all_detector_hashes(DeleteListPtr list);

bool compute_all_detector_hashes(bool needed_only) { // returns true if new hashes were computed
Expand Down Expand Up @@ -86,44 +84,47 @@ static bool compute_all_detector_hashes(DeleteListPtr list) {


find_result_t find_in_archives(filetype_t filetype, size_t detector_id, const FileData *rom, Match *m, bool needed_only) {
auto result = find_in_archives_xxx(filetype, detector_id, rom, m, needed_only);
if (result == FIND_UNKNOWN) {
auto candidates = find_candidates_in_archives(filetype, detector_id, rom, needed_only);

return check_archive_candidates(candidates, filetype, rom, m, needed_only);
}

std::vector<CkmameDB::FindResult> find_candidates_in_archives(filetype_t filetype, size_t detector_id, const FileData *rom, bool needed_only) {
auto candidates = ckmame_cache->find_file(filetype, detector_id, *rom);
if (candidates.empty()) {
if (compute_all_detector_hashes(needed_only)) {
result = find_in_archives_xxx(filetype, detector_id, rom, m, needed_only);
candidates = ckmame_cache->find_file(filetype, detector_id, *rom);
}
}

return result;
return candidates;
}


static find_result_t find_in_archives_xxx(filetype_t filetype, size_t detector_id, const FileData *rom, Match *m, bool needed_only) {
auto results = ckmame_cache->find_file(filetype, detector_id, *rom);

for (const auto& result : results) {
if (needed_only && result.where != FILE_NEEDED) {
find_result_t check_archive_candidates(const std::vector<CkmameDB::FindResult>& candidates, filetype_t filetype, const FileData *rom, Match *match, bool needed_only) {
for (const auto& candidate : candidates) {
if (needed_only && candidate.where != FILE_NEEDED) {
continue;
}
auto a= Archive::open(result.name, filetype, result.where, 0);
if (!a || result.index >= a->files.size()) {
auto a= Archive::open(candidate.name, filetype, candidate.where, 0);
if (!a || candidate.index >= a->files.size()) {
return FIND_ERROR;
}

auto &file = a->files[result.index];
auto &file = a->files[candidate.index];

if (result.detector_id == 0 && !file.hashes.has_all_types(rom->hashes)) {
a->file_ensure_hashes(result.index, rom->hashes.get_types() | db->hashtypes(filetype));
if (candidate.detector_id == 0 && !file.hashes.has_all_types(rom->hashes)) {
a->file_ensure_hashes(candidate.index, rom->hashes.get_types() | db->hashtypes(filetype));
}

if (file.broken || file.get_hashes(result.detector_id).compare(rom->hashes) != Hashes::MATCH) {
if (file.broken || file.get_hashes(candidate.detector_id).compare(rom->hashes) != Hashes::MATCH) {
continue;
}

if (m) {
m->archive = a;
m->index = result.index;
m->where = result.where;
m->quality = Match::COPIED;
if (match) {
match->archive = a;
match->index = candidate.index;
match->where = candidate.where;
match->quality = Match::COPIED;
}

return FIND_EXISTS;
Expand Down
2 changes: 2 additions & 0 deletions src/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum find_result { FIND_ERROR = -1, FIND_UNKNOWN, FIND_MISSING, FIND_EXISTS };

typedef enum find_result find_result_t;

std::vector<CkmameDB::FindResult> find_candidates_in_archives(filetype_t filetype, size_t detector_id, const FileData *rom, bool needed_only);
find_result_t check_archive_candidates(const std::vector<CkmameDB::FindResult>& candidates, filetype_t filetype, const FileData *rom, Match *match, bool needed_only);

find_result_t find_in_archives(filetype_t filetype, size_t detector_id, const FileData *r, Match *m, bool needed_only);
find_result_t find_in_old(filetype_t filetype, const FileData *file, Archive *archive, Match *match);
Expand Down

0 comments on commit 28eef32

Please sign in to comment.