Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- // Disable dot-notation warnings in this file since we handle a lot
- // of random incoming view options, some that can't be dot-referenced.
- /* jshint -W069 */
- function cbCompare(a, b) {
- if (Array.isArray(a) && Array.isArray(b)) {
- if (a.length < b.length) {
- return -1;
- } else if (a.length > b.length) {
- return +1;
- }
- for (var i = 0; i < a.length; ++i) {
- var subCmp = cbCompare(a[i], b[i]);
- if (subCmp !== 0) {
- return subCmp;
- }
- }
- return true;
- }
- if (a < b ) {
- return -1;
- } else if (a > b) {
- return +1;
- } else {
- return 0;
- }
- }
- function cbIndexOf(arr, val) {
- for (var i = 0; i < arr.length; ++i) {
- if (cbCompare(arr[i], val) === 0) {
- return i;
- }
- }
- return -1;
- }
- function cbNormKey(key, groupLevel) {
- if (Array.isArray(key)) {
- if (groupLevel === -1) {
- return key;
- } else if (groupLevel === 0) {
- return null;
- } else {
- return key.slice(0, groupLevel);
- }
- } else {
- return key;
- }
- }
- function optTryParse(opt) {
- if (opt) {
- return JSON.parse(opt);
- } else {
- return undefined;
- }
- }
- function optDefault(opt, defaultVal) {
- if (opt) {
- return opt;
- } else {
- return defaultVal;
- }
- }
- function execute(options, indexedItems, viewReduceFunc) {
- //console.log('indexer opts', options);
- var startKey = optTryParse(options['startkey']);
- var startKeyDocId = optTryParse(options['startkey_docid']);
- var endKey = optTryParse(options['endkey']);
- var endKeyDocId = optTryParse(options['endkey_docid']);
- var inclusiveStart = true;
- var inclusiveEnd = true;
- if (options['inclusive_end'] !== undefined) {
- if (!options['inclusive_end'] || options['inclusive_end'] === 'false') {
- inclusiveEnd = false;
- }
- }
- if (options['descending']) {
- var _startKey = startKey;
- startKey = endKey;
- endKey = _startKey;
- var _startKeyDocId = startKeyDocId;
- startKeyDocId = endKeyDocId;
- endKeyDocId = _startKeyDocId;
- var _inclusiveStart = inclusiveStart;
- inclusiveStart = inclusiveEnd;
- inclusiveEnd = _inclusiveStart;
- }
- var key = optTryParse(options['key']);
- var keys = optTryParse(options['keys']);
- var results = [];
- for (var cur in indexedItems.results) {
- var docId = cur;
- var row = indexedItems.results[docId];
- var docKey = row[0];
- var docVal = row[1];
- if (key !== undefined) {
- if (cbCompare(docKey, key) !== 0) {
- //console.log('[skip] no key match', docKey, key);
- continue;
- }
- }
- if (keys !== undefined) {
- if (keys.indexOf(docKey) === -1) {
- //console.log('[skip] no keys match', docKey);
- continue;
- }
- }
- if (inclusiveStart) {
- if (startKey && cbCompare(docKey, startKey) < 0) {
- //console.log('[skip] before start key incl', docKey, startKey);
- continue;
- }
- if (startKeyDocId && cbCompare(docId, startKeyDocId) < 0 ) {
- //console.log('[skip] before start id incl', docId, startKeyDocId);
- continue;
- }
- } else {
- if (startKey && cbCompare(docKey, startKey) <= 0) {
- //console.log('[skip] before start key', docKey, startKey);
- continue;
- }
- if (startKeyDocId && cbCompare(docId, startKeyDocId) <= 0) {
- //console.log('[skip] before start id', docId, startKeyDocId);
- continue;
- }
- }
- if (inclusiveEnd) {
- if (endKey && cbCompare(docKey, endKey) > 0) {
- //console.log('[skip] after start key incl', docKey, endKey);
- continue;
- }
- if (endKeyDocId && cbCompare(docId, endKeyDocId) > 0) {
- //console.log('[skip] after start id incl', docId, endKeyDocId);
- continue;
- }
- } else {
- if (endKey && cbCompare(docKey, endKey) >= 0) {
- //console.log('[skip] after start key', docKey, endKey);
- continue;
- }
- if (endKeyDocId && cbCompare(docId, endKeyDocId) >= 0) {
- //console.log('[skip] after start id', docId, endKeyDocId);
- continue;
- }
- }
- var itemOut = {
- id: docId,
- key: docKey,
- value: docVal
- };
- results.push(itemOut);
- }
- if (options['descending']) {
- results.sort(function(a,b){ return cbCompare(a, b); });
- } else {
- results.sort(function(a,b){ return cbCompare(b, a); });
- }
- /* REDUCER */
- var doReduce = true;
- if (options['reduce'] !== undefined) {
- if (!options['reduce'] || options['reduce'] === 'false') {
- doReduce = false;
- }
- }
- var groupLevel = optDefault(options['group_level'], 0);
- if (options['group'] === 'true') {
- // `group=true` sets group_level to infinite
- groupLevel = -1;
- }
- //console.log('REDUCER GROUPLEVEL', groupLevel);
- if (doReduce && viewReduceFunc !== undefined) {
- //console.log('VIEW PRE REDUCE', results);
- var keysN = [];
- for (var m = 0; m < results.length; ++m) {
- var keyN = cbNormKey(results[m].key, groupLevel);
- if (cbIndexOf(keysN, keyN) < 0) {
- keysN.push(keyN);
- }
- }
- var reducedResults = [];
- for (var j = 0; j < keysN.length; ++j) {
- var values = [];
- for (var k = 0; k < results.length; ++k) {
- var keyZ = cbNormKey(results[k].key, groupLevel);
- if (cbCompare(keyZ, keysN[j]) === 0) {
- values.push(results[k].value);
- }
- }
- var result = viewReduceFunc(keysN[j], values, false);
- reducedResults.push({
- key: keysN[j],
- value: result
- });
- }
- results = reducedResults;
- }
- /* FINALIZE */
- if (options['skip']) {
- results = results.slice(options['skip']);
- }
- if (options['limit']) {
- results = results.slice(0, options['limit']);
- }
- // Finally, output the results
- var topLevel = {
- rows: results,
- total_rows: indexedItems.length
- };
- return topLevel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement