File: index_check9.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 (135 lines) | stat: -rw-r--r-- 3,165 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Randomized index testing

Random.setRandomSeed();

t = db.test_index_check9;

function doIt() {

t.drop();

function sort() {
    var sort = {};
    for( var i = 0; i < n; ++i ) {
        sort[ fields[ i ] ] = Random.rand() > 0.5 ? 1 : -1;
    }    
    return sort;
}

var fields = [ 'a', 'b', 'c', 'd', 'e' ];
n = Random.randInt( 5 ) + 1;
var idx = sort();

var chars = "abcdefghijklmnopqrstuvwxyz";
var alphas = []
for( var i = 0; i < n; ++i ) {
    alphas.push( Random.rand() > 0.5 );
}
    
t.ensureIndex( idx );

function obj() {
    var ret = {};
    for( var i = 0; i < n; ++i ) {
        ret[ fields[ i ] ] = r( alphas[ i ] );
    }
    return ret;
}

function r( alpha ) {
    if ( !alpha ) {
        return Random.randInt( 10 );
    } else {
        var len = Random.randInt( 10 );
        buf = "";
        for( var i = 0; i < len; ++i ) {
            buf += chars.charAt( Random.randInt( chars.length ) );
        }
        return buf;
    }
}

function check() {
    var v = t.validate();
    if ( !t.valid ) {
        printjson( t );
        assert( t.valid );
    }
    var spec = {};
    for( var i = 0; i < n; ++i ) {
        var predicateType = Random.randInt( 4 );
        switch( predicateType ) {
        case 0 /* range */ : {
            var bounds = [ r( alphas[ i ] ), r( alphas[ i ] ) ];
            if ( bounds[ 0 ] > bounds[ 1 ] ) {
                bounds.reverse();
            }
            var s = {};
            if ( Random.rand() > 0.5 ) {
                s[ "$gte" ] = bounds[ 0 ];
            } else {
                s[ "$gt" ] = bounds[ 0 ];
            }
            if ( Random.rand() > 0.5 ) {
                s[ "$lte" ] = bounds[ 1 ];
            } else {
                s[ "$lt" ] = bounds[ 1 ];
            }
            spec[ fields[ i ] ] = s;
            break;
        }
        case 1 /* $in */ : {
            var vals = []
            var inLength = Random.randInt( 15 );
            for( var j = 0; j < inLength; ++j ) {
                vals.push( r( alphas[ i ] ) );
            }
            spec[ fields[ i ] ] = { $in: vals };
            break;
        }
        case 2 /* equality */ : {
            spec[ fields[ i ] ] = r( alphas[ i ] );
            break;
        }
        default /* no predicate */ :
            break;
        }
    }
    s = sort();
    c1 = t.find( spec, { _id:null } ).sort( s ).hint( idx ).toArray();
    c2 = t.find( spec ).sort( s ).explain().nscanned;
    c3 = t.find( spec, { _id:null } ).sort( s ).hint( {$natural:1} ).toArray();
    count = t.count( spec );
    //    assert.eq( c1, c3, "spec: " + tojson( spec ) + ", sort: " + tojson( s ) );
    //    assert.eq( c1.length, c2 );
    assert.eq( c1, c3 );
    assert.eq( c3.length, count );
}

for( var i = 0; i < 10000; ++i ) {
    t.save( obj() );
    if( Random.rand() > 0.999 ) {
        print( i );
        check();
    }
}

for( var i = 0; i < 100000; ++i ) {
    if ( Random.rand() > 0.9 ) {
        t.save( obj() );
    } else {
        t.remove( obj() ); // improve
    }
    if( Random.rand() > 0.999 ) {
        print( i );
        check();
    }
}

check();

}

for( var z = 0; z < 5; ++z ) {
    doIt();
}