topology_base.js 4.4 KB

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