1 // mr.js
  2 
  3 MR = {};
  4 
  5 MR.init = function(){
  6     $max = 0;
  7     $arr = [];
  8     emit = MR.emit;
  9     $numEmits = 0;
 10     $numReduces = 0;
 11     $numReducesToDB = 0;
 12     gc(); // this is just so that keep memory size sane
 13 }
 14 
 15 MR.cleanup = function(){
 16     MR.init();
 17     gc();
 18 }
 19 
 20 MR.emit = function(k,v){
 21     $numEmits++;
 22     var num = nativeHelper.apply( get_num_ , [ k ] );
 23     var data = $arr[num];
 24     if ( ! data ){
 25         data = { key : k , values : new Array(1000) , count : 0 };
 26         $arr[num] = data;
 27     }
 28     data.values[data.count++] = v;
 29     $max = Math.max( $max , data.count );
 30 }
 31 
 32 MR.doReduce = function( useDB ){
 33     $numReduces++;
 34     if ( useDB )
 35         $numReducesToDB++;
 36     $max = 0;
 37     for ( var i=0; i<$arr.length; i++){
 38         var data = $arr[i];
 39         if ( ! data ) 
 40             continue;
 41         
 42         if ( useDB ){
 43             var x = tempcoll.findOne( { _id : data.key } );
 44             if ( x ){
 45                 data.values[data.count++] = x.value;
 46             }
 47         }
 48 
 49         var r = $reduce( data.key , data.values.slice( 0 , data.count ) );
 50         if ( r && r.length && r[0] ){ 
 51             data.values = r; 
 52             data.count = r.length;
 53         }
 54         else{ 
 55             data.values[0] = r;
 56             data.count = 1;
 57         }
 58         
 59         $max = Math.max( $max , data.count ); 
 60         
 61         if ( useDB ){
 62             if ( data.count == 1 ){
 63                 tempcoll.save( { _id : data.key , value : data.values[0] } );
 64             }
 65             else {
 66                 tempcoll.save( { _id : data.key , value : data.values.slice( 0 , data.count ) } );
 67             }
 68         }
 69     }
 70 }
 71 
 72 MR.check = function(){                        
 73     if ( $max < 2000 && $arr.length < 1000 ){ 
 74         return 0; 
 75     }
 76     MR.doReduce();
 77     if ( $max < 2000 && $arr.length < 1000 ){ 
 78         return 1;
 79     }
 80     MR.doReduce( true );
 81     $arr = []; 
 82     $max = 0; 
 83     reset_num();
 84     gc();
 85     return 2;
 86 }
 87 
 88 MR.finalize = function(){
 89     tempcoll.find().forEach( 
 90         function(z){
 91             z.value = $finalize( z._id , z.value );
 92             tempcoll.save( z );
 93         }
 94     );
 95 }
 96