Track the last update time for ratings.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@439 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2007-09-25 16:32:59 +00:00
parent 3eff6e03cd
commit 52259f26dd
2 changed files with 42 additions and 4 deletions
@@ -21,8 +21,11 @@
package com.threerings.parlor.rating.server.persist;
import java.sql.Timestamp;
import com.samskivert.jdbc.depot.Key;
import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.depot.annotation.Column;
import com.samskivert.jdbc.depot.annotation.Entity;
import com.samskivert.jdbc.depot.annotation.Id;
import com.samskivert.jdbc.depot.annotation.Index;
@@ -63,9 +66,16 @@ public class RatingRecord extends PersistentRecord
/** The qualified column identifier for the {@link #experience} field. */
public static final ColumnExp EXPERIENCE_C =
new ColumnExp(RatingRecord.class, EXPERIENCE);
/** The column identifier for the {@link #lastUpdated} field. */
public static final String LAST_UPDATED = "lastUpdated";
/** The qualified column identifier for the {@link #lastUpdated} field. */
public static final ColumnExp LAST_UPDATED_C =
new ColumnExp(RatingRecord.class, LAST_UPDATED);
// AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 2;
public static final int SCHEMA_VERSION = 3;
/** The identifier of the game we're rating for. */
@Id
@@ -81,6 +91,10 @@ public class RatingRecord extends PersistentRecord
/** The number of times the player has played this game. */
public int experience;
/** The last time this rating was updated. */
@Column(type="TIMESTAMP", defaultValue="CURRENT_TIMESTAMP")
public Timestamp lastUpdated;
/**
* An empty constructor for unmarshalling.
*/
@@ -100,6 +114,7 @@ public class RatingRecord extends PersistentRecord
this.playerId = playerId;
this.rating = rating;
this.experience = experience;
this.lastUpdated = new Timestamp(System.currentTimeMillis());
}
@Override // from Object
@@ -21,6 +21,9 @@
package com.threerings.parlor.rating.server.persist;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@@ -33,6 +36,9 @@ import com.samskivert.jdbc.depot.DepotRepository;
import com.samskivert.jdbc.depot.EntityMigration;
import com.samskivert.jdbc.depot.PersistenceContext;
import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.depot.clause.Limit;
import com.samskivert.jdbc.depot.clause.OrderBy;
import com.samskivert.jdbc.depot.clause.QueryClause;
import com.samskivert.jdbc.depot.clause.Where;
import com.samskivert.jdbc.depot.operator.Conditionals.*;
import com.samskivert.jdbc.depot.operator.Logic.And;
@@ -84,12 +90,29 @@ public class RatingRepository extends DepotRepository
}
/**
* Fetch and return all the registered {@link RatingRecord} rows for the given player.
* Fetch and return all the registered {@link RatingRecord} rows for the given player. Ratings
* will be returned in order of most recently to least recently updated.
*
* @param since ratings last updated more than this number of milliseconds in the past will be
* ommitted from the result list. Supplying -1 will return all ratings regardless of age.
* @param count the maximum number of ratings to return or -1 for all ratings.
*/
public List<RatingRecord> getRatings (int playerId)
public List<RatingRecord> getRatings (int playerId, long since, int count)
throws PersistenceException
{
return findAll(RatingRecord.class, new Where(RatingRecord.PLAYER_ID_C, playerId));
ArrayList<QueryClause> clauses = new ArrayList<QueryClause>();
if (since > 0L) {
Timestamp when = new Timestamp(System.currentTimeMillis() - since);
clauses.add(new Where(new And(new Equals(RatingRecord.PLAYER_ID_C, playerId),
new GreaterThan(RatingRecord.LAST_UPDATED_C, when))));
} else {
clauses.add(new Where(RatingRecord.PLAYER_ID_C, playerId));
}
if (count > 0) {
clauses.add(new Limit(0, count));
}
clauses.add(OrderBy.descending(RatingRecord.LAST_UPDATED_C));
return findAll(RatingRecord.class, clauses.toArray(new QueryClause[clauses.size()]));
}
/**