Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // Disable dot-notation warnings in this file since we handle a lot
  4. // of random incoming view options, some that can't be dot-referenced.
  5. /* jshint -W069 */
  6.  
  7. function cbCompare(a, b) {
  8. if (Array.isArray(a) && Array.isArray(b)) {
  9. if (a.length < b.length) {
  10. return -1;
  11. } else if (a.length > b.length) {
  12. return +1;
  13. }
  14. for (var i = 0; i < a.length; ++i) {
  15. var subCmp = cbCompare(a[i], b[i]);
  16. if (subCmp !== 0) {
  17. return subCmp;
  18. }
  19. }
  20. return true;
  21. }
  22. if (a < b ) {
  23. return -1;
  24. } else if (a > b) {
  25. return +1;
  26. } else {
  27. return 0;
  28. }
  29. }
  30.  
  31. function cbIndexOf(arr, val) {
  32. for (var i = 0; i < arr.length; ++i) {
  33. if (cbCompare(arr[i], val) === 0) {
  34. return i;
  35. }
  36. }
  37. return -1;
  38. }
  39.  
  40. function cbNormKey(key, groupLevel) {
  41. if (Array.isArray(key)) {
  42. if (groupLevel === -1) {
  43. return key;
  44. } else if (groupLevel === 0) {
  45. return null;
  46. } else {
  47. return key.slice(0, groupLevel);
  48. }
  49. } else {
  50. return key;
  51. }
  52. }
  53.  
  54. function optTryParse(opt) {
  55. if (opt) {
  56. return JSON.parse(opt);
  57. } else {
  58. return undefined;
  59. }
  60. }
  61. function optDefault(opt, defaultVal) {
  62. if (opt) {
  63. return opt;
  64. } else {
  65. return defaultVal;
  66. }
  67. }
  68.  
  69. function execute(options, indexedItems, viewReduceFunc) {
  70. //console.log('indexer opts', options);
  71. var startKey = optTryParse(options['startkey']);
  72. var startKeyDocId = optTryParse(options['startkey_docid']);
  73. var endKey = optTryParse(options['endkey']);
  74. var endKeyDocId = optTryParse(options['endkey_docid']);
  75.  
  76. var inclusiveStart = true;
  77. var inclusiveEnd = true;
  78.  
  79. if (options['inclusive_end'] !== undefined) {
  80. if (!options['inclusive_end'] || options['inclusive_end'] === 'false') {
  81. inclusiveEnd = false;
  82. }
  83. }
  84.  
  85. if (options['descending']) {
  86. var _startKey = startKey;
  87. startKey = endKey;
  88. endKey = _startKey;
  89. var _startKeyDocId = startKeyDocId;
  90. startKeyDocId = endKeyDocId;
  91. endKeyDocId = _startKeyDocId;
  92. var _inclusiveStart = inclusiveStart;
  93. inclusiveStart = inclusiveEnd;
  94. inclusiveEnd = _inclusiveStart;
  95. }
  96.  
  97. var key = optTryParse(options['key']);
  98. var keys = optTryParse(options['keys']);
  99.  
  100. var results = [];
  101.  
  102. for (var cur in indexedItems.results) {
  103. var docId = cur;
  104. var row = indexedItems.results[docId];
  105. var docKey = row[0];
  106. var docVal = row[1];
  107.  
  108. if (key !== undefined) {
  109. if (cbCompare(docKey, key) !== 0) {
  110. //console.log('[skip] no key match', docKey, key);
  111. continue;
  112. }
  113. }
  114. if (keys !== undefined) {
  115. if (keys.indexOf(docKey) === -1) {
  116. //console.log('[skip] no keys match', docKey);
  117. continue;
  118. }
  119. }
  120.  
  121. if (inclusiveStart) {
  122. if (startKey && cbCompare(docKey, startKey) < 0) {
  123. //console.log('[skip] before start key incl', docKey, startKey);
  124. continue;
  125. }
  126. if (startKeyDocId && cbCompare(docId, startKeyDocId) < 0 ) {
  127. //console.log('[skip] before start id incl', docId, startKeyDocId);
  128. continue;
  129. }
  130. } else {
  131. if (startKey && cbCompare(docKey, startKey) <= 0) {
  132. //console.log('[skip] before start key', docKey, startKey);
  133. continue;
  134. }
  135. if (startKeyDocId && cbCompare(docId, startKeyDocId) <= 0) {
  136. //console.log('[skip] before start id', docId, startKeyDocId);
  137. continue;
  138. }
  139. }
  140.  
  141. if (inclusiveEnd) {
  142. if (endKey && cbCompare(docKey, endKey) > 0) {
  143. //console.log('[skip] after start key incl', docKey, endKey);
  144. continue;
  145. }
  146. if (endKeyDocId && cbCompare(docId, endKeyDocId) > 0) {
  147. //console.log('[skip] after start id incl', docId, endKeyDocId);
  148. continue;
  149. }
  150. } else {
  151. if (endKey && cbCompare(docKey, endKey) >= 0) {
  152. //console.log('[skip] after start key', docKey, endKey);
  153. continue;
  154. }
  155. if (endKeyDocId && cbCompare(docId, endKeyDocId) >= 0) {
  156. //console.log('[skip] after start id', docId, endKeyDocId);
  157. continue;
  158. }
  159. }
  160.  
  161. var itemOut = {
  162. id: docId,
  163. key: docKey,
  164. value: docVal
  165. };
  166. results.push(itemOut);
  167. }
  168.  
  169. if (options['descending']) {
  170. results.sort(function(a,b){ return cbCompare(a, b); });
  171. } else {
  172. results.sort(function(a,b){ return cbCompare(b, a); });
  173. }
  174.  
  175. /* REDUCER */
  176. var doReduce = true;
  177. if (options['reduce'] !== undefined) {
  178. if (!options['reduce'] || options['reduce'] === 'false') {
  179. doReduce = false;
  180. }
  181. }
  182.  
  183. var groupLevel = optDefault(options['group_level'], 0);
  184. if (options['group'] === 'true') {
  185. // `group=true` sets group_level to infinite
  186. groupLevel = -1;
  187. }
  188. //console.log('REDUCER GROUPLEVEL', groupLevel);
  189.  
  190. if (doReduce && viewReduceFunc !== undefined) {
  191. //console.log('VIEW PRE REDUCE', results);
  192.  
  193. var keysN = [];
  194. for (var m = 0; m < results.length; ++m) {
  195. var keyN = cbNormKey(results[m].key, groupLevel);
  196. if (cbIndexOf(keysN, keyN) < 0) {
  197. keysN.push(keyN);
  198. }
  199. }
  200.  
  201. var reducedResults = [];
  202.  
  203. for (var j = 0; j < keysN.length; ++j) {
  204. var values = [];
  205. for (var k = 0; k < results.length; ++k) {
  206. var keyZ = cbNormKey(results[k].key, groupLevel);
  207. if (cbCompare(keyZ, keysN[j]) === 0) {
  208. values.push(results[k].value);
  209. }
  210. }
  211. var result = viewReduceFunc(keysN[j], values, false);
  212. reducedResults.push({
  213. key: keysN[j],
  214. value: result
  215. });
  216. }
  217. results = reducedResults;
  218. }
  219.  
  220. /* FINALIZE */
  221. if (options['skip']) {
  222. results = results.slice(options['skip']);
  223. }
  224. if (options['limit']) {
  225. results = results.slice(0, options['limit']);
  226. }
  227.  
  228. // Finally, output the results
  229. var topLevel = {
  230. rows: results,
  231. total_rows: indexedItems.length
  232. };
  233. return topLevel;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement