Refactor containers and library loading
Replace trackPtr with raw Track* pointers in HashTable, add loadLibrary to PlayerLocal, remove unused File helpers, and fix broken artwork/artist SQL bindings.
This commit is contained in:
@@ -53,8 +53,8 @@ class HashTable {
|
|||||||
HashTable ( HashTable && ) noexcept = default;
|
HashTable ( HashTable && ) noexcept = default;
|
||||||
HashTable &operator= ( HashTable && ) noexcept = default;
|
HashTable &operator= ( HashTable && ) noexcept = default;
|
||||||
|
|
||||||
void ht_insert ( const std::string &key, trackPtr track );
|
void ht_insert ( const std::string &key, Track* t );
|
||||||
trackPtr ht_lookup ( const std::string &key ) const;
|
Track* ht_lookup ( const std::string &key ) const;
|
||||||
bool ht_delete ( const std::string &key );
|
bool ht_delete ( const std::string &key );
|
||||||
|
|
||||||
std::vector<std::string> listAll () const;
|
std::vector<std::string> listAll () const;
|
||||||
@@ -72,7 +72,7 @@ class HashTable {
|
|||||||
struct Entry {
|
struct Entry {
|
||||||
std::string key;
|
std::string key;
|
||||||
uint64_t hash = 0;
|
uint64_t hash = 0;
|
||||||
trackPtr track;
|
Track* track;
|
||||||
};
|
};
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
|||||||
+1
-3
@@ -12,11 +12,9 @@ class File {
|
|||||||
public:
|
public:
|
||||||
Track importFile( const std::string& filePath );
|
Track importFile( const std::string& filePath );
|
||||||
void scanFolder( const std::string& folderPath, std::vector<Track>& tracks );
|
void scanFolder( const std::string& folderPath, std::vector<Track>& tracks );
|
||||||
|
void updateLibrary();
|
||||||
void readFileTag( Track& t );
|
void readFileTag( Track& t );
|
||||||
TagLib::VariantMap importArtwork( Track& t );
|
TagLib::VariantMap importArtwork( Track& t );
|
||||||
std::string parseFiletype( const std::string& path );
|
|
||||||
void updateLibrary();
|
|
||||||
uint64_t hashString( Track t );
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string path_;
|
std::string path_;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class PlayerLocal : public Player {
|
|||||||
public:
|
public:
|
||||||
// Library Controls
|
// Library Controls
|
||||||
void loadFile ( Track track ) override;
|
void loadFile ( Track track ) override;
|
||||||
|
void loadLibrary();
|
||||||
void scanLibrary() override;
|
void scanLibrary() override;
|
||||||
void updateLibrary() override;
|
void updateLibrary() override;
|
||||||
void editMetadata() override;
|
void editMetadata() override;
|
||||||
@@ -30,4 +31,8 @@ class PlayerLocal : public Player {
|
|||||||
unsigned int currentTrack_;
|
unsigned int currentTrack_;
|
||||||
db db_;
|
db db_;
|
||||||
std::string musicFolder_ = "data/";
|
std::string musicFolder_ = "data/";
|
||||||
|
|
||||||
|
ht tracks_;
|
||||||
|
ht artists_;
|
||||||
|
ht albums_;
|
||||||
};
|
};
|
||||||
|
|||||||
+5
-5
@@ -97,7 +97,7 @@ void db::fetchAll( std::vector<Track>& entries ) const {
|
|||||||
t.trackNr = sqlite3_column_int( stmt, 3 ) ? sqlite3_column_int( stmt, 3 ) : 0;
|
t.trackNr = sqlite3_column_int( stmt, 3 ) ? sqlite3_column_int( stmt, 3 ) : 0;
|
||||||
t.filePath = sqlite3_column_text( stmt, 4 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 4 )) : "";
|
t.filePath = sqlite3_column_text( stmt, 4 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 4 )) : "";
|
||||||
t.length = sqlite3_column_int( stmt, 5 ) ? sqlite3_column_int ( stmt, 5 ) : 0;
|
t.length = sqlite3_column_int( stmt, 5 ) ? sqlite3_column_int ( stmt, 5 ) : 0;
|
||||||
t.artwork = sqlite3_column_text( stmt, 6 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 6 )) : "";
|
//t.artwork = ;
|
||||||
t.genre = sqlite3_column_text( stmt, 7 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 7 )) : "";
|
t.genre = sqlite3_column_text( stmt, 7 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 7 )) : "";
|
||||||
t.format = sqlite3_column_text( stmt, 8 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 8 )) : "";
|
t.format = sqlite3_column_text( stmt, 8 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 8 )) : "";
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ void db::fetchAll( std::map<std::string, Track>& dbEntries ) const {
|
|||||||
t.trackNr = sqlite3_column_int( stmt, 3 ) ? sqlite3_column_int( stmt, 3 ) : 0;
|
t.trackNr = sqlite3_column_int( stmt, 3 ) ? sqlite3_column_int( stmt, 3 ) : 0;
|
||||||
t.filePath = sqlite3_column_text( stmt, 4 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 4 )) : "";
|
t.filePath = sqlite3_column_text( stmt, 4 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 4 )) : "";
|
||||||
t.length = sqlite3_column_int( stmt, 5 ) ? sqlite3_column_int ( stmt, 5 ) : 0;
|
t.length = sqlite3_column_int( stmt, 5 ) ? sqlite3_column_int ( stmt, 5 ) : 0;
|
||||||
t.artwork = sqlite3_column_text( stmt, 6 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 6 )) : "";
|
//t.artwork = sqlite3_column_text( stmt, 6 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 6 )) : "";
|
||||||
t.genre = sqlite3_column_text( stmt, 7 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 7 )) : "";
|
t.genre = sqlite3_column_text( stmt, 7 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 7 )) : "";
|
||||||
t.format = sqlite3_column_text( stmt, 8 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 8 )) : "";
|
t.format = sqlite3_column_text( stmt, 8 ) ? reinterpret_cast<const char*> ( sqlite3_column_text( stmt, 8 )) : "";
|
||||||
|
|
||||||
@@ -200,11 +200,11 @@ void db::addTrack( Track t) {
|
|||||||
|
|
||||||
sqlite3_bind_text( stmt, 1,t.artist.toCString(), -1, SQLITE_STATIC );
|
sqlite3_bind_text( stmt, 1,t.artist.toCString(), -1, SQLITE_STATIC );
|
||||||
sqlite3_bind_text( stmt, 2,t.title.toCString(), -1, SQLITE_STATIC );
|
sqlite3_bind_text( stmt, 2,t.title.toCString(), -1, SQLITE_STATIC );
|
||||||
sqlite3_bind_int( stmt, 3, ));
|
//sqlite3_bind_int( stmt, 3, ));
|
||||||
sqlite3_bind_int( stmt, 4, t.trackNr );
|
sqlite3_bind_int( stmt, 4, t.trackNr );
|
||||||
sqlite3_bind_int( stmt, 5, t.length );
|
sqlite3_bind_int( stmt, 5, t.length );
|
||||||
sqlite3_bind_text( stmt, 6,t.filePath.c_str(), -1, SQLITE_STATIC );
|
sqlite3_bind_text( stmt, 6,t.filePath.c_str(), -1, SQLITE_STATIC );
|
||||||
sqlite3_bind_text( stmt, 7,t.artwork.c_str(), -1, SQLITE_STATIC );
|
//sqlite3_bind_text( stmt, 7,t.artwork.c_str(), -1, SQLITE_STATIC );
|
||||||
sqlite3_bind_text( stmt, 8,t.genre.toCString(), -1, SQLITE_STATIC );
|
sqlite3_bind_text( stmt, 8,t.genre.toCString(), -1, SQLITE_STATIC );
|
||||||
sqlite3_bind_text( stmt, 9, t.format.c_str(), -1, SQLITE_STATIC );
|
sqlite3_bind_text( stmt, 9, t.format.c_str(), -1, SQLITE_STATIC );
|
||||||
sqlite3_bind_int( stmt, 10, t.hash );
|
sqlite3_bind_int( stmt, 10, t.hash );
|
||||||
@@ -224,7 +224,7 @@ void db::addAlbum( Track t ) {
|
|||||||
|
|
||||||
int rc = sqlite3_prepare_v2( db_, sql, -1, &stmt, nullptr );
|
int rc = sqlite3_prepare_v2( db_, sql, -1, &stmt, nullptr );
|
||||||
sqlite3_bind_text( stmt, 0, t.album.toCString(), -1, SQLITE_TRANSIENT );
|
sqlite3_bind_text( stmt, 0, t.album.toCString(), -1, SQLITE_TRANSIENT );
|
||||||
sqlite3_bind_int( stmt, 1, artistId, -1, SQLITE_TRANSIENT );
|
//sqlite3_bind_int( stmt, 1, getArtistId( t.artist.toCString()), -1, SQLITE_TRANSIENT );
|
||||||
|
|
||||||
if ( rc != SQLITE_OK ) throw std::runtime_error( sqlite3_errmsg( db_ ));
|
if ( rc != SQLITE_OK ) throw std::runtime_error( sqlite3_errmsg( db_ ));
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -108,7 +108,7 @@ std::ptrdiff_t ht::probe ( const std::string &key, uint64_t hash, bool insert )
|
|||||||
#endif
|
#endif
|
||||||
} // probe
|
} // probe
|
||||||
|
|
||||||
void ht::ht_insert( const std::string& key, trackPtr track ) {
|
void ht::ht_insert( const std::string& key, Track* t ) {
|
||||||
//double loadFactor = static_cast<double> ( size_ ) / static_cast<double> ( ctrl_.size());
|
//double loadFactor = static_cast<double> ( size_ ) / static_cast<double> ( ctrl_.size());
|
||||||
|
|
||||||
if ( loadFactor_ > MAX_LOAD ) rehash( ctrl_.size() * 2 );
|
if ( loadFactor_ > MAX_LOAD ) rehash( ctrl_.size() * 2 );
|
||||||
@@ -120,16 +120,16 @@ void ht::ht_insert( const std::string& key, trackPtr track ) {
|
|||||||
Entry tmp;
|
Entry tmp;
|
||||||
tmp.key = key;
|
tmp.key = key;
|
||||||
tmp.hash = hash;
|
tmp.hash = hash;
|
||||||
tmp.track = track;
|
tmp.track = t;
|
||||||
entries_.at( idx ) = std::move( tmp );
|
entries_.at( idx ) = std::move( tmp );
|
||||||
++size_;
|
++size_;
|
||||||
} else {
|
} else {
|
||||||
entries_.at( idx ).track = track;
|
entries_.at( idx ).track = t;
|
||||||
}
|
}
|
||||||
ctrl_.at( idx ) = fingerprint( hash );
|
ctrl_.at( idx ) = fingerprint( hash );
|
||||||
}
|
}
|
||||||
|
|
||||||
trackPtr ht::ht_lookup( const std::string& key ) const {
|
Track* ht::ht_lookup( const std::string& key ) const {
|
||||||
uint64_t hash = hashString( key );
|
uint64_t hash = hashString( key );
|
||||||
auto idx = probe( key, hash, false );
|
auto idx = probe( key, hash, false );
|
||||||
if ( idx != -1 ) return entries_.at( static_cast<std::size_t> ( idx )).track;
|
if ( idx != -1 ) return entries_.at( static_cast<std::size_t> ( idx )).track;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include "../include/Database.h"
|
#include "../include/Database.h"
|
||||||
#include "../include/Containers.h"
|
#include "../include/Containers.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using db = Database;
|
using db = Database;
|
||||||
|
|
||||||
void Player::play () { PE_.startPlayback(); }
|
void Player::play () { PE_.startPlayback(); }
|
||||||
@@ -55,3 +57,12 @@ void PlayerLocal::scanLibrary(){
|
|||||||
db_.addTrack( t );
|
db_.addTrack( t );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PlayerLocal::loadLibrary() {
|
||||||
|
db_.fetchAll( library );
|
||||||
|
for ( auto t : library ) {
|
||||||
|
tracks_.ht_insert(t.title.toCString(), &t );
|
||||||
|
artists_.ht_insert( t.artist.toCString(), &t );
|
||||||
|
albums_.ht_insert( t.album.toCString(), &t );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user