From 2f70f7944c9998d8ee9aadd0baf6a4ad0bb67aad Mon Sep 17 00:00:00 2001 From: erikolson186 Date: Wed, 14 Jun 2017 20:48:22 -0400 Subject: [PATCH 1/3] Attempt to fix sort and limit bug https://github.com/erikolson186/zangodb/issues/9 --- src/create_next_fn.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/create_next_fn.js b/src/create_next_fn.js index 15327a1..be37d63 100644 --- a/src/create_next_fn.js +++ b/src/create_next_fn.js @@ -2,7 +2,8 @@ const merge = require('deepmerge'); const { hashify, getIDBError } = require('./util.js'), filter = require('./filter.js'), - sort = require('./sort.js'); + sort = require('./sort.js'), + limit = require('./limit.js'); const { build, @@ -77,13 +78,15 @@ const buildPredicates = (pipeline) => { const initPredAndSortSpec = (config) => { const { pipeline } = config, preds = [], - sort_specs = []; + sort_specs = [], + limits = []; let i = 0; for (let [fn, arg] of pipeline) { if (fn === sort) { sort_specs.push(arg); } else if (fn === filter) { preds.push(arg); } + else if (fn === limit) { limits.push(arg); } else { break; } i++; @@ -92,10 +95,14 @@ const initPredAndSortSpec = (config) => { pipeline.splice(0, i); config.pred = joinPredicates(preds); - + if (sort_specs.length) { config.sort_spec = sort_specs.reduce(merge, {}); } + + if (limits.length) { + config.limit_num = limits.reduce((a, b) => a + b); + } }; const getClauses = (col, pred) => { @@ -165,6 +172,14 @@ const initSort = (config) => { } }; +const initLimit = (config) => { + if (config.limit_num === undefined) { return; } + + const { limit_num, pipeline } = config; + + pipeline.push([limit, limit_num]); +}; + const createGetIDBReqFn = ({ pred, clauses, pipeline }) => { let getIDBReq; @@ -325,6 +340,7 @@ module.exports = (cur) => { initClauses(config); initHint(config); initSort(config); + initLimit(config); next = createNextFn(config); } From 8d5aad8b1a8d00d9620f569eabe6a1181eced08b Mon Sep 17 00:00:00 2001 From: erikolson186 Date: Wed, 14 Jun 2017 21:10:36 -0400 Subject: [PATCH 2/3] Remove initPredAndSortSpec and modify initLimit --- src/create_next_fn.js | 65 ++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/create_next_fn.js b/src/create_next_fn.js index be37d63..72b1291 100644 --- a/src/create_next_fn.js +++ b/src/create_next_fn.js @@ -75,36 +75,6 @@ const buildPredicates = (pipeline) => { return new_pipeline; }; -const initPredAndSortSpec = (config) => { - const { pipeline } = config, - preds = [], - sort_specs = [], - limits = []; - - let i = 0; - - for (let [fn, arg] of pipeline) { - if (fn === sort) { sort_specs.push(arg); } - else if (fn === filter) { preds.push(arg); } - else if (fn === limit) { limits.push(arg); } - else { break; } - - i++; - } - - pipeline.splice(0, i); - - config.pred = joinPredicates(preds); - - if (sort_specs.length) { - config.sort_spec = sort_specs.reduce(merge, {}); - } - - if (limits.length) { - config.limit_num = limits.reduce((a, b) => a + b); - } -}; - const getClauses = (col, pred) => { if (!pred) { return []; } @@ -173,11 +143,11 @@ const initSort = (config) => { }; const initLimit = (config) => { - if (config.limit_num === undefined) { return; } - const { limit_num, pipeline } = config; - - pipeline.push([limit, limit_num]); + + if (config.hasOwnProperty('limit_num')) { + pipeline.push([limit, limit_num]); + } }; const createGetIDBReqFn = ({ pred, clauses, pipeline }) => { @@ -330,7 +300,32 @@ module.exports = (cur) => { pipeline }; - initPredAndSortSpec(config); + const preds = [], + sort_specs = [], + limits = []; + + let i = 0; + + for (let [fn, arg] of pipeline) { + if (fn === sort) { sort_specs.push(arg); } + else if (fn === filter) { preds.push(arg); } + else if (fn === limit) { limits.push(arg); } + else { break; } + + i++; + } + + pipeline.splice(0, i); + + config.pred = joinPredicates(preds); + + if (sort_specs.length) { + config.sort_spec = sort_specs.reduce(merge, {}); + } + + if (limits.length) { + config.limit_num = limits.reduce((a, b) => a + b); + } let next; From 4726ff1aeef81bca582b106d6dfe151710cc44d1 Mon Sep 17 00:00:00 2001 From: erikolson186 Date: Thu, 15 Jun 2017 18:05:48 -0400 Subject: [PATCH 3/3] Use unshift for adding sort to pipeline --- src/create_next_fn.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/create_next_fn.js b/src/create_next_fn.js index 72b1291..5a79486 100644 --- a/src/create_next_fn.js +++ b/src/create_next_fn.js @@ -138,7 +138,7 @@ const initSort = (config) => { if (new_clauses.length) { config.clauses = new_clauses; } else { - pipeline.push([sort, spec]); + pipeline.unshift([sort, spec]); } }; @@ -267,7 +267,7 @@ const createParallelNextFn = (config) => { }; const spec = config.sort_spec; - if (spec) { config.pipeline.push([sort, spec]); } + if (spec) { config.pipeline.unshift([sort, spec]); } return next; }; @@ -339,6 +339,6 @@ module.exports = (cur) => { next = createNextFn(config); } - + return addPipelineStages(config, next); };