summaryrefslogtreecommitdiff
path: root/jstests/check_shard_index.js
blob: f85071124fbf8cfe4644e0ce28547fb6fc4d8cf4 (plain)
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
136
137
138
139
140
141
// -------------------------
//  CHECKSHARDINGINDEX TEST UTILS
// -------------------------

f = db.jstests_shardingindex;
f.drop();


// -------------------------
// Case 1: all entries filled or empty should make a valid index
//

f.drop();
f.ensureIndex( { x: 1 , y: 1 } );
assert.eq( 0 , f.count() , "1. initial count should be zero" );

res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( true , res.ok, "1a" );

f.save( { x: 1 , y : 1 } );
assert.eq( 1 , f.count() , "1. count after initial insert should be 1" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( true , res.ok , "1b" );


// -------------------------
// Case 2: entry with null values would make an index unsuitable
//

f.drop();
f.ensureIndex( { x: 1 , y: 1 } );
assert.eq( 0 , f.count() , "2. initial count should be zero" );

f.save( { x: 1 , y : 1 } );
f.save( { x: null , y : 1 } );

res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( true , res.ok , "2a " + tojson(res) );

f.save( { y: 2 } );
assert.eq( 3 , f.count() , "2. count after initial insert should be 3" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( false , res.ok , "2b " + tojson(res) );

// Check _id index
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {_id:1} });
assert.eq( true , res.ok , "2c " + tojson(res) );
assert( res.idskip , "2d " + tojson(res) )

// -------------------------
// Case 3: entry with array values would make an index unsuitable
//

f.drop();
f.ensureIndex( { x: 1 , y: 1 } );
assert.eq( 0 , f.count() , "3. initial count should be zero" );

f.save( { x: 1 , y : 1 } );
f.save( { x: [1, 2] , y : 2 } );

assert.eq( 2 , f.count() , "3. count after initial insert should be 2" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( false , res.ok , "3a " + tojson(res) );

f.remove( { y : 2 } );
f.reIndex();

assert.eq( 1 , f.count() , "3. count after removing array value should be 1" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( true , res.ok , "3b " + tojson(res) );

f.save( { x : 2, y : [1, 2] } )

assert.eq( 2 , f.count() , "3. count after adding array value should be 2" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( false , res.ok , "3c " + tojson(res) );

// -------------------------
// Case 4: Handles prefix shard key indexes.
//

f.drop();
f.ensureIndex( { x: 1 , y: 1, z: 1 } );
assert.eq( 0 , f.count() , "4. initial count should be zero" );

f.save( { x: 1 , y : 1, z : 1 } );

assert.eq( 1 , f.count() , "4. count after initial insert should be 1" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1} });
assert.eq( true , res.ok , "4a " + tojson(res) );

res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( true , res.ok , "4b " + tojson(res) );

f.save( { x: [1, 2] , y : 2, z : 2 } );

assert.eq( 2 , f.count() , "4. count after adding array value should be 2" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1} });
assert.eq( false , res.ok , "4c " + tojson(res) );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( false , res.ok , "4d " + tojson(res) );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1, z:1} });
assert.eq( false , res.ok , "4e " + tojson(res) );


f.remove( { y : 2 } );
f.reIndex();

assert.eq( 1 , f.count() , "4. count after removing array value should be 1" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1, z:1} });
assert.eq( true , res.ok , "4f " + tojson(res) );

f.save( { x : 3, y : [1, 2], z : 3 } )

assert.eq( 2 , f.count() , "4. count after adding array value on second key should be 2" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1} });
assert.eq( false , res.ok , "4g " + tojson(res) );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( false , res.ok , "4h " + tojson(res) );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1, z:1} });
assert.eq( false , res.ok , "4i " + tojson(res) );

f.remove( { x : 3 } );
f.reIndex(); // Necessary so that the index is no longer marked as multikey

assert.eq( 1 , f.count() , "4. count after removing array value should be 1 again" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1, z:1} });
assert.eq( true , res.ok , "4e " + tojson(res) );

f.save( { x : 4, y : 4, z : [1, 2] } )

assert.eq( 2 , f.count() , "4. count after adding array value on third key should be 2" );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1} });
assert.eq( false , res.ok , "4c " + tojson(res) );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1} });
assert.eq( false , res.ok , "4d " + tojson(res) );
res = db.runCommand( { checkShardingIndex: "test.jstests_shardingindex" , keyPattern: {x:1, y:1, z:1} });
assert.eq( false , res.ok , "4e " + tojson(res) );


print("PASSED");