comparison browser-couch.js @ 44:fec67509fc3d

Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
author Atul Varma <varmaa@toolness.com>
date Tue, 14 Apr 2009 11:25:34 -0700
parents 2d4a0ca10783
children 3a34b9ed3a36
comparison
equal deleted inserted replaced
43:eba5866b6adc 44:fec67509fc3d
303 options.map, 303 options.map,
304 dict, 304 dict,
305 progress, 305 progress,
306 chunkSize, 306 chunkSize,
307 function(mapDict) { 307 function(mapDict) {
308 BrowserCouch._reduce(options.reduce, 308 if (options.reduce)
309 mapDict, 309 BrowserCouch._reduce(
310 progress, 310 options.reduce,
311 chunkSize, 311 mapDict,
312 options.finished); 312 progress,
313 chunkSize,
314 function(rows) {
315 options.finished(new BrowserCouch._View(rows));
316 });
317 else
318 options.finished(new BrowserCouch._MapView(mapDict));
313 }); 319 });
314 }; 320 };
315 321
316 storage.get( 322 storage.get(
317 dbName, 323 dbName,
361 }, 367 },
362 368
363 _reduce: function BC__reduce(reduce, mapDict, progress, 369 _reduce: function BC__reduce(reduce, mapDict, progress,
364 chunkSize, finished) { 370 chunkSize, finished) {
365 var rows = []; 371 var rows = [];
372 var mapKeys = this._makeMapKeys(mapDict);
373
374 var i = 0;
375
376 function continueReduce() {
377 var iAtStart = i;
378
379 do {
380 var key = mapKeys[i];
381 var item = mapDict[key];
382
383 // TODO: The map() method is only available on JS 1.6.
384 var keys = item.keys.map(function pairKeyWithDocId(docId) {
385 return [key, docId];
386 });
387 rows.push({key: key,
388 value: reduce(keys, item.values)});
389 i++;
390 } while (i - iAtStart < chunkSize &&
391 i < mapKeys.length)
392
393 if (i == mapKeys.length)
394 finished(rows);
395 else
396 progress("reduce", i / mapKeys.length, continueReduce);
397 }
398
399 continueReduce();
400 },
401
402 _makeMapKeys: function BC__makeMapKeys(mapDict) {
366 var mapKeys = []; 403 var mapKeys = [];
367 for (name in mapDict) 404 for (name in mapDict)
368 mapKeys.push(name); 405 mapKeys.push(name);
369
370 mapKeys.sort(); 406 mapKeys.sort();
371 407 return mapKeys;
372 if (reduce) { 408 },
373 var i = 0; 409
374 410 _View: function BC__View(rows) {
375 function continueReduce() { 411 this.rows = rows;
376 var iAtStart = i; 412 },
377 413
378 do { 414 _MapView: function BC__MapView(mapDict) {
379 var key = mapKeys[i]; 415 var rows = [];
380 var item = mapDict[key]; 416
381 417 this.rows = rows;
382 // TODO: The map() method is only available on JS 1.6. 418
383 var keys = item.keys.map(function pairKeyWithDocId(docId) { 419 var mapKeys = BrowserCouch._makeMapKeys(mapDict);
384 return [key, docId]; 420 for (var i = 0; i < mapKeys.length; i++) {
385 }); 421 var key = mapKeys[i];
386 rows.push({key: key, 422 var item = mapDict[key];
387 value: reduce(keys, item.values)}); 423 for (var j = 0; j < item.keys.length; j++) {
388 i++; 424 var id = item.keys[j];
389 } while (i - iAtStart < chunkSize && 425 var value = item.values[j];
390 i < mapKeys.length) 426 rows.push({id: id,
391 427 key: key,
392 if (i == mapKeys.length) 428 value: value});
393 doneWithReduce();
394 else
395 progress("reduce", i / mapKeys.length, continueReduce);
396 } 429 }
397
398 continueReduce();
399 } else {
400 for (i = 0; i < mapKeys.length; i++) {
401 var key = mapKeys[i];
402 var item = mapDict[key];
403 for (var j = 0; j < item.keys.length; j++) {
404 var id = item.keys[j];
405 var value = item.values[j];
406 rows.push({id: id,
407 key: key,
408 value: value});
409 }
410 }
411
412 doneWithReduce();
413 }
414
415 function doneWithReduce() {
416 finished({rows: rows});
417 } 430 }
418 } 431 }
419 }; 432 };