diff --git a/src/jio.storage/indexstorage.js b/src/jio.storage/indexstorage.js index d6d9c5d0d2bae495e538a6f87ffc20be166ad4a5..6a5ecb8d069f23f2e9b707012767a64876e060e5 100644 --- a/src/jio.storage/indexstorage.js +++ b/src/jio.storage/indexstorage.js @@ -310,8 +310,15 @@ jIO.addStorageType('indexed', function (spec, my) { priv.findBestIndexForQuery = function (syntax) { var i, j, k, l, n, p, o, element, key, block, search_ids, use_index = [], select_ids = {}, index, query_param, - // need to parse into object + current_query, current_query_size; + + // try to parse into object + if (syntax.query !== undefined) { current_query = jIO.ComplexQueries.parse(syntax.query); + } else { + current_query = {}; + current_query_size = 0; + } // loop indices for (i = 0; i < priv.indices.length; i += 1) { @@ -321,36 +328,39 @@ jIO.addStorageType('indexed', function (spec, my) { index.reference = priv.indices[i]; index.reference_size = index.reference.fields.length; - // rebuild search_ids for iteration - if (current_query.query_list === undefined) { - search_ids.push(current_query.id); - } else { - for (j = 0; j < current_query.query_list.length; j += 1) { - if (priv.getPositionInArray(current_query.query_list[j].id, - search_ids) === null) { - search_ids.push(current_query.query_list[j].id); + if (current_query_size !== 0) { + // rebuild search_ids for iteration + if (current_query.query_list === undefined) { + search_ids.push(current_query.id); + } else { + for (j = 0; j < current_query.query_list.length; j += 1) { + if (priv.getPositionInArray(current_query.query_list[j].id, + search_ids) === null) { + search_ids.push(current_query.query_list[j].id); + } + } + } + + // loop search ids and find matches in index + for (k = 0; k < search_ids.length; k += 1) { + query_param = search_ids[0]; + for (l = 0; l < index.reference_size; l += 1) { + if (query_param === index.reference.fields[l]) { + search_ids.splice( + priv.getPositionInArray(query_param, search_ids), + 1 + ); + } } } } + // rebuild select_ids for (o = 0; o < syntax.filter.select_list.length; o += 1) { element = syntax.filter.select_list[o]; select_ids[element] = true; } - // loop search ids and find matches in index - for (k = 0; k < search_ids.length; k += 1) { - query_param = search_ids[0]; - for (l = 0; l < index.reference_size; l += 1) { - if (query_param === index.reference.fields[l]) { - search_ids.splice( - priv.getPositionInArray(query_param, search_ids), - 1 - ); - } - } - } - // search_ids empty = all needed search fields found on index if (search_ids.length === 0) { p = priv.getObjectSize(select_ids); @@ -915,12 +925,9 @@ jIO.addStorageType('indexed', function (spec, my) { option, function (response) { query_syntax = command.getOption('query'); - if (query_syntax !== undefined) { - // build complex query object query_object = priv.constructQueryObject(response, query_syntax); - if (query_object.length === 0) { that.addJob( "allDocs", diff --git a/src/queries/query.js b/src/queries/query.js index 5f54bb9e3620743721766eb6de498582af823f2b..340242d5816e0021465b7c552952296a26cbb2a3 100644 --- a/src/queries/query.js +++ b/src/queries/query.js @@ -165,12 +165,16 @@ Object.defineProperty(scope.ComplexQueries,"query",{ //////////////////////////////////////////////////////////// result_list = [], result_list_tmp = [], j; object_list = object_list || []; - for (j=0; j<object_list.length; ++j) { - if ( itemMatchesQuery ( - object_list[j], scope.ComplexQueries.parse (query.query) - )) { - result_list.push(object_list[j]); - } + if (query.query === undefined) { + result_list = object_list; + } else { + for (j=0; j<object_list.length; ++j) { + if ( itemMatchesQuery ( + object_list[j], scope.ComplexQueries.parse (query.query) + )) { + result_list.push(object_list[j]); + } + } } if (query.filter) { select(result_list,query.filter.select_list || []); diff --git a/test/jiotests.js b/test/jiotests.js index 8e437f9e269c614183a82c959a6f3f198c969df7..f707cb7c9b9b6eb02953ad5695c2ffd4a5164b03 100644 --- a/test/jiotests.js +++ b/test/jiotests.js @@ -1179,6 +1179,38 @@ test ("AllDocs", function(){ }, o.f); o.tick(o); + // empty query returns all + o.thisShouldBeTheAnswer5 = [ + {"title": "The Good, The Bad and The Ugly"}, + {"title": "The Dark Knight"}, + {"title": "Star Wars Episode V"}, + {"title": "Shawshank Redemption"}, + {"title": "Schindlers List"}, + {"title": "Pulp Fiction"}, + {"title": "One flew over the Cuckoo's Nest"}, + {"title": "Lord of the Rings - Return of the King"}, + {"title": "Lord Of the Rings - Fellowship of the Ring"}, + {"title": "Inception"}, + {"title": "Godfellas"}, + {"title": "Godfather 2"}, + {"title": "Godfather"}, + {"title": "Fight Club"}, + {"title": "12 Angry Men"} + ]; + o.spy(o, "value", o.thisShouldBeTheAnswer5, + "allDocs (empty query in complex query)"); + + o.jio.allDocs({ + "query":{ + "filter": { + "sort_on":[['title','descending']], + "select_list":['title'] + }, + "wildcard_character":'%' + } + }, o.f); + o.tick(o); + o.jio.stop(); }); @@ -4693,6 +4725,38 @@ test ("AllDocs Complex Queries", function () { }, o.f); o.tick(o); + // empty query returns all + o.thisShouldBeTheAnswer6 = [ + {"title": "The Good, The Bad and The Ugly"}, + {"title": "The Dark Knight"}, + {"title": "Star Wars Episode V"}, + {"title": "Shawshank Redemption"}, + {"title": "Schindlers List"}, + {"title": "Pulp Fiction"}, + {"title": "One flew over the Cuckoo's Nest"}, + {"title": "Lord of the Rings - Return of the King"}, + {"title": "Lord Of the Rings - Fellowship of the Ring"}, + {"title": "Inception"}, + {"title": "Godfellas"}, + {"title": "Godfather 2"}, + {"title": "Godfather"}, + {"title": "Fight Club"}, + {"title": "12 Angry Men"} + ]; + o.spy(o, "value", o.thisShouldBeTheAnswer6, + "allDocs (empty query in complex query)"); + + o.jio.allDocs({ + "query":{ + "filter": { + "sort_on":[['title','descending']], + "select_list":['title'] + }, + "wildcard_character":'%' + } + }, o.f); + o.tick(o); + o.jio.stop(); }); /*