Depot implementation of the Game Cookie persistence - hopefully schema compatible with the old version.

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@381 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Par Winzell
2007-07-26 20:20:07 +00:00
parent 2885a2b41c
commit 7dea421cca
2 changed files with 120 additions and 77 deletions
@@ -0,0 +1,100 @@
//
// $Id: GameCookieRepository.java 209 2007-02-24 00:37:33Z mdb $
//
// 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.ezgame.server.persist;
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.expression.ColumnExp;
@Entity(name="GAME_COOKIES")
public class GameCookieRecord 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(GameCookieRecord.class, GAME_ID);
/** The column identifier for the {@link #userId} field. */
public static final String USER_ID = "userId";
/** The qualified column identifier for the {@link #userId} field. */
public static final ColumnExp USER_ID_C =
new ColumnExp(GameCookieRecord.class, USER_ID);
/** The column identifier for the {@link #cookie} field. */
public static final String COOKIE = "cookie";
/** The qualified column identifier for the {@link #cookie} field. */
public static final ColumnExp COOKIE_C =
new ColumnExp(GameCookieRecord.class, COOKIE);
// AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 1;
/** The id of the game for which this is a cookie. */
@Id
@Column(name="GAME_ID")
public int gameId;
/** The id of the user for which this is a cookie. */
@Id
@Column(name="USER_ID")
public int userId;
/** The actual cookie, as a byte array. */
@Column(name="COOKIE")
public byte[] cookie;
/** A no-argument constructor for deserialization. */
public GameCookieRecord ()
{
}
/** A constructor for configuring all the fields of this record. */
public GameCookieRecord (int gameId, int userId, byte[] cookie)
{
super();
this.gameId = gameId;
this.userId = userId;
this.cookie = cookie;
}
// AUTO-GENERATED: METHODS START
/**
* Create and return a primary {@link Key} to identify a {@link #GameCookieRecord}
* with the supplied key values.
*/
public static Key<GameCookieRecord> getKey (int gameId, int userId)
{
return new Key<GameCookieRecord>(
GameCookieRecord.class,
new String[] { GAME_ID, USER_ID },
new Comparable[] { gameId, userId });
}
// AUTO-GENERATED: METHODS END
}
@@ -21,26 +21,18 @@
package com.threerings.ezgame.server.persist;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.jdbc.JDBCUtil;
import com.samskivert.jdbc.JORARepository;
import com.samskivert.jdbc.SimpleRepository;
import com.samskivert.jdbc.TransitionRepository;
import com.samskivert.jdbc.depot.DepotRepository;
import com.samskivert.jdbc.depot.PersistenceContext;
import com.samskivert.jdbc.depot.PersistentRecord;
/**
* Provides storage services for user cookies used in games.
*/
public class GameCookieRepository extends SimpleRepository
public class GameCookieRepository extends DepotRepository
{
/** The database identifier used when establishing a connection. */
public static final String COOKIE_DB_IDENT = "gameCookiedb";
@@ -48,48 +40,18 @@ public class GameCookieRepository extends SimpleRepository
public GameCookieRepository (ConnectionProvider conprov)
throws PersistenceException
{
super(conprov, COOKIE_DB_IDENT);
maintenance("analyze", "GAME_COOKIES");
}
@Override
protected void migrateSchema (Connection conn, DatabaseLiaison liaison)
throws SQLException, PersistenceException
{
super.migrateSchema(conn, liaison);
JDBCUtil.createTableIfMissing(conn, "GAME_COOKIES", new String[] {
"GAME_ID integer not null",
"USER_ID integer not null",
"COOKIE blob not null",
"primary key (GAME_ID, USER_ID)" }, "");
super(new PersistenceContext(COOKIE_DB_IDENT, conprov));
}
/**
* Get the specified game cookie, or null if none.
*/
public byte[] getCookie (final int gameId, final int userId)
public byte[] getCookie (int gameId, int userId)
throws PersistenceException
{
return execute(new Operation<byte[]>() {
public byte[] invoke (Connection conn, DatabaseLiaison liaison)
throws SQLException, PersistenceException
{
Statement stmt = conn.createStatement();
try {
ResultSet rs = stmt.executeQuery("select COOKIE " +
"from GAME_COOKIES where GAME_ID=" + gameId +
" and USER_ID=" + userId);
if (rs.next()) {
return rs.getBytes(1);
}
return null;
} finally {
JDBCUtil.close(stmt);
}
}
});
GameCookieRecord record = load(
GameCookieRecord.class, GameCookieRecord.getKey(gameId, userId));
return record != null ? record.cookie : null;
}
/**
@@ -99,35 +61,16 @@ public class GameCookieRepository extends SimpleRepository
final int gameId, final int userId, final byte[] cookie)
throws PersistenceException
{
executeUpdate(new Operation<Void>() {
public Void invoke (Connection conn, DatabaseLiaison liaison)
throws SQLException, PersistenceException
{
if (cookie == null) {
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate("delete from GAME_COOKIES" +
" where GAME_ID=" + gameId +
" and USER_ID=" + userId);
return null;
} finally {
JDBCUtil.close(stmt);
if (cookie != null) {
store(new GameCookieRecord(gameId, userId, cookie));
} else {
delete(GameCookieRecord.class, GameCookieRecord.getKey(gameId, userId));
}
}
} else {
PreparedStatement stmt = conn.prepareStatement(
"insert into GAME_COOKIES (GAME_ID, USER_ID, COOKIE) " +
"values (" + gameId + "," + userId + ",?) " +
"on duplicate key update COOKIE=values(COOKIE)");
try {
stmt.setBytes(1, cookie);
stmt.executeUpdate();
return null;
} finally {
JDBCUtil.close(stmt);
}
}
}
});
@Override // from DepotRepository
protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes)
{
classes.add(GameCookieRecord.class);
}
}