Added support for tracking multiple per-game percentile distributions.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@422 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-09-10 20:44:50 +00:00
parent 81f49e4125
commit 5dd8695a9a
2 changed files with 129 additions and 0 deletions
@@ -0,0 +1,83 @@
//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.rating.server.persist;
import com.samskivert.jdbc.depot.Key;
import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.depot.annotation.Id;
import com.samskivert.jdbc.depot.expression.ColumnExp;
import com.threerings.parlor.rating.util.Percentiler;
/**
* Contains data for a {@link Percentiler} record.
*/
public class PercentileRecord extends PersistentRecord
{
// AUTO-GENERATED: FIELDS START
/** The column identifier for the {@link #gameId} field. */
public static final String GAME_ID = "gameId";
/** The qualified column identifier for the {@link #gameId} field. */
public static final ColumnExp GAME_ID_C =
new ColumnExp(PercentileRecord.class, GAME_ID);
/** The column identifier for the {@link #type} field. */
public static final String TYPE = "type";
/** The qualified column identifier for the {@link #type} field. */
public static final ColumnExp TYPE_C =
new ColumnExp(PercentileRecord.class, TYPE);
/** The column identifier for the {@link #data} field. */
public static final String DATA = "data";
/** The qualified column identifier for the {@link #data} field. */
public static final ColumnExp DATA_C =
new ColumnExp(PercentileRecord.class, DATA);
// AUTO-GENERATED: FIELDS END
/** The id of the game for which we're tracking a percentile distribution. */
@Id
public int gameId;
/** The type of percentile distribution (games can maintain multiple distributions). */
@Id
public int type;
/** The raw percentiler data. */
public byte[] data;
// AUTO-GENERATED: METHODS START
/**
* Create and return a primary {@link Key} to identify a {@link #PercentileRecord}
* with the supplied key values.
*/
public static Key<PercentileRecord> getKey (int gameId, int type)
{
return new Key<PercentileRecord>(
PercentileRecord.class,
new String[] { GAME_ID, TYPE },
new Comparable[] { gameId, type });
}
// AUTO-GENERATED: METHODS END
}
@@ -26,6 +26,8 @@ import java.util.List;
import java.util.Set;
import com.samskivert.io.PersistenceException;
import com.samskivert.util.HashIntMap;
import com.samskivert.util.IntMap;
import com.samskivert.jdbc.depot.DepotRepository;
import com.samskivert.jdbc.depot.PersistenceContext;
@@ -34,6 +36,8 @@ import com.samskivert.jdbc.depot.clause.Where;
import com.samskivert.jdbc.depot.operator.Conditionals.*;
import com.samskivert.jdbc.depot.operator.Logic.And;
import com.threerings.parlor.rating.util.Percentiler;
/**
* Handles the persistent storage of per-user per-game ratings.
*/
@@ -101,9 +105,51 @@ public class RatingRepository extends DepotRepository
delete(RatingRecord.class, RatingRecord.getKey(gameId, playerId));
}
/**
* Loads all percentile distributions associated with the specified game.
*
* @return a map from type to {@link Percentiler} instance.
*/
public IntMap<Percentiler> loadPercentiles (int gameId)
throws PersistenceException
{
HashIntMap results = new HashIntMap();
Where where = new Where(PercentileRecord.GAME_ID_C, gameId);
for (PercentileRecord record : findAll(PercentileRecord.class, where)) {
results.put(record.type, new Percentiler(record.data));
}
return results;
}
/**
* Loads a particular percentiler for the specified game. null will never be returned, rather a
* blank percentiler will be created and returned.
*/
public Percentiler loadPercentile (int gameId, int type)
throws PersistenceException
{
PercentileRecord record =
load(PercentileRecord.class, PercentileRecord.getKey(gameId, type));
return (record == null) ? new Percentiler() : new Percentiler(record.data);
}
/**
* Writes the supplied percentiler's data out to the database.
*/
public void updatePercentile (int gameId, int type, Percentiler tiler)
throws PersistenceException
{
PercentileRecord record = new PercentileRecord();
record.gameId = gameId;
record.type = type;
record.data = tiler.toBytes();
store(record);
}
@Override // from DepotRepository
protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes)
{
classes.add(RatingRecord.class);
classes.add(PercentileRecord.class);
}
}