File: mr_merge.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (60 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

t = db.mr_merge;
t.drop();

t.insert( { a : [ 1 , 2 ] } )
t.insert( { a : [ 2 , 3 ] } )
t.insert( { a : [ 3 , 4 ] } )

outName = "mr_merge_out";
out = db[outName];
out.drop();

m = function(){ for (i=0; i<this.a.length; i++ ) emit( this.a[i] , 1 ); } 
r = function(k,vs){ return Array.sum( vs ); }

function tos( o ){
    var s = "";
    for ( var i=0; i<100; i++ ){
        if ( o[i] )
            s += i + "_" + o[i];
    }
    return s;
}


res = t.mapReduce( m , r , { out : outName } )


expected = { "1" : 1 , "2" : 2 , "3" : 2 , "4" : 1 }
assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "A" );

t.insert( { a : [ 4 , 5 ] } )
out.insert( { _id : 10 , value : "5" } )
res = t.mapReduce( m , r , { out : outName } )

expected["4"]++;
expected["5"] = 1
assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "B" );

t.insert( { a : [ 5 , 6 ] } )
out.insert( { _id : 10 , value : "5" } )
res = t.mapReduce( m , r , { out : { merge : outName } } )

expected["5"]++;
expected["10"] = 5
expected["6"] = 1

assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "C" );

// test that the nonAtomic output gives valid result
t.insert( { a : [ 6 , 7 ] } )
out.insert( { _id : 20 , value : "10" } )
res = t.mapReduce( m , r , { out : { merge : outName, nonAtomic: true } } )

expected["6"]++;
expected["20"] = 10
expected["7"] = 1

assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "D" );