memdb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env node
  2. // Copyright 2015 The MemDB Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. // implied. See the License for the specific language governing
  14. // permissions and limitations under the License. See the AUTHORS file
  15. // for names of contributors.
  16. 'use strict';
  17. var P = require('bluebird');
  18. var path = require('path');
  19. var minimist = require('minimist');
  20. var memdbLogger = require('memdb-logger');
  21. var util = require('util');
  22. var repl = require('repl');
  23. var helpStart = '\
  24. Usage: memdb [options]\n\
  25. Options:\n\
  26. -h, --host ip memdb shard ip\n\
  27. -p, --port port memdb shard port\n\
  28. -s, --shard shardId shardId\n\
  29. -c, --conf path Config file path\n\
  30. --help display help\n\n\
  31. Example: \n\
  32. memdb -h127.0.0.1 -p31017\n\
  33. memdb -s s1 -c memdb.conf.js\n';
  34. var helpCmd = '\n\
  35. Command Reference:\n\n\
  36. db.find(collection, idOrQuery)\n\
  37. db.insert(collection, docs)\n\
  38. db.update(collection, idOrQuery, modifier)\n\
  39. db.remove(collection, idOrQuery)\n\
  40. db.commit()\n\
  41. db.rollback()\n\
  42. _ last result\n';
  43. var startRepl = function(conn){
  44. var deferred = P.defer();
  45. var server = repl.start({
  46. prompt: 'memdb> ',
  47. terminal: true,
  48. input: process.stdin,
  49. output: process.stdout,
  50. useGlobal : false,
  51. ignoreUndefined : true,
  52. });
  53. var originEval = server.eval; //jshint ignore:line
  54. server.eval = function(cmd, context, filename, cb){ //jshint ignore:line
  55. if(cmd.trim() === 'help'){
  56. console.log(helpCmd);
  57. return cb();
  58. }
  59. originEval.call(server, cmd, context, filename, function(err, ret){
  60. if(err){
  61. console.log(helpCmd);
  62. return cb(err, ret);
  63. }
  64. if(P.is(ret)){
  65. ret.nodeify(function(err, ret){
  66. if(err){
  67. err = err.message + '\n (Changes are rolled back)';
  68. }
  69. cb(err, ret);
  70. });
  71. }
  72. else{
  73. cb(err, ret);
  74. }
  75. });
  76. };
  77. server.on('exit', function(){
  78. conn.removeAllListeners('close');
  79. return conn.close()
  80. .then(function(){
  81. console.log('Bye');
  82. deferred.resolve();
  83. }, function(e){
  84. deferred.reject(e);
  85. });
  86. });
  87. server.context.db = conn;
  88. conn.on('error', function(e){
  89. console.error(e.stack);
  90. });
  91. conn.on('close', function(){
  92. console.log('Connection lost');
  93. deferred.resolve();
  94. });
  95. return deferred.promise;
  96. };
  97. if (require.main === module) {
  98. // turn off logger by default
  99. memdbLogger.setGlobalLogLevel(memdbLogger.levels.OFF);
  100. console.log('MemDB shell');
  101. var argv = minimist(process.argv.slice(2));
  102. if(argv.help){
  103. console.log(helpStart);
  104. process.exit(0);
  105. }
  106. var connectOpts = {idleTimeout : 0};
  107. var shardId = argv.shard || argv.s;
  108. var confPath = argv.conf || argv.c;
  109. if(confPath){
  110. confPath = path.resolve(confPath);
  111. }
  112. if(shardId){
  113. var config = require('../app/config');
  114. config.init(confPath);
  115. var shardConfig = config.shardConfig(shardId);
  116. connectOpts.host = shardConfig.host;
  117. connectOpts.port = shardConfig.port;
  118. }
  119. else{
  120. if(confPath){
  121. throw new Error('shard not specified');
  122. }
  123. connectOpts.host = argv.host || argv.h || '127.0.0.1';
  124. connectOpts.port = argv.port || argv.p || 31017;
  125. }
  126. var memdb = require('../lib');
  127. var conn = null;
  128. P.try(function(){
  129. return memdb.connect(connectOpts);
  130. })
  131. .then(function(ret){
  132. console.log('connected to %s:%s', connectOpts.host, connectOpts.port);
  133. return startRepl(ret);
  134. }, function(e){
  135. console.error('failed to connect to %s:%s', connectOpts.host, connectOpts.port);
  136. process.exit(1);
  137. })
  138. .then(function(){
  139. process.exit(0);
  140. })
  141. .catch(function(e){
  142. console.error(e.stack);
  143. process.exit(1);
  144. });
  145. }