Skip to content

Commit

Permalink
Small change to make ensureIndex use index for write
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Jul 27, 2012
1 parent b4bc00c commit edc10e0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.tmp
.project
.settings
.tm_properties
data
node_modules/
output
Expand Down
46 changes: 27 additions & 19 deletions lib/mongodb/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ Db.prototype.reIndex = function(collectionName, callback) {
*
* Options
* - **full** {Boolean, default:false}, returns the full raw index information.
* - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
*
* @param {String} collectionName the name of the collection.
* @param {Object} [options] additional options during update.
Expand All @@ -1263,27 +1264,34 @@ Db.prototype.indexInformation = function(collectionName, options, callback) {
var full = options['full'] == null ? false : options['full'];
// Build selector for the indexes
var selector = collectionName != null ? {ns: (this.databaseName + "." + collectionName)} : {};

// Set read preference if we set one
var readPreference = options['readPreference'] ? options['readPreference'] : ReadPreference.PRIMARY;

// Iterate through all the fields of the index
new Cursor(this, new Collection(this, DbCommand.SYSTEM_INDEX_COLLECTION), selector).toArray(function(err, indexes) {
if(err != null) return callback(err, null);
// Contains all the information
var info = {};

// if full defined just return all the indexes directly
if(full) return callback(null, indexes);

// Process all the indexes
for(var i = 0; i < indexes.length; i++) {
var index = indexes[i];
// Let's unpack the object
info[index.name] = [];
for(var name in index.key) {
info[index.name].push([name, index.key[name]]);
this.collection(DbCommand.SYSTEM_INDEX_COLLECTION, function(err, collection) {
// Perform the find for the collection
collection.find(selector).setReadPreference(readPreference).toArray(function(err, indexes) {
if(err != null) return callback(err, null);
// Contains all the information
var info = {};

// if full defined just return all the indexes directly
if(full) return callback(null, indexes);

// Process all the indexes
for(var i = 0; i < indexes.length; i++) {
var index = indexes[i];
// Let's unpack the object
info[index.name] = [];
for(var name in index.key) {
info[index.name].push([name, index.key[name]]);
}
}
}

// Return all the indexes
callback(null, info);
// Return all the indexes
callback(null, info);
});
});
};

Expand Down Expand Up @@ -1439,7 +1447,7 @@ var __executeQueryCommand = function(self, db_command, options, callback) {
if(self.serverConfig.isMongos() && read != null && read != false) {
db_command.setMongosReadPreference(read);
}

// If we got a callback object
if(typeof callback === 'function' && !onAll) {
// Override connection if we passed in a specific connection
Expand Down
53 changes: 28 additions & 25 deletions test/manual_tests/manual_bug_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,36 @@ var replSet = new ReplSetServers([
], {rs_name:'replica-set-foo'});

var client = new Db(databasename, replSet, {native_parser: false});
client.open(function(err, db_p) {
if (err){
console.log('ERR:'+err);
console.log('DB:'+db_p);
}
client.on("fullsetup", function(err, result) {
if (err){
console.log('ERR:'+err);
console.log('DB:'+db_p);
}

client.authenticate(username, password, function(err, replies) {
if (err){
console.log('ERR AUTH:'+err);
console.log('replies:'+replies);
}
client.authenticate(username, password, function(err, replies) {
if (err){
console.log('ERR AUTH:'+err);
console.log('replies:'+replies);
}

// var ensureIndexOptions = { unique: true, safe: true, background: true };
var ensureIndexOptions = { unique: true, safe: false, background: true };
// Just cleanup
client.collection('userconfirm').ensureIndex({ 'confirmcode':1 }, ensureIndexOptions, function(err, item){
if(err){
console.log('Userconfirm ensure index failed:'+err);
}
// var ensureIndexOptions = { unique: true, safe: true, background: true };
var ensureIndexOptions = { unique: true, safe: false, background: true };
// Just cleanup
client.collection('userconfirm').ensureIndex({ 'confirmcode':1 }, ensureIndexOptions, function(err, item){
if(err){
console.log('Userconfirm ensure index failed:'+err);
}

client.collection('session').ensureIndex({ 'sid': 1 }, ensureIndexOptions, function(err, res){
if(err){
console.log('Session ensure index failed'+err);
}

client.collection('session').ensureIndex({ 'sid': 1 }, ensureIndexOptions, function(err, res){
if(err){
console.log('Session ensure index failed'+err);
}
client.close();
});
});
});
})

client.close();
});
});
});
client.open(function(err, db_p) {
});

0 comments on commit edc10e0

Please sign in to comment.