(view, results, options)
| 8789 | } |
| 8790 | |
| 8791 | function reduceView(view, results, options) { |
| 8792 | if (options.group_level === 0) { |
| 8793 | delete options.group_level; |
| 8794 | } |
| 8795 | |
| 8796 | const shouldGroup = options.group || options.group_level; |
| 8797 | const reduceFun = reducer(view.reduceFun); |
| 8798 | const groups = []; |
| 8799 | const lvl = isNaN(options.group_level) |
| 8800 | ? Number.POSITIVE_INFINITY |
| 8801 | : options.group_level; |
| 8802 | |
| 8803 | for (const result of results) { |
| 8804 | const last = groups[groups.length - 1]; |
| 8805 | let groupKey = shouldGroup ? result.key : null; |
| 8806 | |
| 8807 | // only set group_level for array keys |
| 8808 | if (shouldGroup && Array.isArray(groupKey)) { |
| 8809 | groupKey = groupKey.slice(0, lvl); |
| 8810 | } |
| 8811 | |
| 8812 | if (last && collate(last.groupKey, groupKey) === 0) { |
| 8813 | last.keys.push([result.key, result.id]); |
| 8814 | last.values.push(result.value); |
| 8815 | continue; |
| 8816 | } |
| 8817 | groups.push({ |
| 8818 | keys: [[result.key, result.id]], |
| 8819 | values: [result.value], |
| 8820 | groupKey |
| 8821 | }); |
| 8822 | } |
| 8823 | |
| 8824 | results = []; |
| 8825 | for (const group of groups) { |
| 8826 | const reduceTry = tryReduce(view.sourceDB, reduceFun, group.keys, group.values, false); |
| 8827 | if (reduceTry.error && reduceTry.error instanceof BuiltInError) { |
| 8828 | // CouchDB returns an error if a built-in errors out |
| 8829 | throw reduceTry.error; |
| 8830 | } |
| 8831 | results.push({ |
| 8832 | // CouchDB just sets the value to null if a non-built-in errors out |
| 8833 | value: reduceTry.error ? null : reduceTry.output, |
| 8834 | key: group.groupKey |
| 8835 | }); |
| 8836 | } |
| 8837 | // no total_rows/offset when reducing |
| 8838 | return { rows: sliceResults(results, options.limit, options.skip) }; |
| 8839 | } |
| 8840 | |
| 8841 | function queryView(view, opts) { |
| 8842 | return sequentialize(getQueue(view), function () { |
no test coverage detected
searching dependent graphs…