Allow scene updates to increment the version number by more than 1.
git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@572 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -33,20 +33,18 @@ import com.threerings.util.ActionScript;
|
|||||||
import com.threerings.whirled.Log;
|
import com.threerings.whirled.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to encapsulate updates to scenes in such a manner that updates can
|
* Used to encapsulate updates to scenes in such a manner that updates can be stored persistently
|
||||||
* be stored persistently and sent to clients to update their own local
|
* and sent to clients to update their own local copies of scenes.
|
||||||
* copies of scenes.
|
|
||||||
*/
|
*/
|
||||||
public class SceneUpdate
|
public class SceneUpdate
|
||||||
implements Streamable, Cloneable
|
implements Streamable, Cloneable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Initializes this scene update such that it will operate on a scene
|
* Initializes this scene update such that it will operate on a scene with the specified target
|
||||||
* with the specified target scene and version number.
|
* scene and version number.
|
||||||
*
|
*
|
||||||
* @param targetId the id of the scene on which we are to operate.
|
* @param targetId the id of the scene on which we are to operate.
|
||||||
* @param targetVersion the version of the scene on which we are to
|
* @param targetVersion the version of the scene on which we are to operate.
|
||||||
* operate.
|
|
||||||
*/
|
*/
|
||||||
public void init (int targetId, int targetVersion)
|
public void init (int targetId, int targetVersion)
|
||||||
{
|
{
|
||||||
@@ -71,43 +69,48 @@ public class SceneUpdate
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called to ensure that the scene is in the appropriate state prior
|
* Returns the amount by which this update increments the scene version.
|
||||||
* to applying the update.
|
*/
|
||||||
|
public int getVersionIncrement ()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called to ensure that the scene is in the appropriate state prior to applying the update.
|
||||||
*
|
*
|
||||||
* @exception IllegalStateException thrown if the update cannot be
|
* @exception IllegalStateException thrown if the update cannot be applied to the scene because
|
||||||
* applied to the scene because it is not in a valid state
|
* it is not in a valid state (appropriate previous updates were not applied, it's the wrong
|
||||||
* (appropriate previous updates were not applied, it's the wrong kind
|
* kind of scene, etc.).
|
||||||
* of scene, etc.).
|
|
||||||
*/
|
*/
|
||||||
public void validate (SceneModel model)
|
public void validate (SceneModel model)
|
||||||
throws IllegalStateException
|
throws IllegalStateException
|
||||||
{
|
{
|
||||||
if (model.sceneId != _targetId) {
|
if (model.sceneId != _targetId) {
|
||||||
String errmsg = "Wrong target scene, expected id " +
|
String errmsg = "Wrong target scene, expected id " + _targetId +
|
||||||
_targetId + " got id " + model.sceneId;
|
" got id " + model.sceneId;
|
||||||
throw new IllegalStateException(errmsg);
|
throw new IllegalStateException(errmsg);
|
||||||
}
|
}
|
||||||
if (model.version != _targetVersion) {
|
if (model.version != _targetVersion) {
|
||||||
String errmsg = "Target scene not proper version, expected " +
|
String errmsg = "Target scene not proper version, expected " + _targetVersion +
|
||||||
_targetVersion + " got " + model.version;
|
" got " + model.version;
|
||||||
throw new IllegalStateException(errmsg);
|
throw new IllegalStateException(errmsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies this update to the specified scene model. Derived classes
|
* Applies this update to the specified scene model. Derived classes will want to override this
|
||||||
* will want to override this method and apply updates of their own,
|
* method and apply updates of their own, being sure to call <code>super.apply</code>.
|
||||||
* being sure to call <code>super.apply</code>.
|
|
||||||
*/
|
*/
|
||||||
public void apply (SceneModel model)
|
public void apply (SceneModel model)
|
||||||
{
|
{
|
||||||
// increment the version; disallowing integer overflow
|
// increment the version; disallowing integer overflow
|
||||||
model.version = Math.max(_targetVersion + 1, model.version);
|
model.version = Math.max(_targetVersion + getVersionIncrement(), model.version);
|
||||||
|
|
||||||
// sanity check for the amazing two billion updates
|
// sanity check for the amazing two billion updates
|
||||||
if (model.version == _targetVersion) {
|
if (model.version == _targetVersion) {
|
||||||
Log.warning("Egads! This scene has been updated two billion " +
|
Log.warning("Egads! This scene has been updated two billion times [model=" + model +
|
||||||
"times [model=" + model + ", update=" + this + "].");
|
", update=" + this + "].");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,10 +141,9 @@ public class SceneUpdate
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serializes the bare representation of this instance without the scene id
|
* Serializes the bare representation of this instance without the scene id and version fields.
|
||||||
* and version fields. Useful when storing updates in a database where the
|
* Useful when storing updates in a database where the scene id and version fields are stored
|
||||||
* scene id and version fields are stored in separate columns and the rest
|
* in separate columns and the rest if the representation is contained in an opaque blob.
|
||||||
* if the representation is contained in an opaque blob.
|
|
||||||
*/
|
*/
|
||||||
@ActionScript(omit=true)
|
@ActionScript(omit=true)
|
||||||
public void persistTo (ObjectOutputStream out)
|
public void persistTo (ObjectOutputStream out)
|
||||||
@@ -156,8 +158,7 @@ public class SceneUpdate
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unserializes this instance from the bare representation created by
|
* Unserializes this instance from the bare representation created by {@link #persistTo}.
|
||||||
* {@link #persistTo}.
|
|
||||||
*/
|
*/
|
||||||
@ActionScript(omit=true)
|
@ActionScript(omit=true)
|
||||||
public void unpersistFrom (ObjectInputStream in)
|
public void unpersistFrom (ObjectInputStream in)
|
||||||
@@ -182,8 +183,7 @@ public class SceneUpdate
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An extensible mechanism for generating a string representation of
|
* An extensible mechanism for generating a string representation of this instance.
|
||||||
* this instance.
|
|
||||||
*/
|
*/
|
||||||
@ActionScript(name="toStringBuilder")
|
@ActionScript(name="toStringBuilder")
|
||||||
protected void toString (StringBuilder buf)
|
protected void toString (StringBuilder buf)
|
||||||
|
|||||||
@@ -149,8 +149,7 @@ public class SceneManager extends PlaceManager
|
|||||||
WhirledServer.invoker.postUnit(new Invoker.Unit() {
|
WhirledServer.invoker.postUnit(new Invoker.Unit() {
|
||||||
public boolean invoke () {
|
public boolean invoke () {
|
||||||
try {
|
try {
|
||||||
_screg.getSceneRepository().applyAndRecordUpdate(
|
_screg.getSceneRepository().applyAndRecordUpdate(_scene.getSceneModel(), update);
|
||||||
_scene.getSceneModel(), update);
|
|
||||||
} catch (PersistenceException pe) {
|
} catch (PersistenceException pe) {
|
||||||
Log.warning("Failed to apply scene update " + update + ".");
|
Log.warning("Failed to apply scene update " + update + ".");
|
||||||
Log.logStackTrace(pe);
|
Log.logStackTrace(pe);
|
||||||
|
|||||||
@@ -21,7 +21,9 @@
|
|||||||
|
|
||||||
package com.threerings.whirled.util;
|
package com.threerings.whirled.util;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import com.threerings.whirled.Log;
|
import com.threerings.whirled.Log;
|
||||||
import com.threerings.whirled.data.SceneUpdate;
|
import com.threerings.whirled.data.SceneUpdate;
|
||||||
@@ -31,77 +33,66 @@ import com.threerings.whirled.data.SceneUpdate;
|
|||||||
*/
|
*/
|
||||||
public class UpdateList
|
public class UpdateList
|
||||||
{
|
{
|
||||||
public UpdateList ()
|
|
||||||
{
|
|
||||||
_updates = new ArrayList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an update to this list. The update must follow appropriately
|
* Adds an update to this list. The update must follow appropriately the chain of updates
|
||||||
* the chain of updates established by the updates already in the list
|
* established by the updates already in the list (meaning it must operate on one version
|
||||||
* (meaning it must operate on one version higher than the most recent
|
* higher than the most recent update already in the list).
|
||||||
* update already in the list).
|
|
||||||
*/
|
*/
|
||||||
public void addUpdate (SceneUpdate update)
|
public void addUpdate (SceneUpdate update)
|
||||||
{
|
{
|
||||||
if (_minVersion == -1) {
|
// if this is our first update, great, we let it in with no questions asked
|
||||||
// this is our first update, so we initialize our min version
|
if (_updates.isEmpty()) {
|
||||||
_minVersion = update.getSceneVersion();
|
_updates.add(update);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
// otherwise make sure this update conforms to our update sequence
|
||||||
|
SceneUpdate last = _updates.get(_updates.size()-1);
|
||||||
|
int expVersion = last.getSceneVersion() + last.getVersionIncrement();
|
||||||
int gotVersion = update.getSceneVersion();
|
int gotVersion = update.getSceneVersion();
|
||||||
int expVersion = _minVersion + _updates.size();
|
|
||||||
if (gotVersion > expVersion) {
|
if (gotVersion > expVersion) {
|
||||||
Log.warning("Update continuity broken, flushing list " +
|
Log.warning("Update continuity broken, flushing list [got=" + update +
|
||||||
"[got=" + update + ", expect=" + expVersion +
|
", expect=" + expVersion + ", ucount=" + _updates.size() + "].");
|
||||||
", ucount=" + _updates.size() + "].");
|
_updates.clear(); // flush out our old updates, fall through and add this one
|
||||||
// flush out our old updates and start anew from here
|
|
||||||
_updates.clear();
|
|
||||||
_minVersion = expVersion;
|
|
||||||
|
|
||||||
} else if (gotVersion < expVersion) {
|
} else if (gotVersion < expVersion) {
|
||||||
// we somehow got an update that's older than updates we
|
// we somehow got an update that's older than updates we already have?
|
||||||
// already have? wick wick wack
|
String errmsg = "Invalid update version [want=" + expVersion + ", got=" + update + "]";
|
||||||
String errmsg = "Invalid update version " +
|
|
||||||
"[want=" + expVersion + ", got=" + update + "]";
|
|
||||||
throw new IllegalArgumentException(errmsg);
|
throw new IllegalArgumentException(errmsg);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
_updates.add(update);
|
_updates.add(update);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all of the updates that should be applied to a scene with
|
* Returns all of the updates that should be applied to a scene with the specified version to
|
||||||
* the specified version to bring it up to date. <code>null</code> is
|
* bring it up to date. <code>null</code> is returned if the scene's version is older than the
|
||||||
* returned if the scene's version is older than the oldest update in
|
* oldest update in our list, in which case it cannot be brought up to date by applying updates
|
||||||
* our list, in which case it cannot be brought up to date by applying
|
* from this list.
|
||||||
* updates from this list.
|
|
||||||
*/
|
*/
|
||||||
public SceneUpdate[] getUpdates (int fromVersion)
|
public SceneUpdate[] getUpdates (int fromVersion)
|
||||||
{
|
{
|
||||||
if (_minVersion == -1 || fromVersion < _minVersion) {
|
List<SceneUpdate> updates = Lists.newArrayList();
|
||||||
return null;
|
for (SceneUpdate update : _updates) {
|
||||||
|
if (update.getSceneVersion() >= fromVersion) {
|
||||||
|
updates.add(update);
|
||||||
}
|
}
|
||||||
|
|
||||||
int offset = fromVersion - _minVersion;
|
|
||||||
int ucount = _updates.size() - offset;
|
|
||||||
SceneUpdate[] updates = new SceneUpdate[ucount];
|
|
||||||
for (int ii = 0; ii < ucount; ii++) {
|
|
||||||
updates[ii] = (SceneUpdate)_updates.get(ii+offset);
|
|
||||||
}
|
}
|
||||||
return updates;
|
return updates.toArray(new SceneUpdate[updates.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the supplied actual scene version is in accordance
|
* Returns true if the supplied actual scene version is in accordance with the updates
|
||||||
* with the updates contained in this list.
|
* contained in this list.
|
||||||
*/
|
*/
|
||||||
public boolean validate (int sceneVersion)
|
public boolean validate (int sceneVersion)
|
||||||
{
|
{
|
||||||
return ((_minVersion == -1) || // we have no updates
|
if (_updates.size() == 0) {
|
||||||
(_minVersion + _updates.size() == sceneVersion));
|
return true;
|
||||||
|
}
|
||||||
|
SceneUpdate last = _updates.get(_updates.size()-1);
|
||||||
|
return sceneVersion == (last.getSceneVersion() + last.getVersionIncrement());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ArrayList _updates;
|
protected List<SceneUpdate> _updates = Lists.newArrayList();
|
||||||
protected int _minVersion = -1;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user