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:
2026-06-22 11:51:27 +02:00
parent 05bd44a354
commit 00c8d9b843
6 changed files with 29 additions and 15 deletions
+3 -3
View File
@@ -53,8 +53,8 @@ class HashTable {
HashTable ( HashTable && ) noexcept = default;
HashTable &operator= ( HashTable && ) noexcept = default;
void ht_insert ( const std::string &key, trackPtr track );
trackPtr ht_lookup ( const std::string &key ) const;
void ht_insert ( const std::string &key, Track* t );
Track* ht_lookup ( const std::string &key ) const;
bool ht_delete ( const std::string &key );
std::vector<std::string> listAll () const;
@@ -72,7 +72,7 @@ class HashTable {
struct Entry {
std::string key;
uint64_t hash = 0;
trackPtr track;
Track* track;
};
// helpers
+1 -3
View File
@@ -12,11 +12,9 @@ class File {
public:
Track importFile( const std::string& filePath );
void scanFolder( const std::string& folderPath, std::vector<Track>& tracks );
void updateLibrary();
void readFileTag( Track& t );
TagLib::VariantMap importArtwork( Track& t );
std::string parseFiletype( const std::string& path );
void updateLibrary();
uint64_t hashString( Track t );
private:
std::string path_;
+5
View File
@@ -9,6 +9,7 @@ class PlayerLocal : public Player {
public:
// Library Controls
void loadFile ( Track track ) override;
void loadLibrary();
void scanLibrary() override;
void updateLibrary() override;
void editMetadata() override;
@@ -30,4 +31,8 @@ class PlayerLocal : public Player {
unsigned int currentTrack_;
db db_;
std::string musicFolder_ = "data/";
ht tracks_;
ht artists_;
ht albums_;
};
+5 -5
View File
@@ -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.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.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.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.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.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.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, 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, 5, t.length );
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, 9, t.format.c_str(), -1, SQLITE_STATIC );
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 );
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_ ));
}
+4 -4
View File
@@ -108,7 +108,7 @@ std::ptrdiff_t ht::probe ( const std::string &key, uint64_t hash, bool insert )
#endif
} // 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());
if ( loadFactor_ > MAX_LOAD ) rehash( ctrl_.size() * 2 );
@@ -120,16 +120,16 @@ void ht::ht_insert( const std::string& key, trackPtr track ) {
Entry tmp;
tmp.key = key;
tmp.hash = hash;
tmp.track = track;
tmp.track = t;
entries_.at( idx ) = std::move( tmp );
++size_;
} else {
entries_.at( idx ).track = track;
entries_.at( idx ).track = t;
}
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 );
auto idx = probe( key, hash, false );
if ( idx != -1 ) return entries_.at( static_cast<std::size_t> ( idx )).track;
+11
View File
@@ -4,6 +4,8 @@
#include "../include/Database.h"
#include "../include/Containers.h"
#include <memory>
using db = Database;
void Player::play () { PE_.startPlayback(); }
@@ -55,3 +57,12 @@ void PlayerLocal::scanLibrary(){
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 );
}
}