connection.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var Logger = require('memdb-logger');
  18. var consts = require('./consts');
  19. var Collection = require('./collection');
  20. var util = require('util');
  21. var utils = require('./utils');
  22. var Connection = function(opts){
  23. opts = opts || {};
  24. this._id = opts._id;
  25. this.shard = opts.shard;
  26. this.config = opts.config || {};
  27. this.collections = {};
  28. this.lockedKeys = utils.forceHashMap();
  29. this.logger = Logger.getLogger('memdb', __filename, 'shard:' + this.shard._id);
  30. };
  31. var proto = Connection.prototype;
  32. proto.close = function(){
  33. if(this.isDirty()){
  34. this.rollback();
  35. }
  36. for(var name in this.collections){
  37. this.collections[name].close();
  38. }
  39. };
  40. consts.collMethods.forEach(function(method){
  41. proto[method] = function(name){
  42. var collection = this.getCollection(name);
  43. // remove 'name' arg
  44. var args = [].slice.call(arguments, 1);
  45. this.logger.debug('[conn:%s] %s.%s(%j)', this._id, name, method, args);
  46. return collection[method].apply(collection, args);
  47. };
  48. });
  49. proto.commit = function(){
  50. var self = this;
  51. return P.each(Object.keys(this.collections), function(name){
  52. var collection = self.collections[name];
  53. return collection.commitIndex();
  54. })
  55. .then(function(){
  56. return self.shard.commit(self._id, Object.keys(self.lockedKeys));
  57. })
  58. .then(function(){
  59. self.lockedKeys = {};
  60. self.logger.debug('[conn:%s] commited', self._id);
  61. return true;
  62. });
  63. };
  64. // sync method
  65. proto.rollback = function(){
  66. var self = this;
  67. Object.keys(this.collections).forEach(function(name){
  68. self.collections[name].rollbackIndex();
  69. });
  70. this.shard.rollback(this._id, Object.keys(this.lockedKeys));
  71. this.lockedKeys = {};
  72. this.logger.debug('[conn:%s] rolledback', this._id);
  73. return true;
  74. };
  75. proto.flushBackend = function(){
  76. return this.shard.flushBackend(this._id);
  77. };
  78. // for internal use
  79. proto.$unload = function(key){
  80. return this.shard.$unload(key);
  81. };
  82. // for internal use
  83. proto.$findReadOnly = function(key, fields){
  84. return this.shard.find(null, key, fields);
  85. };
  86. proto.getCollection = function(name, isIndex){
  87. if(!isIndex && name && name.indexOf('index.') === 0){
  88. throw new Error('Collection name can not begin with "index."');
  89. }
  90. var self = this;
  91. if(!this.collections[name]){
  92. var collection = new Collection({
  93. name : name,
  94. shard : this.shard,
  95. conn : this,
  96. config : this.config.collections[name] || {},
  97. });
  98. collection.on('lock', function(id){
  99. var key = name + '$' + id;
  100. self.lockedKeys[key] = true;
  101. });
  102. this.collections[name] = collection;
  103. }
  104. return this.collections[name];
  105. };
  106. proto.isDirty = function(){
  107. return Object.keys(this.lockedKeys).length > 0;
  108. };
  109. module.exports = Connection;