No need to use 'this.' to call methods in the same object. Converted
ByteArrayOutputStream to a byte[] once and use that in the three places it's needed. Some other small formatting tweaks. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@665 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -63,8 +63,8 @@ public class StatRepository extends DepotRepository
|
|||||||
{
|
{
|
||||||
Where where = new Where(StatRecord.PLAYER_ID_C, playerId, StatRecord.STAT_CODE_C, statCode);
|
Where where = new Where(StatRecord.PLAYER_ID_C, playerId, StatRecord.STAT_CODE_C, statCode);
|
||||||
StatRecord record = load(StatRecord.class, where);
|
StatRecord record = load(StatRecord.class, where);
|
||||||
return (null != record ? decodeStat(record.statCode, record.statData, record.modCount)
|
return (null == record) ? null :
|
||||||
: null);
|
decodeStat(record.statCode, record.statData, record.modCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,7 +78,7 @@ public class StatRepository extends DepotRepository
|
|||||||
public boolean updateStatIfCurrent (int playerId, Stat stat)
|
public boolean updateStatIfCurrent (int playerId, Stat stat)
|
||||||
throws PersistenceException
|
throws PersistenceException
|
||||||
{
|
{
|
||||||
return this.updateStat(playerId, stat, true);
|
return updateStat(playerId, stat, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,20 +245,19 @@ public class StatRepository extends DepotRepository
|
|||||||
try {
|
try {
|
||||||
stat.persistTo(new ObjectOutputStream(out), this);
|
stat.persistTo(new ObjectOutputStream(out), this);
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
String errmsg = "Error serializing stat " + stat;
|
throw new PersistenceException("Error serializing stat " + stat, ioe);
|
||||||
throw new PersistenceException(errmsg, ioe);
|
|
||||||
}
|
}
|
||||||
|
byte[] data = out.toByteArray();
|
||||||
byte nextModCount = (byte)((stat.getModCount() + 1) % Byte.MAX_VALUE);
|
byte nextModCount = (byte)((stat.getModCount() + 1) % Byte.MAX_VALUE);
|
||||||
|
|
||||||
// update the row in the database only if it has the expected modCount
|
// update the row in the database only if it has the expected modCount
|
||||||
int numRows = updatePartial(
|
int numRows = updatePartial(
|
||||||
StatRecord.class,
|
StatRecord.class,
|
||||||
new Where(StatRecord.PLAYER_ID_C, playerId,
|
new Where(StatRecord.PLAYER_ID_C, playerId,
|
||||||
StatRecord.STAT_CODE_C, stat.getCode(),
|
StatRecord.STAT_CODE_C, stat.getCode(),
|
||||||
StatRecord.MOD_COUNT_C, stat.getModCount()),
|
StatRecord.MOD_COUNT_C, stat.getModCount()),
|
||||||
StatRecord.getKey(playerId, stat.getCode()),
|
StatRecord.getKey(playerId, stat.getCode()),
|
||||||
StatRecord.STAT_DATA, out.toByteArray(), StatRecord.MOD_COUNT, nextModCount);
|
StatRecord.STAT_DATA, data, StatRecord.MOD_COUNT, nextModCount);
|
||||||
|
|
||||||
if (numRows == 0) {
|
if (numRows == 0) {
|
||||||
// If we failed to update any rows, it could be because we saw an unexpected modCount,
|
// If we failed to update any rows, it could be because we saw an unexpected modCount,
|
||||||
@@ -266,8 +265,7 @@ public class StatRepository extends DepotRepository
|
|||||||
// try to create it.
|
// try to create it.
|
||||||
if (loadStat(playerId, stat.getCode()) == null) {
|
if (loadStat(playerId, stat.getCode()) == null) {
|
||||||
try {
|
try {
|
||||||
insert(new StatRecord(playerId, stat.getCode(), out.toByteArray(),
|
insert(new StatRecord(playerId, stat.getCode(), data, nextModCount));
|
||||||
nextModCount));
|
|
||||||
numRows = 1;
|
numRows = 1;
|
||||||
} catch (DuplicateKeyException e) {
|
} catch (DuplicateKeyException e) {
|
||||||
// somebody else inserted the StatRecord before we were able to.
|
// somebody else inserted the StatRecord before we were able to.
|
||||||
@@ -277,9 +275,9 @@ public class StatRepository extends DepotRepository
|
|||||||
|
|
||||||
if (numRows == 0 && !failIfUncurrent) {
|
if (numRows == 0 && !failIfUncurrent) {
|
||||||
// give up and store the stat anyway
|
// give up and store the stat anyway
|
||||||
log.warning("Possible collision while storing StatRecord (playerId: " +
|
log.warning("Possible collision while storing StatRecord",
|
||||||
playerId + " stat: " + stat.getType().name() + ")");
|
"playerId", playerId, "stat", stat.getType().name());
|
||||||
store(new StatRecord(playerId, stat.getCode(), out.toByteArray(), nextModCount));
|
store(new StatRecord(playerId, stat.getCode(), data, nextModCount));
|
||||||
numRows = 1;
|
numRows = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user