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