From 83ae9ccbd6b42c66861c7ffcb3e75361dbd0a1c1 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 24 Oct 2008 01:34:26 +0000 Subject: [PATCH] Added support for tracking multiple percentilers for a particular game. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@766 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../server/persist/PercentileRecord.java | 21 ++++++++--- .../server/persist/RatingRepository.java | 37 ++++++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/java/com/threerings/parlor/rating/server/persist/PercentileRecord.java b/src/java/com/threerings/parlor/rating/server/persist/PercentileRecord.java index 432fb1d8..dfaeec8c 100644 --- a/src/java/com/threerings/parlor/rating/server/persist/PercentileRecord.java +++ b/src/java/com/threerings/parlor/rating/server/persist/PercentileRecord.java @@ -42,6 +42,13 @@ public class PercentileRecord extends PersistentRecord public static final ColumnExp GAME_ID_C = new ColumnExp(PercentileRecord.class, GAME_ID); + /** The column identifier for the {@link #gameMode} field. */ + public static final String GAME_MODE = "gameMode"; + + /** The qualified column identifier for the {@link #gameMode} field. */ + public static final ColumnExp GAME_MODE_C = + new ColumnExp(PercentileRecord.class, GAME_MODE); + /** The column identifier for the {@link #data} field. */ public static final String DATA = "data"; @@ -51,11 +58,13 @@ public class PercentileRecord extends PersistentRecord // AUTO-GENERATED: FIELDS END /** Increment this value to reflect changes to this object's schema. */ - public static final int SCHEMA_VERSION = 3; + public static final int SCHEMA_VERSION = 4; /** The id of the game for which we're tracking a percentile distribution. */ - @Id - public int gameId; + @Id public int gameId; + + /** The mode of the game. Games can maintain distributions for many different modes. */ + @Id public int gameMode; /** The raw percentiler data. */ @Column(length=500) @@ -66,12 +75,12 @@ public class PercentileRecord extends PersistentRecord * Create and return a primary {@link Key} to identify a {@link PercentileRecord} * with the supplied key values. */ - public static Key getKey (int gameId) + public static Key getKey (int gameId, int gameMode) { return new Key( PercentileRecord.class, - new String[] { GAME_ID }, - new Comparable[] { gameId }); + new String[] { GAME_ID, GAME_MODE }, + new Comparable[] { gameId, gameMode }); } // AUTO-GENERATED: METHODS END } diff --git a/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java b/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java index 07dbdaf4..f84cc279 100644 --- a/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java +++ b/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java @@ -26,8 +26,10 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Set; +import com.google.common.collect.Maps; import com.google.common.collect.Lists; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -157,26 +159,51 @@ public class RatingRepository extends DepotRepository } /** - * Loads the percentile distribution associated with the specified game. null will never be - * returned, rather a blank percentiler will be created and returned. + * Loads the percentile distribution associated with the specified game and mode. null will + * never be returned, rather a blank percentiler will be created and returned. */ - public Percentiler loadPercentile (int gameId) + public Percentiler loadPercentile (int gameId, int gameMode) { - PercentileRecord record = load(PercentileRecord.class, PercentileRecord.getKey(gameId)); + PercentileRecord record = load( + PercentileRecord.class, PercentileRecord.getKey(gameId, gameMode)); return (record == null) ? new Percentiler() : new Percentiler(record.data); } + /** + * Loads all the percentile distributions associated with the specified game. The percentilers + * will be mapped by their game mode. The map may be zero size if the game has recorded no + * distributions. + */ + public Map loadPercentiles (int gameId) + { + Map tilers = Maps.newHashMap(); + for (PercentileRecord record : findAll( + PercentileRecord.class, new Where(PercentileRecord.GAME_ID_C, gameId))) { + tilers.put(record.gameMode, new Percentiler(record.data)); + } + return tilers; + } + /** * Writes the supplied percentiler's data out to the database. */ - public void updatePercentile (int gameId, Percentiler tiler) + public void updatePercentile (int gameId, int gameMode, Percentiler tiler) { PercentileRecord record = new PercentileRecord(); record.gameId = gameId; + record.gameMode = gameMode; record.data = tiler.toBytes(); store(record); } + /** + * Deletes all percentile records for the specified game. + */ + public void deletePercentiles (int gameId) + { + deleteAll(PercentileRecord.class, new Where(PercentileRecord.GAME_ID_C, gameId)); + } + @Override protected void getManagedRecords (Set> classes) {