From 57c939a53665f808b2429461d547271adf2c5908 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 20 Feb 2005 22:06:56 +0000 Subject: [PATCH] Fetch all the "max score for song" entries in one go instead of one at a time. --- bigscreen/Makefile | 2 +- bigscreen/fetch_max_score_for_song.cpp | 13 ------------- bigscreen/fetch_max_score_for_song.h | 16 ---------------- bigscreen/fetch_max_score_for_songs.cpp | 20 ++++++++++++++++++++ bigscreen/fetch_max_score_for_songs.h | 18 ++++++++++++++++++ bigscreen/groupscreen.cpp | 14 +++++++++++--- 6 files changed, 50 insertions(+), 33 deletions(-) delete mode 100644 bigscreen/fetch_max_score_for_song.cpp delete mode 100644 bigscreen/fetch_max_score_for_song.h create mode 100644 bigscreen/fetch_max_score_for_songs.cpp create mode 100644 bigscreen/fetch_max_score_for_songs.h diff --git a/bigscreen/Makefile b/bigscreen/Makefile index 56cd5de..cf4fcb9 100644 --- a/bigscreen/Makefile +++ b/bigscreen/Makefile @@ -4,7 +4,7 @@ CPPFLAGS=-I/usr/include/postgresql $(shell freetype-config --cflags) -Itinyptc/ CXXFLAGS=-g -Wall LDFLAGS=-L/usr/X11R6/lib LIBS=$(shell freetype-config --libs) $(shell libpq3-config) -lpqxx tinyptc/libtinyptc.a -lX11 -CCBS_BIGSCREEN_OBJS=ccbs_bigscreen.o flagtrigger.o widestring.o fetch_current_tournament.o fetch_list_of_active_groups.o fetch_max_score_for_song.o fetch_max_score_for_player.o fetch_group.o fetch_needs_update.o fetch_highscore.o fonts.o groupscreen.o splitscreen.o rotatescreen.o screen.o +CCBS_BIGSCREEN_OBJS=ccbs_bigscreen.o flagtrigger.o widestring.o fetch_current_tournament.o fetch_list_of_active_groups.o fetch_max_score_for_songs.o fetch_max_score_for_player.o fetch_group.o fetch_needs_update.o fetch_highscore.o fonts.o groupscreen.o splitscreen.o rotatescreen.o screen.o all: ccbs-bigscreen diff --git a/bigscreen/fetch_max_score_for_song.cpp b/bigscreen/fetch_max_score_for_song.cpp deleted file mode 100644 index efac925..0000000 --- a/bigscreen/fetch_max_score_for_song.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "fetch_max_score_for_song.h" - -FetchMaxScoreForSong::FetchMaxScoreForSong(unsigned tournament, unsigned song, unsigned *score) - : tournament(tournament), song(song), score(score) {} - -void FetchMaxScoreForSong::operator() (pqxx::transaction<> &t) -{ - pqxx::result res( t.exec("SELECT MAX(feetrating)*1000 AS max_score FROM songratings WHERE song=" + - pqxx::to_string(song) + " AND machine=( SELECT machine FROM tournaments WHERE tournament=" + - pqxx::to_string(tournament) + ")") ); - - *score = res.at(0)["max_score"].as(*score); -} diff --git a/bigscreen/fetch_max_score_for_song.h b/bigscreen/fetch_max_score_for_song.h deleted file mode 100644 index f3b9f77..0000000 --- a/bigscreen/fetch_max_score_for_song.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef _FETCH_MAX_SCORE_FOR_SONG_H -#define _FETCH_MAX_SCORE_FOR_SONG_H 1 - -#include - -/* A transactor that fetches the maximum score for a song */ -class FetchMaxScoreForSong : public pqxx::transactor<> { -private: - unsigned tournament, song, *score; - -public: - FetchMaxScoreForSong(unsigned tournament, unsigned song, unsigned *score); - void operator() (pqxx::transaction<> &t); -}; - -#endif /* !defined(_FETCH_MAX_SCORE_FOR_SONG_H) */ diff --git a/bigscreen/fetch_max_score_for_songs.cpp b/bigscreen/fetch_max_score_for_songs.cpp new file mode 100644 index 0000000..d9a8841 --- /dev/null +++ b/bigscreen/fetch_max_score_for_songs.cpp @@ -0,0 +1,20 @@ +#include "fetch_max_score_for_songs.h" + +FetchMaxScoreForSongs::FetchMaxScoreForSongs(unsigned tournament, std::map *score) + : tournament(tournament), score(score) {} + +void FetchMaxScoreForSongs::operator() (pqxx::transaction<> &t) +{ + score->erase(score->begin(), score->end()); + + pqxx::result res( t.exec("SELECT song,MAX(feetrating)*1000 AS max_score FROM songratings WHERE " + " machine=( SELECT machine FROM tournaments WHERE tournament=" + pqxx::to_string(tournament) + ") GROUP BY song") ); + + for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) { + unsigned song, max_score; + song = i["song"].as(song); + max_score = i["max_score"].as(max_score); + + score->insert(std::make_pair(song, max_score)); + } +} diff --git a/bigscreen/fetch_max_score_for_songs.h b/bigscreen/fetch_max_score_for_songs.h new file mode 100644 index 0000000..935dd0a --- /dev/null +++ b/bigscreen/fetch_max_score_for_songs.h @@ -0,0 +1,18 @@ +#ifndef _FETCH_MAX_SCORE_FOR_SONGS_H +#define _FETCH_MAX_SCORE_FOR_SONGS_H 1 + +#include +#include + +/* A transactor that fetches the maximum score for all songs */ +class FetchMaxScoreForSongs : public pqxx::transactor<> { +private: + unsigned tournament; + std::map *score; + +public: + FetchMaxScoreForSongs(unsigned tournament, std::map *score); + void operator() (pqxx::transaction<> &t); +}; + +#endif /* !defined(_FETCH_MAX_SCORE_FOR_SONGS_H) */ diff --git a/bigscreen/groupscreen.cpp b/bigscreen/groupscreen.cpp index 6d38c4c..fbdcc5e 100644 --- a/bigscreen/groupscreen.cpp +++ b/bigscreen/groupscreen.cpp @@ -1,9 +1,10 @@ #include #include +#include #include "groupscreen.h" #include "fetch_group.h" -#include "fetch_max_score_for_song.h" +#include "fetch_max_score_for_songs.h" #include "fetch_max_score_for_player.h" #include "fetch_needs_update.h" #include "fetch_highscore.h" @@ -40,6 +41,13 @@ void GroupScreen::draw(unsigned char *buf) scores_changed.reset_flag(); + /* + * We'll probably need some values from here later on (although not all), just fetch them + * all while we're at it. + */ + std::map song_scores; + conn.perform(FetchMaxScoreForSongs(tournament, &song_scores)); + Group group; conn.perform(FetchGroup(tournament, round, parallel, &group)); gettimeofday(&last_updated, NULL); @@ -218,7 +226,7 @@ void GroupScreen::draw(unsigned char *buf) unsigned max_score_this_song; if (j->song.id != -1) { // random song, or we know what song the player picked - conn.perform(FetchMaxScoreForSong(tournament, j->song.id, &max_score_this_song)); + max_score_this_song = song_scores[j->song.id]; } else { conn.perform(FetchMaxScoreForPlayer(tournament, i->id, round, &max_score_this_song)); } @@ -350,7 +358,7 @@ void GroupScreen::draw(unsigned char *buf) unsigned max_score_this_song; if (next_song->song.id != -1) { // random song, or we know what song the player picked - conn.perform(FetchMaxScoreForSong(tournament, next_song->song.id, &max_score_this_song)); + max_score_this_song = song_scores[next_song->song.id]; } else { conn.perform(FetchMaxScoreForPlayer(tournament, next_player->id, round, &max_score_this_song)); } -- 2.39.2