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
This commit is contained in:
Michael Bayne
2008-10-24 01:34:26 +00:00
parent 3afd325c31
commit 83ae9ccbd6
2 changed files with 47 additions and 11 deletions
@@ -42,6 +42,13 @@ public class PercentileRecord extends PersistentRecord
public static final ColumnExp GAME_ID_C = public static final ColumnExp GAME_ID_C =
new ColumnExp(PercentileRecord.class, GAME_ID); 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. */ /** The column identifier for the {@link #data} field. */
public static final String DATA = "data"; public static final String DATA = "data";
@@ -51,11 +58,13 @@ public class PercentileRecord extends PersistentRecord
// AUTO-GENERATED: FIELDS END // AUTO-GENERATED: FIELDS END
/** Increment this value to reflect changes to this object's schema. */ /** 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. */ /** The id of the game for which we're tracking a percentile distribution. */
@Id @Id public int gameId;
public int gameId;
/** The mode of the game. Games can maintain distributions for many different modes. */
@Id public int gameMode;
/** The raw percentiler data. */ /** The raw percentiler data. */
@Column(length=500) @Column(length=500)
@@ -66,12 +75,12 @@ public class PercentileRecord extends PersistentRecord
* Create and return a primary {@link Key} to identify a {@link PercentileRecord} * Create and return a primary {@link Key} to identify a {@link PercentileRecord}
* with the supplied key values. * with the supplied key values.
*/ */
public static Key<PercentileRecord> getKey (int gameId) public static Key<PercentileRecord> getKey (int gameId, int gameMode)
{ {
return new Key<PercentileRecord>( return new Key<PercentileRecord>(
PercentileRecord.class, PercentileRecord.class,
new String[] { GAME_ID }, new String[] { GAME_ID, GAME_MODE },
new Comparable[] { gameId }); new Comparable[] { gameId, gameMode });
} }
// AUTO-GENERATED: METHODS END // AUTO-GENERATED: METHODS END
} }
@@ -26,8 +26,10 @@ import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; 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 * Loads the percentile distribution associated with the specified game and mode. null will
* returned, rather a blank percentiler will be created and returned. * 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); 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<Integer, Percentiler> loadPercentiles (int gameId)
{
Map<Integer, Percentiler> 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. * 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(); PercentileRecord record = new PercentileRecord();
record.gameId = gameId; record.gameId = gameId;
record.gameMode = gameMode;
record.data = tiler.toBytes(); record.data = tiler.toBytes();
store(record); 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 @Override
protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes) protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes)
{ {