Commit 08c3aa1d authored by Tristan Cavelier's avatar Tristan Cavelier

Update IndexStorage and Jio tests

parent 2722fe85
......@@ -5,8 +5,8 @@ var newIndexStorage = function ( spec, my ) {
priv.secondstorage_spec = spec.storage || {type:'base'};
priv.secondstorage_string = JSON.stringify (priv.secondstorage_spec);
var storage_array_name = 'jio/indexed_storage_array';
var storage_file_array_name = 'jio/indexed_file_array/'+
var storage_object_name = 'jio/indexed_storage_object';
var storage_file_object_name = 'jio/indexed_file_object/'+
priv.secondstorage_string;
var super_serialized = that.serialized;
......@@ -24,273 +24,184 @@ var newIndexStorage = function ( spec, my ) {
return '';
};
/**
* Check if the indexed storage array exists.
* @method isStorageArrayIndexed
* @return {boolean} true if exists, else false
*/
priv.isStorageArrayIndexed = function () {
return (LocalOrCookieStorage.getItem(
storage_array_name) ? true : false);
};
/**
* Returns a list of indexed storages.
* @method getIndexedStorageArray
* @return {array} The list of indexed storages.
*/
priv.getIndexedStorageArray = function () {
return LocalOrCookieStorage.getItem(
storage_array_name) || [];
priv.secureDocId = function (string) {
var split = string.split('/'), i;
if (split[0] === '') {
split = split.slice(1);
}
for (i = 0; i < split.length; i+= 1) {
if (split[i] === '') { return ''; }
}
return split.join('%2F');
};
/**
* Adds a storage to the indexed storage list.
* @method indexStorage
* @param {object/json} storage The new indexed storage.
*/
priv.indexStorage = function (storage) {
var indexed_storage_array = priv.getIndexedStorageArray();
indexed_storage_array.push(typeof storage === 'string'? storage:
JSON.stringify (storage));
LocalOrCookieStorage.setItem(storage_array_name,
indexed_storage_array);
priv.indexStorage = function () {
var obj = LocalOrCookieStorage.getItem (storage_object_name) || {};
obj[priv.secondstorage_spec] = new Date().getTime();
LocalOrCookieStorage.setItem (storage_object_name,obj);
};
/**
* Checks if a storage exists in the indexed storage list.
* @method isAnIndexedStorage
* @param {object/json} storage The storage to find.
* @return {boolean} true if found, else false
*/
priv.isAnIndexedStorage = function (storage) {
var json_storage = (typeof storage === 'string'?storage:
JSON.stringify(storage)),
i,l,array = priv.getIndexedStorageArray();
for (i = 0, l = array.length; i < l; i+= 1) {
if (JSON.stringify(array[i]) === json_storage) {
return true;
}
priv.formatToFileObject = function (row) {
var k, obj = {_id:row.id};
for (k in row.value) {
obj[k] = row.value[k];
}
return false;
return obj;
};
/**
* Checks if the file array exists.
* @method fileArrayExists
* @return {boolean} true if exists, else false
*/
priv.fileArrayExists = function () {
return (LocalOrCookieStorage.getItem (
storage_file_array_name) ? true : false);
};
/**
* Returns the file from the indexed storage but not there content.
* @method getFileArray
* @return {array} All the existing file.
*/
priv.getFileArray = function () {
return LocalOrCookieStorage.getItem(
storage_file_array_name) || [];
priv.allDocs = function (files_object) {
var k, obj = {rows:[]}, i = 0;
for (k in files_object) {
obj.rows[i] = {};
obj.rows[i].value = files_object[k];
obj.rows[i].id = obj.rows[i].key = obj.rows[i].value._id;
delete obj.rows[i].value._id;
i ++;
}
obj.total_rows = obj.rows.length;
return obj;
};
/**
* Sets the file array list.
* @method setFileArray
* @param {array} file_array The array containing files.
*/
priv.setFileArray = function (file_array) {
return LocalOrCookieStorage.setItem(
storage_file_array_name,
file_array);
var i, obj = {};
for (i = 0; i < file_array.length; i+= 1) {
obj[file_array[i].id] = priv.formatToFileObject(file_array[i]);
}
LocalOrCookieStorage.setItem (storage_file_object_name,obj);
};
/**
* Checks if the file already exists in the array.
* @method isFileIndexed
* @param {string} file_name The file we want to find.
* @return {boolean} true if found, else false
*/
priv.isFileIndexed = function (file_name) {
var i, l, array = priv.getFileArray();
for (i = 0, l = array.length; i < l; i+= 1) {
if (array[i].name === file_name){
return true;
}
}
return false;
priv.getFileObject = function (docid) {
var obj = LocalOrCookieStorage.getItem (storage_file_object_name) || {};
return obj[docid];
};
/**
* Adds a file to the local file array.
* @method addFile
* @param {object} file The new file.
*/
priv.addFile = function (file) {
var file_array = priv.getFileArray();
file_array.push(file);
LocalOrCookieStorage.setItem(storage_file_array_name,
file_array);
priv.addFile = function (file_obj) {
var obj = LocalOrCookieStorage.getItem (storage_file_object_name) || {};
obj[file_obj._id] = file_obj;
LocalOrCookieStorage.setItem (storage_file_object_name,obj);
};
/**
* Removes a file from the local file array.
* @method removeFile
* @param {string} file_name The file to remove.
*/
priv.removeFile = function (file_name) {
var i, l, array = priv.getFileArray(), new_array = [];
for (i = 0, l = array.length; i < l; i+= 1) {
if (array[i].name !== file_name) {
new_array.push(array[i]);
}
}
LocalOrCookieStorage.setItem(storage_file_array_name,
new_array);
priv.removeFile = function (docid) {
var obj = LocalOrCookieStorage.getItem (storage_file_object_name) || {};
delete obj[docid];
LocalOrCookieStorage.setItem (storage_file_object_name,obj);
};
/**
* Updates the storage.
* updates the storage.
* It will retreive all files from a storage. It is an asynchronous task
* so the update can be on going even if IndexedStorage has already
* returned the result.
* @method update
*/
priv.update = function () {
// retreive list before, and then retreive all files
var getlist_onSuccess = function (result) {
if (!priv.isAnIndexedStorage(priv.secondstorage_string)) {
priv.indexStorage(priv.secondstorage_string);
}
priv.setFileArray(result);
var success = function (val) {
priv.setFileArray(val.rows);
};
that.addJob ( that.newStorage (priv.secondstorage_spec),
that.newCommand ('getDocumentList',
{path:'.',
option:{success:getlist_onSuccess,
max_retry: 3}}) );
that.addJob ('allDocs', priv.secondstorage_spec,null,
{max_retry:3},success,function(){});
};
that.post = function (command) {
that.put(command);
};
/**
* Saves a document.
* @method saveDocument
* @method put
*/
that.saveDocument = function (command) {
var newcommand = command.clone();
newcommand.onSuccessDo (function (result) {
if (!priv.isFileIndexed(command.getPath())) {
priv.addFile({name:command.getPath(),
last_modified:0,creation_date:0});
}
that.put = function (command) {
var cloned_doc = command.cloneDoc(),
cloned_option = command.cloneOption(),
success = function (val) {
priv.update();
that.success();
});
newcommand.onErrorDo (function (result) {
that.error(result);
});
that.addJob ( that.newStorage(priv.secondstorage_spec),
newcommand );
}; // end saveDocument
that.success(val);
},
error = function (err) {
that.error(err);
};
priv.indexStorage();
that.addJob ('put',priv.secondstorage_spec,cloned_doc,
cloned_option,success,error);
}; // end put
/**
* Loads a document.
* @method loadDocument
* @method get
*/
that.loadDocument = function (command) {
var file_array, i, l, new_job,
loadOnSuccess = function (result) {
// if (file_array[i].last_modified !==
// result.return_value.last_modified ||
// file_array[i].creation_date !==
// result.return_value.creation_date) {
// // the file in the index storage is different than
// // the one in the second storage. priv.update will
// // take care of refresh the indexed storage
// }
that.success(result);
that.get = function (command) {
var file_array,
success = function (val) {
that.success(val);
},
loadOnError = function (result) {
that.error(result);
error = function (err) {
that.error(err);
},
secondLoadDocument = function () {
var newcommand = command.clone();
newcommand.onErrorDo (loadOnError);
newcommand.onSuccessDo (loadOnSuccess);
that.addJob ( that.newStorage(priv.secondstorage_spec),
newcommand );
get = function () {
var cloned_option = command.cloneOption();
that.addJob ('get',priv.secondstorage_spec,command.cloneDoc(),
cloned_option,success,error);
that.end();
};
priv.indexStorage();
priv.update();
if (command.getOption('metadata_only')) {
setTimeout(function () {
if (priv.fileArrayExists()) {
file_array = priv.getFileArray();
for (i = 0, l = file_array.length; i < l; i+= 1) {
if (file_array[i].name === command.getPath()) {
return that.success(file_array[i]);
}
}
var file_obj = priv.getFileObject(command.getDocId());
if (file_obj &&
(file_obj._last_modified ||
file_obj._creation_date)) {
that.success (file_obj);
} else {
secondLoadDocument();
get();
}
},100);
});
} else {
secondLoadDocument();
get();
}
}; // end loadDocument
}; // end get
/**
* Gets a document list.
* @method getDocumentList
* @method allDocs
*/
that.getDocumentList = function (command) {
var id, newcommand, timeout = false;
priv.update();
if (command.getOption('metadata_only')) {
id = setInterval(function () {
if (timeout) {
that.error({status:0,statusText:'Timeout',
message:'The request has timed out.'});
clearInterval(id);
}
if (priv.fileArrayExists()) {
that.success(priv.getFileArray());
clearInterval(id);
}
},100);
setTimeout (function () {
timeout = true;
}, 10000); // 10 sec
} else {
newcommand = command.clone();
newcommand.onSuccessDo (function (result) {
that.success(result);
});
newcommand.onErrorDo (function (result) {
that.error(result);
that.allDocs = function (command) {
var obj = LocalOrCookieStorage.getItem (storage_file_object_name);
if (obj) {
priv.update();
setTimeout(function (){
that.success (priv.allDocs(obj));
});
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
} else {
var success = function (val) {
priv.setFileArray(val.rows);
that.success(val);
},
error = function (err) {
that.error(err);
};
that.addJob ('allDocs', priv.secondstorage_spec,null,
command.cloneOption(),success,error);
}
}; // end getDocumentList
}; // end allDocs
/**
* Removes a document.
* @method removeDocument
* @method remove
*/
that.removeDocument = function (command) {
var newcommand = command.clone();
newcommand.onSuccessDo (function (result) {
priv.removeFile(command.getPath());
that.remove = function (command) {
var success = function (val) {
priv.removeFile(command.getDocId());
priv.update();
that.success();
});
newcommand.onErrorDo (function (result) {
that.error(result);
});
that.addJob( that.newStorage(priv.secondstorage_spec),
newcommand );
};
that.success(val);
},
error = function (err) {
that.error(err);
};
that.addJob ('remove',priv.secondstorage_spec,command.cloneDoc(),
command.cloneOption(),success,error);
}; // end remove
return that;
};
Jio.addStorageType ('indexed', newIndexStorage);
......@@ -1031,34 +1031,36 @@ test ('Document load', function () {
o.jio = JIO.newJio({type:'indexed',storage:{type:'dummyall3tries'}});
// loading must take long time with dummyall3tries
o.f = this.spy();
o.jio.loadDocument('memo',{max_retry:3,success:o.f,error:o.f,
metadata_only:true});
o.jio.get('memo',{max_retry:3,metadata_only:true},o.f);
o.clock.tick(1000);
ok(!o.f.called,'Callback must not be called');
// wait long time too retreive list
o.clock.tick(10000);
o.clock.tick(1000);
// now we can test if the document metadata are loaded faster.
o.doc = {name:'memo',last_modified:25000,creation_date:20000};
o.f2 = function (result) {
deepEqual (result,o.doc,'Document metadata retrieved');
o.doc = {_id:'memo',_last_modified:25000,_creation_date:20000};
o.f2 = function (err,val) {
deepEqual (err||val,o.doc,'Document metadata retrieved');
};
this.spy(o,'f2');
o.jio.loadDocument('memo',{max_retry:3,success:o.f2,error:o.f2,
metadata_only:true});
o.jio.get('memo',{max_retry:3,metadata_only:true},o.f2);
o.clock.tick(1000);
if (!o.f2.calledOnce) {
ok (false, 'no response / too much results');
if (o.f2.called) {
ok (false, 'too much results');
} else {
ok (false, 'no response');
}
}
// test a simple document loading
o.doc2 = {name:'file',last_modified:17000,
creation_date:11000,content:'content2'};
o.f3 = function (result) {
deepEqual (result,o.doc2,'Simple document loading');
o.doc2 = {_id:'file',_last_modified:17000,
_creation_date:11000,content:'content file'};
o.f3 = function (err,val) {
deepEqual (err||val,o.doc2,'Simple document loading');
};
this.spy(o,'f3');
o.jio.loadDocument('file',{max_retry:3,success:o.f3,error:o.f3});
o.jio.get('file',{max_retry:3},o.f3);
o.clock.tick(2000);
if (!o.f3.calledOnce) {
ok (false, 'no response / too much results');
......@@ -1072,17 +1074,15 @@ test ('Document save', function () {
o.jio = JIO.newJio({type:'indexed',
storage:{type:'dummyall3tries',
username:'indexsave'}});
o.f = function (result) {
if (!result) {
result = 'done';
} else {
result = 'fail';
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (result,'done','document save');
deepEqual (err || val,{ok:true,id:'file'},'document save');
};
this.spy(o,'f');
o.jio.saveDocument('file','content',{max_retry:3,success:o.f,error:o.f});
o.clock.tick(10000);
o.jio.put({_id:'file',content:'content'},{max_retry:3},o.f);
o.clock.tick(2000);
if (!o.f.calledOnce){
ok (false, 'no response / too much results');
}
......@@ -1095,21 +1095,24 @@ test ('Get document list', function () {
o.jio = JIO.newJio({type:'indexed',
storage:{type:'dummyall3tries',
username:'indexgetlist'}});
o.doc1 = {name:'file',last_modified:15000,creation_date:10000};
o.doc2 = {name:'memo',last_modified:25000,creation_date:20000};
o.doc1 = {id:'file',key:'file',value:{
_last_modified:15000,_creation_date:10000}};
o.doc2 = {id:'memo',key:'memo',value:{
_last_modified:25000,_creation_date:20000}};
// getting list must take long time with dummyall3tries
o.f = this.spy();
o.jio.getDocumentList('.',{max_retry:3,onResponse:o.f});
o.jio.allDocs({max_retry:3},o.f);
o.clock.tick(1000);
ok(!o.f.called,'Callback must not be called');
// wail long time too retreive list
o.clock.tick(10000);
o.clock.tick(1000);
// now we can test if the document list is loaded faster
o.f2 = function (result) {
deepEqual (result,[o.doc1,o.doc2],'get document list');
o.f2 = function (err,val) {
deepEqual (err || objectifyDocumentArray(val.rows),
objectifyDocumentArray([o.doc1,o.doc2]),'get document list');
};
this.spy(o,'f2');
o.jio.getDocumentList('.',{max_retry:3,success:o.f2,error:o.f2});
o.jio.allDocs({max_retry:3},o.f2);
o.clock.tick(1000)
if (!o.f2.calledOnce) {
ok (false, 'no response / too much results');
......@@ -1119,23 +1122,27 @@ test ('Get document list', function () {
test ('Remove document', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers();
o.clock.tick(base_tick);
o.jio = JIO.newJio({type:'indexed',
storage:{type:'dummyall3tries',
username:'indexremove'}});
o.f = function (result) {
if (!result) {
result = 'done';
} else {
result = 'fail';
o.secondstorage = {type:'dummyall3tries',username:'indexremove'}
o.storage_file_object_name = 'jio/indexed_file_object/'+
JSON.stringify (o.secondstorage);
o.jio = JIO.newJio({type:'indexed',storage:o.secondstorage});
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (result,'done','document remove');
deepEqual (err || val,{ok:true,id:'file'},'document remove');
};
this.spy(o,'f');
o.jio.removeDocument('file',{max_retry:3,success:o.f,error:o.f});
o.clock.tick(10000);
o.jio.remove({_id:'file'},{max_retry:3},o.f);
o.clock.tick(2000);
if (!o.f.calledOnce){
ok (false, 'no response / too much results');
}
o.tmp = LocalOrCookieStorage.getItem(o.storage_file_object_name) || {};
ok (!o.tmp.file,'File does not exists anymore');
o.jio.stop();
});
......
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