Fetch all the "max score for song" entries in one go instead of one at a time.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 20 Feb 2005 22:06:56 +0000 (22:06 +0000)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 20 Feb 2005 22:06:56 +0000 (22:06 +0000)
bigscreen/Makefile
bigscreen/fetch_max_score_for_song.cpp [deleted file]
bigscreen/fetch_max_score_for_song.h [deleted file]
bigscreen/fetch_max_score_for_songs.cpp [new file with mode: 0644]
bigscreen/fetch_max_score_for_songs.h [new file with mode: 0644]
bigscreen/groupscreen.cpp

index 56cd5de..cf4fcb9 100644 (file)
@@ -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 (file)
index efac925..0000000
+++ /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 (file)
index f3b9f77..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef _FETCH_MAX_SCORE_FOR_SONG_H
-#define _FETCH_MAX_SCORE_FOR_SONG_H 1
-
-#include <pqxx/transactor>
-
-/* 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 (file)
index 0000000..d9a8841
--- /dev/null
@@ -0,0 +1,20 @@
+#include "fetch_max_score_for_songs.h"
+
+FetchMaxScoreForSongs::FetchMaxScoreForSongs(unsigned tournament, std::map<unsigned, unsigned> *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 (file)
index 0000000..935dd0a
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef _FETCH_MAX_SCORE_FOR_SONGS_H
+#define _FETCH_MAX_SCORE_FOR_SONGS_H 1
+
+#include <pqxx/transactor>
+#include <map>
+
+/* A transactor that fetches the maximum score for all songs */
+class FetchMaxScoreForSongs : public pqxx::transactor<> {
+private:
+       unsigned tournament;
+       std::map<unsigned, unsigned> *score;
+
+public:
+       FetchMaxScoreForSongs(unsigned tournament, std::map<unsigned, unsigned> *score);
+       void operator() (pqxx::transaction<> &t);
+};
+
+#endif /* !defined(_FETCH_MAX_SCORE_FOR_SONGS_H) */
index 6d38c4c..fbdcc5e 100644 (file)
@@ -1,9 +1,10 @@
 #include <cstdio>
 #include <algorithm>
+#include <map>
 
 #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<unsigned, unsigned> 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));
                        }