From 8a4e9fc2bcbf61f3a9a3996cbb305bbbf35d52a8 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 17 Oct 2015 10:07:58 -0400 Subject: [PATCH] core/database: Convert class to a struct In addition, we lowercase variable names and add a db_ prefix to all struct members. Signed-off-by: Anna Schumaker --- core/index.cpp | 4 +- core/tags/album.cpp | 2 +- core/tags/artist.cpp | 2 +- core/tags/genre.cpp | 2 +- core/tags/library.cpp | 2 +- core/tags/track.cpp | 4 +- include/core/database.h | 30 ++++------ include/core/database.hpp | 114 ++++++++++++++++++------------------ include/core/index.h | 2 +- tests/core/database.cpp | 16 ++--- tests/core/tags/album.cpp | 2 +- tests/core/tags/artist.cpp | 2 +- tests/core/tags/genre.cpp | 2 +- tests/core/tags/library.cpp | 2 +- tests/core/tags/track.cpp | 2 +- 15 files changed, 90 insertions(+), 98 deletions(-) diff --git a/core/index.cpp b/core/index.cpp index a30208d4..cd88f65a 100644 --- a/core/index.cpp +++ b/core/index.cpp @@ -70,14 +70,14 @@ void IndexEntry :: read(file &file) Index :: Index(const std::string &filepath, bool autosave) - : Database(filepath, autosave) + : database(filepath, autosave) {} void Index :: insert(const std::string &key, unsigned int value) { IndexEntry *it = find(key); if (it == NULL) - it = Database :: insert(IndexEntry(key)); + it = database :: insert(IndexEntry(key)); it->insert(value); autosave(); diff --git a/core/tags/album.cpp b/core/tags/album.cpp index 5479f39d..87716008 100644 --- a/core/tags/album.cpp +++ b/core/tags/album.cpp @@ -6,7 +6,7 @@ #include -static Database album_db("album.db", true); +static database album_db("album.db", true); static const std::string make_key(const std::string &name, unsigned int year) { diff --git a/core/tags/artist.cpp b/core/tags/artist.cpp index d91ae9d8..a458468c 100644 --- a/core/tags/artist.cpp +++ b/core/tags/artist.cpp @@ -4,7 +4,7 @@ #include -static Database artist_db("artist.db", true); +static database artist_db("artist.db", true); Artist :: Artist() : GenericTag() {} diff --git a/core/tags/genre.cpp b/core/tags/genre.cpp index a57dff8f..e25877a2 100644 --- a/core/tags/genre.cpp +++ b/core/tags/genre.cpp @@ -4,7 +4,7 @@ #include -static Database genre_db("genre.db", true); +static database genre_db("genre.db", true); Genre :: Genre() : GenericTag() {} diff --git a/core/tags/library.cpp b/core/tags/library.cpp index 1b8d31c0..77752dc2 100644 --- a/core/tags/library.cpp +++ b/core/tags/library.cpp @@ -4,7 +4,7 @@ #include -static Database library_db("library.db", true); +static database library_db("library.db", true); Library :: Library() diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 7ab43aa3..2bf80586 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -8,7 +8,7 @@ #include -static Database track_db("track.db", false); +static database track_db("track.db", false); Track :: Track() @@ -165,7 +165,7 @@ void tags :: remove_track(Track *track) void tags :: remove_library_tracks(Library *library) { - Database::iterator it; + database::iterator it; for (it = track_db.begin(); it != track_db.end(); it = track_db.next(it)) { if ((*it)->library() == library) diff --git a/include/core/database.h b/include/core/database.h index faa57c3f..3cef82c1 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -12,7 +12,7 @@ extern "C" { #include #include -template class Database; +template struct database; /** * The DatabaseEntry class is the base class for storing @@ -20,7 +20,7 @@ template class Database; */ class DatabaseEntry { private: - template friend class Database; + template friend struct database; unsigned int _index; /**< The location of an item in the Database. */ public: @@ -81,24 +81,14 @@ public: * The Database class will add a newline after each DatabaseEntry. */ template -class Database { -private: - /** Databases are backed by a std::vector. */ - std::vector _db; +struct database { + unsigned int db_size; /* The database's count of valid entries. */ + bool db_autosave; /* True if the database saves when changed. */ + struct file db_file; /* The database's associated file object. */ - /** Used for keeping track of primary keys. */ - std::map _keys; + std::vector db_entries; + std::map db_keys; - /** The number of valid DatabaseEntries in the Database. */ - unsigned int _size; - - /** Set to True if the Database should be saved when changed. */ - bool _autosave; - - /** File object for reading and writing a Database. */ - file _file; - -public: /** Iterator access for our backing std::vector */ typedef typename std::vector::iterator iterator; /** Const iterator access for our backing std::vector */ @@ -114,12 +104,12 @@ public: * to False then the Database will have to be saved * manually. */ - Database(std::string, bool); + database(std::string, bool); /** * Deletes all remaining entries in a Database to prevent memory leaks. */ - ~Database(); + ~database(); /** * Called to save the Database to disk. diff --git a/include/core/database.hpp b/include/core/database.hpp index 399ea37f..835e8800 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -8,14 +8,14 @@ #define OCARINA_DATABASE_HPP template -Database :: Database(std::string filepath, bool autosave) - : _size(0), _autosave(autosave) +database :: database(std::string filepath, bool autosave) + : db_size(0), db_autosave(autosave) { - file_init(&_file, filepath.c_str(), 0); + file_init(&db_file, filepath.c_str(), 0); } template -Database :: ~Database() +database :: ~database() { iterator it; for (it = begin(); it != end(); it = next(it)) @@ -23,63 +23,65 @@ Database :: ~Database() } template -void Database :: save() +void database :: save() { - if (file_open(&_file, OPEN_WRITE) == false) + if (file_open(&db_file, OPEN_WRITE) == false) return; - file_writef(&_file, "%u\n", actual_size()); - for (unsigned int i = 0; i < _db.size(); i++) { - if (_db[i] == NULL) - file_writef(&_file, "%d\n", false); + file_writef(&db_file, "%u\n", actual_size()); + for (unsigned int i = 0; i < db_entries.size(); i++) { + if (db_entries[i] == NULL) + file_writef(&db_file, "%d\n", false); else { - file_writef(&_file, "%d ", true); - _db[i]->write(_file); - file_writef(&_file, "\n"); + file_writef(&db_file, "%d ", true); + db_entries[i]->write(db_file); + file_writef(&db_file, "\n"); } } - file_close(&_file); + file_close(&db_file); } template -void Database :: autosave() +void database :: autosave() { - if (_autosave == true) + if (db_autosave == true) save(); } template -void Database :: load() +void database :: load() { - unsigned int db_size; + unsigned int size; + std::string str; int valid; - if (file_exists(&_file) == false) + if (file_exists(&db_file) == false) return; - else if (file_open(&_file, OPEN_READ) == false) + else if (file_open(&db_file, OPEN_READ) == false) return; - file_readf(&_file, "%u", &db_size); - _db.resize(db_size); - for (unsigned int i = 0; i < db_size; i++) { - file_readf(&_file, "%d", &valid); + file_readf(&db_file, "%u", &size); + db_entries.resize(size); + for (unsigned int i = 0; i < size; i++) { + file_readf(&db_file, "%d", &valid); if (valid == false) { - _db[i] = NULL; + db_entries[i] = NULL; } else { - _db[i] = new T; - _db[i]->_index = i; - _db[i]->read(_file); - _keys[_db[i]->primary_key()] = i; - _size++; + db_entries[i] = new T; + db_entries[i]->_index = i; + db_entries[i]->read(db_file); + str = db_entries[i]->primary_key(); + db_keys[str] = i; + db_size++; } } - file_close(&_file); + file_close(&db_file); } template -T *Database :: insert(const T &item) +T *database :: insert(const T &item) { T *t = find(item.primary_key()); @@ -88,60 +90,60 @@ T *Database :: insert(const T &item) t = new T(item); t->_index = actual_size(); - _db.push_back(t); + db_entries.push_back(t); - _keys[t->primary_key()] = t->index(); - _size++; + db_keys[t->primary_key()] = t->index(); + db_size++; autosave(); return t; } template -void Database :: remove(unsigned int index) +void database :: remove(unsigned int index) { if (index >= actual_size()) return; - if (_db[index] == NULL) + if (db_entries[index] == NULL) return; - _keys.erase(_db[index]->primary_key()); - delete _db[index]; - _db[index] = NULL; - _size--; + db_keys.erase(db_entries[index]->primary_key()); + delete db_entries[index]; + db_entries[index] = NULL; + db_size--; autosave(); } template -unsigned int Database :: size() +unsigned int database :: size() { - return _size; + return db_size; } template -unsigned int Database :: actual_size() +unsigned int database :: actual_size() { - return _db.size(); + return db_entries.size(); } template -typename Database::iterator Database :: begin() +typename database::iterator database :: begin() { if (size() == 0) return end(); - iterator it = _db.begin(); + iterator it = db_entries.begin(); if ( (*it) != NULL ) return it; return next(it); } template -typename Database::iterator Database :: end() +typename database::iterator database :: end() { - return _db.end(); + return db_entries.end(); } template -typename Database::iterator Database :: next(iterator &it) +typename database::iterator database :: next(iterator &it) { do { it++; @@ -150,20 +152,20 @@ typename Database::iterator Database :: next(iterator &it) } template -T *Database :: at(unsigned int index) +T *database :: at(unsigned int index) { if (index >= actual_size()) return NULL; - return _db[index]; + return db_entries[index]; } template -T *Database :: find(const std::string &key) +T *database :: find(const std::string &key) { std::map::iterator it; - it = _keys.find(key); - if (it == _keys.end()) + it = db_keys.find(key); + if (it == db_keys.end()) return NULL; - return _db[it->second]; + return db_entries[it->second]; } #endif /* OCARINA_DATABASE_HPP */ diff --git a/include/core/index.h b/include/core/index.h index 37cad964..746e679a 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -103,7 +103,7 @@ public: * An Index is a special Database for mapping std::strings to a std::set of * integer identifiers. */ -class Index : public Database { +class Index : public database { public: /** * Index constructor. This simply passes the filepath and autosave diff --git a/tests/core/database.cpp b/tests/core/database.cpp index f28ab1ac..3acfed66 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -61,7 +61,7 @@ static void test_db_entry() static void test_init() { - Database db("database.db", false); + database db("database.db", false); /* Check initial sizes. */ test_equal(db.actual_size(), 0); @@ -70,8 +70,8 @@ static void test_init() static void test_stress(unsigned int N) { - Database db("stress.db", false); - Database::iterator it; + database db("stress.db", false); + database::iterator it; std::vector ptrs; struct int_entry *dbe; unsigned int i; @@ -143,8 +143,8 @@ static void test_stress_100K() { test_stress(100000); } static void test_save_load() { - Database db1("save_load.db", true); - Database db2("save_load.db", false); + database db1("save_load.db", true); + database db2("save_load.db", false); struct int_entry *dbe; const unsigned int N = 10; unsigned int i; @@ -166,7 +166,7 @@ static void test_save_load() for (i = 0; i < N; i += 2) db1.remove(i); - db2 = Database("save_load.db", false); + db2 = database("save_load.db", false); db2.load(); test_equal(db2.size(), N / 2); for (i = 1; i < N; i++) { @@ -185,12 +185,12 @@ static void test_save_load() for (i = N; i < (2 * N); i += 2) db2.remove(i); - db1 = Database("save_load.db", false); + db1 = database("save_load.db", false); db1.load(); test_equal(db1.size(), N / 2); db2.save(); - db1 = Database("save_load.db", false); + db1 = database("save_load.db", false); db1.load(); test_equal(db1.size(), N); diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index 0f5c4708..64e126d7 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -37,7 +37,7 @@ static void test_album_tag() static void test_album_tag_lookup() { - Database album_db("album.db", false); + database album_db("album.db", false); Album *album = tags :: get_album("Hyrule Symphony", 1998); test_equal(album->name(), (std::string)"Hyrule Symphony"); diff --git a/tests/core/tags/artist.cpp b/tests/core/tags/artist.cpp index c436a12f..b07c4e78 100644 --- a/tests/core/tags/artist.cpp +++ b/tests/core/tags/artist.cpp @@ -19,7 +19,7 @@ static void test_artist_tag() static void test_artist_tag_lookup() { - Database artist_db("artist.db", false); + database artist_db("artist.db", false); Artist *artist = tags :: get_artist("Koji Kondo"); test_equal(artist->name(), (std::string)"Koji Kondo"); diff --git a/tests/core/tags/genre.cpp b/tests/core/tags/genre.cpp index d0e5270c..6d6ab425 100644 --- a/tests/core/tags/genre.cpp +++ b/tests/core/tags/genre.cpp @@ -19,7 +19,7 @@ static void test_artist_tag() static void test_genere_tag_lookup() { - Database genre_db("genre.db", false); + database genre_db("genre.db", false); Genre *genre = tags :: get_genre("Video Game Music"); test_equal(genre->name(), (std::string)"Video Game Music"); diff --git a/tests/core/tags/library.cpp b/tests/core/tags/library.cpp index a9cf4a43..6943b63f 100644 --- a/tests/core/tags/library.cpp +++ b/tests/core/tags/library.cpp @@ -44,7 +44,7 @@ static void test_library_tag() static void test_library_tag_lookup() { - Database library_db("library.db", false); + database library_db("library.db", false); Library *library; test_equal(tags :: library_size(), (unsigned)0); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index ee6d9df6..11911336 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -107,7 +107,7 @@ static void test_track_tag_destructor() static void test_track_tag_load_db(unsigned int size) { - Database track_db("track.db", false); + database track_db("track.db", false); track_db.load(); test_equal(track_db.size(), size); }