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