From 52259f26dde1bae821b1d55b0ee1c0dd14c31c82 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 25 Sep 2007 16:32:59 +0000 Subject: [PATCH] Track the last update time for ratings. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@439 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../rating/server/persist/RatingRecord.java | 17 ++++++++++- .../server/persist/RatingRepository.java | 29 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/java/com/threerings/parlor/rating/server/persist/RatingRecord.java b/src/java/com/threerings/parlor/rating/server/persist/RatingRecord.java index f1484185..51c2b785 100644 --- a/src/java/com/threerings/parlor/rating/server/persist/RatingRecord.java +++ b/src/java/com/threerings/parlor/rating/server/persist/RatingRecord.java @@ -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 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 7688f873..83cf5db9 100644 --- a/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java +++ b/src/java/com/threerings/parlor/rating/server/persist/RatingRepository.java @@ -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 getRatings (int playerId) + public List getRatings (int playerId, long since, int count) throws PersistenceException { - return findAll(RatingRecord.class, new Where(RatingRecord.PLAYER_ID_C, playerId)); + ArrayList clauses = new ArrayList(); + 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()])); } /**