Skip to content

Commit

Permalink
Get rid of archive by id cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Mar 20, 2024
1 parent 219f452 commit 7fa082b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 46 deletions.
34 changes: 3 additions & 31 deletions src/Archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@

bool Archive::read_only_mode = false;

uint64_t ArchiveContents::next_id = 0;
std::unordered_map<ArchiveContents::TypeAndName, std::weak_ptr<ArchiveContents>> ArchiveContents::archive_by_name;
std::unordered_map<uint64_t, ArchiveContentsPtr> ArchiveContents::archive_by_id;
std::unordered_map<ArchiveContents::TypeAndName, ArchiveContentsPtr> ArchiveContents::archive_by_name;

ArchiveContents::ArchiveContents(ArchiveType type_, std::string name_, filetype_t filetype_, where_t where_, int flags_, std::string filename_extension_) :
id(0),
Expand Down Expand Up @@ -132,16 +130,6 @@ ArchivePtr Archive::open(const ArchiveContentsPtr& contents, int flags) {
return archive;
}

ArchivePtr Archive::by_id(uint64_t id) {
auto contents = ArchiveContents::by_id(id);

if (!contents) {
return nullptr;
}

return open(contents);
}


int Archive::close() {
int ret;
Expand Down Expand Up @@ -574,39 +562,23 @@ bool ArchiveContents::read_infos_from_cachedb(std::vector<File> *cached_files) {
}

void ArchiveContents::enter_in_maps(const ArchiveContentsPtr &contents) {
if (!(contents->flags & ARCHIVE_FL_NOCACHE)) {
contents->id = ++next_id;
archive_by_id[contents->id] = contents;
}

archive_by_name[TypeAndName(contents->filetype, contents->name)] = contents;
}

ArchiveContentsPtr ArchiveContents::by_id(uint64_t id) {
auto it = archive_by_id.find(id);

if (it == archive_by_id.end()) {
return nullptr;
}

return it->second;
}

ArchiveContentsPtr ArchiveContents::by_name(filetype_t filetype, const std::string &name) {
auto it = archive_by_name.find(TypeAndName(filetype, name));

if (it == archive_by_name.end() || it->second.expired()) {
if (it == archive_by_name.end()) {
return nullptr;
}

return it->second.lock();
return it->second;
}


void ArchiveContents::clear_cache() {
archive_by_name.clear();
archive_by_id.clear();
next_id = 0;
}


Expand Down
12 changes: 2 additions & 10 deletions src/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ typedef std::shared_ptr<Archive> ArchivePtr;
typedef std::shared_ptr<ArchiveContents> ArchiveContentsPtr;

#define ARCHIVE_FL_CREATE 0x00100
#define ARCHIVE_FL_NOCACHE 0x00800
#define ARCHIVE_FL_RDONLY 0x01000
#define ARCHIVE_FL_TOP_LEVEL_ONLY 0x02000

Expand Down Expand Up @@ -98,7 +97,6 @@ class ArchiveContents {
[[nodiscard]] int is_cache_up_to_date() const;

static void enter_in_maps(const ArchiveContentsPtr& contents);
static ArchiveContentsPtr by_id(uint64_t id);
static ArchiveContentsPtr by_name(filetype_t filetype, const std::string &name);
static void clear_cache();

Expand All @@ -113,10 +111,7 @@ class ArchiveContents {
};

private:
static uint64_t next_id;
static std::unordered_map<TypeAndName, std::weak_ptr<ArchiveContents>> archive_by_name;
static std::unordered_map<uint64_t, ArchiveContentsPtr> archive_by_id;

static std::unordered_map<TypeAndName, ArchiveContentsPtr> archive_by_name;
};

namespace std {
Expand Down Expand Up @@ -146,9 +141,7 @@ class Archive {
ZipSourcePtr source;
std::string file;
};

static ArchivePtr by_id(uint64_t id);


static ArchivePtr open(const std::string &name, filetype_t filetype, where_t where, int flags);
static ArchivePtr open_toplevel(const std::string &name, filetype_t filetype, where_t where, int flags);

Expand Down Expand Up @@ -184,7 +177,6 @@ class Archive {
[[nodiscard]] bool is_empty() const;
[[nodiscard]] bool is_file_deleted(uint64_t index) const { return changes[index].status == Change::DELETED; }
[[nodiscard]] bool is_writable() const { return (contents->flags & ARCHIVE_FL_RDONLY) == 0; }
[[nodiscard]] bool is_indexed() const { return (contents->flags & ARCHIVE_FL_NOCACHE) == 0 && IS_EXTERNAL(where); }
virtual bool check() { return true; } // This is done as part of the constructor, remove?
virtual bool close_xxx() { return true; }
virtual bool commit_xxx() = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/ParserDir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool ParserDir::parse() {
auto dir_empty = true;

{
auto images = Archive::open(filepath, TYPE_DISK, FILE_NOWHERE, ARCHIVE_FL_NOCACHE);
auto images = Archive::open(filepath, TYPE_DISK, FILE_NOWHERE, 0);

if (images && !images->is_empty()) {
dir_empty = false;
Expand All @@ -74,7 +74,7 @@ bool ParserDir::parse() {
{
// TODO: Remove this ugly hack.
configuration.roms_zipped = false;
auto files = Archive::open(filepath, TYPE_ROM, FILE_NOWHERE, ARCHIVE_FL_NOCACHE);
auto files = Archive::open(filepath, TYPE_ROM, FILE_NOWHERE, 0);
configuration.roms_zipped = true;

if (files && !files->is_empty()) {
Expand All @@ -86,7 +86,7 @@ bool ParserDir::parse() {
continue;
}
else if (is_ziplike(file.name)) {
auto a = Archive::open(filepath / file.name, TYPE_ROM, FILE_NOWHERE, ARCHIVE_FL_NOCACHE);
auto a = Archive::open(filepath / file.name, TYPE_ROM, FILE_NOWHERE, 0);
if (a) {
auto name = a->name;
if (!runtest) {
Expand All @@ -113,7 +113,7 @@ bool ParserDir::parse() {
switch (name_type(filepath)) {
case NAME_ZIP: {
/* TODO: handle errors */
auto a = Archive::open(filepath, TYPE_ROM, FILE_NOWHERE, ARCHIVE_FL_NOCACHE);
auto a = Archive::open(filepath, TYPE_ROM, FILE_NOWHERE, 0);
if (a) {
auto name = a->name;
if (!runtest) {
Expand Down Expand Up @@ -169,7 +169,7 @@ bool ParserDir::parse() {
while ((filepath = dir.next()) != "") {
if (std::filesystem::is_directory(filepath)) {
/* TODO: handle errors */
auto a = Archive::open(filepath, TYPE_ROM, FILE_NOWHERE, ARCHIVE_FL_NOCACHE);
auto a = Archive::open(filepath, TYPE_ROM, FILE_NOWHERE, 0);
if (a) {
start_game(a->name, directory_name);
parse_archive(TYPE_ROM, a.get());
Expand Down

0 comments on commit 7fa082b

Please sign in to comment.