Commit 8fb729ac authored by Tristan Cavelier's avatar Tristan Cavelier

revision generating changed

parent 339eae00
...@@ -74,10 +74,15 @@ jIO.addStorageType('revision', function (spec, my) { ...@@ -74,10 +74,15 @@ jIO.addStorageType('revision', function (spec, my) {
* to generate a hash code. * to generate a hash code.
* @methode generateNextRev * @methode generateNextRev
* @param {string} previous_revision The previous revision * @param {string} previous_revision The previous revision
* @param {string} string String to help generate hash code * @param {object} doc The document metadata
* @param {object} revisions The revision history
* @param {boolean} deleted_flag The deleted flag
* @return {array} 0:The next revision number and 1:the hash code * @return {array} 0:The next revision number and 1:the hash code
*/ */
priv.generateNextRevision = function (previous_revision, string) { priv.generateNextRevision = function (previous_revision,
doc, revisions, deleted_flag) {
var string = JSON.stringify(doc) + JSON.stringify(revisions) +
JSON.stringify(deleted_flag? true: false);
if (typeof previous_revision === "number") { if (typeof previous_revision === "number") {
return [previous_revision + 1, priv.hashCode(string)]; return [previous_revision + 1, priv.hashCode(string)];
} }
...@@ -247,7 +252,8 @@ jIO.addStorageType('revision', function (spec, my) { ...@@ -247,7 +252,8 @@ jIO.addStorageType('revision', function (spec, my) {
} }
} }
next_rev = priv.generateNextRevision( next_rev = priv.generateNextRevision(
doc._rev || 0, JSON.stringify(doc) + JSON.stringify(revs_info)); doc._rev || 0, doc, priv.revsInfoToHistory(revs_info),
set_node_to_deleted);
next_rev_str = next_rev.join("-"); next_rev_str = next_rev.join("-");
next_rev_status = set_node_to_deleted === true ? "deleted" : "available"; next_rev_status = set_node_to_deleted === true ? "deleted" : "available";
......
...@@ -21,6 +21,13 @@ contains = function (array,content) { ...@@ -21,6 +21,13 @@ contains = function (array,content) {
clone = function (obj) { clone = function (obj) {
return JSON.parse(JSON.stringify(obj)); return JSON.parse(JSON.stringify(obj));
}, },
// generates a revision hash from document metadata, revision history
// and the deleted_flag
generateRevisionHash = function (doc, revisions, deleted_flag) {
var string = JSON.stringify(doc) + JSON.stringify(revisions) +
JSON.stringify(deleted_flag? true: false);
return hex_sha256(string);
},
// localStorage wrapper // localStorage wrapper
localstorage = { localstorage = {
clear: function () { clear: function () {
...@@ -1086,8 +1093,8 @@ test ("Post", function(){ ...@@ -1086,8 +1093,8 @@ test ("Post", function(){
// post non empty document // post non empty document
o.doc = {"_id": "post1", "title": "myPost1"}; o.doc = {"_id": "post1", "title": "myPost1"};
o.revs_info = []; o.revisions = {"start": 0, "ids": []};
o.rev = "1-"+hex_sha256(JSON.stringify(o.doc)+JSON.stringify(o.revs_info)); o.rev = "1-"+generateRevisionHash(o.doc, o.revisions);
o.spy (o, "value", {"ok": true, "id": "post1", "rev": o.rev}, "Post"); o.spy (o, "value", {"ok": true, "id": "post1", "rev": o.rev}, "Post");
o.jio.post(o.doc, o.f); o.jio.post(o.doc, o.f);
o.tick(o); o.tick(o);
...@@ -1101,8 +1108,7 @@ test ("Post", function(){ ...@@ -1101,8 +1108,7 @@ test ("Post", function(){
// post and document already exists // post and document already exists
o.doc = {"_id": "post1", "title": "myPost2"}; o.doc = {"_id": "post1", "title": "myPost2"};
o.revs_info = []; o.rev = "1-"+generateRevisionHash(o.doc, o.revisions);
o.rev = "1-"+hex_sha256(JSON.stringify(o.doc)+JSON.stringify(o.revs_info));
o.spy (o, "value", { o.spy (o, "value", {
"ok": true, "id": "post1", "rev": o.rev "ok": true, "id": "post1", "rev": o.rev
}, "Post and document already exists"); }, "Post and document already exists");
...@@ -1111,8 +1117,8 @@ test ("Post", function(){ ...@@ -1111,8 +1117,8 @@ test ("Post", function(){
// post + revision // post + revision
o.doc = {"_id": "post1", "_rev": o.rev, "title": "myPost2"}; o.doc = {"_id": "post1", "_rev": o.rev, "title": "myPost2"};
o.revs_info = [{"rev": o.rev, "status": "available"}]; o.revisions = {"start": 1, "ids": [o.rev.split('-')[1]]};
o.rev = "2-"+hex_sha256(JSON.stringify(o.doc)+JSON.stringify(o.revs_info)); o.rev = "2-"+generateRevisionHash(o.doc, o.revisions);
o.spy (o, "status", undefined, "Post + revision"); o.spy (o, "status", undefined, "Post + revision");
o.jio.post(o.doc, o.f); o.jio.post(o.doc, o.f);
o.tick(o); o.tick(o);
...@@ -1152,8 +1158,8 @@ test ("Put", function(){ ...@@ -1152,8 +1158,8 @@ test ("Put", function(){
// put non empty document // put non empty document
o.doc = {"_id": "put1", "title": "myPut1"}; o.doc = {"_id": "put1", "title": "myPut1"};
o.revs_info = []; o.revisions = {"start": 0, "ids": []};
o.rev = "1-"+hex_sha256(JSON.stringify(o.doc)+JSON.stringify(o.revs_info)); o.rev = "1-"+generateRevisionHash(o.doc, o.revisions);
o.spy (o, "value", {"ok": true, "id": "put1", "rev": o.rev}, o.spy (o, "value", {"ok": true, "id": "put1", "rev": o.rev},
"Creates a document"); "Creates a document");
o.jio.put(o.doc, o.f); o.jio.put(o.doc, o.f);
...@@ -1173,8 +1179,8 @@ test ("Put", function(){ ...@@ -1173,8 +1179,8 @@ test ("Put", function(){
// post + revision // post + revision
o.doc = {"_id": "put1", "_rev": o.rev, "title": "myPut2"}; o.doc = {"_id": "put1", "_rev": o.rev, "title": "myPut2"};
o.revs_info = [{"rev": o.rev, "status": "available"}]; o.revisions = {"start": 1, "ids": [o.rev.split('-')[1]]};
o.rev = "2-"+hex_sha256(JSON.stringify(o.doc)+JSON.stringify(o.revs_info)); o.rev = "2-"+generateRevisionHash(o.doc, o.revisions);
o.spy (o, "status", undefined, "Put + revision"); o.spy (o, "status", undefined, "Put + revision");
o.jio.put(o.doc, o.f); o.jio.put(o.doc, o.f);
o.tick(o); o.tick(o);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment