From cac0aee2e6b3c53aa56b33734e41b7e28124af80 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 25 Mar 2014 17:51:37 -0400 Subject: [PATCH] database: Insert through a const reference This should offer a performance increase since the item to insert will only be copied if it is not already in the database. Signed-off-by: Anna Schumaker --- DESIGN | 32 ++++++++++++++++---------------- include/database.h | 4 ++-- include/database.hpp | 2 +- include/index.h | 2 +- include/library.h | 8 ++++---- include/tags.h | 12 ++++++------ lib/index.cpp | 2 +- lib/library.cpp | 8 ++++---- lib/tags.cpp | 12 ++++++------ tests/src/database.cpp | 4 ++-- tests/src/db_entry.cpp | 4 ++-- 11 files changed, 45 insertions(+), 45 deletions(-) diff --git a/DESIGN b/DESIGN index a006e590..cf449ef9 100644 --- a/DESIGN +++ b/DESIGN @@ -203,7 +203,7 @@ Database Entry: DatabaseEntry(); virtual void ~DatabaseEntry() = 0; - virtual const std::string primary_key() = 0; + virtual const std::string primary_key() const = 0; virtual void write(File &) = 0; virtual void read(File &) = 0; @@ -213,7 +213,7 @@ Database Entry: DatabaseEntry :: DatabaseEntry(): Set id = 0. - const std::string DatabaseEntry :: primary_key(); + const std::string DatabaseEntry :: primary_key() const; This function should return a unique string representing this DatabaseEntry instance, which will be used to prevent duplicates in a database. This string is not expected to @@ -274,7 +274,7 @@ Database: void save(); void load(); - T *insert(T); + T *insert(const T &); void remove(unsigned int); unsigned int size(); unsigned int actual_size(); @@ -308,7 +308,7 @@ Database: void Database :: load(); Load the database from disk. - T *Database :: insert(T item); + T *Database :: insert(const T &item); Look up the item in the _keys map. If we find an item with the same key: - Return a pointer to the found item. @@ -373,7 +373,7 @@ Index: set values; IndexEntry(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void insert(unsigned int); void remove(unsigned int); @@ -388,7 +388,7 @@ Index: IndexEntry :: IndexEntry(); Creat an empty IndexEntry. - std::string IndexEntry :: primary_key(); + std::string IndexEntry :: primary_key() const; return key; void IndexEntry :: insert(unsigned int value); @@ -609,7 +609,7 @@ Artist Tag: Artist(); Artist(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -624,7 +624,7 @@ Artist Tag: Artist(const std::string &artist_name); Set artist_name and find the lowercase form. - const std::string Artist :: primary_key(); + const std::string Artist :: primary_key() const; Use artist name as primary key. void Artist :: read(File &f); @@ -647,7 +647,7 @@ Album Tag: Album(); Album(const std::string &, unsigned int); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -663,7 +663,7 @@ Album Tag: Set name and year from album name and album_year. Find the lowercase form of the album name. - const std::string Album :: primary_key(); + const std::string Album :: primary_key() const; Return the string: "$year.$name" void Album :: read(File &f); @@ -687,7 +687,7 @@ Genre Tag: Genre(); Genre(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -702,7 +702,7 @@ Genre Tag: Genre(const std::string &genre_name); Set genre from genre name and find the lowercase form. - const std::string Genre :: primary_key(); + const std::string Genre :: primary_key() const; Use genre as primary key. void Genre :: read(File &f); @@ -728,7 +728,7 @@ Library Tag: Library(); Library(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -747,7 +747,7 @@ Library Tag: Set count = 0. Set enabled = true. - const std::string Library :: primary_key(); + const std::string Library :: primary_key() const; Use root_path as the primary key, void read(File &f); @@ -797,7 +797,7 @@ Track Tag: Track(); Track(const std::string &, Library *); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); @@ -824,7 +824,7 @@ Track Tag: the result to set filepath. - Set library = lib. - const std::string Track :: primary_key(); + const std::string Track :: primary_key() const; return path(); void read(File &f); diff --git a/include/database.h b/include/database.h index 8c9e5493..f36720fb 100644 --- a/include/database.h +++ b/include/database.h @@ -17,7 +17,7 @@ public: DatabaseEntry(); virtual ~DatabaseEntry() = 0; - virtual const std::string primary_key() = 0; + virtual const std::string primary_key() const = 0; virtual void write(File &) = 0; virtual void read(File &) = 0; }; @@ -43,7 +43,7 @@ public: void autosave(); void load(); - T *insert(T); + T *insert(const T &); void remove(unsigned int); unsigned int size(); unsigned int actual_size(); diff --git a/include/database.hpp b/include/database.hpp index 89163090..c5855bcd 100644 --- a/include/database.hpp +++ b/include/database.hpp @@ -80,7 +80,7 @@ void Database :: load() } template -T *Database :: insert(T val) +T *Database :: insert(const T &val) { T *t = find(val.primary_key()); diff --git a/include/index.h b/include/index.h index 258d4fd1..f8ee53f5 100644 --- a/include/index.h +++ b/include/index.h @@ -17,7 +17,7 @@ public: IndexEntry(); IndexEntry(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void insert(unsigned int); void remove(unsigned int); diff --git a/include/library.h b/include/library.h index 4a1b9003..003c8222 100644 --- a/include/library.h +++ b/include/library.h @@ -33,7 +33,7 @@ namespace library AGInfo(); AGInfo(DB_Type, TagLib :: Tag *); AGInfo(DB_Type, const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -48,7 +48,7 @@ namespace library Album(); Album(TagLib :: Tag *, unsigned int); Album(const std::string &, unsigned int, unsigned int); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -62,7 +62,7 @@ namespace library Library(); Library(const std::string &, bool); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -94,7 +94,7 @@ namespace library unsigned int, const std :: string &); Track(ImportData *, unsigned int, unsigned int, unsigned int, unsigned int); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; diff --git a/include/tags.h b/include/tags.h index 9e2c8627..0eefc8d2 100644 --- a/include/tags.h +++ b/include/tags.h @@ -27,7 +27,7 @@ public: Artist(); Artist(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -41,7 +41,7 @@ public: Album(); Album(const std::string &, unsigned int); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -54,7 +54,7 @@ public: Genre(); Genre(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -68,7 +68,7 @@ public: Library(); Library(const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); }; @@ -95,12 +95,12 @@ public: Track(); Track(const std::string &, Library *); - const std::string primary_key(); + const std::string primary_key() const; void read(File &); void write(File &); void tag(); - const std::string path(); + const std::string path() const; bool less_than(Track *, sort_t); }; diff --git a/lib/index.cpp b/lib/index.cpp index 536f3c4f..ff27f0f4 100644 --- a/lib/index.cpp +++ b/lib/index.cpp @@ -9,7 +9,7 @@ IndexEntry :: IndexEntry(const std::string &k) : key(k) {} -const std::string IndexEntry :: primary_key() +const std::string IndexEntry :: primary_key() const { return key; } diff --git a/lib/library.cpp b/lib/library.cpp index 58b64010..4fd005ec 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -60,7 +60,7 @@ library :: AGInfo :: AGInfo(DB_Type type, const std::string &str) throw -E_INVAL; } -const std::string library :: AGInfo :: primary_key() +const std::string library :: AGInfo :: primary_key() const { return name; } @@ -100,7 +100,7 @@ library :: Album :: Album(const std::string &str, unsigned int yr, unsigned int name_lower = filter :: lowercase(name); } -const std::string library :: Album :: primary_key() +const std::string library :: Album :: primary_key() const { std::stringstream ss; ss << name << "." << year; @@ -135,7 +135,7 @@ library :: Library :: Library(const std::string &path, bool is_enabled) { } -const std::string library :: Library :: primary_key() +const std::string library :: Library :: primary_key() const { return root_path; } @@ -211,7 +211,7 @@ library :: Track :: Track(struct ImportData *data, unsigned int lib, length_str = ss.str(); } -const std::string library :: Track :: primary_key() +const std::string library :: Track :: primary_key() const { return full_path; } diff --git a/lib/tags.cpp b/lib/tags.cpp index db898f36..7d383d39 100644 --- a/lib/tags.cpp +++ b/lib/tags.cpp @@ -28,7 +28,7 @@ Artist :: Artist(const std::string &s) { } -const std::string Artist :: primary_key() +const std::string Artist :: primary_key() const { return name; } @@ -59,7 +59,7 @@ Album :: Album(const std::string &s, unsigned int y) { } -const std::string Album :: primary_key() +const std::string Album :: primary_key() const { std::stringstream ss; ss << year << "." << name; @@ -93,7 +93,7 @@ Genre :: Genre(const std::string &s) { } -const std::string Genre :: primary_key() +const std::string Genre :: primary_key() const { return name; } @@ -127,7 +127,7 @@ Library :: Library(const std::string &s) { } -const std::string Library :: primary_key() +const std::string Library :: primary_key() const { return root_path; } @@ -159,7 +159,7 @@ Track :: Track(const std::string &f, Library *l) { } -const std::string Track :: primary_key() +const std::string Track :: primary_key() const { return path(); } @@ -176,7 +176,7 @@ void Track :: tag() { } -const std::string Track :: path() +const std::string Track :: path() const { return library->root_path + "/" + filepath; } diff --git a/tests/src/database.cpp b/tests/src/database.cpp index 34b959d2..8351b92f 100644 --- a/tests/src/database.cpp +++ b/tests/src/database.cpp @@ -20,7 +20,7 @@ public: IntEntry(); IntEntry(unsigned int); - const std::string primary_key(); + const std::string primary_key() const; void write(File &); void read(File &); void print(); @@ -28,7 +28,7 @@ public: IntEntry :: IntEntry() : val(0) {} IntEntry :: IntEntry(unsigned int v) : val(v) {} -const std::string IntEntry :: primary_key() +const std::string IntEntry :: primary_key() const { std::stringstream ss; ss << val; diff --git a/tests/src/db_entry.cpp b/tests/src/db_entry.cpp index fca5228c..213f712d 100644 --- a/tests/src/db_entry.cpp +++ b/tests/src/db_entry.cpp @@ -19,7 +19,7 @@ public: std::string key; IntEntry(unsigned int, const std::string &); - const std::string primary_key(); + const std::string primary_key() const; void write(File &); void read(File &); void print(); @@ -31,7 +31,7 @@ IntEntry :: IntEntry(unsigned int i, const std::string &s) key = s; } -const std::string IntEntry :: primary_key() +const std::string IntEntry :: primary_key() const { return key; }