Commit bc17b228 authored by Sven Franck's avatar Sven Franck

jslint pass revisionstorage.js

parent e45edf9f
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, hex_sha256: true, setTimeout: true */
/**
* JIO Revision Storage.
* It manages document version and can generate conflicts.
......@@ -30,19 +32,17 @@ jIO.addStorageType('revision', function (spec, my) {
*/
priv.generateUuid = function () {
var S4 = function () {
/* 65536 */
var i, string = Math.floor(
Math.random() * 0x10000 /* 65536 */
Math.random() * 0x10000
).toString(16);
for (i = string.length; i < 4; i += 1) {
string = '0'+string;
string = '0' + string;
}
return string;
};
return S4() + S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + S4() + S4();
return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() +
S4() + S4();
};
/**
......@@ -63,8 +63,8 @@ jIO.addStorageType('revision', function (spec, my) {
*/
priv.revisionToArray = function (revision) {
if (typeof revision === "string") {
return [parseInt(revision.split('-')[0],10),
revision.split('-')[1]]
return [parseInt(revision.split('-')[0], 10),
revision.split('-')[1]];
}
return revision;
};
......@@ -82,12 +82,12 @@ jIO.addStorageType('revision', function (spec, my) {
priv.generateNextRevision = function (previous_revision,
doc, revisions, deleted_flag) {
var string = JSON.stringify(doc) + JSON.stringify(revisions) +
JSON.stringify(deleted_flag? true: false);
JSON.stringify(deleted_flag ? true : false);
if (typeof previous_revision === "number") {
return [previous_revision + 1, priv.hashCode(string)];
}
previous_revision = priv.revisionToArray(previous_revision);
return [previous_revision[0]+1, priv.hashCode(string)];
return [previous_revision[0] + 1, priv.hashCode(string)];
};
/**
......@@ -106,8 +106,10 @@ jIO.addStorageType('revision', function (spec, my) {
* @param {array} children An array of children (optional)
* @return {object} The new document tree
*/
priv.createDocumentTree = function(children) {
return {"children":children || []};
priv.createDocumentTree = function (children) {
return {
"children": children || []
};
};
/**
......@@ -118,8 +120,12 @@ jIO.addStorageType('revision', function (spec, my) {
* @param {array} children An array of children (optional)
* @return {object} The new document tree node
*/
priv.createDocumentTreeNode = function(revision,status,children) {
return {"rev":revision,"status":status,"children":children || []};
priv.createDocumentTreeNode = function (revision, status, children) {
return {
"rev": revision,
"status": status,
"children": children || []
};
};
/**
......@@ -130,16 +136,16 @@ jIO.addStorageType('revision', function (spec, my) {
* @return {array} The good revs info array
*/
priv.getRevisionFromDocumentTree = function (document_tree, revision) {
var i, result, search, revs_info = [];
var result, search, revs_info = [];
result = [];
// search method fills "result" with the good revs info
search = function (document_tree) {
var i;
if (typeof document_tree.rev !== "undefined") {
if (document_tree.rev !== undefined) {
// node is not root
revs_info.unshift({
"rev":document_tree.rev,
"status":document_tree.status
"rev": document_tree.rev,
"status": document_tree.status
});
if (document_tree.rev === revision) {
result = revs_info;
......@@ -169,20 +175,20 @@ jIO.addStorageType('revision', function (spec, my) {
* @return {array} The winner revs info array
*/
priv.getWinnerRevisionFromDocumentTree = function (document_tree) {
var i, result, search, revs_info = [];
var result, search, revs_info = [];
result = [];
// search method fills "result" with the winner revs info
search = function (document_tree, deep) {
var i;
if (typeof document_tree.rev !== "undefined") {
if (document_tree.rev !== undefined) {
// node is not root
revs_info.unshift({
"rev":document_tree.rev,
"status":document_tree.status
"rev": document_tree.rev,
"status": document_tree.status
});
}
if (document_tree.children.length === 0 &&
document_tree.status !== "deleted" ) {
if (document_tree.children.length === 0 && document_tree.status !==
"deleted") {
// This node is a leaf
if (result.length < deep) {
// The leaf is deeper than result
......@@ -196,7 +202,7 @@ jIO.addStorageType('revision', function (spec, my) {
// This node has children
for (i = 0; i < document_tree.children.length; i += 1) {
// searching deeper to find the deeper leaf
search(document_tree.children[i], deep+1);
search(document_tree.children[i], deep + 1);
revs_info.shift();
}
};
......@@ -220,21 +226,23 @@ jIO.addStorageType('revision', function (spec, my) {
selected_node = doctree;
selectNode = function (node) {
var i;
if (typeof node.rev !== "undefined") {
if (node.rev !== undefined) {
// node is not root
revs_info.unshift({"rev":node.rev,"status":node.status});
revs_info.unshift({
"rev": node.rev,
"status": node.status
});
}
if (node.rev === doc._rev) {
selected_node = node;
return "node_selected";
} else {
}
for (i = 0; i < node.children.length; i += 1) {
if (selectNode(node.children[i]) === "node_selected") {
return "node_selected";
}
revs_info.shift();
}
}
};
if (typeof doc._rev === "string") {
// document has a previous revision
......@@ -253,8 +261,11 @@ jIO.addStorageType('revision', function (spec, my) {
}
}
next_rev = priv.generateNextRevision(
doc._rev || 0, doc, priv.revsInfoToHistory(revs_info),
set_node_to_deleted);
doc._rev || 0,
doc,
priv.revsInfoToHistory(revs_info),
set_node_to_deleted
);
next_rev_str = next_rev.join("-");
// don't add if the next rev already exists
for (i = 0; i < selected_node.children.length; i += 1) {
......@@ -291,7 +302,7 @@ jIO.addStorageType('revision', function (spec, my) {
* @return {array} The array of leaves revisions
*/
priv.getLeavesFromDocumentTree = function (document_tree, except) {
var i, result, search;
var result, search;
result = [];
// search method fills [result] with the winner revision
search = function (document_tree) {
......@@ -299,8 +310,8 @@ jIO.addStorageType('revision', function (spec, my) {
if (except !== undefined && except === document_tree.rev) {
return;
}
if (document_tree.children.length === 0 &&
document_tree.status !== "deleted") {
if (document_tree.children.length === 0 && document_tree.status !==
"deleted") {
// This node is a leaf
result.push(document_tree.rev);
return;
......@@ -323,17 +334,17 @@ jIO.addStorageType('revision', function (spec, my) {
* @return {boolean} true/false
*/
priv.isRevisionALeaf = function (document_tree, revision) {
var i, result, search;
var result, search;
result = undefined;
// search method fills "result" with the good revs info
search = function (document_tree) {
var i;
if (typeof document_tree.rev !== "undefined") {
if (document_tree.rev !== undefined) {
// node is not root
if (document_tree.rev === revision) {
if (document_tree.children.length === 0) {
// This node is a leaf
result = true
result = true;
return;
}
result = false;
......@@ -361,9 +372,12 @@ jIO.addStorageType('revision', function (spec, my) {
* @return {object} The revisions history
*/
priv.revsInfoToHistory = function (revs_info) {
var revisions = {"start":0,"ids":[]}, i;
var revisions = {
"start": 0,
"ids": []
}, i;
if (revs_info.length > 0) {
revisions.start = parseInt(revs_info[0].rev.split('-')[0],10);
revisions.start = parseInt(revs_info[0].rev.split('-')[0], 10);
}
for (i = 0; i < revs_info.length; i += 1) {
revisions.ids.push(revs_info[i].rev.split('-')[1]);
......@@ -401,13 +415,12 @@ jIO.addStorageType('revision', function (spec, my) {
doc = command.cloneDoc();
docid = command.getDocId();
if (typeof doc._rev === "string" &&
!priv.checkRevisionFormat(doc._rev)) {
if (typeof doc._rev === "string" && !priv.checkRevisionFormat(doc._rev)) {
that.error({
"status": 31,
"statusText": "Wrong Revision Format",
"error": "wrong_revision_format",
"message": "The document previous revision does not match "+
"message": "The document previous revision does not match " +
"^[0-9]+-[0-9a-zA-Z]+$",
"reason": "Previous revision is wrong"
});
......@@ -422,14 +435,15 @@ jIO.addStorageType('revision', function (spec, my) {
}
f.getDocumentTree = function () {
var option = command.cloneOption();
if (option["max_retry"] === 0) {
option["max_retry"] = 3;
if (option.max_retry === 0) {
option.max_retry = 3;
}
that.addJob(
"get",
priv.substorage,
docid+priv.doctree_suffix,
docid + priv.doctree_suffix,
option,
function (response) {
doctree = response;
......@@ -444,8 +458,9 @@ jIO.addStorageType('revision', function (spec, my) {
"reason": "Document update conflict"
});
}
}, function (err) {
switch(err.status) {
},
function (err) {
switch (err.status) {
case 404:
doctree = priv.createDocumentTree();
f.postDocument("post");
......@@ -460,23 +475,23 @@ jIO.addStorageType('revision', function (spec, my) {
};
f.postDocument = function (doctree_update_method) {
revs_info = priv.postToDocumentTree(doctree, doc);
doc._id = docid+"."+revs_info[0].rev;
doc._id = docid + "." + revs_info[0].rev;
that.addJob(
"post",
priv.substorage,
doc,
command.cloneOption(),
function (response) {
f.sendDocumentTree (doctree_update_method);
function () {
f.sendDocumentTree(doctree_update_method);
},
function (err) {
switch(err.status) {
switch (err.status) {
case 409:
// file already exists
f.sendDocumentTree (doctree_update_method);
f.sendDocumentTree(doctree_update_method);
break;
default:
err.message = "Cannot upload document".
err.message = "Cannot upload document";
that.error(err);
break;
}
......@@ -484,17 +499,17 @@ jIO.addStorageType('revision', function (spec, my) {
);
};
f.sendDocumentTree = function (method) {
doctree._id = docid+priv.doctree_suffix;
doctree._id = docid + priv.doctree_suffix;
that.addJob(
method,
priv.substorage,
doctree,
command.cloneOption(),
function (response) {
function () {
that.success({
"ok":true,
"id":docid,
"rev":revs_info[0].rev
"ok": true,
"id": docid,
"rev": revs_info[0].rev
});
},
function (err) {
......@@ -534,8 +549,8 @@ jIO.addStorageType('revision', function (spec, my) {
that.get = function (command) {
var f = {}, doctree, revs_info, prev_rev, option;
option = command.cloneOption();
if (option["max_retry"] === 0) {
option["max_retry"] = 3;
if (option.max_retry === 0) {
option.max_retry = 3;
}
prev_rev = command.getOption("rev");
if (typeof prev_rev === "string") {
......@@ -544,7 +559,7 @@ jIO.addStorageType('revision', function (spec, my) {
"status": 31,
"statusText": "Wrong Revision Format",
"error": "wrong_revision_format",
"message": "The document previous revision does not match "+
"message": "The document previous revision does not match " +
"[0-9]+-[0-9a-zA-Z]+",
"reason": "Previous revision is wrong"
});
......@@ -555,22 +570,21 @@ jIO.addStorageType('revision', function (spec, my) {
that.addJob(
"get",
priv.substorage,
command.getDocId()+priv.doctree_suffix,
command.getDocId() + priv.doctree_suffix,
option,
function (response) {
doctree = response;
if (prev_rev === undefined) {
revs_info =
priv.getWinnerRevisionFromDocumentTree(doctree);
revs_info = priv.getWinnerRevisionFromDocumentTree(doctree);
prev_rev = revs_info[0].rev;
} else {
revs_info =
priv.getRevisionFromDocumentTree(doctree, prev_rev);
revs_info = priv.getRevisionFromDocumentTree(doctree, prev_rev);
}
f.getDocument(command.getDocId()+"."+prev_rev,
f.getDocument(command.getDocId() + "." + prev_rev,
command.getAttachmentId());
}, function (err) {
switch(err.status) {
},
function (err) {
switch (err.status) {
case 404:
that.error(err);
break;
......@@ -595,11 +609,12 @@ jIO.addStorageType('revision', function (spec, my) {
if (response._attachments !== undefined) {
attmt = response._attachments[attmtid];
if (attmt !== undefined) {
prev_rev =
priv.getRevisionFromPosition(
revs_info, attmt.revpos);
f.getDocument(command.getDocId()+"."+
prev_rev+"/"+attmtid);
prev_rev = priv.getRevisionFromPosition(
revs_info,
attmt.revpos
);
f.getDocument(command.getDocId() + "." + prev_rev + "/" +
attmtid);
return;
}
}
......@@ -615,30 +630,31 @@ jIO.addStorageType('revision', function (spec, my) {
response._id = command.getDocId();
response._rev = prev_rev;
if (command.getOption("revs") === true) {
response._revisions =
priv.revsInfoToHistory(revs_info);
response._revisions = priv.revsInfoToHistory(revs_info);
}
if (command.getOption("revs_info") === true) {
response._revs_info = revs_info;
}
if (command.getOption("conflicts") === true) {
response._conflicts =
priv.getLeavesFromDocumentTree(
doctree, prev_rev);
response._conflicts = priv.getLeavesFromDocumentTree(
doctree,
prev_rev
);
if (response._conflicts.length === 0) {
delete response._conflicts;
}
}
}
that.success(response);
}, function (err) {
},
function (err) {
that.error(err);
}
);
};
if (command.getAttachmentId() && prev_rev !== undefined) {
f.getDocument(command.getDocId()+"."+prev_rev+
"/"+command.getAttachmentId());
f.getDocument(command.getDocId() + "." + prev_rev +
"/" + command.getAttachmentId());
} else {
f.getDocumentTree();
}
......@@ -652,43 +668,44 @@ jIO.addStorageType('revision', function (spec, my) {
* @param {object} command The JIO command
*/
that.remove = function (command) {
var f = {}, del_rev, option, new_doc, revs_info, new_id;
var f = {}, del_rev, option, new_doc, revs_info;
option = command.cloneOption();
if (option["max_retry"] === 0) {
option["max_retry"] = 3;
if (option.max_retry === 0) {
option.max_retry = 3;
}
del_rev = command.getDoc()._rev;
f.removeDocument = function (docid, doctree) {
if (command.getOption("keep_revision_history") !== true) {
if (command.getAttachmentId() === undefined){
if (command.getAttachmentId() === undefined) {
// update tree
revs_info = priv.postToDocumentTree(doctree,
command.getDoc(), true);
revs_info = priv.postToDocumentTree(
doctree,
command.getDoc(),
true
);
// remove revision
that.addJob(
"remove",
priv.substorage,
docid,
option,
function (response) {
function () {
// put tree
doctree._id = command.getDocId()+
priv.doctree_suffix;
doctree._id = command.getDocId() + priv.doctree_suffix;
that.addJob(
"put",
priv.substorage,
doctree,
command.cloneOption(),
function (response) {
function () {
that.success({
"ok":true,
"id":command.getDocId(),
"rev":revs_info[0].rev
"ok": true,
"id": command.getDocId(),
"rev": revs_info[0].rev
});
},
function (err){
function () {
that.error({
"status": 409,
"statusText": "Conflict",
......@@ -700,7 +717,7 @@ jIO.addStorageType('revision', function (spec, my) {
}
);
},
function (err) {
function () {
that.error({
"status": 404,
"statusText": "Not Found",
......@@ -716,16 +733,14 @@ jIO.addStorageType('revision', function (spec, my) {
that.addJob(
"get",
priv.substorage,
command.getDocId()+"."+del_rev,
command.getDocId() + "." + del_rev,
option,
function (response) {
// update tree
revs_info = priv.postToDocumentTree(doctree,
command.getDoc());
revs_info = priv.postToDocumentTree(doctree, command.getDoc());
new_doc = response;
delete new_doc._attachments;
new_doc._id = new_doc._id+"."+revs_info[0].rev;
new_doc._id = new_doc._id + "." + revs_info[0].rev;
// post new document version
that.addJob(
......@@ -733,21 +748,19 @@ jIO.addStorageType('revision', function (spec, my) {
priv.substorage,
new_doc,
command.cloneOption(),
function (response) {
function () {
// put tree
doctree._id = command.getDocId()+
priv.doctree_suffix;
doctree._id = command.getDocId() + priv.doctree_suffix;
that.addJob(
"put",
priv.substorage,
doctree,
command.cloneOption(),
function (response) {
function () {
that.success({
"ok":true,
"id":new_doc._id,
"rev":revs_info[0].rev
"ok": true,
"id": new_doc._id,
"rev": revs_info[0].rev
});
},
function (err) {
......@@ -756,9 +769,8 @@ jIO.addStorageType('revision', function (spec, my) {
that.error(err);
}
);
},
function (err){
function () {
that.error({
"status": 409,
"statusText": "Conflict",
......@@ -770,7 +782,7 @@ jIO.addStorageType('revision', function (spec, my) {
}
);
},
function (err) {
function () {
that.error({
"status": 404,
"statusText": "Not Found",
......@@ -782,8 +794,6 @@ jIO.addStorageType('revision', function (spec, my) {
}
);
}
} else {
// keep history = update document tree only
}
};
if (typeof del_rev === "string") {
......@@ -792,7 +802,7 @@ jIO.addStorageType('revision', function (spec, my) {
"status": 31,
"statusText": "Wrong Revision Format",
"error": "wrong_revision_format",
"message": "The document previous revision does not match "+
"message": "The document previous revision does not match " +
"[0-9]+-[0-9a-zA-Z]+",
"reason": "Previous revision is wrong"
});
......@@ -804,7 +814,7 @@ jIO.addStorageType('revision', function (spec, my) {
that.addJob(
"get",
priv.substorage,
command.getDocId()+priv.doctree_suffix,
command.getDocId() + priv.doctree_suffix,
option,
function (response) {
response._conflicts = priv.getLeavesFromDocumentTree(response);
......@@ -819,16 +829,15 @@ jIO.addStorageType('revision', function (spec, my) {
"reason": "Cannot delete a document without revision"
});
return;
} else {
}
// revision provided
if (priv.isRevisionALeaf( response, del_rev) === true){
if (typeof command.getAttachmentId() === "string"){
f.removeDocument(command.getDocId()+"."+del_rev+
"/"+command.getAttachmentId(), response);
if (priv.isRevisionALeaf(response, del_rev) === true) {
if (typeof command.getAttachmentId() === "string") {
f.removeDocument(command.getDocId() + "." + del_rev +
"/" + command.getAttachmentId(), response);
} else {
f.removeDocument(command.getDocId()+"."+ del_rev,
response
);
f.removeDocument(command.getDocId() + "." + del_rev,
response);
}
} else {
that.error({
......@@ -840,9 +849,8 @@ jIO.addStorageType('revision', function (spec, my) {
});
return;
}
}
},
function (err) {
function () {
that.error({
"status": 404,
"statusText": "Not Found",
......@@ -860,7 +868,7 @@ jIO.addStorageType('revision', function (spec, my) {
* @method allDocs
* @param {object} command The JIO command
*/
that.allDocs = function (command) {
that.allDocs = function () {
setTimeout(function () {
that.error({
"status": 405,
......
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