-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add commitQuorum option to createIndexes command
Adds commitQuorum option (new in MongoDB 4.4) to createIndexes command and associated helpers: db.createIndex, collection.createIndex, collection.createIndexes NODE-2569
- Loading branch information
Showing
7 changed files
with
195 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,108 @@ | ||
'use strict'; | ||
|
||
const ReadPreference = require('../read_preference'); | ||
const { Aspect, defineAspects, OperationBase } = require('./operation'); | ||
const { executeCommand } = require('./db_ops'); | ||
const { Aspect, defineAspects } = require('./operation'); | ||
const { MongoError } = require('../error'); | ||
const CommandOperationV2 = require('./command_v2'); | ||
const { maxWireVersion, parseIndexOptions } = require('../utils'); | ||
|
||
class CreateIndexesOperation extends OperationBase { | ||
constructor(collection, indexSpecs, options) { | ||
super(options); | ||
const validIndexOptions = new Set([ | ||
'unique', | ||
'partialFilterExpression', | ||
'sparse', | ||
'background', | ||
'expireAfterSeconds', | ||
'storageEngine', | ||
'collation' | ||
]); | ||
|
||
class CreateIndexesOperation extends CommandOperationV2 { | ||
constructor(parent, collection, indexes, options) { | ||
super(parent, options); | ||
this.collection = collection; | ||
this.indexSpecs = indexSpecs; | ||
|
||
// createIndex can be called with a variety of styles: | ||
// coll.createIndex('a'); | ||
// coll.createIndex({ a: 1 }); | ||
// coll.createIndex([['a', 1]]); | ||
// createIndexes is always called with an array of index spec objects | ||
if (!Array.isArray(indexes) || Array.isArray(indexes[0])) { | ||
this.onlyReturnNameOfCreatedIndex = true; | ||
// TODO: remove in v4 (breaking change); make createIndex return full response as createIndexes does | ||
|
||
const indexParameters = parseIndexOptions(indexes); | ||
// Generate the index name | ||
const name = typeof options.name === 'string' ? options.name : indexParameters.name; | ||
// Set up the index | ||
const indexSpec = { name, key: indexParameters.fieldHash }; | ||
// merge valid index options into the index spec | ||
for (let optionName in options) { | ||
if (validIndexOptions.has(optionName)) { | ||
indexSpec[optionName] = options[optionName]; | ||
} | ||
} | ||
this.indexes = [indexSpec]; | ||
return; | ||
} | ||
|
||
this.indexes = indexes; | ||
} | ||
|
||
execute(callback) { | ||
const coll = this.collection; | ||
const indexSpecs = this.indexSpecs; | ||
let options = this.options; | ||
execute(server, callback) { | ||
const options = this.options; | ||
const indexes = this.indexes; | ||
|
||
const capabilities = coll.s.topology.capabilities(); | ||
const serverWireVersion = maxWireVersion(server); | ||
|
||
// Ensure we generate the correct name if the parameter is not set | ||
for (let i = 0; i < indexSpecs.length; i++) { | ||
if (indexSpecs[i].name == null) { | ||
const keys = []; | ||
for (let i = 0; i < indexes.length; i++) { | ||
// Did the user pass in a collation, check if our write server supports it | ||
if (indexes[i].collation && serverWireVersion < 5) { | ||
callback( | ||
new MongoError( | ||
`Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation` | ||
) | ||
); | ||
return; | ||
} | ||
|
||
// Did the user pass in a collation, check if our write server supports it | ||
if (indexSpecs[i].collation && capabilities && !capabilities.commandsTakeCollation) { | ||
return callback(new MongoError('server/primary/mongos does not support collation')); | ||
} | ||
if (indexes[i].name == null) { | ||
const keys = []; | ||
|
||
for (let name in indexSpecs[i].key) { | ||
keys.push(`${name}_${indexSpecs[i].key[name]}`); | ||
for (let name in indexes[i].key) { | ||
keys.push(`${name}_${indexes[i].key[name]}`); | ||
} | ||
|
||
// Set the name | ||
indexSpecs[i].name = keys.join('_'); | ||
indexes[i].name = keys.join('_'); | ||
} | ||
} | ||
|
||
const cmd = { createIndexes: this.collection, indexes }; | ||
|
||
if (options.commitQuorum != null) { | ||
if (serverWireVersion < 9) { | ||
callback( | ||
new MongoError('`commitQuorum` option for `createIndexes` not supported on servers < 4.4') | ||
); | ||
return; | ||
} | ||
cmd.commitQuorum = options.commitQuorum; | ||
} | ||
|
||
options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY }); | ||
|
||
// Execute the index | ||
executeCommand( | ||
coll.s.db, | ||
{ | ||
createIndexes: coll.collectionName, | ||
indexes: indexSpecs | ||
}, | ||
options, | ||
callback | ||
); | ||
// collation is set on each index, it should not be defined at the root | ||
this.options.collation = undefined; | ||
|
||
super.executeCommand(server, cmd, (err, result) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
callback(null, this.onlyReturnNameOfCreatedIndex ? indexes[0].name : result); | ||
}); | ||
} | ||
} | ||
|
||
defineAspects(CreateIndexesOperation, Aspect.WRITE_OPERATION); | ||
defineAspects(CreateIndexesOperation, [Aspect.WRITE_OPERATION, Aspect.EXECUTE_WITH_SELECTION]); | ||
|
||
module.exports = CreateIndexesOperation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.