com.threerings.stats.server.persist
Class StatRepository

java.lang.Object
  extended by com.samskivert.depot.DepotRepository
      extended by com.threerings.stats.server.persist.StatRepository
All Implemented Interfaces:
Stat.AuxDataSource

public class StatRepository
extends com.samskivert.depot.DepotRepository
implements Stat.AuxDataSource

Responsible for the persistent storage of per-player statistics.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.samskivert.depot.DepotRepository
com.samskivert.depot.DepotRepository.CacheStrategy
 
Field Summary
protected  Map<Stat.Type,IntMap<String>> _codeToString
           
protected  Map<Stat.Type,Map<String,Integer>> _stringToCode
           
protected static int MAX_UPDATE_TRIES
           
 
Fields inherited from class com.samskivert.depot.DepotRepository
_ctx, _dataMigs
 
Constructor Summary
StatRepository(com.samskivert.depot.PersistenceContext context)
          Constructs a new statistics repository with the specified persistence context.
 
Method Summary
protected  Integer assignStringCode(Stat.Type type, String value)
          Helper function for getStringCode(com.threerings.stats.data.Stat.Type, java.lang.String).
 void clearMapping(Stat.Type type, String value)
          This is only used for testing.
protected  Stat decodeStat(int statCode, byte[] data, byte modCount)
          Instantiates the appropriate stat class and decodes the stat from the data.
protected  Stat decodeStat(Stat stat, byte[] data, byte modCount)
          Instantiates the appropriate stat class and decodes the stat from the data.
 void deleteStats(int playerId)
          Deletes all stats associated with the specified player.
 String getCodeString(Stat.Type type, int code)
          Maps the specified unique code back to its string value.
protected  void getManagedRecords(Set<Class<? extends com.samskivert.depot.PersistentRecord>> classes)
           
 int getStringCode(Stat.Type type, String value)
          Maps the specified string to a unique integer value.
protected  void init()
           
 ArrayList<Stat> loadStats(int playerId)
          Loads the stats associated with the specified player.
protected  void loadStringCodes(Stat.Type type)
          Helper function used at repository startup.
protected  void mapStringCode(Stat.Type type, String value, int code)
          Helper function used at repository startup.
 void purgePlayers(Collection<Integer> playerIds)
          Deletes all data associated with the supplied players.
protected  boolean updateStat(int playerId, Stat stat, boolean forceWrite)
          Updates the specified stat in the database, inserting it if necessary.
<T extends Stat>
T
updateStat(int playerId, StatModifier<T> modifier)
          Applies a modification to a single stat.
 void writeModified(int playerId, Iterable<Stat> stats)
          Writes out any of the stats in the supplied iterable that have been modified since they were first loaded.
 void writeModified(int playerId, Stat[] stats)
          Writes out any of the stats in the supplied array that have been modified since they were first loaded.
 
Methods inherited from class com.samskivert.depot.DepotRepository
delete, delete, deleteAll, deleteAll, deleteAll, deleteAll, doUpdate, findAll, findAll, findAll, findAll, findAllKeys, findAllKeys, from, insert, load, load, load, load, loadAll, loadAll, makeValue, map, registerMigration, requireNotComputed, resolveRecords, runMigration, store, update, update, updatePartial, updatePartial, updatePartial, updatePartial, updatePartial
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_stringToCode

protected Map<Stat.Type,Map<String,Integer>> _stringToCode

_codeToString

protected Map<Stat.Type,IntMap<String>> _codeToString

MAX_UPDATE_TRIES

protected static final int MAX_UPDATE_TRIES
See Also:
Constant Field Values
Constructor Detail

StatRepository

@Inject
public StatRepository(com.samskivert.depot.PersistenceContext context)
Constructs a new statistics repository with the specified persistence context.

Method Detail

updateStat

public <T extends Stat> T updateStat(int playerId,
                                     StatModifier<T> modifier)
Applies a modification to a single stat. If the stat in question does not exist, a blank instance will be created via Stat.Type.newStat().

Returns:
the modified Stat, if any modification took place; or null if the modification had no effect on the stat's data.

loadStats

public ArrayList<Stat> loadStats(int playerId)
Loads the stats associated with the specified player.


deleteStats

public void deleteStats(int playerId)
Deletes all stats associated with the specified player.


writeModified

public void writeModified(int playerId,
                          Stat[] stats)
Writes out any of the stats in the supplied array that have been modified since they were first loaded. Exceptions that occur while writing the stats will be caught and logged.


writeModified

public void writeModified(int playerId,
                          Iterable<Stat> stats)
Writes out any of the stats in the supplied iterable that have been modified since they were first loaded. Exceptions that occur while writing the stats will be caught and logged.


getStringCode

public int getStringCode(Stat.Type type,
                         String value)
Description copied from interface: Stat.AuxDataSource
Maps the specified string to a unique integer value.

Specified by:
getStringCode in interface Stat.AuxDataSource

getCodeString

public String getCodeString(Stat.Type type,
                            int code)
Description copied from interface: Stat.AuxDataSource
Maps the specified unique code back to its string value.

Specified by:
getCodeString in interface Stat.AuxDataSource

clearMapping

public void clearMapping(Stat.Type type,
                         String value)
This is only used for testing. Do not call this method.


purgePlayers

public void purgePlayers(Collection<Integer> playerIds)
Deletes all data associated with the supplied players.


decodeStat

protected Stat decodeStat(int statCode,
                          byte[] data,
                          byte modCount)
Instantiates the appropriate stat class and decodes the stat from the data.


decodeStat

protected Stat decodeStat(Stat stat,
                          byte[] data,
                          byte modCount)
Instantiates the appropriate stat class and decodes the stat from the data.


updateStat

protected boolean updateStat(int playerId,
                             Stat stat,
                             boolean forceWrite)
Updates the specified stat in the database, inserting it if necessary.

Returns:
true if the update was successful, false if it failed due to the stat being simultaneously modified by another database client.

assignStringCode

protected Integer assignStringCode(Stat.Type type,
                                   String value)
Helper function for getStringCode(com.threerings.stats.data.Stat.Type, java.lang.String).


loadStringCodes

protected void loadStringCodes(Stat.Type type)
Helper function used at repository startup.


mapStringCode

protected void mapStringCode(Stat.Type type,
                             String value,
                             int code)
Helper function used at repository startup.


init

protected void init()
Overrides:
init in class com.samskivert.depot.DepotRepository

getManagedRecords

protected void getManagedRecords(Set<Class<? extends com.samskivert.depot.PersistentRecord>> classes)
Specified by:
getManagedRecords in class com.samskivert.depot.DepotRepository