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) {
index.reference = priv.indices[i];
index.name = index.reference["name"];
index.size = priv.getObjectSize(indices[index.name]);
index.result_array;
if (index.size > 0) {
if (priv.searchIndexByValue(indices[index.name], doc._id, "bool")) {
......@@ -164,6 +165,35 @@ jIO.addStorageType('indexed', function (spec, my) {
}
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
* @method createEmptyIndexArray
......@@ -414,23 +444,92 @@ jIO.addStorageType('indexed', function (spec, my) {
}
);
};
/**
* Removes a document.
/**
* Remove document or attachment - removing documents updates index!.
* @method remove
*//*
* @param {object} command The JIO command
*/
that.remove = function (command) {
var success = function (val) {
priv.removeFile(command.getDocId());
priv.update();
that.success(val);
},
error = function (err) {
that.error(err);
};
that.addJob('remove', priv.sub_storage_spec, command.cloneDoc(),
command.cloneOption(), success, error);
var f = {}, indices, doc, docid, option;
doc = command.cloneDoc();
option = command.cloneOption();
if (option.max_retry === 0) {
option.max_retry = 3;
}
f.removeDocument = function (type) {
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.
* @method allDocs
*/
......
......@@ -3027,34 +3027,152 @@ test ('Get document list', function () {
ok (false, 'no response / too much results');
}
});
*/
test ('Remove document', 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);
test ("Remove", function(){
o.jio = JIO.newJio({type:'indexed',storage:o.sub_storage});
o.f = function (err,val) {
if (err) {
err = err.status;
}
deepEqual (err || val,{ok:true,id:'file'},'document remove');
// not sure these need to be run, because the index does not change
// and only small modifications have been made to handle putAttachment
// tests are from localStorage putAttachment
var o = generateTools(this);
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.remove({_id:'file'},{max_retry:3},o.f);
o.clock.tick(2000);
if (!o.f.calledOnce){
ok (false, 'no response / too much results');
}
o.jio.get("irem_indices.json",function(err, response){
o.actualIndex = response;
deepEqual(o.actualIndex, o.fakeIndex, "Check index file");
});
o.tick(o);
o.tmp = LocalOrCookieStorage.getItem(o.storage_file_object_name) || {};
ok (!o.tmp.file,'File does not exists anymore');
// check document
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();
});
*/
/*
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