Add an index, document index use for Depot operations. Do you think this is worthwhile? We should really just about never see a non-indexed query during normal production use and one way to maintain that discipline is with a comment line like this.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@399 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Par Winzell
2007-08-07 18:05:44 +00:00
parent 96b42efbae
commit e386d6c3a0
5 changed files with 27 additions and 12 deletions
@@ -45,6 +45,7 @@ public class GameCookieRepository extends DepotRepository
public byte[] getCookie (int gameId, int userId) public byte[] getCookie (int gameId, int userId)
throws PersistenceException throws PersistenceException
{ {
// INDEX: Primary key.
GameCookieRecord record = load( GameCookieRecord record = load(
GameCookieRecord.class, GameCookieRecord.getKey(gameId, userId)); GameCookieRecord.class, GameCookieRecord.getKey(gameId, userId));
return record != null ? record.cookie : null; return record != null ? record.cookie : null;
@@ -58,8 +59,10 @@ public class GameCookieRepository extends DepotRepository
throws PersistenceException throws PersistenceException
{ {
if (cookie != null) { if (cookie != null) {
// INDEX: Primary key.
store(new GameCookieRecord(gameId, userId, cookie)); store(new GameCookieRecord(gameId, userId, cookie));
} else { } else {
// INDEX: Primary key.
delete(GameCookieRecord.class, GameCookieRecord.getKey(gameId, userId)); delete(GameCookieRecord.class, GameCookieRecord.getKey(gameId, userId));
} }
} }
@@ -25,9 +25,12 @@ import com.samskivert.jdbc.depot.Key;
import com.samskivert.jdbc.depot.PersistentRecord; import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.depot.annotation.Entity; import com.samskivert.jdbc.depot.annotation.Entity;
import com.samskivert.jdbc.depot.annotation.Id; import com.samskivert.jdbc.depot.annotation.Id;
import com.samskivert.jdbc.depot.annotation.Index;
import com.samskivert.jdbc.depot.expression.ColumnExp; import com.samskivert.jdbc.depot.expression.ColumnExp;
@Entity @Entity(indices={
@Index(name="ixPlayerId", columns={ RatingRecord.PLAYER_ID })
})
public class RatingRecord extends PersistentRecord public class RatingRecord extends PersistentRecord
{ {
// AUTO-GENERATED: FIELDS START // AUTO-GENERATED: FIELDS START
@@ -60,19 +63,19 @@ public class RatingRecord extends PersistentRecord
new ColumnExp(RatingRecord.class, EXPERIENCE); new ColumnExp(RatingRecord.class, EXPERIENCE);
// AUTO-GENERATED: FIELDS END // AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 1; public static final int SCHEMA_VERSION = 2;
/** The identifier of the game we're rating for. */ /** The identifier of the game we're rating for. */
@Id @Id
public int gameId; public int gameId;
/** The identifier of the player we're rating. */ /** The identifier of the player we're rating. */
@Id @Id
public int playerId; public int playerId;
/** The player's current rating. */ /** The player's current rating. */
public int rating; public int rating;
/** The number of times the player has played this game. */ /** The number of times the player has played this game. */
public int experience; public int experience;
@@ -83,14 +86,14 @@ public class RatingRecord extends PersistentRecord
{ {
super(); super();
} }
/** /**
* A constructor that populates all our fields. * A constructor that populates all our fields.
*/ */
public RatingRecord (int gameId, int playerId, int rating, int experience) public RatingRecord (int gameId, int playerId, int rating, int experience)
{ {
super(); super();
this.gameId = gameId; this.gameId = gameId;
this.playerId = playerId; this.playerId = playerId;
this.rating = rating; this.rating = rating;
@@ -54,15 +54,16 @@ public class RatingRepository extends DepotRepository
public RatingRecord getRating (int gameId, int playerId) public RatingRecord getRating (int gameId, int playerId)
throws PersistenceException throws PersistenceException
{ {
return load(RatingRecord.class, // INDEX: Full primary key.
RatingRecord.GAME_ID, gameId, return load(RatingRecord.class, RatingRecord.getKey(gameId, playerId));
RatingRecord.PLAYER_ID, playerId);
} }
/** /**
* Fetch the ratings registered for any of the given players for the given game and return * Fetch the ratings registered for any of the given players for the given game and return
* them as a list of {@link RatingRecord} objects. The size of this list is no less than zero * them as a list of {@link RatingRecord} objects. The size of this list is no less than zero
* and no greater than the number of given players. * and no greater than the number of given players.
*
* INDEXING: Uses primary key.
*/ */
public List<RatingRecord> getRatings (int gameId, Integer... players) public List<RatingRecord> getRatings (int gameId, Integer... players)
throws PersistenceException throws PersistenceException
@@ -70,17 +71,19 @@ public class RatingRepository extends DepotRepository
if (players.length == 0) { if (players.length == 0) {
return Collections.emptyList(); return Collections.emptyList();
} }
// INDEX: Full primary key.
return findAll(RatingRecord.class, return findAll(RatingRecord.class,
new Where(new And(new Equals(RatingRecord.GAME_ID_C, gameId), new Where(new And(new Equals(RatingRecord.GAME_ID_C, gameId),
new In(RatingRecord.PLAYER_ID_C, players)))); new In(RatingRecord.PLAYER_ID_C, players))));
} }
/** /**
* 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.
*/ */
public List<RatingRecord> getRatings (int playerId) public List<RatingRecord> getRatings (int playerId)
throws PersistenceException throws PersistenceException
{ {
// INDEX: ixPlayerId
return findAll(RatingRecord.class, new Where(RatingRecord.PLAYER_ID_C, playerId)); return findAll(RatingRecord.class, new Where(RatingRecord.PLAYER_ID_C, playerId));
} }
@@ -65,7 +65,7 @@ public class StatRecord extends PersistentRecord
@Id @Id
@Column(name="STAT_CODE") @Column(name="STAT_CODE")
public int statCode; public int statCode;
/** The data associated with the stat. */ /** The data associated with the stat. */
@Column(name="STAT_DATA", length=65535) @Column(name="STAT_DATA", length=65535)
public byte[] statData; public byte[] statData;
@@ -55,11 +55,13 @@ public class StatRepository extends DepotRepository
/** /**
* Loads the stats associated with the specified player. * Loads the stats associated with the specified player.
*
*/ */
public ArrayList<Stat> loadStats (int playerId) public ArrayList<Stat> loadStats (int playerId)
throws PersistenceException throws PersistenceException
{ {
ArrayList<Stat> stats = new ArrayList<Stat>(); ArrayList<Stat> stats = new ArrayList<Stat>();
// INDEX: Left-most primary key field;
Where where = new Where(StatRecord.PLAYER_ID_C, playerId); Where where = new Where(StatRecord.PLAYER_ID_C, playerId);
for (StatRecord record : findAll(StatRecord.class, where)) { for (StatRecord record : findAll(StatRecord.class, where)) {
Stat stat = decodeStat(record.statCode, record.statData); Stat stat = decodeStat(record.statCode, record.statData);
@@ -86,6 +88,7 @@ public class StatRepository extends DepotRepository
}); });
} }
}; };
// INDEX: Primary key.
deleteAll(StatRecord.class, new Where(StatRecord.PLAYER_ID_C, playerId), invalidator); deleteAll(StatRecord.class, new Where(StatRecord.PLAYER_ID_C, playerId), invalidator);
} }
@@ -225,6 +228,7 @@ public class StatRepository extends DepotRepository
throws PersistenceException throws PersistenceException
{ {
for (int ii = 0; ii < 10; ii++) { for (int ii = 0; ii < 10; ii++) {
// INDEX: Left-most primary key field.
MaxStatCodeRecord maxRecord = load( MaxStatCodeRecord maxRecord = load(
MaxStatCodeRecord.class, MaxStatCodeRecord.class,
new FromOverride(StringCodeRecord.class), new FromOverride(StringCodeRecord.class),
@@ -253,6 +257,7 @@ public class StatRepository extends DepotRepository
// if it is a duplicate row exception, possibly someone inserted our value // if it is a duplicate row exception, possibly someone inserted our value
// before we could, in which case we can just look up the new mapping // before we could, in which case we can just look up the new mapping
// INDEX: Full primary key.
StringCodeRecord record = load( StringCodeRecord record = load(
StringCodeRecord.class, StringCodeRecord.getKey(type.code(), value)); StringCodeRecord.class, StringCodeRecord.getKey(type.code(), value));
if (record != null) { if (record != null) {
@@ -277,6 +282,7 @@ public class StatRepository extends DepotRepository
{ {
QueryClause[] clauses; QueryClause[] clauses;
if (type != null) { if (type != null) {
// INDEX: Left-most primary key field.
clauses = new QueryClause[] { new Where(StringCodeRecord.STAT_CODE_C, type.code()) }; clauses = new QueryClause[] { new Where(StringCodeRecord.STAT_CODE_C, type.code()) };
} else { } else {
clauses = new QueryClause[0]; clauses = new QueryClause[0];