Commit 090573c4 authored by Sven Franck's avatar Sven Franck

jslint pass conflictmanagerstorage.js

parent 531ca1e8
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, hex_sha256: true, setTimeout: true */
jIO.addStorageType('conflictmanager', function (spec, my) {
spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
var that, priv, storage_exists, local_namespace, empty_fun,
super_serialized;
var storage_exists = (spec.storage?true:false);
priv.sub_storage_spec = spec.storage || {type:'base'};
priv.sub_storage_string = JSON.stringify (priv.sub_storage_spec);
spec = spec || {};
that = my.basicStorage(spec, my);
priv = {};
storage_exists = (spec.storage ? true : false);
priv.sub_storage_spec = spec.storage || {
type: 'base'
};
priv.sub_storage_string = JSON.stringify(priv.sub_storage_spec);
var local_namespace = 'jio/conflictmanager/'+
priv.sub_storage_string+'/';
local_namespace = 'jio/conflictmanager/' + priv.sub_storage_string + '/';
var empty_fun = function (){};
empty_fun = function () {};
var super_serialized = that.serialized;
super_serialized = that.serialized;
that.serialized = function () {
var o = super_serialized();
o.storage = priv.sub_storage_spec;
......@@ -25,71 +31,91 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
return 'Need at least one parameter: "storage".';
};
priv.getDistantMetadata = function (command,path,success,error) {
var cloned_option = command.cloneOption ();
priv.getDistantMetadata = function (command, path, success, error) {
var cloned_option = command.cloneOption();
cloned_option.metadata_only = false;
that.addJob ('get',priv.sub_storage_spec,path,cloned_option,
that.addJob('get', priv.sub_storage_spec, path, cloned_option,
success, error);
};
priv.saveMetadataToDistant = function (command,path,content,success,error) {
that.addJob ('put',priv.sub_storage_spec,
{_id:path,content:JSON.stringify (content)},
command.cloneOption(),success,error);
priv.saveMetadataToDistant = function (command, path, content, success,
error) {
that.addJob('put', priv.sub_storage_spec, {
_id: path,
content: JSON.stringify(content)
},
command.cloneOption(), success, error);
};
priv.saveNewRevision = function (command,path,content,success,error) {
that.addJob ('post',priv.sub_storage_spec,{_id:path,content:content},
command.cloneOption(),success,error);
priv.saveNewRevision = function (command, path, content, success, error) {
that.addJob('post', priv.sub_storage_spec, {
_id: path,
content: content
},
command.cloneOption(), success, error);
};
priv.loadRevision = function (command,path,success,error) {
that.addJob('get',priv.sub_storage_spec,path,command.cloneOption(),
priv.loadRevision = function (command, path, success, error) {
that.addJob('get', priv.sub_storage_spec, path, command.cloneOption(),
success, error);
};
priv.deleteAFile = function (command,path,success,error) {
var cloned_option = command.cloneOption();
that.addJob ('remove',priv.sub_storage_spec,{_id:path},
priv.deleteAFile = function (command, path, success, error) {
that.addJob('remove', priv.sub_storage_spec, {
_id: path
},
command.cloneOption(), success, error);
};
priv.chooseARevision = function (metadata) {
var tmp_last_modified = 0, ret_rev = '', rev;
var tmp_last_modified = 0,
ret_rev = '',
rev;
for (rev in metadata) {
if (tmp_last_modified <
metadata[rev]._last_modified) {
tmp_last_modified =
metadata[rev]._last_modified;
if (metadata.hasOwnProperty(rev)) {
if (tmp_last_modified < metadata[rev]._last_modified) {
tmp_last_modified = metadata[rev]._last_modified;
ret_rev = rev;
}
}
}
return ret_rev;
};
priv._revs = function (metadata,revision) {
if (!(metadata && revision)) { return null; }
if (metadata[revision]) {
return {start:metadata[revision]._revisions.length,
ids:metadata[revision]._revisions};
} else {
priv._revs = function (metadata, revision) {
if (!(metadata && revision)) {
return null;
}
if (metadata[revision]) {
return {
start: metadata[revision]._revisions.length,
ids: metadata[revision]._revisions
};
}
return null;
};
priv._revs_info = function (metadata) {
if (!metadata) { return null; }
if (!metadata) {
return null;
}
var k, l = [];
for (k in metadata) {
if (metadata.hasOwnProperty(k)) {
l.push({
rev:k,status:(metadata[k]?(
metadata[k]._deleted?'deleted':'available'):'missing')
rev: k,
status: (
metadata[k] ? (
metadata[k]._deleted ? 'deleted' : 'available'
) : 'missing'
)
});
}
}
return l;
};
priv.solveConflict = function (doc,option,param) {
priv.solveConflict = function (doc, option, param) {
var o = {}, am = priv.newAsyncModule(),
command = param.command,
......@@ -97,77 +123,91 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
current_revision = '',
current_revision_file_path = '',
metadata_file_content = null,
on_conflict = false, conflict_object = {total_rows:0,rows:[]},
on_conflict = false,
conflict_object = {
total_rows: 0,
rows: []
},
on_remove = param._deleted,
previous_revision = param.previous_revision,
previous_revision_content_object = null,
now = new Date(),
failerror;
o.getDistantMetadata = function (){
priv.getDistantMetadata (
command, metadata_file_path,
o.getDistantMetadata = function () {
priv.getDistantMetadata(
command,
metadata_file_path,
function (result) {
var previous_revision_number =
parseInt(previous_revision.split('-')[0],10);
metadata_file_content = JSON.parse (result.content);
parseInt(previous_revision.split('-')[0], 10);
metadata_file_content = JSON.parse(result.content);
// set current revision
// jslint: removed '' in hex_sha256(''...
current_revision = (previous_revision_number + 1) + '-' +
hex_sha256 ('' + doc.content +
previous_revision +
JSON.stringify (metadata_file_content));
current_revision_file_path = param.docid + '.' +
current_revision;
hex_sha256(doc.content + previous_revision +
JSON.stringify(metadata_file_content));
current_revision_file_path = param.docid + '.' + current_revision;
previous_revision_content_object = metadata_file_content[
previous_revision] || {};
previous_revision
] || {};
if (!on_remove) {
am.wait(o,'saveMetadataOnDistant',1);
am.call(o,'saveNewRevision');
am.wait(o, 'saveMetadataOnDistant', 1);
am.call(o, 'saveNewRevision');
}
am.call(o,'previousUpdateMetadata');
},function (error) {
am.call(o,'error',[error]);
am.call(o, 'previousUpdateMetadata');
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.saveNewRevision = function (){
priv.saveNewRevision (
command, current_revision_file_path, doc.content,
function (result) {
am.call(o,'saveMetadataOnDistant');
}, function (error) {
am.call(o,'error',[error]);
o.saveNewRevision = function () {
priv.saveNewRevision(
command,
current_revision_file_path,
doc.content,
function () {
am.call(o, 'saveMetadataOnDistant');
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.previousUpdateMetadata = function () {
var i;
for (i = 0; i < param.key.length; i+= 1) {
for (i = 0; i < param.key.length; i += 1) {
delete metadata_file_content[param.key[i]];
}
am.call(o,'checkForConflicts');
am.call(o, 'checkForConflicts');
};
o.checkForConflicts = function () {
var rev;
for (rev in metadata_file_content) {
var revision_index;
if (metadata_file_content.hasOwnProperty(rev)) {
on_conflict = true;
failerror = {
status:409,error:'conflict',
statusText:'Conflict',reason:'document update conflict',
message:'There is one or more conflicts'
status: 409,
error: 'conflict',
statusText: 'Conflict',
reason: 'document update conflict',
message: 'There is one or more conflicts'
};
break;
}
am.call(o,'updateMetadata');
}
am.call(o, 'updateMetadata');
};
o.updateMetadata = function (){
o.updateMetadata = function () {
var revision_history, id = '';
id = current_revision.split('-'); id.shift(); id = id.join('-');
id = current_revision.split('-');
id.shift();
id = id.join('-');
revision_history = previous_revision_content_object._revisions;
revision_history.unshift(id);
metadata_file_content[current_revision] = {
_creation_date:previous_revision_content_object._creation_date||
_creation_date: previous_revision_content_object._creation_date ||
now.getTime(),
_last_modified: now.getTime(),
_revisions: revision_history,
......@@ -175,42 +215,56 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
_deleted: on_remove
};
if (on_conflict) {
conflict_object =
priv.createConflictObject(
command, metadata_file_content, current_revision
conflict_object = priv.createConflictObject(
command,
metadata_file_content,
current_revision
);
}
am.call(o,'saveMetadataOnDistant');
am.call(o, 'saveMetadataOnDistant');
};
o.saveMetadataOnDistant = function (){
o.saveMetadataOnDistant = function () {
priv.saveMetadataToDistant(
command, metadata_file_path, metadata_file_content,
function (result) {
am.call(o,'deleteAllConflictingRevision');
command,
metadata_file_path,
metadata_file_content,
function () {
am.call(o, 'deleteAllConflictingRevision');
if (on_conflict) {
am.call(o,'error');
am.call(o, 'error');
} else {
am.call(o,'success');
am.call(o, 'success');
}
},function (error) {
am.call(o,'error',[error]);
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.deleteAllConflictingRevision = function (){
o.deleteAllConflictingRevision = function () {
var i;
for (i = 0; i < param.key.length; i+= 1) {
priv.deleteAFile (
command, param.docid+'.'+param.key[i], empty_fun,empty_fun);
for (i = 0; i < param.key.length; i += 1) {
priv.deleteAFile(
command,
param.docid + '.' + param.key[i],
empty_fun,
empty_fun
);
}
};
o.success = function (){
var a = {ok:true,id:param.docid,rev:current_revision};
am.neverCall(o,'error');
am.neverCall(o,'success');
o.success = function () {
var a = {
ok: true,
id: param.docid,
rev: current_revision
};
am.neverCall(o, 'error');
am.neverCall(o, 'success');
if (option.revs) {
a.revisions = priv._revs(metadata_file_content,
current_revision);
a.revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (option.revs_info) {
a.revs_info = priv._revs_info(metadata_file_content);
......@@ -220,35 +274,47 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
}
param.success(a);
};
o.error = function (error){
var err = error || failerror ||
{status:0,statusText:'Unknown',error:'unknown_error',
message:'Unknown error.',reason:'unknown error'};
o.error = function (error) {
var err = error || failerror || {
status: 0,
statusText: 'Unknown',
error: 'unknown_error',
message: 'Unknown error.',
reason: 'unknown error'
};
if (current_revision) {
err.rev = current_revision;
}
if (option.revs) {
err.revisions = priv._revs(metadata_file_content,
current_revision);
err.revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (option.revs_info) {
err.revs_info = priv._revs_info(metadata_file_content);
err.revs_info = priv._revs_info(
metadata_file_content
);
}
if (option.conflicts) {
err.conflicts = conflict_object;
}
am.neverCall(o,'error');
am.neverCall(o,'success');
am.neverCall(o, 'error');
am.neverCall(o, 'success');
param.error(err);
};
am.call(o,'getDistantMetadata');
am.call(o, 'getDistantMetadata');
};
priv.createConflictObject = function (command, metadata, revision) {
return {
total_rows:1,
rows:[priv.createConflictRow(command,command.getDocId(),
metadata,revision)]
total_rows: 1,
rows: [priv.createConflictRow(
command,
command.getDocId(),
metadata,
revision
)]
};
};
......@@ -256,25 +322,25 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
var param = {}, i = 0;
if (typeof list[i] === 'string') {
param.content = list[i];
i ++;
i += 1;
}
if (typeof list[i] === 'object') {
param.options = list[i];
i ++;
i += 1;
} else {
param.options = {};
}
param.callback = function (err,val){};
param.callback = function () {};
param.success = function (val) {
param.callback(undefined,val);
param.callback(undefined, val);
};
param.error = function (err) {
param.callback(err,undefined);
param.callback(err, undefined);
};
if (typeof list[i] === 'function') {
if (typeof list[i+1] === 'function') {
if (typeof list[i + 1] === 'function') {
param.success = list[i];
param.error = list[i+1];
param.error = list[i + 1];
} else {
param.callback = list[i];
}
......@@ -283,8 +349,12 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
};
priv.createConflictRow = function (command, docid, metadata, revision) {
var row = {id:docid,key:[],value:{
_solveConflict: function (/*content, option, success, error*/) {
var row = {
id: docid,
key: [],
value: {
// jslint: removed params /* content, option, success, error */
_solveConflict: function () {
var param = {}, got = priv.getParam(arguments);
if (got.content === undefined) {
param._deleted = true;
......@@ -297,37 +367,42 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
param.docid = docid;
param.key = row.key;
param.command = command.clone();
return priv.solveConflict (
{_id:docid,content:got.content,_rev:revision},
got.options,param
);
return priv.solveConflict({
_id: docid,
content: got.content,
_rev: revision
},
got.options, param);
}
}
}}, k;
}, k;
for (k in metadata) {
if (metadata.hasOwnProperty(k)) {
row.key.push(k);
}
}
return row;
};
priv.newAsyncModule = function () {
var async = {};
async.call = function (obj,function_name,arglist) {
async.call = function (obj, function_name, arglist) {
obj._wait = obj._wait || {};
if (obj._wait[function_name]) {
obj._wait[function_name]--;
obj._wait[function_name] -= 1;
return empty_fun;
}
// ok if undef or 0
arglist = arglist || [];
setTimeout(function(){
obj[function_name].apply(obj[function_name],arglist);
setTimeout(function () {
obj[function_name].apply(obj[function_name], arglist);
});
};
async.neverCall = function (obj,function_name) {
async.neverCall = function (obj, function_name) {
obj._wait = obj._wait || {};
obj._wait[function_name] = -1;
};
async.wait = function (obj,function_name,times) {
async.wait = function (obj, function_name, times) {
obj._wait = obj._wait || {};
obj._wait[function_name] = times;
};
......@@ -338,7 +413,7 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
};
that.post = function (command) {
that.put (command);
that.put(command);
};
/**
......@@ -352,73 +427,86 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
current_revision = '',
current_revision_file_path = '',
metadata_file_content = null,
on_conflict = false, conflict_object = {total_rows:0,rows:[]},
on_conflict = false,
conflict_object = {
total_rows: 0,
rows: []
},
previous_revision = command.getDocInfo('_rev') || '0',
previous_revision_file_path = command.getDocId() + '.' +
previous_revision,
now = new Date(),
failerror;
o.getDistantMetadata = function (){
priv.getDistantMetadata (
command,metadata_file_path,
o.getDistantMetadata = function () {
priv.getDistantMetadata(
command,
metadata_file_path,
function (result) {
var previous_revision_number =
parseInt(previous_revision.split('-')[0],10);
metadata_file_content = JSON.parse (result.content);
parseInt(previous_revision.split('-')[0], 10);
metadata_file_content = JSON.parse(result.content);
// set current revision
current_revision = (previous_revision_number + 1) + '-' +
hex_sha256 ('' + command.getDocContent() +
previous_revision +
JSON.stringify (metadata_file_content));
// jslint: removed hex_sha256(''+...
hex_sha256(command.getDocContent() + previous_revision +
JSON.stringify(metadata_file_content));
current_revision_file_path = command.getDocId() + '.' +
current_revision;
am.wait(o,'saveMetadataOnDistant',1);
am.call(o,'saveNewRevision');
am.call(o,'checkForConflicts');
},function (error) {
am.wait(o, 'saveMetadataOnDistant', 1);
am.call(o, 'saveNewRevision');
am.call(o, 'checkForConflicts');
},
function (error) {
if (error.status === 404) {
current_revision = '1-' +
hex_sha256 (command.getDocContent());
current_revision = '1-' + hex_sha256(command.getDocContent());
current_revision_file_path = command.getDocId() + '.' +
current_revision;
am.wait(o,'saveMetadataOnDistant',1);
am.call(o,'saveNewRevision');
am.call(o,'createMetadata');
am.wait(o, 'saveMetadataOnDistant', 1);
am.call(o, 'saveNewRevision');
am.call(o, 'createMetadata');
} else {
am.call(o,'error',[error]);
am.call(o, 'error', [error]);
}
}
);
};
o.saveNewRevision = function (){
priv.saveNewRevision (
command,current_revision_file_path,command.getDocContent(),
function (result) {
am.call(o,'saveMetadataOnDistant');
}, function (error) {
am.call(o,'error',[error]);
o.saveNewRevision = function () {
priv.saveNewRevision(
command,
current_revision_file_path,
command.getDocContent(),
function () {
am.call(o, 'saveMetadataOnDistant');
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.checkForConflicts = function () {
var rev;
for (rev in metadata_file_content) {
if (rev !== previous_revision) {
if (metadata_file_content.hasOwnProperty(rev) &&
rev !== previous_revision) {
on_conflict = true;
failerror = {
status:409,error:'conflict',
statusText:'Conflict',reason:'document update conflict',
message:'Document update conflict.'
status: 409,
error: 'conflict',
statusText: 'Conflict',
reason: 'document update conflict',
message: 'Document update conflict.'
};
break;
}
}
am.call(o,'updateMetadata');
am.call(o, 'updateMetadata');
};
o.createMetadata = function (){
o.createMetadata = function () {
var id = current_revision;
id = id.split('-'); id.shift(); id = id.join('-');
id = id.split('-');
id.shift();
id = id.join('-');
metadata_file_content = {};
metadata_file_content[current_revision] = {
_creation_date: now.getTime(),
......@@ -427,18 +515,23 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
_conflict: false,
_deleted: false
};
am.call(o,'saveMetadataOnDistant');
am.call(o, 'saveMetadataOnDistant');
};
o.updateMetadata = function (){
var previous_creation_date, revision_history = [], id = '';
o.updateMetadata = function () {
var previous_creation_date, revision_history = [],
id = '';
if (metadata_file_content[previous_revision]) {
previous_creation_date = metadata_file_content[
previous_revision]._creation_date;
previous_revision
]._creation_date;
revision_history = metadata_file_content[
previous_revision]._revisions;
previous_revision
]._revisions;
delete metadata_file_content[previous_revision];
}
id = current_revision.split('-'); id.shift(); id = id.join('-');
id = current_revision.split('-');
id.shift();
id = id.join('-');
revision_history.unshift(id);
metadata_file_content[current_revision] = {
_creation_date: previous_creation_date || now.getTime(),
......@@ -448,42 +541,56 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
_deleted: false
};
if (on_conflict) {
conflict_object =
priv.createConflictObject(
command, metadata_file_content, current_revision
conflict_object = priv.createConflictObject(
command,
metadata_file_content,
current_revision
);
}
am.call(o,'saveMetadataOnDistant');
am.call(o, 'saveMetadataOnDistant');
};
o.saveMetadataOnDistant = function (){
o.saveMetadataOnDistant = function () {
priv.saveMetadataToDistant(
command, metadata_file_path, metadata_file_content,
function (result) {
am.call(o,'deletePreviousRevision');
command,
metadata_file_path,
metadata_file_content,
function () {
am.call(o, 'deletePreviousRevision');
if (on_conflict) {
am.call(o,'error');
am.call(o, 'error');
} else {
am.call(o,'success');
am.call(o, 'success');
}
},function (error) {
am.call(o,'error',[error]);
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.deletePreviousRevision = function (){
if (previous_revision !== '0' /*&& !on_conflict*/) {
priv.deleteAFile (
command, previous_revision_file_path,
empty_fun,empty_fun);
o.deletePreviousRevision = function () {
// jslint: removed /*&& !on_conflict*/
if (previous_revision !== '0') {
priv.deleteAFile(
command,
previous_revision_file_path,
empty_fun,
empty_fun
);
}
};
o.success = function () {
var a = {ok:true,id:command.getDocId(),rev:current_revision};
am.neverCall(o,'error');
am.neverCall(o,'success');
var a = {
ok: true,
id: command.getDocId(),
rev: current_revision
};
am.neverCall(o, 'error');
am.neverCall(o, 'success');
if (command.getOption('revs')) {
a.revisions = priv._revs(metadata_file_content,
current_revision);
a.revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (command.getOption('revs_info')) {
a.revs_info = priv._revs_info(metadata_file_content);
......@@ -494,15 +601,21 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
that.success(a);
};
o.error = function (error) {
var err = error || failerror ||
{status:0,statusText:'Unknown',error:'unknown_error',
message:'Unknown error.',reason:'unknown error'};
var err = error || failerror || {
status: 0,
statusText: 'Unknown',
error: 'unknown_error',
message: 'Unknown error.',
reason: 'unknown error'
};
if (current_revision) {
err.rev = current_revision;
}
if (command.getOption('revs')) {
err.revisions = priv._revs(metadata_file_content,
current_revision);
err.revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (command.getOption('revs_info')) {
err.revs_info = priv._revs_info(metadata_file_content);
......@@ -510,11 +623,11 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
if (command.getOption('conflicts')) {
err.conflicts = conflict_object;
}
am.neverCall(o,'error');
am.neverCall(o,'success');
am.neverCall(o, 'error');
am.neverCall(o, 'success');
that.error(err);
};
am.call(o,'getDistantMetadata');
am.call(o, 'getDistantMetadata');
}; // end put
/**
......@@ -524,33 +637,42 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
*/
that.get = function (command) {
var o = {}, am = priv.newAsyncModule(),
metadata_file_path = command.getDocId() + '.metadata',
current_revision = command.getOption('rev') || '',
metadata_file_content = null,
metadata_only = command.getOption('metadata_only'),
on_conflict = false, conflict_object = {total_rows:0,rows:[]},
now = new Date(),
doc = {_id:command.getDocId()},
on_conflict = false,
conflict_object = {
total_rows: 0,
rows: []
},
doc = {
_id: command.getDocId()
},
call404 = function (message) {
am.call(o,'error',[{
status:404,statusText:'Not Found',error:'not_found',
message:message,reason:message
am.call(o, 'error', [{
status: 404,
statusText: 'Not Found',
error: 'not_found',
message: message,
reason: message
}]);
};
o.getDistantMetadata = function (){
priv.getDistantMetadata (
command,metadata_file_path,
o.getDistantMetadata = function () {
priv.getDistantMetadata(
command,
metadata_file_path,
function (result) {
metadata_file_content = JSON.parse (result.content);
metadata_file_content = JSON.parse(result.content);
if (!metadata_only) {
am.wait(o,'success',1);
am.wait(o, 'success', 1);
}
am.call(o,'affectMetadata');
am.call(o,'checkForConflicts');
},function (error) {
am.call(o,'error',[error]);
am.call(o, 'affectMetadata');
am.call(o, 'checkForConflicts');
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
......@@ -568,44 +690,47 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
metadata_file_content[current_revision]._creation_date;
doc._rev = current_revision;
if (metadata_only) {
am.call(o,'success');
am.call(o, 'success');
} else {
am.call(o,'loadRevision');
am.call(o, 'loadRevision');
}
};
o.loadRevision = function (){
o.loadRevision = function () {
if (!current_revision ||
metadata_file_content[current_revision]._deleted) {
return call404('Document has been removed.');
}
priv.loadRevision (
command, doc._id+'.'+current_revision,
priv.loadRevision(
command,
doc._id + '.' + current_revision,
function (result) {
doc.content = result.content;
am.call(o,'success');
}, function (error) {
am.call(o,'error',[error]);
am.call(o, 'success');
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.checkForConflicts = function () {
if (metadata_file_content[current_revision]._conflict) {
on_conflict = true;
conflict_object =
priv.createConflictObject(
conflict_object = priv.createConflictObject(
command,
metadata_file_content,
current_revision
);
}
am.call(o,'success');
am.call(o, 'success');
};
o.success = function (){
am.neverCall(o,'error');
am.neverCall(o,'success');
o.success = function () {
am.neverCall(o, 'error');
am.neverCall(o, 'success');
if (command.getOption('revs')) {
doc._revisions = priv._revs(metadata_file_content,
current_revision);
doc._revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (command.getOption('revs_info')) {
doc._revs_info = priv._revs_info(metadata_file_content);
......@@ -616,11 +741,16 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
that.success(doc);
};
o.error = function (error) {
var err = error || {status:0,statusText:'Unknown',
message:'Unknown error.'};
var err = error || {
status: 0,
statusText: 'Unknown',
message: 'Unknown error.'
};
if (command.getOption('revs')) {
err._revisions = priv._revs(metadata_file_content,
current_revision);
err._revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (command.getOption('revs_info')) {
err._revs_info = priv._revs_info(metadata_file_content);
......@@ -628,11 +758,11 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
if (command.getOption('conflicts')) {
err._conflicts = conflict_object;
}
am.neverCall(o,'error');
am.neverCall(o,'success');
am.neverCall(o, 'error');
am.neverCall(o, 'success');
that.error(err);
};
am.call(o,'getDistantMetadata');
am.call(o, 'getDistantMetadata');
};
/**
......@@ -643,106 +773,120 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
that.allDocs = function (command) {
var o = {}, am = priv.newAsyncModule(),
metadata_only = command.getOption('metadata_only'),
result_list = [], conflict_object = {total_rows:0,rows:[]},
nb_loaded_file = 0,
success_count = 0, success_max = 0;
result_list = [],
conflict_object = {
total_rows: 0,
rows: []
},
success_count = 0,
success_max = 0;
o.retreiveList = function () {
var cloned_option = command.cloneOption (),
var cloned_option = command.cloneOption(),
success = function (result) {
am.call(o,'filterTheList',[result]);
},error = function (error) {
am.call(o,'error',[error]);
am.call(o, 'filterTheList', [result]);
},
error = function (error) {
am.call(o, 'error', [error]);
};
cloned_option.metadata_only = true;
that.addJob ('allDocs',priv.sub_storage_spec,null,cloned_option,
success,error);
that.addJob('allDocs', priv.sub_storage_spec, null, cloned_option,
success, error
);
};
o.filterTheList = function (result) {
var i;
success_max ++;
for (i = 0; i < result.total_rows; i+= 1) {
var splitname = result.rows[i].id.split('.') || [];
if (splitname.length > 0 &&
splitname[splitname.length-1] === 'metadata') {
success_max ++;
splitname.length --;
am.call(o,'loadMetadataFile',[splitname.join('.')]);
var i, splitname;
success_max += 1;
for (i = 0; i < result.total_rows; i += 1) {
splitname = result.rows[i].id.split('.') || [];
if (splitname.length > 0 && splitname[splitname.length - 1] ===
'metadata') {
success_max += 1;
splitname.length -= 1;
am.call(o, 'loadMetadataFile', [splitname.join('.')]);
}
}
am.call(o,'success');
am.call(o, 'success');
};
o.loadMetadataFile = function (path) {
priv.getDistantMetadata (
command, path+'.metadata',
priv.getDistantMetadata(
command,
path + '.metadata',
function (data) {
data = JSON.parse (data.content);
data = JSON.parse(data.content);
var revision = priv.chooseARevision(data);
if (!data[revision]._deleted) {
am.call(
o,'loadFile',[path,revision,data]
);
am.call(o, 'loadFile', [path, revision, data]);
} else {
am.call(o,'success');
am.call(o, 'success');
}
},function (error) {
am.call(o,'error',[error]);
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.loadFile = function (path,revision,data) {
o.loadFile = function (path, revision, data) {
var doc = {
id: path,key: path,value:{
_last_modified:data[revision]._last_modified,
_creation_date:data[revision]._creation_date,
_rev:revision
id: path,
key: path,
value: {
_last_modified: data[revision]._last_modified,
_creation_date: data[revision]._creation_date,
_rev: revision
}
};
if (command.getOption('revs')) {
doc.value._revisions = priv._revs(data,revision);
doc.value._revisions = priv._revs(data, revision);
}
if (command.getOption('revs_info')) {
doc.value._revs_info = priv._revs_info(data,revision);
doc.value._revs_info = priv._revs_info(data, revision);
}
if (command.getOption('conflicts')) {
if (data[revision]._conflict) {
conflict_object.total_rows ++;
conflict_object.rows.push(priv.createConflictRow(
command, path, data, revision
));
conflict_object.total_rows += 1;
conflict_object.rows.push(
priv.createConflictRow(command, path, data, revision)
);
}
}
if (!metadata_only) {
priv.loadRevision (
command,path+'.'+revision,
priv.loadRevision(
command,
path + '.' + revision,
function (data) {
doc.content = data.content;
result_list.push(doc);
am.call(o,'success');
},function (error) {
am.call(o,'error',[error]);
});
am.call(o, 'success');
},
function (error) {
am.call(o, 'error', [error]);
}
);
} else {
result_list.push(doc);
am.call(o,'success');
am.call(o, 'success');
}
};
o.success = function (){
o.success = function () {
var obj;
success_count ++;
success_count += 1;
if (success_count >= success_max) {
am.end();
obj = {total_rows:result_list.length,rows:result_list};
obj = {
total_rows: result_list.length,
rows: result_list
};
if (command.getOption('conflicts')) {
obj.conflicts = conflict_object;
}
that.success(obj);
}
};
o.error = function (error){
o.error = function (error) {
am.end();
that.error(error);
};
am.call(o,'retreiveList');
am.call(o, 'retreiveList');
}; // end allDocs
/**
......@@ -756,42 +900,51 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
current_revision = '',
current_revision_file_path = '',
metadata_file_content = null,
on_conflict = false, conflict_object = {total_rows:0,rows:[]},
on_conflict = false,
conflict_object = {
total_rows: 0,
rows: []
},
previous_revision = command.getOption('rev') || '0',
previous_revision_file_path = command.getDocId() + '.' +
previous_revision,
now = new Date(),
failerror;
o.getDistantMetadata = function (){
priv.getDistantMetadata (
command,metadata_file_path,
o.getDistantMetadata = function () {
priv.getDistantMetadata(
command,
metadata_file_path,
function (result) {
metadata_file_content = JSON.parse (result.content);
metadata_file_content = JSON.parse(result.content);
if (previous_revision === 'last') {
previous_revision =
priv.chooseARevision (metadata_file_content);
previous_revision = priv.chooseARevision(metadata_file_content);
previous_revision_file_path = command.getDocId() + '.' +
previous_revision;
}
var previous_revision_number =
parseInt(previous_revision.split('-')[0],10) || 0;
parseInt(previous_revision.split('-')[0], 10) || 0;
// set current revision
current_revision = (previous_revision_number + 1) + '-' +
hex_sha256 ('' + previous_revision +
JSON.stringify (metadata_file_content));
//jslint: removed hex_sha256(''...
hex_sha256(previous_revision +
JSON.stringify(metadata_file_content)
);
current_revision_file_path = command.getDocId() + '.' +
current_revision;
am.call(o,'checkForConflicts');
},function (error) {
am.call(o, 'checkForConflicts');
},
function (error) {
if (error.status === 404) {
am.call(o,'error',[{
status:404,statusText:'Not Found',
error:'not_found',reason:'missing',
message:'Document not found.'
am.call(o, 'error', [{
status: 404,
statusText: 'Not Found',
error: 'not_found',
reason: 'missing',
message: 'Document not found.'
}]);
} else {
am.call(o,'error',[error]);
am.call(o, 'error', [error]);
}
}
);
......@@ -799,29 +952,37 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
o.checkForConflicts = function () {
var rev;
for (rev in metadata_file_content) {
if (rev !== previous_revision) {
if (metadata_file_content.hasOwnProperty(rev) &&
rev !== previous_revision) {
on_conflict = true;
failerror = {
status:409,error:'conflict',
statusText:'Conflict',reason:'document update conflict',
message:'There is one or more conflicts'
status: 409,
error: 'conflict',
statusText: 'Conflict',
reason: 'document update conflict',
message: 'There is one or more conflicts'
};
break;
}
}
am.call(o,'updateMetadata');
am.call(o, 'updateMetadata');
};
o.updateMetadata = function (){
var previous_creation_date, revision_history = [], id = '';
o.updateMetadata = function () {
var previous_creation_date, revision_history = [],
id = '';
if (metadata_file_content[previous_revision]) {
previous_creation_date = metadata_file_content[
previous_revision]._creation_date;
previous_revision
]._creation_date;
revision_history = metadata_file_content[
previous_revision]._revisions;
previous_revision
]._revisions;
delete metadata_file_content[previous_revision];
}
id = current_revision;
id = id.split('-'); id.shift(); id = id.join('-');
id = id.split('-');
id.shift();
id = id.join('-');
revision_history.unshift(id);
metadata_file_content[current_revision] = {
_creation_date: previous_creation_date || now.getTime(),
......@@ -831,43 +992,56 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
_deleted: true
};
if (on_conflict) {
conflict_object =
priv.createConflictObject(
command, metadata_file_content, current_revision
conflict_object = priv.createConflictObject(
command,
metadata_file_content,
current_revision
);
}
am.call(o,'saveMetadataOnDistant');
am.call(o, 'saveMetadataOnDistant');
};
o.saveMetadataOnDistant = function (){
o.saveMetadataOnDistant = function () {
priv.saveMetadataToDistant(
command, metadata_file_path, metadata_file_content,
function (result) {
am.call(o,'deletePreviousRevision');
command,
metadata_file_path,
metadata_file_content,
function () {
am.call(o, 'deletePreviousRevision');
if (on_conflict) {
am.call(o,'error');
am.call(o, 'error');
} else {
am.call(o,'success');
am.call(o, 'success');
}
},function (error) {
am.call(o,'error',[error]);
},
function (error) {
am.call(o, 'error', [error]);
}
);
};
o.deletePreviousRevision = function (){
if (previous_revision !== '0' /*&& !on_conflict*/) {
priv.deleteAFile (
command, previous_revision_file_path,
empty_fun,empty_fun);
o.deletePreviousRevision = function () {
// jslint: removed /*&& !on_conflict*/
if (previous_revision !== '0') {
priv.deleteAFile(
command,
previous_revision_file_path,
empty_fun,
empty_fun
);
}
};
o.success = function (revision){
var a = {ok:true,id:command.getDocId(),
rev:revision || current_revision};
am.neverCall(o,'error');
am.neverCall(o,'success');
o.success = function (revision) {
var a = {
ok: true,
id: command.getDocId(),
rev: revision || current_revision
};
am.neverCall(o, 'error');
am.neverCall(o, 'success');
if (command.getOption('revs')) {
a.revisions = priv._revs(metadata_file_content,
current_revision);
a.revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (command.getOption('revs_info')) {
a.revs_info = priv._revs_info(metadata_file_content);
......@@ -877,16 +1051,22 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
}
that.success(a);
};
o.error = function (error){
var err = error || failerror ||
{status:0,statusText:'Unknown',error:'unknown_error',
message:'Unknown error.',reason:'unknown error'};
o.error = function (error) {
var err = error || failerror || {
status: 0,
statusText: 'Unknown',
error: 'unknown_error',
message: 'Unknown error.',
reason: 'unknown error'
};
if (current_revision) {
err.rev = current_revision;
}
if (command.getOption('revs')) {
err.revisions = priv._revs(metadata_file_content,
current_revision);
err.revisions = priv._revs(
metadata_file_content,
current_revision
);
}
if (command.getOption('revs_info')) {
err.revs_info = priv._revs_info(metadata_file_content);
......@@ -894,12 +1074,12 @@ jIO.addStorageType('conflictmanager', function (spec, my) {
if (command.getOption('conflicts')) {
err.conflicts = conflict_object;
}
am.neverCall(o,'error');
am.neverCall(o,'success');
am.neverCall(o, 'error');
am.neverCall(o, 'success');
that.error(err);
};
am.call(o,'getDistantMetadata');
am.call(o, 'getDistantMetadata');
}; // end remove
return that;
};
});
\ No newline at end of file
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