Commit 921c87d1 authored by Tristan Cavelier's avatar Tristan Cavelier

localstorage.js: updated to support complex queries + tests

parent fe6aabc5
...@@ -3,8 +3,11 @@ ...@@ -3,8 +3,11 @@
* Released under the LGPL license. * Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html * http://www.gnu.org/licenses/lgpl.html
*/ */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, localStorage: true, setTimeout: true */ /*global jIO: true, localStorage: true, setTimeout: true,
complex_queries: true */
/** /**
* JIO Local Storage. Type = 'local'. * JIO Local Storage. Type = 'local'.
* Local browser "database" storage. * Local browser "database" storage.
...@@ -359,47 +362,65 @@ jIO.addStorageType('local', function (spec, my) { ...@@ -359,47 +362,65 @@ jIO.addStorageType('local', function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.allDocs = function (command) { that.allDocs = function (command) {
var i, j, file, items = 0, var i, row, path_re, rows = [], document_list = [], option, document_object;
s = new RegExp("^" + priv.localpath + "\\/[^/]+$"), path_re = new RegExp(
all_doc_response = {}, "^" + complex_queries.stringEscapeRegexpCharacters(priv.localpath) +
query_object = [], query_syntax, query_response = []; "/[^/]+$"
);
query_syntax = command.getOption('query'); option = command.cloneOption();
if (query_syntax === undefined) { if (typeof complex_queries !== "object" ||
all_doc_response.rows = []; option.query === undefined && option.sort_on === undefined &&
option.select_list === undefined && option.include_docs === undefined) {
rows = [];
for (i in localStorage) { for (i in localStorage) {
if (localStorage.hasOwnProperty(i)) { if (localStorage.hasOwnProperty(i)) {
// filter non-documents // filter non-documents
if (s.test(i)) { if (path_re.test(i)) {
items += 1; row = { value: {} };
j = i.split('/').slice(-1)[0]; row.id = i.split('/').slice(-1)[0];
row.key = row.id;
file = { value: {} };
file.id = j;
file.key = j;
if (command.getOption('include_docs')) { if (command.getOption('include_docs')) {
file.doc = JSON.parse(localStorage.getItem(i)); row.doc = JSON.parse(localStorage.getItem(i));
} }
all_doc_response.rows.push(file); rows.push(row);
} }
} }
} }
all_doc_response.total_rows = items; that.success({"rows": rows, "total_rows": rows.length});
that.success(all_doc_response);
} else { } else {
// create complex query object from returned results // create complex query object from returned results
for (i in localStorage) { for (i in localStorage) {
if (localStorage.hasOwnProperty(i)) { if (localStorage.hasOwnProperty(i)) {
if (s.test(i)) { if (path_re.test(i)) {
items += 1; document_list.push(localstorage.getItem(i));
j = i.split('/').slice(-1)[0];
query_object.push(localstorage.getItem(i));
} }
} }
} }
query_response = jIO.ComplexQueries.query(query_syntax, query_object); option.select_list = option.select_list || [];
that.success(query_response); option.select_list.push("_id");
if (option.include_docs === true) {
document_object = {};
document_list.forEach(function (meta) {
document_object[meta._id] = meta;
});
}
complex_queries.QueryFactory.create(option.query || "").
exec(document_list, option);
document_list = document_list.map(function (value) {
var o = {
"id": value._id,
"key": value._id
};
if (option.include_docs === true) {
o.doc = document_object[value._id];
delete document_object[value._id];
}
delete value._id;
o.value = value;
return o;
});
that.success({"total_rows": document_list.length,
"rows": document_list});
} }
}; };
......
...@@ -1161,7 +1161,7 @@ test ("AllDocs", function(){ ...@@ -1161,7 +1161,7 @@ test ("AllDocs", function(){
// include docs // include docs
o.allDocsResponse = {}; o.allDocsResponse = {};
o.allDocsResponse.rows = []; o.allDocsResponse.rows = [];
o.allDocsResponse.total_rows = 15; o.allDocsResponse.total_rows = m;
for (i = 0; i < m; i += 1) { for (i = 0; i < m; i += 1) {
o.allDocsResponse.rows.push({ o.allDocsResponse.rows.push({
"id": "doc_"+(i < 10 ? "0"+i : i), "id": "doc_"+(i < 10 ? "0"+i : i),
...@@ -1173,7 +1173,7 @@ test ("AllDocs", function(){ ...@@ -1173,7 +1173,7 @@ test ("AllDocs", function(){
// alldocs // alldocs
o.spy(o, "value", o.allDocsResponse, "All docs (include docs)"); o.spy(o, "value", o.allDocsResponse, "All docs (include docs)");
o.jio.allDocs({"include_docs":true}, function (err, response) { o.jio.allDocs({"include_docs": true}, function (err, response) {
if (response && response.rows) { if (response && response.rows) {
response.rows.sort(function (a, b) { response.rows.sort(function (a, b) {
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0; return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
...@@ -1184,65 +1184,56 @@ test ("AllDocs", function(){ ...@@ -1184,65 +1184,56 @@ test ("AllDocs", function(){
o.tick(o); o.tick(o);
// complex queries // complex queries
o.thisShouldBeTheAnswer4 = [ o.thisShouldBeTheAnswer4 = {"total_rows": 0, "rows": []};
{"title": "Inception", "year": 2010}, o.allDocsResponse.rows.forEach(function (row) {
{"title": "The Dark Knight", "year": 2008}, var new_row;
{"title": "Lord of the Rings - Return of the King", "year": 2003}, if (row.doc.year >= 1980) {
{"title": "Lord Of the Rings - Fellowship of the Ring", "year": 2001}, new_row = JSON.parse(JSON.stringify(row));
{"title": "Fight Club", "year": 1999} new_row.value.title = row.doc.title;
]; new_row.value.year = row.doc.year;
delete new_row.doc;
o.thisShouldBeTheAnswer4.rows.push(new_row);
o.thisShouldBeTheAnswer4.total_rows += 1;
}
});
o.thisShouldBeTheAnswer4.rows.sort(function (a, b) {
return a.value.year > b.value.year ? -1 :
a.value.year < b.value.year ? 1 : 0;
});
o.thisShouldBeTheAnswer4.total_rows = 5;
o.thisShouldBeTheAnswer4.rows.length = 5;
o.spy(o, "value", o.thisShouldBeTheAnswer4, o.spy(o, "value", o.thisShouldBeTheAnswer4,
"allDocs (complex queries year >= 1980, all query options)"); "allDocs (complex queries year >= 1980, all query options)");
o.jio.allDocs({ o.jio.allDocs({
"query":{ "query": '(year: >= "1980")',
"query":'(year: >= "1980")', "limit": [0,5],
"filter": { "sort_on": [["year", "descending"]],
"limit":[0,5], "select_list": ["title", "year"]
"sort_on":[['year','descending']],
"select_list":['title','year']
},
"wildcard_character":'%'
}
}, o.f); }, o.f);
o.tick(o); o.tick(o);
// empty query returns all // empty query returns all
o.thisShouldBeTheAnswer5 = [ o.thisShouldBeTheAnswer5 = {"total_rows": 0, "rows": []};
{"title": "The Good, The Bad and The Ugly"}, o.allDocsResponse.rows.forEach(function (row) {
{"title": "The Dark Knight"}, var new_row = JSON.parse(JSON.stringify(row));
{"title": "Star Wars Episode V"}, new_row.value.title = row.doc.title;
{"title": "Shawshank Redemption"}, o.thisShouldBeTheAnswer5.rows.push(new_row);
{"title": "Schindlers List"}, o.thisShouldBeTheAnswer5.total_rows += 1;
{"title": "Pulp Fiction"}, });
{"title": "One flew over the Cuckoo's Nest"}, o.thisShouldBeTheAnswer5.rows.sort(function (a, b) {
{"title": "Lord of the Rings - Return of the King"}, return a.value.title > b.value.title ? -1 :
{"title": "Lord Of the Rings - Fellowship of the Ring"}, a.value.title < b.value.title ? 1 : 0;
{"title": "Inception"}, });
{"title": "Godfellas"},
{"title": "Godfather 2"},
{"title": "Godfather"},
{"title": "Fight Club"},
{"title": "12 Angry Men"}
];
o.spy(o, "value", o.thisShouldBeTheAnswer5, o.spy(o, "value", o.thisShouldBeTheAnswer5,
"allDocs (empty query in complex query)"); "allDocs (empty query in complex query)");
o.jio.allDocs({ o.jio.allDocs({
"query":{ "sort_on": [["title", "descending"]],
"filter": { "select_list": ["title"],
"sort_on":[['title','descending']], "include_docs": true
"select_list":['title'] }, o.f);
},
"wildcard_character":'%'
}
}, function (err, response) {
if (response && response.rows) {
response.rows.sort(function (a, b) {
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
});
}
o.f(err, response);
});
o.tick(o); o.tick(o);
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