Commit 59280bf3 authored by Sven Franck's avatar Sven Franck

indexStorage: pass JSLINT

parent a0aaf798
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
jIO.addStorageType('indexed', function (spec, my) { jIO.addStorageType('indexed', function (spec, my) {
"use strict"; "use strict";
var that, priv = {}, spec; var that, priv = {};
spec = spec || {}; spec = spec || {};
that = my.basicStorage(spec, my); that = my.basicStorage(spec, my);
...@@ -81,9 +81,9 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -81,9 +81,9 @@ jIO.addStorageType('indexed', function (spec, my) {
priv.getObjectSize = function (obj) { priv.getObjectSize = function (obj) {
var size = 0, key; var size = 0, key;
for (key in obj) { for (key in obj) {
if (obj.hasOwnProperty(key)) { if (obj.hasOwnProperty(key)) {
size += 1; size += 1;
} }
} }
return size; return size;
}; };
...@@ -95,12 +95,12 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -95,12 +95,12 @@ jIO.addStorageType('indexed', function (spec, my) {
* @return {object} The new index array * @return {object} The new index array
*/ */
priv.createEmptyIndexArray = function (indices) { priv.createEmptyIndexArray = function (indices) {
var obj, i, j = priv.indices.length, var i, j = priv.indices.length,
new_index_object = {}, new_index_name; new_index_object = {}, new_index_name;
if (indices === undefined) { if (indices === undefined) {
for (i = 0; i < j; i += 1) { for (i = 0; i < j; i += 1) {
new_index_name = priv.indices[i]["name"]; new_index_name = priv.indices[i].name;
new_index_object[new_index_name] = {}; new_index_object[new_index_name] = {};
} }
} }
...@@ -119,6 +119,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -119,6 +119,7 @@ jIO.addStorageType('indexed', function (spec, my) {
var key, obj, prop; var key, obj, prop;
for (key in indexToSearch) { for (key in indexToSearch) {
if (indexToSearch.hasOwnProperty(key)) {
obj = indexToSearch[key]; obj = indexToSearch[key];
for (prop in obj) { for (prop in obj) {
if (obj[prop] === docid) { if (obj[prop] === docid) {
...@@ -126,8 +127,9 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -126,8 +127,9 @@ jIO.addStorageType('indexed', function (spec, my) {
} }
} }
} }
}
return false; return false;
} };
/** /**
* Find id in indices * Find id in indices
...@@ -143,39 +145,44 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -143,39 +145,44 @@ jIO.addStorageType('indexed', function (spec, my) {
for (i = 0; i < l; i += 1) { for (i = 0; i < l; i += 1) {
index = {}; index = {};
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")) {
return true return true;
}; }
} }
} }
return false; return false;
} };
/**
* Clean up indexes when removing a file
* @method cleanIndices
* @param {object} indices The file containing the indeces
* @param {object} doc The document which should be added to the index
* @return {object} indices The cleaned up file
*/
priv.cleanIndices = function (indices, doc) { priv.cleanIndices = function (indices, doc) {
var i, j, k, index, key, obj, prop, l = priv.indices.length, var i, j, k, index, key, l = priv.indices.length;
docid = doc._id;
// loop indices (indexA, indexAB...) // loop indices (indexA, indexAB...)
for (i = 0; i < l; i += 1) { for (i = 0; i < l; i += 1) {
// index object (reference and current-iteration) // index object (reference and current-iteration)
index = {}; index = {};
index.reference = priv.indices[i]; index.reference = priv.indices[i];
index.name = index.reference["name"]; index.current = indices[index.reference.name];
index.current = indices[index.name];
index.current_size = priv.getObjectSize(index.current); index.current_size = priv.getObjectSize(index.current);
for (j = 0; j < index.current_size; j++) { for (j = 0; j < index.current_size; j += 1) {
key = priv.searchIndexByValue(index.current, doc._id, "key"); key = priv.searchIndexByValue(index.current, doc._id, "key");
index.result_array = index.current[key]; index.result_array = index.current[key];
if (!!key) { if (!!key) {
// if there is more than one docid in the result array, // if there is more than one docid in the result array,
// just remove this one and not the whole array // just remove this one and not the whole array
if (index.result_array.length > 1) { if (index.result_array.length > 1) {
index.result_array.splice(k,1); index.result_array.splice(k, 1);
} else { } else {
delete index.current[key]; delete index.current[key];
} }
...@@ -183,7 +190,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -183,7 +190,7 @@ jIO.addStorageType('indexed', function (spec, my) {
} }
} }
return indices; return indices;
} };
/** /**
* Adds entries to indices * Adds entries to indices
* @method createEmptyIndexArray * @method createEmptyIndexArray
...@@ -191,7 +198,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -191,7 +198,7 @@ jIO.addStorageType('indexed', function (spec, my) {
* @param {object} doc The document which should be added to the index * @param {object} doc The document which should be added to the index
*/ */
priv.updateIndices = function (indices, doc) { priv.updateIndices = function (indices, doc) {
var i, j, k, m, index,value, label, key, l = priv.indices.length; var i, j, k, m, index, value, label, key, l = priv.indices.length;
// loop indices // loop indices
for (i = 0; i < l; i += 1) { for (i = 0; i < l; i += 1) {
...@@ -199,14 +206,13 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -199,14 +206,13 @@ jIO.addStorageType('indexed', function (spec, my) {
index = {}; index = {};
index.reference = priv.indices[i]; index.reference = priv.indices[i];
index.reference_size = index.reference.fields.length; index.reference_size = index.reference.fields.length;
index.name = index.reference["name"];
index.field_array = []; index.field_array = [];
index.current = indices[index.name]; index.current = indices[index.reference.name];
index.current_size = priv.getObjectSize(index.current); index.current_size = priv.getObjectSize(index.current);
// build array of values to create entries in index // build array of values to create entries in index
for (j = 0; j < index.reference_size; j += 1) { for (j = 0; j < index.reference_size; j += 1) {
label = index.reference.fields[j] label = index.reference.fields[j];
value = doc[label]; value = doc[label];
if (value !== undefined) { if (value !== undefined) {
// add a new entry // add a new entry
...@@ -215,7 +221,8 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -215,7 +221,8 @@ jIO.addStorageType('indexed', function (spec, my) {
// remove existing entries with same docid // remove existing entries with same docid
// because items are stored as "keyword:id" pairs this is tricky // because items are stored as "keyword:id" pairs this is tricky
if (index.current_size > 0) { if (index.current_size > 0) {
key = priv.searchIndexByValue(indices[index.name], doc._id, "key"); key = priv.searchIndexByValue(indices[index.reference.name],
doc._id, "key");
if (!!key) { if (!!key) {
delete index.current[key]; delete index.current[key];
} }
...@@ -239,6 +246,9 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -239,6 +246,9 @@ jIO.addStorageType('indexed', function (spec, my) {
return indices; return indices;
}; };
priv.getDocContent = function () {
};
/** /**
* Build the alldocs response from the index file (overriding substorage) * Build the alldocs response from the index file (overriding substorage)
* @method allDocsResponseFromIndex * @method allDocsResponseFromIndex
...@@ -248,33 +258,58 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -248,33 +258,58 @@ jIO.addStorageType('indexed', function (spec, my) {
* @returns {object} response The allDocs response * @returns {object} response The allDocs response
*/ */
priv.allDocsResponseFromIndex = function (indices, include_docs, option) { priv.allDocsResponseFromIndex = function (indices, include_docs, option) {
var i, j, k, m, n = 0, l = priv.indices.length, var i, j, k, m, n = 0, l = priv.indices.length,
index, key, obj, prop, found, file, index, key, obj, prop, found, file,
unique_count = 0, unique_docids = [], all_doc_response = {}; unique_count = 0, unique_docids = [], all_doc_response = {},
success = function (content) {
file = { value: {} };
file.id = unique_docids[n];
file.key = unique_docids[n];
file.doc = content;
all_doc_response.rows.push(file);
// async counter, must be in callback
n += 1;
if (n === unique_count) {
that.success(all_doc_response);
}
},
error = function () {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Cannot get a document from substorage"
});
return;
};
// loop indices // loop indices
for (i = 0; i < l; i += 1) { for (i = 0; i < l; i += 1) {
index = {}; index = {};
index.reference = priv.indices[i]; index.reference = priv.indices[i];
index.name = index.reference["name"]; index.current = indices[index.reference.name];
index.current = indices[index.name];
index.current_size = priv.getObjectSize(index.current); index.current_size = priv.getObjectSize(index.current);
// a lot of loops, not sure this is the fastest way // a lot of loops, not sure this is the fastest way
for (j = 0; j < index.current_size; j += 1) { for (j = 0; j < index.current_size; j += 1) {
for (key in index.current) { for (key in index.current) {
obj = index.current[key]; if (index.current.hasOwnProperty(key)) {
for (prop in obj) { obj = index.current[key];
for ( k = 0; k < unique_docids.length; k++ ) { for (prop in obj) {
if ( obj[prop] === unique_docids[k] ) { if (obj.hasOwnProperty(prop)) {
found = true; for (k = 0; k < unique_docids.length; k += 1) {
break; if (obj[prop] === unique_docids[k]) {
found = true;
break;
}
}
if (!found) {
unique_docids.push(obj[prop]);
unique_count += 1;
}
} }
} }
if (!found) {
unique_docids.push( obj[prop] );
unique_count += 1;
}
} }
} }
} }
...@@ -290,34 +325,15 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -290,34 +325,15 @@ jIO.addStorageType('indexed', function (spec, my) {
priv.substorage, priv.substorage,
unique_docids[m], unique_docids[m],
option, option,
function (content) { success,
file = { value: {} }; error
file.id = unique_docids[n];
file.key = unique_docids[n];
file.doc = content;
all_doc_response.rows.push(file);
// async counter, must be in callback
n += 1;
if (n === (unique_count)) {
that.success(all_doc_response);
}
},
function (error) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Cannot get a document from substorage"
});
}
); );
} else { } else {
file = { value: {} }; file = { value: {} };
file.id = unique_docids[m]; file.id = unique_docids[m];
file.key = unique_docids[m]; file.key = unique_docids[m];
all_doc_response.rows.push(file); all_doc_response.rows.push(file);
if (m === (unique_count-1)) { if (m === (unique_count - 1)) {
return all_doc_response; return all_doc_response;
} }
} }
...@@ -366,7 +382,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -366,7 +382,7 @@ jIO.addStorageType('indexed', function (spec, my) {
"message": "Document not found", "message": "Document not found",
"reason": "Document not found" "reason": "Document not found"
}); });
return; return;
} }
break; break;
default: default:
...@@ -388,47 +404,46 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -388,47 +404,46 @@ jIO.addStorageType('indexed', function (spec, my) {
"reason": "Document already exists" "reason": "Document already exists"
}); });
return; return;
} else { }
if (source !== 'PUTATTACHMENT') { if (source !== 'PUTATTACHMENT') {
indices = priv.updateIndices(indices, doc); indices = priv.updateIndices(indices, doc);
} }
that.addJob( that.addJob(
source === 'PUTATTACHMENT' ? "putAttachment" : "post", source === 'PUTATTACHMENT' ? "putAttachment" : "post",
priv.substorage, priv.substorage,
doc, doc,
command.cloneOption(), command.cloneOption(),
function () { function () {
if (source !== 'PUTATTACHMENT') {
f.sendIndices(index_update_method);
} else {
docid = docid + '/' + command.getAttachmentId();
that.success({
"ok": true,
"id": docid
});
}
},
function (err) {
switch (err.status) {
case 409:
// file already exists
if (source !== 'PUTATTACHMENT') { if (source !== 'PUTATTACHMENT') {
f.sendIndices(index_update_method); f.sendIndices(index_update_method);
} else { } else {
docid = docid + '/' + command.getAttachmentId();
that.success({ that.success({
"ok": true, "ok": true,
"id": docid "id": docid
}); });
} }
}, break;
function (err) { default:
switch (err.status) { err.message = "Cannot upload document";
case 409: that.error(err);
// file already exists break;
if (source !== 'PUTATTACHMENT') {
f.sendIndices(index_update_method);
} else {
that.success({
"ok": true,
"id": docid
});
}
break;
default:
err.message = "Cannot upload document";
that.error(err);
break;
}
} }
); }
} );
}; };
f.sendIndices = function (method) { f.sendIndices = function (method) {
indices._id = priv.index_suffix; indices._id = priv.index_suffix;
...@@ -508,7 +523,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -508,7 +523,7 @@ jIO.addStorageType('indexed', function (spec, my) {
function (response) { function (response) {
that.success(response); that.success(response);
}, },
function (err) { function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -548,7 +563,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -548,7 +563,7 @@ jIO.addStorageType('indexed', function (spec, my) {
function (response) { function (response) {
that.success(response); that.success(response);
}, },
function (err) { function () {
that.error({ that.error({
"status": 409, "status": 409,
"statusText": "Conflict", "statusText": "Conflict",
...@@ -567,8 +582,8 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -567,8 +582,8 @@ jIO.addStorageType('indexed', function (spec, my) {
option, option,
function (response) { function (response) {
// if deleting an attachment // if deleting an attachment
if (typeof command.getAttachmentId() === 'string'){ if (typeof command.getAttachmentId() === 'string') {
f.removeDocument('attachment') f.removeDocument('attachment');
} else { } else {
indices = priv.cleanIndices(response, doc); indices = priv.cleanIndices(response, doc);
// store update index file // store update index file
...@@ -588,7 +603,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -588,7 +603,7 @@ jIO.addStorageType('indexed', function (spec, my) {
); );
} }
}, },
function (err) { function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -596,7 +611,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -596,7 +611,7 @@ jIO.addStorageType('indexed', function (spec, my) {
"message": "Document index not found, please check document ID", "message": "Document index not found, please check document ID",
"reason": "Incorrect document ID" "reason": "Incorrect document ID"
}); });
return; return;
} }
); );
}; };
...@@ -623,7 +638,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -623,7 +638,7 @@ jIO.addStorageType('indexed', function (spec, my) {
// ] // ]
//} //}
that.allDocs = function (command) { that.allDocs = function (command) {
var f = {}, indices, option, include_docs, all_docs_response; var f = {}, option, all_docs_response;
option = command.cloneOption(); option = command.cloneOption();
if (option.max_retry === 0) { if (option.max_retry === 0) {
option.max_retry = 3; option.max_retry = 3;
...@@ -641,10 +656,10 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -641,10 +656,10 @@ jIO.addStorageType('indexed', function (spec, my) {
} else { } else {
all_docs_response = all_docs_response =
priv.allDocsResponseFromIndex(response, false, option); priv.allDocsResponseFromIndex(response, false, option);
that.success(all_docs_response); that.success(all_docs_response);
} }
}, },
function (err) { function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -652,7 +667,7 @@ jIO.addStorageType('indexed', function (spec, my) { ...@@ -652,7 +667,7 @@ jIO.addStorageType('indexed', function (spec, my) {
"message": "Document index not found", "message": "Document index not found",
"reason": "There are no documents in the storage" "reason": "There are no documents in the storage"
}); });
return; return;
} }
); );
}; };
......
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