topology_base.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. var MongoError = require('mongodb-core').MongoError
  3. , f = require('util').format;
  4. // The store of ops
  5. var Store = function(topology, storeOptions) {
  6. var self = this;
  7. var storedOps = [];
  8. storeOptions = storeOptions || {force:false, bufferMaxEntries: -1}
  9. // Internal state
  10. this.s = {
  11. storedOps: storedOps
  12. , storeOptions: storeOptions
  13. , topology: topology
  14. }
  15. Object.defineProperty(this, 'length', {
  16. enumerable:true, get: function() { return self.s.storedOps.length; }
  17. });
  18. }
  19. Store.prototype.add = function(opType, ns, ops, options, callback) {
  20. if(this.s.storeOptions.force) {
  21. return callback(MongoError.create({message: "db closed by application", driver:true}));
  22. }
  23. if(this.s.storeOptions.bufferMaxEntries == 0) {
  24. return callback(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
  25. }
  26. if(this.s.storeOptions.bufferMaxEntries > 0 && this.s.storedOps.length > this.s.storeOptions.bufferMaxEntries) {
  27. while(this.s.storedOps.length > 0) {
  28. var op = this.s.storedOps.shift();
  29. op.c(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
  30. }
  31. return;
  32. }
  33. this.s.storedOps.push({t: opType, n: ns, o: ops, op: options, c: callback})
  34. }
  35. Store.prototype.addObjectAndMethod = function(opType, object, method, params, callback) {
  36. if(this.s.storeOptions.force) {
  37. return callback(MongoError.create({message: "db closed by application", driver:true }));
  38. }
  39. if(this.s.storeOptions.bufferMaxEntries == 0) {
  40. return callback(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
  41. }
  42. if(this.s.storeOptions.bufferMaxEntries > 0 && this.s.storedOps.length > this.s.storeOptions.bufferMaxEntries) {
  43. while(this.s.storedOps.length > 0) {
  44. var op = this.s.storedOps.shift();
  45. op.c(MongoError.create({message: f("no connection available for operation and number of stored operation > %s", this.s.storeOptions.bufferMaxEntries), driver:true }));
  46. }
  47. return;
  48. }
  49. this.s.storedOps.push({t: opType, m: method, o: object, p: params, c: callback})
  50. }
  51. Store.prototype.flush = function() {
  52. while(this.s.storedOps.length > 0) {
  53. this.s.storedOps.shift().c(MongoError.create({message: f("no connection available for operation"), driver:true }));
  54. }
  55. }
  56. Store.prototype.execute = function() {
  57. // Get current ops
  58. var ops = this.s.storedOps;
  59. // Reset the ops
  60. this.s.storedOps = [];
  61. // Execute all the stored ops
  62. while(ops.length > 0) {
  63. var op = ops.shift();
  64. if(op.t == 'cursor') {
  65. op.o[op.m].apply(op.o, op.p);
  66. } else {
  67. this.s.topology[op.t](op.n, op.o, op.op, op.c);
  68. }
  69. }
  70. }
  71. Store.prototype.all = function() {
  72. return this.s.storedOps;
  73. }
  74. // Server capabilities
  75. var ServerCapabilities = function(ismaster) {
  76. var setup_get_property = function(object, name, value) {
  77. Object.defineProperty(object, name, {
  78. enumerable: true
  79. , get: function () { return value; }
  80. });
  81. }
  82. // Capabilities
  83. var aggregationCursor = false;
  84. var writeCommands = false;
  85. var textSearch = false;
  86. var authCommands = false;
  87. var listCollections = false;
  88. var listIndexes = false;
  89. var maxNumberOfDocsInBatch = ismaster.maxWriteBatchSize || 1000;
  90. if(ismaster.minWireVersion >= 0) {
  91. textSearch = true;
  92. }
  93. if(ismaster.maxWireVersion >= 1) {
  94. aggregationCursor = true;
  95. authCommands = true;
  96. }
  97. if(ismaster.maxWireVersion >= 2) {
  98. writeCommands = true;
  99. }
  100. if(ismaster.maxWireVersion >= 3) {
  101. listCollections = true;
  102. listIndexes = true;
  103. }
  104. // If no min or max wire version set to 0
  105. if(ismaster.minWireVersion == null) {
  106. ismaster.minWireVersion = 0;
  107. }
  108. if(ismaster.maxWireVersion == null) {
  109. ismaster.maxWireVersion = 0;
  110. }
  111. // Map up read only parameters
  112. setup_get_property(this, "hasAggregationCursor", aggregationCursor);
  113. setup_get_property(this, "hasWriteCommands", writeCommands);
  114. setup_get_property(this, "hasTextSearch", textSearch);
  115. setup_get_property(this, "hasAuthCommands", authCommands);
  116. setup_get_property(this, "hasListCollectionsCommand", listCollections);
  117. setup_get_property(this, "hasListIndexesCommand", listIndexes);
  118. setup_get_property(this, "minWireVersion", ismaster.minWireVersion);
  119. setup_get_property(this, "maxWireVersion", ismaster.maxWireVersion);
  120. setup_get_property(this, "maxNumberOfDocsInBatch", maxNumberOfDocsInBatch);
  121. }
  122. exports.Store = Store;
  123. exports.ServerCapabilities = ServerCapabilities;