Commit 52486182 authored by Sven Franck's avatar Sven Franck

indexStorage: REMOVE new API and qunit tests

parent 39f13bfd
...@@ -155,6 +155,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -155,6 +155,7 @@ jIO.addStorageType('indexed', function (spec, my) {
index.reference = priv.indices[i]; index.reference = priv.indices[i];
index.name = index.reference["name"]; index.name = index.reference["name"];
index.size = priv.getObjectSize(indices[index.name]); index.size = priv.getObjectSize(indices[index.name]);
index.result_array;
if (index.size > 0) { if (index.size > 0) {
if (priv.searchIndexByValue(indices[index.name], doc._id, "bool")) { if (priv.searchIndexByValue(indices[index.name], doc._id, "bool")) {
...@@ -164,6 +165,35 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -164,6 +165,35 @@ jIO.addStorageType('indexed', function (spec, my) {
} }
return false; return false;
} }
priv.cleanIndices = function (indices, doc) {
var i, j, k, index, key, obj, prop, l = priv.indices.length,
docid = doc._id;
// loop indices (indexA, indexAB...)
for (i = 0; i < l; i += 1) {
// index object (reference and current-iteration)
index = {};
index.reference = priv.indices[i];
index.name = index.reference["name"];
index.current = indices[index.name];
index.current_size = priv.getObjectSize(index.current);
for (j = 0; j < index.current_size; j++) {
key = priv.searchIndexByValue(index.current, doc._id, "key");
index.result_array = index.current[key];
if (!!key) {
// if there is more than one docid in the result array,
// just remove this one and not the whole array
if (index.result_array.length > 1) {
index.result_array.splice(k,1);
} else {
delete index.current[key];
}
}
}
}
return indices;
}
/** /**
* Adds entries to indices * Adds entries to indices
* @method createEmptyIndexArray * @method createEmptyIndexArray
...@@ -414,23 +444,92 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -414,23 +444,92 @@ jIO.addStorageType('indexed', function (spec, my) {
} }
); );
}; };
/**
* Removes a document. /**
* Remove document or attachment - removing documents updates index!.
* @method remove * @method remove
*//* * @param {object} command The JIO command
*/
that.remove = function (command) { that.remove = function (command) {
var success = function (val) { var f = {}, indices, doc, docid, option;
priv.removeFile(command.getDocId());
priv.update(); doc = command.cloneDoc();
that.success(val); option = command.cloneOption();
}, if (option.max_retry === 0) {
error = function (err) { option.max_retry = 3;
that.error(err); }
};
that.addJob('remove', priv.sub_storage_spec, command.cloneDoc(), f.removeDocument = function (type) {
command.cloneOption(), success, error); if (type === 'doc') {
docid = command.getDocId();
} else {
docid = command.getDocId() + '/' + command.getAttachmentId();
}
that.addJob(
"remove",
priv.substorage,
docid,
option,
function (response) {
that.success(response);
},
function (err) {
that.error({
"status": 409,
"statusText": "Conflict",
"error": "conflict",
"message": "Document Update Conflict",
"reason": "Could not delete document or attachment"
});
}
);
};
f.getIndices = function () {
that.addJob(
"get",
priv.substorage,
priv.index_suffix,
option,
function (response) {
// if deleting an attachment
if (typeof command.getAttachmentId() === 'string'){
f.removeDocument('attachment')
} else {
indices = priv.cleanIndices(response, doc);
// store update index file
that.addJob(
"put",
priv.substorage,
indices,
command.cloneOption(),
function () {
// remove actual document
f.removeDocument('doc');
},
function (err) {
// xxx do we try to delete the posted document ?
err.message = "Cannot save index file";
that.error(err);
}
);
}
},
function (err) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Document index not found, please check document ID",
"reason": "Incorrect document ID"
});
return;
}
);
};
f.getIndices();
}; };
*//**
/**
* Gets a document list. * Gets a document list.
* @method allDocs * @method allDocs
*/ */
......
...@@ -3027,34 +3027,152 @@ test ('Get document list', function () { ...@@ -3027,34 +3027,152 @@ test ('Get document list', function () {
ok (false, 'no response / too much results'); ok (false, 'no response / too much results');
} }
}); });
*/
test ('Remove document', function () { test ("Remove", function(){
var o = {}; o.clock = this.sandbox.useFakeTimers();
o.clock.tick(base_tick);
o.sub_storage = {type:'dummyall3tries',username:'indexremove'}
o.storage_file_object_name = 'jio/indexed_file_object/'+
JSON.stringify (o.sub_storage);
o.jio = JIO.newJio({type:'indexed',storage:o.sub_storage}); // not sure these need to be run, because the index does not change
o.f = function (err,val) { // and only small modifications have been made to handle putAttachment
if (err) { // tests are from localStorage putAttachment
err = err.status; var o = generateTools(this);
}
deepEqual (err || val,{ok:true,id:'file'},'document remove'); o.jio = JIO.newJio({
"type": "indexed",
"indices": [
{"name":"indexA", "fields":["author"]},
{"name":"indexAB", "fields":["author","year"]}
],
"sub_storage": {
"type": "local",
"username": "irem",
"application_name": "irem"
}
});
// remove inexistent document
o.spy(o, "status", 404, "Remove inexistent document");
o.jio.remove({"_id": "remove1"}, o.f);
o.tick(o);
// remove inexistent document/attachment
o.spy(o, "status", 404, "Remove inexistent document/attachment");
o.jio.remove({"_id": "remove1/remove2"}, o.f);
o.tick(o);
// adding a document
o.jio.put({"_id": "remove1", "title": "myRemove1",
"author": "Mr. President", "year": "2525"
});
o.tick(o);
// adding a 2nd document with same keywords
o.jio.put({"_id": "removeAlso", "title": "myRemove2",
"author": "Martin Mustermann", "year": "2525"
});
o.tick(o);
// remove document
o.spy(o, "value", {"ok": true, "id": "remove1"}, "Remove document");
o.jio.remove({"_id": "remove1"}, o.f);
o.tick(o);
// check index
o.fakeIndex = {
"_id": "irem_indices.json",
"indexA": { "Martin Mustermann": ["removeAlso"]},
"indexAB": {"2525": ["removeAlso"],"Martin Mustermann": ["removeAlso"]}
}; };
this.spy(o,'f'); o.jio.get("irem_indices.json",function(err, response){
o.jio.remove({_id:'file'},{max_retry:3},o.f); o.actualIndex = response;
o.clock.tick(2000); deepEqual(o.actualIndex, o.fakeIndex, "Check index file");
if (!o.f.calledOnce){ });
ok (false, 'no response / too much results'); o.tick(o);
}
o.tmp = LocalOrCookieStorage.getItem(o.storage_file_object_name) || {}; // check document
ok (!o.tmp.file,'File does not exists anymore'); o.spy(o, "status", 404, "Check if document has been removed");
o.jio.get("remove1", o.f);
o.tick(o);
// adding a new document
o.jio.put({"_id": "remove3",
"title": "myRemove1",
"author": "Mrs Sunshine",
"year": "1234"
});
o.tick(o);
// adding an attachment
o.jio.putAttachment({"id":"remove3/removeAtt", "mimetype":"text/plain",
"content":"hello"});
o.tick(o);
// add another attachment
o.jio.putAttachment({"id":"remove3/removeAtt2", "mimetype":"text/plain",
"content":"hello2"});
o.tick(o);
// remove attachment
o.spy(o, "value", {"ok": true, "id": "remove3/removeAtt2"},
"Remove one of multiple attachment");
o.jio.remove({"_id": "remove3/removeAtt2"}, o.f);
o.tick(o);
// check index
o.fakeIndex = {
"_id": "irem_indices.json",
"indexA": {
"Martin Mustermann": ["removeAlso"],
"Mrs Sunshine": ["remove3"]
},
"indexAB": {
"1234": ["remove3"],
"2525": ["removeAlso"],
"Martin Mustermann": ["removeAlso"],
"Mrs Sunshine": ["remove3"]
}
};
o.jio.get("irem_indices.json",function(err, response){
o.actualIndex = response;
deepEqual(o.actualIndex, o.fakeIndex, "Check index file");
});
o.tick(o);
// remove document and attachment together
o.spy(o, "value", {"ok": true, "id": "remove3"},
"Remove one document and attachment together");
o.jio.remove({"_id": "remove3"}, o.f);
o.tick(o);
// check index
o.fakeIndex = {
"_id": "irem_indices.json",
"indexA": {
"Martin Mustermann": ["removeAlso"]
},
"indexAB": {
"2525": ["removeAlso"],
"Martin Mustermann": ["removeAlso"]
}
};
o.jio.get("irem_indices.json",function(err, response){
o.actualIndex = response;
deepEqual(o.actualIndex, o.fakeIndex, "Check index file");
});
o.tick(o);
// check attachment
o.spy(o, "status", 404, "Check if attachment has been removed");
o.jio.get("remove3/removeAtt", o.f);
o.tick(o);
// check document
o.spy(o, "status", 404, "Check if document has been removed");
o.jio.get("remove3", o.f);
o.tick(o);
o.jio.stop(); o.jio.stop();
}); });
*/
/* /*
module ('Jio CryptedStorage'); module ('Jio CryptedStorage');
......
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