diff options
Diffstat (limited to 'jstests/sharding/query_config.js')
-rw-r--r-- | jstests/sharding/query_config.js | 168 |
1 files changed, 104 insertions, 64 deletions
diff --git a/jstests/sharding/query_config.js b/jstests/sharding/query_config.js index dea4cf92258..c6b08b8b7c0 100644 --- a/jstests/sharding/query_config.js +++ b/jstests/sharding/query_config.js @@ -9,13 +9,14 @@ }; var getListIndexesCursor = function(coll, options, subsequentBatchSize) { - return new DBCommandCursor(coll.getDB().getMongo(), - coll.runCommand("listIndexes", options), - subsequentBatchSize); + return new DBCommandCursor( + coll.getDB().getMongo(), coll.runCommand("listIndexes", options), subsequentBatchSize); }; var arrayGetNames = function(array) { - return array.map(function(spec) { return spec.name; }); + return array.map(function(spec) { + return spec.name; + }); }; var cursorGetCollectionNames = function(cursor) { @@ -23,7 +24,9 @@ }; var sortArrayByName = function(array) { - return array.sort(function(a, b) { return a.name > b.name; }); + return array.sort(function(a, b) { + return a.name > b.name; + }); }; var cursorGetIndexNames = function(cursor) { @@ -31,13 +34,15 @@ }; var sortArrayById = function(array) { - return array.sort(function(a, b) { return a._id > b._id; }); + return array.sort(function(a, b) { + return a._id > b._id; + }); }; var dropCollectionIfExists = function(coll) { try { coll.drop(); - } catch(err) { + } catch (err) { assert.eq(err.code, ErrorCodes.NamespaceNotFound); } }; @@ -51,15 +56,17 @@ // testKeys and testCollNames are parallel arrays, testKeys contains the shard key of the // corresponding collection whose name is in testCollNames. var testCollNames = ["4a1", "1a12", "3a1b1", "2a1b1c1", "b1", "b1c1", "d1"]; - var testKeys = [{a: 1}, {a: 1}, {a: 1, b: 1}, {a: 1, b: 1, c: 1}, {b: 1}, {b: 1, c: 1}, - {d: 1}]; + var testKeys = + [{a: 1}, {a: 1}, {a: 1, b: 1}, {a: 1, b: 1, c: 1}, {b: 1}, {b: 1, c: 1}, {d: 1}]; var testDB = st.s.getDB("test"); assert.commandWorked(st.s.adminCommand({enablesharding: testDB.getName()})); - var testNamespaces = testCollNames.map(function(e) { return testDB.getName() + "." + e; }); + var testNamespaces = testCollNames.map(function(e) { + return testDB.getName() + "." + e; + }); for (var i = 0; i < testKeys.length; i++) { - assert.commandWorked(st.s.adminCommand({shardcollection: testNamespaces[i], - key: testKeys[i]})); + assert.commandWorked( + st.s.adminCommand({shardcollection: testNamespaces[i], key: testKeys[i]})); } return testNamespaces; @@ -72,8 +79,19 @@ var testListConfigCollections = function(st) { // This test depends on all the collections in the configCollList being in the config // database. - var configCollList = ["changelog", "chunks", "collections", "databases", "lockpings", - "locks", "mongos", "settings", "shards", "tags", "version"]; + var configCollList = [ + "changelog", + "chunks", + "collections", + "databases", + "lockpings", + "locks", + "mongos", + "settings", + "shards", + "tags", + "version" + ]; var configDB = st.s.getDB("config"); var userAddedColl = configDB.userAddedColl; var cursor; @@ -134,8 +152,8 @@ // Find query. cursor = configDB.collections.find({"key.a": 1}, {dropped: 1, "key.a": 1, "key.c": 1}) - .sort({"_id": 1}) - .batchSize(2); + .sort({"_id": 1}) + .batchSize(2); assert.eq(cursor.objsLeftInBatch(), 2); assert.eq(cursor.next(), {_id: testNamespaces[1], dropped: false, key: {a: 1}}); assert.eq(cursor.next(), {_id: testNamespaces[3], dropped: false, key: {a: 1, c: 1}}); @@ -146,9 +164,11 @@ assert(!cursor.hasNext()); // Aggregate query. - cursor = configDB.collections.aggregate([{$match: {"key.b": 1}}, - {$sort: {"_id": 1}}, - {$project: {"keyb":"$key.b", "keyc":"$key.c"}}], + cursor = configDB.collections.aggregate([ + {$match: {"key.b": 1}}, + {$sort: {"_id": 1}}, + {$project: {"keyb": "$key.b", "keyc": "$key.c"}} + ], {cursor: {batchSize: 2}}); assert.eq(cursor.objsLeftInBatch(), 2); assert.eq(cursor.next(), {_id: testNamespaces[3], keyb: 1, keyc: 1}); @@ -175,7 +195,7 @@ var result; // Get shard names. - cursor = configDB.shards.find().sort({_id:1}); + cursor = configDB.shards.find().sort({_id: 1}); var shard1 = cursor.next()._id; var shard2 = cursor.next()._id; assert(!cursor.hasNext()); @@ -183,8 +203,8 @@ st.ensurePrimaryShard(testDB.getName(), shard1); // Setup. - assert.commandWorked(st.s.adminCommand({shardcollection: testColl.getFullName(), - key: {e: 1}})); + assert.commandWorked( + st.s.adminCommand({shardcollection: testColl.getFullName(), key: {e: 1}})); for (var i = 0; i < testCollData.length; i++) { assert.writeOK(testColl.insert(testCollData[i])); } @@ -192,19 +212,16 @@ assert.commandWorked(st.s.adminCommand({split: testColl.getFullName(), middle: {e: 6}})); assert.commandWorked(st.s.adminCommand({split: testColl.getFullName(), middle: {e: 8}})); assert.commandWorked(st.s.adminCommand({split: testColl.getFullName(), middle: {e: 11}})); - assert.commandWorked(st.s.adminCommand({movechunk: testColl.getFullName(), - find: {e: 1}, - to: shard2})); - assert.commandWorked(st.s.adminCommand({movechunk: testColl.getFullName(), - find: {e: 9}, - to: shard2})); - assert.commandWorked(st.s.adminCommand({movechunk: testColl.getFullName(), - find: {e: 12}, - to: shard2})); + assert.commandWorked( + st.s.adminCommand({movechunk: testColl.getFullName(), find: {e: 1}, to: shard2})); + assert.commandWorked( + st.s.adminCommand({movechunk: testColl.getFullName(), find: {e: 9}, to: shard2})); + assert.commandWorked( + st.s.adminCommand({movechunk: testColl.getFullName(), find: {e: 12}, to: shard2})); // Find query. cursor = configDB.chunks.find({ns: testColl.getFullName()}, - {_id:0, min:1, max:1, shard:1}).sort({"min.e":1}); + {_id: 0, min: 1, max: 1, shard: 1}).sort({"min.e": 1}); assert.eq(cursor.next(), {min: {e: {"$minKey": 1}}, "max": {"e": 2}, shard: shard2}); assert.eq(cursor.next(), {min: {e: 2}, max: {e: 6}, shard: shard1}); assert.eq(cursor.next(), {min: {e: 6}, max: {e: 8}, shard: shard1}); @@ -219,13 +236,20 @@ assert.eq(configDB.chunks.distinct("shard").sort(), [shard1, shard2]); // Group query. - result = configDB.chunks.group({key: {shard: 1}, - cond: {ns: testColl.getFullName()}, - reduce: function(curr, res) { res.chunks++; }, - initial: {chunks: 0}, - finalize: function(res) { res._id = res.shard; }}); - assert.eq(sortArrayById(result), [{shard: shard1, chunks: 2, _id: shard1}, - {shard: shard2, chunks: 3, _id: shard2}]); + result = configDB.chunks.group({ + key: {shard: 1}, + cond: {ns: testColl.getFullName()}, + reduce: function(curr, res) { + res.chunks++; + }, + initial: {chunks: 0}, + finalize: function(res) { + res._id = res.shard; + } + }); + assert.eq( + sortArrayById(result), + [{shard: shard1, chunks: 2, _id: shard1}, {shard: shard2, chunks: 3, _id: shard2}]); // Map reduce query. var mapFunction = function() { @@ -234,12 +258,14 @@ } }; var reduceFunction = function(key, values) { - return {chunks: values.length}; + return { + chunks: values.length + }; }; result = configDB.chunks.mapReduce(mapFunction, reduceFunction, {out: {inline: 1}}); assert.eq(result.ok, 1); - assert.eq(sortArrayById(result.results), [{_id: shard1, value: {chunks: 2}}, - {_id: shard2, value: {chunks: 3}}]); + assert.eq(sortArrayById(result.results), + [{_id: shard1, value: {chunks: 2}}, {_id: shard2, value: {chunks: 3}}]); }; /** @@ -247,13 +273,15 @@ */ var queryUserCreated = function(database) { var userColl = database.userColl; - var userCollData = [{_id: 1, g: 1, c: 4, s: "c", u: [1, 2]}, - {_id: 2, g: 1, c: 5, s: "b", u: [1]}, - {_id: 3, g: 2, c: 16, s: "g", u: [3]}, - {_id: 4, g: 2, c: 1, s: "a", u: [2, 4]}, - {_id: 5, g: 2, c: 18, s: "d", u: [3]}, - {_id: 6, g: 3, c: 11, s: "e", u: [2, 3]}, - {_id: 7, g: 3, c: 2, s: "f", u: [1]}]; + var userCollData = [ + {_id: 1, g: 1, c: 4, s: "c", u: [1, 2]}, + {_id: 2, g: 1, c: 5, s: "b", u: [1]}, + {_id: 3, g: 2, c: 16, s: "g", u: [3]}, + {_id: 4, g: 2, c: 1, s: "a", u: [2, 4]}, + {_id: 5, g: 2, c: 18, s: "d", u: [3]}, + {_id: 6, g: 3, c: 11, s: "e", u: [2, 3]}, + {_id: 7, g: 3, c: 2, s: "f", u: [1]} + ]; var userCollIndexes = ["_id_", "s_1"]; var cursor; var cursorArray; @@ -294,10 +322,12 @@ assert(!cursor.hasNext()); // Aggregate query. - cursor = userColl.aggregate([{$match: {c: {$gt: 1}}}, - {$unwind: "$u"}, - {$group: {_id: "$u", sum: {$sum: "$c"}}}, - {$sort: {_id: 1}}], + cursor = userColl.aggregate([ + {$match: {c: {$gt: 1}}}, + {$unwind: "$u"}, + {$group: {_id: "$u", sum: {$sum: "$c"}}}, + {$sort: {_id: 1}} + ], {cursor: {batchSize: 2}}); assert.eq(cursor.objsLeftInBatch(), 2); assert.eq(cursor.next(), {_id: 1, sum: 11}); @@ -317,26 +347,36 @@ assert.eq(userColl.distinct("g").sort(), [1, 2, 3]); // Group query. - result = userColl.group({key: {g: 1}, - reduce: function(curr, res) { res.prod *= curr.c; }, - initial: {prod: 1}, - finalize: function(res) { res._id = res.g; }}); - assert.eq(sortArrayById(result), [{g: 1, prod: 20, _id: 1}, - {g: 2, prod: 288, _id: 2}, - {g: 3, prod: 22, _id: 3}]); + result = userColl.group({ + key: {g: 1}, + reduce: function(curr, res) { + res.prod *= curr.c; + }, + initial: {prod: 1}, + finalize: function(res) { + res._id = res.g; + } + }); + assert.eq(sortArrayById(result), + [{g: 1, prod: 20, _id: 1}, {g: 2, prod: 288, _id: 2}, {g: 3, prod: 22, _id: 3}]); // Map reduce query. var mapFunction = function() { emit(this.g, 1); }; var reduceFunction = function(key, values) { - return {count: values.length}; + return { + count: values.length + }; }; result = userColl.mapReduce(mapFunction, reduceFunction, {out: {inline: 1}}); assert.eq(result.ok, 1); - assert.eq(sortArrayById(result.results), [{_id: 1, value: {count: 2}}, - {_id: 2, value: {count: 3}}, - {_id: 3, value: {count: 2}}]); + assert.eq(sortArrayById(result.results), + [ + {_id: 1, value: {count: 2}}, + {_id: 2, value: {count: 3}}, + {_id: 3, value: {count: 2}} + ]); assert(userColl.drop()); }; |