Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transactions.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. const MongoError = require('./error').MongoError;
  3. let TxnState;
  4. let stateMachine;
  5. (() => {
  6. const NO_TRANSACTION = 'NO_TRANSACTION';
  7. const STARTING_TRANSACTION = 'STARTING_TRANSACTION';
  8. const TRANSACTION_IN_PROGRESS = 'TRANSACTION_IN_PROGRESS';
  9. const TRANSACTION_COMMITTED = 'TRANSACTION_COMMITTED';
  10. const TRANSACTION_COMMITTED_EMPTY = 'TRANSACTION_COMMITTED_EMPTY';
  11. const TRANSACTION_ABORTED = 'TRANSACTION_ABORTED';
  12. TxnState = {
  13. NO_TRANSACTION,
  14. STARTING_TRANSACTION,
  15. TRANSACTION_IN_PROGRESS,
  16. TRANSACTION_COMMITTED,
  17. TRANSACTION_COMMITTED_EMPTY,
  18. TRANSACTION_ABORTED
  19. };
  20. stateMachine = {
  21. [NO_TRANSACTION]: [NO_TRANSACTION, STARTING_TRANSACTION],
  22. [STARTING_TRANSACTION]: [
  23. TRANSACTION_IN_PROGRESS,
  24. TRANSACTION_COMMITTED,
  25. TRANSACTION_COMMITTED_EMPTY,
  26. TRANSACTION_ABORTED
  27. ],
  28. [TRANSACTION_IN_PROGRESS]: [
  29. TRANSACTION_IN_PROGRESS,
  30. TRANSACTION_COMMITTED,
  31. TRANSACTION_ABORTED
  32. ],
  33. [TRANSACTION_COMMITTED]: [
  34. TRANSACTION_COMMITTED,
  35. TRANSACTION_COMMITTED_EMPTY,
  36. STARTING_TRANSACTION,
  37. NO_TRANSACTION
  38. ],
  39. [TRANSACTION_ABORTED]: [STARTING_TRANSACTION, NO_TRANSACTION],
  40. [TRANSACTION_COMMITTED_EMPTY]: [TRANSACTION_COMMITTED_EMPTY, NO_TRANSACTION]
  41. };
  42. })();
  43. /**
  44. * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
  45. * of the data read from replica sets and replica set shards.
  46. * @typedef {Object} ReadConcern
  47. * @property {'local'|'available'|'majority'|'linearizable'|'snapshot'} level The readConcern Level
  48. * @see https://docs.mongodb.com/manual/reference/read-concern/
  49. */
  50. /**
  51. * A MongoDB WriteConcern, which describes the level of acknowledgement
  52. * requested from MongoDB for write operations.
  53. * @typedef {Object} WriteConcern
  54. * @property {number|'majority'|string} [w=1] requests acknowledgement that the write operation has
  55. * propagated to a specified number of mongod hosts
  56. * @property {boolean} [j=false] requests acknowledgement from MongoDB that the write operation has
  57. * been written to the journal
  58. * @property {number} [wtimeout] a time limit, in milliseconds, for the write concern
  59. * @see https://docs.mongodb.com/manual/reference/write-concern/
  60. */
  61. /**
  62. * Configuration options for a transaction.
  63. * @typedef {Object} TransactionOptions
  64. * @property {ReadConcern} [readConcern] A default read concern for commands in this transaction
  65. * @property {WriteConcern} [writeConcern] A default writeConcern for commands in this transaction
  66. * @property {ReadPreference} [readPreference] A default read preference for commands in this transaction
  67. */
  68. /**
  69. * A class maintaining state related to a server transaction. Internal Only
  70. * @ignore
  71. */
  72. class Transaction {
  73. /**
  74. * Create a transaction
  75. *
  76. * @ignore
  77. * @param {TransactionOptions} [options] Optional settings
  78. */
  79. constructor(options) {
  80. options = options || {};
  81. this.state = TxnState.NO_TRANSACTION;
  82. this.options = {};
  83. if (options.writeConcern || typeof options.w !== 'undefined') {
  84. const w = options.writeConcern ? options.writeConcern.w : options.w;
  85. if (w <= 0) {
  86. throw new MongoError('Transactions do not support unacknowledged write concern');
  87. }
  88. this.options.writeConcern = options.writeConcern ? options.writeConcern : { w: options.w };
  89. }
  90. if (options.readConcern) this.options.readConcern = options.readConcern;
  91. if (options.readPreference) this.options.readPreference = options.readPreference;
  92. }
  93. /**
  94. * @ignore
  95. * @return Whether this session is presently in a transaction
  96. */
  97. get isActive() {
  98. return (
  99. [TxnState.STARTING_TRANSACTION, TxnState.TRANSACTION_IN_PROGRESS].indexOf(this.state) !== -1
  100. );
  101. }
  102. /**
  103. * Transition the transaction in the state machine
  104. * @ignore
  105. * @param {TxnState} state The new state to transition to
  106. */
  107. transition(nextState) {
  108. const nextStates = stateMachine[this.state];
  109. if (nextStates && nextStates.indexOf(nextState) !== -1) {
  110. this.state = nextState;
  111. return;
  112. }
  113. throw new MongoError(
  114. `Attempted illegal state transition from [${this.state}] to [${nextState}]`
  115. );
  116. }
  117. }
  118. module.exports = { TxnState, Transaction };