Skip to content

Commit

Permalink
Hash: Json.stringify() to string-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
caohanyang committed Mar 25, 2016
1 parent e5be710 commit 7d340a4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 120 deletions.
26 changes: 14 additions & 12 deletions JSON-Diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var lcs = require('./LCS.js');
var unchangedArea = require('./unchangedArea.js');
var patchArea = require('./patchArea.js');
var hashObject = require('./hashObject.js');
// var hash = require('string-hash');


exports.diff = diff;
exports.apply = apply;
Expand Down Expand Up @@ -51,18 +51,20 @@ function generateValueDiff(oldJson, newJson, unchanged, patches, path) {
// the endpoint
if (newJson !== oldJson) {
// console.log({ op: "replace", path: path, value: copy.clone(newJson)});
patches.push(hashObject.hash({ op: "replace", path: path, value: newJson}));
patches.push({ op: "replace", path: path, value: newJson});
}

}

function generateArrayDiff(oldJson, newJson, unchanged, patches, path) {
// console.log("--------This is Array-------------");
// x, y is the hash of json
var x = hashObject.map(hashObject.hash, oldJson);
var y = hashObject.map(hashObject.hash, newJson);
// Use LCS
var tmpPatches = [];
lcs.LCS(x, y, unchanged, tmpPatches, path);
var tmpPatchHashes = [];
lcs.LCS(x, y, oldJson, newJson, unchanged, tmpPatches, tmpPatchHashes, path);
for (var l = 0; l < tmpPatches.length; l++) {
patches.push(tmpPatches[l]);
}
Expand Down Expand Up @@ -96,7 +98,7 @@ function generateObjectDiff(oldJson, newJson, unchanged, patches, path) {
// Remove
// console.log({ op: "remove", path: path + "/" + patchPointString(oldKey), value: copy.clone(oldValue) });
removed = true;
patches.push(hashObject.hash({ op: "remove", path: path + "/" + patchPointString(oldKey), value: oldValue }));
patches.push({ op: "remove", path: path + "/" + patchPointString(oldKey), value: oldValue });
}

}
Expand All @@ -120,26 +122,26 @@ function generateObjectDiff(oldJson, newJson, unchanged, patches, path) {
if (pointer) {
//COPY
// console.log({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer});
patches.push(hashObject.hash({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer}));
patches.push({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer});
} else {
// no json.stringnify
if (typeof newVal === "string") {
// Ajust 333 => "333"
newVal = "\"" + newVal + "\"";
}
// if (typeof newVal === "string") {
// // Ajust 333 => "333"
// newVal = "\"" + newVal + "\"";
// }
var previousIndex = patchArea.findValueInPatch(newVal, patches);
// console.log("previousIndex: " + previousIndex);

if (previousIndex !== -1) {
// MOVE
var oldPath = JSON.parse(patches[previousIndex]).path;
var oldPath = patches[previousIndex].path;
patches.splice(previousIndex, 1);
// console.log({ op: "move", from: oldPath, path: path + "/" + patchPointString(newKey)});
patches.push(hashObject.hash({ op: "move", from: oldPath, path: path + "/" + patchPointString(newKey)}));
patches.push({ op: "move", from: oldPath, path: path + "/" + patchPointString(newKey)});
} else {
//ADD
// console.log({ op: "add", path: path + "/" + patchPointString(newKey), value: copy.clone(newVal)});
patches.push(hashObject.hash({ op: "add", path: path + "/" + patchPointString(newKey), value: newVal}));
patches.push({ op: "add", path: path + "/" + patchPointString(newKey), value: newVal});
}

}
Expand Down
52 changes: 30 additions & 22 deletions LCS.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var hashObject = require('./hashObject.js');

exports.LCS = LCS;

function LCS (x, y, unchanged, patches, path) {
function LCS (x, y, oldJson, newJson, unchanged, patches, patchHashes, path) {
//get the trimed sequence
var start = 0;
var x_end = x.length - 1;
Expand Down Expand Up @@ -34,7 +34,7 @@ function LCS (x, y, unchanged, patches, path) {
var offset = {};
offset.value = 1;
// pass offset reference
printDiff(newX, newY, matrix, newX.length - 1, newY.length -1, start, offset, unchanged, patches, path);
printDiff(newX, newY, oldJson, newJson, matrix, newX.length - 1, newY.length -1, start, offset, unchanged, patches, patchHashes, path);
}

function lcsMatrix(x, y) {
Expand Down Expand Up @@ -93,65 +93,71 @@ function backtrack(x, y, matrix, i, j) {
}


function printDiff(x, y, matrix, i, j, start, offset, unchanged, patches, path) {
function printDiff(x, y, oldJson, newJson, matrix, i, j, start, offset, unchanged, patches, patchHashes, path) {
if (i > -1 && j > -1 && x[i] === y[j]) {

printDiff(x, y, matrix, i-1, j-1, start, offset, unchanged, patches, path);
printDiff(x, y, oldJson, newJson, matrix, i-1, j-1, start, offset, unchanged, patches, patchHashes, path);

} else if (j > -1 && (i === -1 || matrix[i+1][j] >= matrix[i][j+1])) {

printDiff(x, y, matrix, i, j-1, start, offset, unchanged, patches, path);
printDiff(x, y, oldJson, newJson, matrix, i, j-1, start, offset, unchanged, patches, patchHashes, path);

// First Add or Replace
var lastElement = patches[patches.length - 1];
var tmpPath = path + "/" + (i + start + offset.value);

if (lastElement !== void 0) {
lastElement = JSON.parse(lastElement);
if (lastElement !== void 0 && lastElement.op === "remove" && lastElement.path === tmpPath) {
// lastElement = JSON.parse(lastElement);
if (lastElement.op === "remove" && lastElement.path === tmpPath) {
//First Replace
lastElement.op = "replace";
lastElement.value = JSON.parse(y[j]);
patches.splice(patches.length - 1, 1, JSON.stringify(lastElement));
lastElement.value = newJson[j+start];

var lastHash = patchHashes[patchHashes.length - 1];
lastHash.op = "replace";
lastHash.value = y[j];
// patches.splice(patches.length - 1, 1, JSON.stringify(lastElement));

} else {
addCopyMove(x, y, matrix, i, j, start, offset, unchanged, patches, path, tmpPath);
addCopyMove(x, y, oldJson, newJson, matrix, i, j, start, offset, unchanged, patches, patchHashes, path, tmpPath);
}
} else {
addCopyMove(x, y, matrix, i, j, start, offset, unchanged, patches, path, tmpPath);
addCopyMove(x, y, oldJson, newJson, matrix, i, j, start, offset, unchanged, patches, patchHashes, path, tmpPath);
}
//Then change offset
offset.value++;

} else if (i > -1 && (j === -1 || matrix[i+1][j] < matrix[i][j+1])) {

printDiff(x, y, matrix, i-1, j, start, offset, unchanged, patches, path);
printDiff(x, y, oldJson, newJson, matrix, i-1, j, start, offset, unchanged, patches, patchHashes, path);
//First change offset
offset.value--;
//Then remove
// console.log({ op: "remove", path: path + "/" + (i + start + offset.value), value: x[i] });
patches.push(hashObject.hash({ op: "remove", path: path + "/" + (i + start + offset.value), value: JSON.parse(x[i]) }));
// patches.push({ op: "remove", path: path + "/" + (i + start + offset.value), value: oldJson[i] });
patchHashes.push({ op: "remove", path: path + "/" + (i + start + offset.value), value: x[i] });
patches.push({ op: "remove", path: path + "/" + (i + start + offset.value), value: oldJson[i+start] });
} else {
// console.log("reach the end i = " + i);
}
}

function addCopyMove(x, y, matrix, i, j, start, offset, unchanged, patches, path, tmpPath) {
function addCopyMove(x, y, oldJson, newJson, matrix, i, j, start, offset, unchanged, patches, patchHashes, path, tmpPath) {
// First MOVE or ADD or COPY
// var templeOps = hashObject({ op: "remove", path: tmpPath, value: JSON.parse(y[j])});
var previousIndex = patchArea.findValueInPatch(y[j], patches);
var previousIndex = patchArea.findValueInPatchHashes(y[j], patchHashes);
// var previousIndex = -1; //save 4ms
// //
// ********Need to be fiexed*****************
// only move when the previousIndex is 0 and patchLength is 1
// if (previousIndex !== -1)
if (previousIndex === 0 && patches.length === 1) {
// MOVE
var oldPath = JSON.parse(patches[previousIndex]).path;
var oldPath = patches[previousIndex].path;
// console.log({ op: "move", from: oldPath, path: tmpPath});
patchHashes.splice(previousIndex, 1);
patches.splice(previousIndex, 1);
patches.push(hashObject.hash({ op: "move", from: oldPath, path: tmpPath}));
patchHashes.push({ op: "move", from: oldPath, path: tmpPath});
patches.push({ op: "move", from: oldPath, path: tmpPath});
} else {
// ADD OR COPY
//Try to find the value in the unchanged area
Expand All @@ -177,17 +183,19 @@ function addCopyMove(x, y, matrix, i, j, start, offset, unchanged, patches, path
if (index !== -1) {
var newPointer = pointer.slice(0, index + 1) + newIndex;
// console.log({ op: "copy", path: tmpPath, from: newPointer });
patches.push(hashObject.hash({ op: "copy", path: tmpPath, from: newPointer }));
patchHashes.push({ op: "copy", path: tmpPath, from: newPointer });
patches.push({ op: "copy", path: tmpPath, from: newPointer });
}
} else {
// newIndex < 0, hence the element doesn't exist in the array, add
patches.push(hashObject.hash({ op: "add", path: tmpPath, value: JSON.parse(y[j]) }));
patchHashes.push({ op: "add", path: tmpPath, value: y[j] });
patches.push({ op: "add", path: tmpPath, value: newJson[j+start] });
// patches.push({ op: "add", path: tmpPath, value: newJson[j] });
}
} else {
// ADD
patches.push(hashObject.hash({ op: "add", path: tmpPath, value: JSON.parse(y[j]) }));
// patches.push({ op: "add", path: tmpPath, value: newJson[j] });
patchHashes.push({ op: "add", path: tmpPath, value: y[j] });
patches.push({ op: "add", path: tmpPath, value: newJson[j+start] });

}
}
Expand Down
10 changes: 6 additions & 4 deletions hashObject.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var hashToNum = require('string-hash');

exports.hash = hash;
exports.map = map;

function hash(obj) {
//Default hash
return JSON.stringify(obj);
// return JSON.stringify(obj);

//String-hash
// return hash(obj);
return hashToNum(obj);
}

/**
Expand All @@ -21,8 +22,9 @@ function map(f, a) {

var b = new Array(a.length);
for (var i = 0; i < a.length; i++) {
// b[i] = f(typeof a[i] === "string"? a[i]: JSON.stringify(a[i]));
b[i] = f(a[i]);
b[i] = f(typeof a[i] === "string"? a[i]: JSON.stringify(a[i]));
// JSON.stringnify
// b[i] = f(a[i]);
}
return b;
}
Loading

0 comments on commit 7d340a4

Please sign in to comment.