Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,24 @@ public void testTransformStringDiff()

}

public void testInvalidStringTransformThrowsException()
throws Exception {
String origin = "Line 1\nLine 2\nReplace me";
try
{
JSONObject transformed = JSONDiff.transform(
"=14\t+Before%0A\t=10\t+%0AAfter",
"=14\t-10\t+BYE",
origin
);
fail("Patch transform should have failed.");
}
catch(Exception e)
{
// Test passed
}
}

public void testTransformObjectDiffChangeRemovedKey()
throws Exception {

Expand Down
14 changes: 8 additions & 6 deletions Simperium/src/main/java/com/simperium/client/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ public Ghost applyRemoteChange(RemoteChange change)
try {
T object;
Boolean isNew;
Boolean shouldUpdateObject = true;

if (change.isAddOperation()) {
object = newObject(change.getKey());
Expand Down Expand Up @@ -962,7 +963,6 @@ public Ghost applyRemoteChange(RemoteChange change)
mSchema.updateWithDefaults(object, updatedProperties);
addObject(object);
} else {

if (localModifications != null && localModifications.length() > 0) {
try {
JSONObject incomingDiff = change.getPatch();
Expand All @@ -971,15 +971,17 @@ public Ghost applyRemoteChange(RemoteChange change)
JSONObject transformedDiff = JSONDiff.transform(localDiff, incomingDiff, currentProperties);

updatedProperties = JSONDiff.apply(updatedProperties, transformedDiff);

} catch (JSONException | IllegalArgumentException e) {
// could not transform properties
// continue with updated properties
// We couldn't merge the local and remote changes.
// Hold off on updating the object so that the local change can sync
shouldUpdateObject = false;
}
}

mSchema.update(object, updatedProperties);
updateObject(object);
if (shouldUpdateObject) {
mSchema.update(object, updatedProperties);
updateObject(object);
}
}

} catch(SimperiumException e) {
Expand Down
10 changes: 9 additions & 1 deletion Simperium/src/main/java/com/simperium/util/JSONDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ public static JSONObject transform(String o_diff, String diff, String source)
LinkedList<Patch> patches = dmp.patch_make(source, dmp.diff_fromDelta(source, diff));

String text = (String) dmp.patch_apply(patches, source)[0];
String combined = (String) dmp.patch_apply(o_patches, text)[0];

Object[] appliedPatch = dmp.patch_apply(o_patches, text);
boolean[] results = (boolean[]) appliedPatch[1];
for (boolean result : results) {
if (!result) {
throw new JSONException("Could not cleanly transform patch.");
}
}

String combined = (String)appliedPatch[0];
if (text.equals(combined)) {
// text is the same, return empty diff
return transformed;
Expand Down