HTMLify
transactions.js
Views: 6 | Author: cody
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 'use strict'; const MongoError = require('./error').MongoError; const ReadPreference = require('./topologies/read_preference'); const ReadConcern = require('../read_concern'); const WriteConcern = require('../write_concern'); let TxnState; let stateMachine; (() => { const NO_TRANSACTION = 'NO_TRANSACTION'; const STARTING_TRANSACTION = 'STARTING_TRANSACTION'; const TRANSACTION_IN_PROGRESS = 'TRANSACTION_IN_PROGRESS'; const TRANSACTION_COMMITTED = 'TRANSACTION_COMMITTED'; const TRANSACTION_COMMITTED_EMPTY = 'TRANSACTION_COMMITTED_EMPTY'; const TRANSACTION_ABORTED = 'TRANSACTION_ABORTED'; TxnState = { NO_TRANSACTION, STARTING_TRANSACTION, TRANSACTION_IN_PROGRESS, TRANSACTION_COMMITTED, TRANSACTION_COMMITTED_EMPTY, TRANSACTION_ABORTED }; stateMachine = { [NO_TRANSACTION]: [NO_TRANSACTION, STARTING_TRANSACTION], [STARTING_TRANSACTION]: [ TRANSACTION_IN_PROGRESS, TRANSACTION_COMMITTED, TRANSACTION_COMMITTED_EMPTY, TRANSACTION_ABORTED ], [TRANSACTION_IN_PROGRESS]: [ TRANSACTION_IN_PROGRESS, TRANSACTION_COMMITTED, TRANSACTION_ABORTED ], [TRANSACTION_COMMITTED]: [ TRANSACTION_COMMITTED, TRANSACTION_COMMITTED_EMPTY, STARTING_TRANSACTION, NO_TRANSACTION ], [TRANSACTION_ABORTED]: [STARTING_TRANSACTION, NO_TRANSACTION], [TRANSACTION_COMMITTED_EMPTY]: [TRANSACTION_COMMITTED_EMPTY, NO_TRANSACTION] }; })(); /** * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties * of the data read from replica sets and replica set shards. * @typedef {Object} ReadConcern * @property {'local'|'available'|'majority'|'linearizable'|'snapshot'} level The readConcern Level * @see https://docs.mongodb.com/manual/reference/read-concern/ */ /** * A MongoDB WriteConcern, which describes the level of acknowledgement * requested from MongoDB for write operations. * @typedef {Object} WriteConcern * @property {number|'majority'|string} [w=1] requests acknowledgement that the write operation has * propagated to a specified number of mongod hosts * @property {boolean} [j=false] requests acknowledgement from MongoDB that the write operation has * been written to the journal * @property {number} [wtimeout] a time limit, in milliseconds, for the write concern * @see https://docs.mongodb.com/manual/reference/write-concern/ */ /** * Configuration options for a transaction. * @typedef {Object} TransactionOptions * @property {ReadConcern} [readConcern] A default read concern for commands in this transaction * @property {WriteConcern} [writeConcern] A default writeConcern for commands in this transaction * @property {ReadPreference} [readPreference] A default read preference for commands in this transaction */ /** * A class maintaining state related to a server transaction. Internal Only * @ignore */ class Transaction { /** * Create a transaction * * @ignore * @param {TransactionOptions} [options] Optional settings */ constructor(options) { options = options || {}; this.state = TxnState.NO_TRANSACTION; this.options = {}; const writeConcern = WriteConcern.fromOptions(options); if (writeConcern) { if (writeConcern.w <= 0) { throw new MongoError('Transactions do not support unacknowledged write concern'); } this.options.writeConcern = writeConcern; } if (options.readConcern) { this.options.readConcern = ReadConcern.fromOptions(options); } if (options.readPreference) { this.options.readPreference = ReadPreference.fromOptions(options); } if (options.maxCommitTimeMS) { this.options.maxTimeMS = options.maxCommitTimeMS; } // TODO: This isn't technically necessary this._pinnedServer = undefined; this._recoveryToken = undefined; } get server() { return this._pinnedServer; } get recoveryToken() { return this._recoveryToken; } get isPinned() { return !!this.server; } /** * @ignore * @return Whether this session is presently in a transaction */ get isActive() { return ( [TxnState.STARTING_TRANSACTION, TxnState.TRANSACTION_IN_PROGRESS].indexOf(this.state) !== -1 ); } /** * Transition the transaction in the state machine * @ignore * @param {TxnState} state The new state to transition to */ transition(nextState) { const nextStates = stateMachine[this.state]; if (nextStates && nextStates.indexOf(nextState) !== -1) { this.state = nextState; if (this.state === TxnState.NO_TRANSACTION || this.state === TxnState.STARTING_TRANSACTION) { this.unpinServer(); } return; } throw new MongoError( `Attempted illegal state transition from [${this.state}] to [${nextState}]` ); } pinServer(server) { if (this.isActive) { this._pinnedServer = server; } } unpinServer() { this._pinnedServer = undefined; } } function isTransactionCommand(command) { return !!(command.commitTransaction || command.abortTransaction); } module.exports = { TxnState, Transaction, isTransactionCommand }; |